feat: use tag as version number

This commit is contained in:
Yifei Zhang
2023-03-30 16:46:17 +00:00
parent 1b140a1ed3
commit 7783545bff
5 changed files with 18 additions and 17 deletions

View File

@@ -1,7 +1,7 @@
import { create } from "zustand";
import { persist } from "zustand/middleware";
import { FETCH_COMMIT_URL } from "../constant";
import { getCurrentCommitId } from "../utils";
import { FETCH_COMMIT_URL, FETCH_TAG_URL } from "../constant";
import { getCurrentVersion } from "../utils";
export interface UpdateStore {
lastUpdate: number;
@@ -19,15 +19,15 @@ export const useUpdateStore = create<UpdateStore>()(
remoteId: "",
async getLatestCommitId(force = false) {
const overOneHour = Date.now() - get().lastUpdate > 3600 * 1000;
const shouldFetch = force || overOneHour;
const overTenMins = Date.now() - get().lastUpdate > 10 * 60 * 1000;
const shouldFetch = force || overTenMins;
if (!shouldFetch) {
return getCurrentCommitId();
return getCurrentVersion();
}
try {
const data = await (await fetch(FETCH_COMMIT_URL)).json();
const sha = data[0].sha as string;
const data = await (await fetch(FETCH_TAG_URL)).json();
const sha = data[0].name as string;
const remoteId = sha.substring(0, 7);
set(() => ({
lastUpdate: Date.now(),
@@ -37,13 +37,13 @@ export const useUpdateStore = create<UpdateStore>()(
return remoteId;
} catch (error) {
console.error("[Fetch Upstream Commit Id]", error);
return getCurrentCommitId();
return getCurrentVersion();
}
},
}),
{
name: UPDATE_KEY,
version: 1,
}
)
},
),
);