forked from XiaoMo/ChatGPT-Next-Web
feat: check usage throttle
This commit is contained in:
parent
525a2ff9a7
commit
fdc8278b90
@ -32,3 +32,14 @@
|
|||||||
min-width: 80%;
|
min-width: 80%;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.user-prompt-modal {
|
||||||
|
.user-prompt-search {
|
||||||
|
}
|
||||||
|
|
||||||
|
.user-prompt-list {
|
||||||
|
}
|
||||||
|
|
||||||
|
.user-prompt-actions {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { useState, useEffect, useMemo, HTMLProps } from "react";
|
import { useState, useEffect, useMemo, HTMLProps, useRef } from "react";
|
||||||
|
|
||||||
import EmojiPicker, { Theme as EmojiTheme } from "emoji-picker-react";
|
import EmojiPicker, { Theme as EmojiTheme } from "emoji-picker-react";
|
||||||
|
|
||||||
@ -34,6 +34,15 @@ import { requestUsage } from "../requests";
|
|||||||
import { ErrorBoundary } from "./error";
|
import { ErrorBoundary } from "./error";
|
||||||
import { InputRange } from "./input-range";
|
import { InputRange } from "./input-range";
|
||||||
|
|
||||||
|
function UserPromptModal() {
|
||||||
|
const promptStore = usePromptStore();
|
||||||
|
const prompts = Array.from(promptStore.prompts.values()).sort(
|
||||||
|
(a, b) => a.id ?? 0 - (b.id ?? 0),
|
||||||
|
);
|
||||||
|
|
||||||
|
return <></>;
|
||||||
|
}
|
||||||
|
|
||||||
function SettingItem(props: {
|
function SettingItem(props: {
|
||||||
title: string;
|
title: string;
|
||||||
subTitle?: string;
|
subTitle?: string;
|
||||||
@ -99,16 +108,14 @@ export function Settings(props: { closeSettings: () => void }) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const [usage, setUsage] = useState<{
|
const usage = {
|
||||||
used?: number;
|
used: updateStore.used,
|
||||||
subscription?: number;
|
subscription: updateStore.subscription,
|
||||||
}>();
|
};
|
||||||
const [loadingUsage, setLoadingUsage] = useState(false);
|
const [loadingUsage, setLoadingUsage] = useState(false);
|
||||||
function checkUsage() {
|
function checkUsage() {
|
||||||
setLoadingUsage(true);
|
setLoadingUsage(true);
|
||||||
requestUsage()
|
updateStore.updateUsage().finally(() => {
|
||||||
.then((res) => setUsage(res))
|
|
||||||
.finally(() => {
|
|
||||||
setLoadingUsage(false);
|
setLoadingUsage(false);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -126,6 +133,7 @@ export function Settings(props: { closeSettings: () => void }) {
|
|||||||
|
|
||||||
const showUsage = accessStore.isAuthorized();
|
const showUsage = accessStore.isAuthorized();
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
// checks per minutes
|
||||||
checkUpdate();
|
checkUpdate();
|
||||||
showUsage && checkUsage();
|
showUsage && checkUsage();
|
||||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||||
|
@ -1,13 +1,19 @@
|
|||||||
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 { requestUsage } from "../requests";
|
||||||
|
|
||||||
export interface UpdateStore {
|
export interface UpdateStore {
|
||||||
lastUpdate: number;
|
lastUpdate: number;
|
||||||
remoteVersion: string;
|
remoteVersion: string;
|
||||||
|
|
||||||
|
used?: number;
|
||||||
|
subscription?: number;
|
||||||
|
lastUpdateUsage: number;
|
||||||
|
|
||||||
version: string;
|
version: string;
|
||||||
getLatestVersion: (force: boolean) => Promise<string>;
|
getLatestVersion: (force?: boolean) => Promise<void>;
|
||||||
|
updateUsage: (force?: boolean) => Promise<void>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const UPDATE_KEY = "chat-update";
|
export const UPDATE_KEY = "chat-update";
|
||||||
@ -26,22 +32,23 @@ function queryMeta(key: string, defaultValue?: string): string {
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const ONE_MINUTE = 60 * 1000;
|
||||||
|
|
||||||
export const useUpdateStore = create<UpdateStore>()(
|
export const useUpdateStore = create<UpdateStore>()(
|
||||||
persist(
|
persist(
|
||||||
(set, get) => ({
|
(set, get) => ({
|
||||||
lastUpdate: 0,
|
lastUpdate: 0,
|
||||||
remoteVersion: "",
|
remoteVersion: "",
|
||||||
|
|
||||||
|
lastUpdateUsage: 0,
|
||||||
|
|
||||||
version: "unknown",
|
version: "unknown",
|
||||||
|
|
||||||
async getLatestVersion(force = false) {
|
async getLatestVersion(force = false) {
|
||||||
set(() => ({ version: queryMeta("version") }));
|
set(() => ({ version: queryMeta("version") ?? "unknown" }));
|
||||||
|
|
||||||
const overTenMins = Date.now() - get().lastUpdate > 10 * 60 * 1000;
|
const overTenMins = Date.now() - get().lastUpdate > 10 * ONE_MINUTE;
|
||||||
const shouldFetch = force || overTenMins;
|
if (!force && !overTenMins) return;
|
||||||
if (!shouldFetch) {
|
|
||||||
return get().version ?? "unknown";
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// const data = await (await fetch(FETCH_TAG_URL)).json();
|
// const data = await (await fetch(FETCH_TAG_URL)).json();
|
||||||
@ -53,10 +60,19 @@ export const useUpdateStore = create<UpdateStore>()(
|
|||||||
remoteVersion: remoteId,
|
remoteVersion: remoteId,
|
||||||
}));
|
}));
|
||||||
console.log("[Got Upstream] ", remoteId);
|
console.log("[Got Upstream] ", remoteId);
|
||||||
return remoteId;
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("[Fetch Upstream Commit Id]", error);
|
console.error("[Fetch Upstream Commit Id]", error);
|
||||||
return get().version ?? "";
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
async updateUsage(force = false) {
|
||||||
|
const overOneMinute = Date.now() - get().lastUpdateUsage >= ONE_MINUTE;
|
||||||
|
if (!overOneMinute && !force) return;
|
||||||
|
|
||||||
|
const usage = await requestUsage();
|
||||||
|
|
||||||
|
if (usage) {
|
||||||
|
set(() => usage);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
|
Loading…
Reference in New Issue
Block a user