feat: #1000 ready to support client-side only

This commit is contained in:
Yidadaa
2023-06-14 00:37:42 +08:00
parent e6b49a60c0
commit 50cd33dbb2
13 changed files with 133 additions and 36 deletions

View File

@@ -1,9 +1,10 @@
import { create } from "zustand";
import { persist } from "zustand/middleware";
import { StoreKey } from "../constant";
import { DEFAULT_API_HOST, StoreKey } from "../constant";
import { getHeaders } from "../client/api";
import { BOT_HELLO } from "./chat";
import { ALL_MODELS } from "./config";
import { getClientConfig } from "../config/client";
export interface AccessControlStore {
accessCode: string;
@@ -15,6 +16,7 @@ export interface AccessControlStore {
updateToken: (_: string) => void;
updateCode: (_: string) => void;
updateOpenAiUrl: (_: string) => void;
enabledAccessControl: () => boolean;
isAuthorized: () => boolean;
fetch: () => void;
@@ -22,6 +24,10 @@ export interface AccessControlStore {
let fetchState = 0; // 0 not fetch, 1 fetching, 2 done
const DEFAULT_OPENAI_URL =
getClientConfig()?.buildMode === "export" ? DEFAULT_API_HOST : "/api/openai/";
console.log("[API] default openai url", DEFAULT_OPENAI_URL);
export const useAccessStore = create<AccessControlStore>()(
persist(
(set, get) => ({
@@ -29,7 +35,7 @@ export const useAccessStore = create<AccessControlStore>()(
accessCode: "",
needCode: true,
hideUserApiKey: false,
openaiUrl: "/api/openai/",
openaiUrl: DEFAULT_OPENAI_URL,
enabledAccessControl() {
get().fetch();
@@ -42,6 +48,9 @@ export const useAccessStore = create<AccessControlStore>()(
updateToken(token: string) {
set(() => ({ token }));
},
updateOpenAiUrl(url: string) {
set(() => ({ openaiUrl: url }));
},
isAuthorized() {
get().fetch();

View File

@@ -2,7 +2,7 @@ import { create } from "zustand";
import { persist } from "zustand/middleware";
import { FETCH_COMMIT_URL, StoreKey } from "../constant";
import { api } from "../client/api";
import { showToast } from "../components/ui-lib";
import { getClientConfig } from "../config/client";
export interface UpdateStore {
lastUpdate: number;
@@ -17,20 +17,6 @@ export interface UpdateStore {
updateUsage: (force?: boolean) => Promise<void>;
}
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;
}
const ONE_MINUTE = 60 * 1000;
export const useUpdateStore = create<UpdateStore>()(
@@ -44,7 +30,7 @@ export const useUpdateStore = create<UpdateStore>()(
version: "unknown",
async getLatestVersion(force = false) {
set(() => ({ version: queryMeta("version") ?? "unknown" }));
set(() => ({ version: getClientConfig()?.commitId ?? "unknown" }));
const overTenMins = Date.now() - get().lastUpdate > 10 * ONE_MINUTE;
if (!force && !overTenMins) return;