|
@@ -1,28 +1,46 @@
|
|
import { create } from "zustand";
|
|
import { create } from "zustand";
|
|
import { persist } from "zustand/middleware";
|
|
import { persist } from "zustand/middleware";
|
|
import { FETCH_COMMIT_URL, FETCH_TAG_URL } from "../constant";
|
|
import { FETCH_COMMIT_URL, FETCH_TAG_URL } from "../constant";
|
|
-import { getCurrentVersion } from "../utils";
|
|
|
|
|
|
|
|
export interface UpdateStore {
|
|
export interface UpdateStore {
|
|
lastUpdate: number;
|
|
lastUpdate: number;
|
|
- remoteId: string;
|
|
|
|
|
|
+ remoteVersion: string;
|
|
|
|
|
|
- getLatestCommitId: (force: boolean) => Promise<string>;
|
|
|
|
|
|
+ version: string;
|
|
|
|
+ getLatestVersion: (force: boolean) => Promise<string>;
|
|
}
|
|
}
|
|
|
|
|
|
export const UPDATE_KEY = "chat-update";
|
|
export const UPDATE_KEY = "chat-update";
|
|
|
|
|
|
|
|
+function queryMeta(key: string, defaultValue?: string): string {
|
|
|
|
+ let ret: string;
|
|
|
|
+ if (document) {
|
|
|
|
+ const meta = document.head.querySelector(
|
|
|
|
+ `meta[name='${key}']`,
|
|
|
|
+ ) as HTMLMetaElement;
|
|
|
|
+ ret = meta?.content ?? "";
|
|
|
|
+ } else {
|
|
|
|
+ ret = defaultValue ?? "";
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return ret;
|
|
|
|
+}
|
|
|
|
+
|
|
export const useUpdateStore = create<UpdateStore>()(
|
|
export const useUpdateStore = create<UpdateStore>()(
|
|
persist(
|
|
persist(
|
|
(set, get) => ({
|
|
(set, get) => ({
|
|
lastUpdate: 0,
|
|
lastUpdate: 0,
|
|
- remoteId: "",
|
|
|
|
|
|
+ remoteVersion: "",
|
|
|
|
+
|
|
|
|
+ version: "unknown",
|
|
|
|
+
|
|
|
|
+ async getLatestVersion(force = false) {
|
|
|
|
+ set(() => ({ version: queryMeta("version") }));
|
|
|
|
|
|
- async getLatestCommitId(force = false) {
|
|
|
|
const overTenMins = Date.now() - get().lastUpdate > 10 * 60 * 1000;
|
|
const overTenMins = Date.now() - get().lastUpdate > 10 * 60 * 1000;
|
|
const shouldFetch = force || overTenMins;
|
|
const shouldFetch = force || overTenMins;
|
|
if (!shouldFetch) {
|
|
if (!shouldFetch) {
|
|
- return getCurrentVersion();
|
|
|
|
|
|
+ return get().version ?? "unknown";
|
|
}
|
|
}
|
|
|
|
|
|
try {
|
|
try {
|
|
@@ -32,13 +50,13 @@ export const useUpdateStore = create<UpdateStore>()(
|
|
const remoteId = (data[0].sha as string).substring(0, 7);
|
|
const remoteId = (data[0].sha as string).substring(0, 7);
|
|
set(() => ({
|
|
set(() => ({
|
|
lastUpdate: Date.now(),
|
|
lastUpdate: Date.now(),
|
|
- remoteId,
|
|
|
|
|
|
+ remoteVersion: remoteId,
|
|
}));
|
|
}));
|
|
console.log("[Got Upstream] ", remoteId);
|
|
console.log("[Got Upstream] ", remoteId);
|
|
return remoteId;
|
|
return remoteId;
|
|
} catch (error) {
|
|
} catch (error) {
|
|
console.error("[Fetch Upstream Commit Id]", error);
|
|
console.error("[Fetch Upstream Commit Id]", error);
|
|
- return getCurrentVersion();
|
|
|
|
|
|
+ return get().version ?? "";
|
|
}
|
|
}
|
|
},
|
|
},
|
|
}),
|
|
}),
|