feat: 启动时自动恢复配置,无需每次手动保存
InitConfig 组件在应用挂载时从 localStorage 读取保存的 API Key/模式,自动调用 PUT /api/config 恢复后端设置
This commit is contained in:
25
frontend/src/components/InitConfig.tsx
Normal file
25
frontend/src/components/InitConfig.tsx
Normal file
@@ -0,0 +1,25 @@
|
||||
import { useEffect } from 'react'
|
||||
|
||||
export default function InitConfig() {
|
||||
useEffect(() => {
|
||||
const openaiKey = localStorage.getItem('openai-key')
|
||||
const deepseekKey = localStorage.getItem('deepseek-key')
|
||||
const openaiBaseUrl = localStorage.getItem('openai-base-url')
|
||||
const deepseekBaseUrl = localStorage.getItem('deepseek-base-url')
|
||||
const aiMock = localStorage.getItem('ai-mock') ?? 'true'
|
||||
|
||||
const body: Record<string, string | undefined> = { aiMock }
|
||||
if (openaiKey) body.openaiKey = openaiKey
|
||||
if (deepseekKey) body.deepseekKey = deepseekKey
|
||||
if (openaiBaseUrl) body.openaiBaseUrl = openaiBaseUrl
|
||||
if (deepseekBaseUrl) body.deepseekBaseUrl = deepseekBaseUrl
|
||||
|
||||
fetch('/api/config', {
|
||||
method: 'PUT',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(body),
|
||||
}).catch(() => {})
|
||||
}, [])
|
||||
|
||||
return null
|
||||
}
|
||||
@@ -3,6 +3,7 @@ import { createRoot } from 'react-dom/client'
|
||||
import { RouterProvider } from 'react-router-dom'
|
||||
import { QueryClientProvider } from '@tanstack/react-query'
|
||||
import { ErrorBoundary } from './components/ErrorBoundary'
|
||||
import InitConfig from './components/InitConfig'
|
||||
import { router } from './router'
|
||||
import { queryClient } from './lib/queryClient'
|
||||
import './index.css'
|
||||
@@ -11,6 +12,7 @@ createRoot(document.getElementById('root')!).render(
|
||||
<StrictMode>
|
||||
<ErrorBoundary>
|
||||
<QueryClientProvider client={queryClient}>
|
||||
<InitConfig />
|
||||
<RouterProvider router={router} />
|
||||
</QueryClientProvider>
|
||||
</ErrorBoundary>
|
||||
|
||||
Reference in New Issue
Block a user