refactor: #6 check update over one hour and debound scroll
parent
29de957395
commit
99b88f36fd
@ -1,3 +1,5 @@
|
|||||||
export const REPO_URL = "https://github.com/Yidadaa/ChatGPT-Next-Web";
|
export const OWNER = "Yidadaa";
|
||||||
export const UPDATE_URL =
|
export const REPO = "ChatGPT-Next-Web";
|
||||||
"https://github.com/Yidadaa/ChatGPT-Next-Web#%E4%BF%9D%E6%8C%81%E6%9B%B4%E6%96%B0-keep-updated";
|
export const REPO_URL = `https://github.com/${OWNER}/${REPO}`;
|
||||||
|
export const UPDATE_URL = `${REPO_URL}#%E4%BF%9D%E6%8C%81%E6%9B%B4%E6%96%B0-keep-updated`;
|
||||||
|
export const FETCH_COMMIT_URL = `https://api.github.com/repos/${OWNER}/${REPO}/commits?per_page=1`;
|
||||||
|
@ -0,0 +1,2 @@
|
|||||||
|
export * from "./app";
|
||||||
|
export * from "./update";
|
@ -0,0 +1,49 @@
|
|||||||
|
import { create } from "zustand";
|
||||||
|
import { persist } from "zustand/middleware";
|
||||||
|
import { FETCH_COMMIT_URL } from "../constant";
|
||||||
|
import { getCurrentCommitId } from "../utils";
|
||||||
|
|
||||||
|
export interface UpdateStore {
|
||||||
|
lastUpdate: number;
|
||||||
|
remoteId: string;
|
||||||
|
|
||||||
|
getLatestCommitId: (force: boolean) => Promise<string>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const UPDATE_KEY = "chat-update";
|
||||||
|
|
||||||
|
export const useUpdateStore = create<UpdateStore>()(
|
||||||
|
persist(
|
||||||
|
(set, get) => ({
|
||||||
|
lastUpdate: 0,
|
||||||
|
remoteId: "",
|
||||||
|
|
||||||
|
async getLatestCommitId(force = false) {
|
||||||
|
const overOneHour = Date.now() - get().lastUpdate > 3600 * 1000;
|
||||||
|
const shouldFetch = force || overOneHour;
|
||||||
|
if (!shouldFetch) {
|
||||||
|
return getCurrentCommitId();
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const data = await (await fetch(FETCH_COMMIT_URL)).json();
|
||||||
|
const sha = data[0].sha as string;
|
||||||
|
const remoteId = sha.substring(0, 7);
|
||||||
|
set(() => ({
|
||||||
|
lastUpdate: Date.now(),
|
||||||
|
remoteId,
|
||||||
|
}));
|
||||||
|
console.log("[Got Upstream] ", remoteId);
|
||||||
|
return remoteId;
|
||||||
|
} catch (error) {
|
||||||
|
console.error("[Fetch Upstream Commit Id]", error);
|
||||||
|
return getCurrentCommitId();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
{
|
||||||
|
name: UPDATE_KEY,
|
||||||
|
version: 1,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
);
|
Loading…
Reference in new issue