From 58563ece932b11c07f22cb369b826a15368e663e Mon Sep 17 00:00:00 2001 From: abearxiong Date: Fri, 27 Feb 2026 01:59:06 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E8=BF=9C=E7=AB=AF?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E4=BF=9D=E5=AD=98=E4=B8=8E=E5=8A=A0=E8=BD=BD?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=EF=BC=8C=E4=BC=98=E5=8C=96=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E6=A3=80=E6=9F=A5=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pages/config/gitea/page.tsx | 21 ++++++++++- src/pages/config/gitea/store/index.ts | 53 ++++++++++++++++++++++++++- 2 files changed, 72 insertions(+), 2 deletions(-) diff --git a/src/pages/config/gitea/page.tsx b/src/pages/config/gitea/page.tsx index 0c9c0d8..d54847d 100644 --- a/src/pages/config/gitea/page.tsx +++ b/src/pages/config/gitea/page.tsx @@ -5,10 +5,20 @@ import { Label } from '@/components/ui/label'; import { Button } from '@/components/ui/button'; import { giteaConfigSchema } from './store/schema'; import { toast } from 'sonner'; +import { useLayoutStore } from '../../auth/store'; +import { useShallow } from 'zustand/shallow'; +import { useEffect } from 'react'; export const GiteaConfigPage = () => { - const { config, setConfig, resetConfig } = useGiteaConfigStore(); + const { config, setConfig, resetConfig, saveToRemote, loadFromRemote, checkConfig } = useGiteaConfigStore(); + const layoutStore = useLayoutStore(useShallow(state => ({ me: state.me }))) + + useEffect(() => { + if (layoutStore.me) { + checkConfig({ isUser: !!layoutStore.me, reload: true }) + } + }, []) const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); @@ -66,6 +76,15 @@ export const GiteaConfigPage = () => { + {layoutStore.me && <> + + + + } diff --git a/src/pages/config/gitea/store/index.ts b/src/pages/config/gitea/store/index.ts index 36807ce..fa03eba 100644 --- a/src/pages/config/gitea/store/index.ts +++ b/src/pages/config/gitea/store/index.ts @@ -1,10 +1,15 @@ import { create } from 'zustand'; import type { GiteaConfig } from './schema'; +import { queryLogin } from '@/modules/query'; +import { toast } from 'sonner'; type GiteaConfigState = { config: GiteaConfig; setConfig: (config: Partial) => void; resetConfig: () => void; + saveToRemote: () => Promise; + loadFromRemote: () => Promise; + checkConfig: (opts?: { isUser?: boolean, reload?: boolean }) => Promise; }; const DEFAULT_CONFIG: GiteaConfig = { @@ -35,7 +40,7 @@ const saveConfig = (config: GiteaConfig) => { } }; -export const useGiteaConfigStore = create()((set) => ({ +export const useGiteaConfigStore = create()((set, get) => ({ config: loadInitialConfig(), setConfig: (newConfig) => set((state) => { @@ -47,4 +52,50 @@ export const useGiteaConfigStore = create()((set) => ({ saveConfig(DEFAULT_CONFIG); return set({ config: DEFAULT_CONFIG }); }, + saveToRemote: async () => { + const _config = get().config; + const res = await queryLogin.post({ + path: 'config', + key: 'update', + data: { + key: 'gitea_config.json', + data: _config, + } + }); + if (res.code === 200) { + toast.success('保存到远端成功') + } else { + toast.error('保存到远端失败') + } + }, + loadFromRemote: async () => { + const setConfig = (config: GiteaConfig) => set({ config }); + const res = await queryLogin.post({ + path: 'config', + key: 'get', + data: { + key: 'gitea_config.json', + } + }) + if (res.code === 404) { + toast.error('远端配置不存在') + return; + } + if (res.code === 200) { + const config = res.data?.data as GiteaConfig; + setConfig(config); + toast.success('获取远端配置成功') + } + }, + checkConfig: async (opts?: { isUser?: boolean, reload?: boolean }) => { + const { GITEA_TOKEN } = get().config; + if (!GITEA_TOKEN && opts?.isUser) { + await get().loadFromRemote(); + if (opts?.reload) { + location.reload(); + } + return true + } + return false + } }));