♻️ refactor: refactor the logic of fetchTokenKeys and SetupCheck
This commit is contained in:
@@ -25,7 +25,7 @@ import Playground from './pages/Playground/index.js';
|
|||||||
import OAuth2Callback from './components/auth/OAuth2Callback.js';
|
import OAuth2Callback from './components/auth/OAuth2Callback.js';
|
||||||
import PersonalSetting from './components/settings/PersonalSetting.js';
|
import PersonalSetting from './components/settings/PersonalSetting.js';
|
||||||
import Setup from './pages/Setup/index.js';
|
import Setup from './pages/Setup/index.js';
|
||||||
import SetupCheck from './components/SetupCheck';
|
import { useSetupCheck } from './hooks/useSetupCheck.js';
|
||||||
|
|
||||||
const Home = lazy(() => import('./pages/Home'));
|
const Home = lazy(() => import('./pages/Home'));
|
||||||
const Detail = lazy(() => import('./pages/Detail'));
|
const Detail = lazy(() => import('./pages/Detail'));
|
||||||
@@ -35,7 +35,7 @@ function App() {
|
|||||||
const location = useLocation();
|
const location = useLocation();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<SetupCheck>
|
<useSetupCheck>
|
||||||
<Routes>
|
<Routes>
|
||||||
<Route
|
<Route
|
||||||
path='/'
|
path='/'
|
||||||
@@ -290,7 +290,7 @@ function App() {
|
|||||||
/>
|
/>
|
||||||
<Route path='*' element={<NotFound />} />
|
<Route path='*' element={<NotFound />} />
|
||||||
</Routes>
|
</Routes>
|
||||||
</SetupCheck>
|
</useSetupCheck>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,18 +0,0 @@
|
|||||||
import React, { useContext, useEffect } from 'react';
|
|
||||||
import { Navigate, useLocation } from 'react-router-dom';
|
|
||||||
import { StatusContext } from '../context/Status';
|
|
||||||
|
|
||||||
const SetupCheck = ({ children }) => {
|
|
||||||
const [statusState] = useContext(StatusContext);
|
|
||||||
const location = useLocation();
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (statusState?.status?.setup === false && location.pathname !== '/setup') {
|
|
||||||
window.location.href = '/setup';
|
|
||||||
}
|
|
||||||
}, [statusState?.status?.setup, location.pathname]);
|
|
||||||
|
|
||||||
return children;
|
|
||||||
};
|
|
||||||
|
|
||||||
export default SetupCheck;
|
|
||||||
@@ -1,68 +0,0 @@
|
|||||||
// src/hooks/useTokenKeys.js
|
|
||||||
import { useEffect, useState } from 'react';
|
|
||||||
import { API, showError } from '../helpers';
|
|
||||||
|
|
||||||
async function fetchTokenKeys() {
|
|
||||||
try {
|
|
||||||
const response = await API.get('/api/token/?p=0&size=100');
|
|
||||||
const { success, data } = response.data;
|
|
||||||
if (success) {
|
|
||||||
const activeTokens = data.filter((token) => token.status === 1);
|
|
||||||
return activeTokens.map((token) => token.key);
|
|
||||||
} else {
|
|
||||||
throw new Error('Failed to fetch token keys');
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Error fetching token keys:', error);
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function getServerAddress() {
|
|
||||||
let status = localStorage.getItem('status');
|
|
||||||
let serverAddress = '';
|
|
||||||
|
|
||||||
if (status) {
|
|
||||||
try {
|
|
||||||
status = JSON.parse(status);
|
|
||||||
serverAddress = status.server_address || '';
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Failed to parse status from localStorage:', error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!serverAddress) {
|
|
||||||
serverAddress = window.location.origin;
|
|
||||||
}
|
|
||||||
|
|
||||||
return serverAddress;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function useTokenKeys(id) {
|
|
||||||
const [keys, setKeys] = useState([]);
|
|
||||||
// const [chatLink, setChatLink] = useState('');
|
|
||||||
const [serverAddress, setServerAddress] = useState('');
|
|
||||||
const [isLoading, setIsLoading] = useState(true);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
const loadAllData = async () => {
|
|
||||||
const fetchedKeys = await fetchTokenKeys();
|
|
||||||
if (fetchedKeys.length === 0) {
|
|
||||||
showError('当前没有可用的启用令牌,请确认是否有令牌处于启用状态!');
|
|
||||||
setTimeout(() => {
|
|
||||||
window.location.href = '/token';
|
|
||||||
}, 1500); // 延迟 1.5 秒后跳转
|
|
||||||
}
|
|
||||||
setKeys(fetchedKeys);
|
|
||||||
setIsLoading(false);
|
|
||||||
// setChatLink(link);
|
|
||||||
|
|
||||||
const address = getServerAddress();
|
|
||||||
setServerAddress(address);
|
|
||||||
};
|
|
||||||
|
|
||||||
loadAllData();
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
return { keys, serverAddress, isLoading };
|
|
||||||
}
|
|
||||||
@@ -5,3 +5,4 @@ export * from './api';
|
|||||||
export * from './render';
|
export * from './render';
|
||||||
export * from './log';
|
export * from './log';
|
||||||
export * from './data';
|
export * from './data';
|
||||||
|
export * from './token';
|
||||||
|
|||||||
45
web/src/helpers/token.js
Normal file
45
web/src/helpers/token.js
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
import { API } from './api';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取可用的token keys
|
||||||
|
* @returns {Promise<string[]>} 返回active状态的token key数组
|
||||||
|
*/
|
||||||
|
export async function fetchTokenKeys() {
|
||||||
|
try {
|
||||||
|
const response = await API.get('/api/token/?p=0&size=100');
|
||||||
|
const { success, data } = response.data;
|
||||||
|
if (success) {
|
||||||
|
const activeTokens = data.filter((token) => token.status === 1);
|
||||||
|
return activeTokens.map((token) => token.key);
|
||||||
|
} else {
|
||||||
|
throw new Error('Failed to fetch token keys');
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error fetching token keys:', error);
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取服务器地址
|
||||||
|
* @returns {string} 服务器地址
|
||||||
|
*/
|
||||||
|
export function getServerAddress() {
|
||||||
|
let status = localStorage.getItem('status');
|
||||||
|
let serverAddress = '';
|
||||||
|
|
||||||
|
if (status) {
|
||||||
|
try {
|
||||||
|
status = JSON.parse(status);
|
||||||
|
serverAddress = status.server_address || '';
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Failed to parse status from localStorage:', error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!serverAddress) {
|
||||||
|
serverAddress = window.location.origin;
|
||||||
|
}
|
||||||
|
|
||||||
|
return serverAddress;
|
||||||
|
}
|
||||||
32
web/src/hooks/useSetupCheck.js
Normal file
32
web/src/hooks/useSetupCheck.js
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
import { useContext, useEffect } from 'react';
|
||||||
|
import { useLocation } from 'react-router-dom';
|
||||||
|
import { StatusContext } from '../context/Status';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 自定义Hook:检查系统setup状态并进行重定向
|
||||||
|
* @param {Object} options - 配置选项
|
||||||
|
* @param {boolean} options.autoRedirect - 是否自动重定向,默认true
|
||||||
|
* @param {string} options.setupPath - setup页面路径,默认'/setup'
|
||||||
|
* @returns {Object} 返回setup状态信息
|
||||||
|
*/
|
||||||
|
export function useSetupCheck(options = {}) {
|
||||||
|
const { autoRedirect = true, setupPath = '/setup' } = options;
|
||||||
|
const [statusState] = useContext(StatusContext);
|
||||||
|
const location = useLocation();
|
||||||
|
|
||||||
|
const isSetupComplete = statusState?.status?.setup !== false;
|
||||||
|
const needsSetup = !isSetupComplete && location.pathname !== setupPath;
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (autoRedirect && needsSetup) {
|
||||||
|
window.location.href = setupPath;
|
||||||
|
}
|
||||||
|
}, [autoRedirect, needsSetup, setupPath]);
|
||||||
|
|
||||||
|
return {
|
||||||
|
isSetupComplete,
|
||||||
|
needsSetup,
|
||||||
|
statusState,
|
||||||
|
currentPath: location.pathname
|
||||||
|
};
|
||||||
|
}
|
||||||
30
web/src/hooks/useTokenKeys.js
Normal file
30
web/src/hooks/useTokenKeys.js
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
import { useEffect, useState } from 'react';
|
||||||
|
import { fetchTokenKeys, getServerAddress } from '../helpers/token';
|
||||||
|
import { showError } from '../helpers';
|
||||||
|
|
||||||
|
export function useTokenKeys(id) {
|
||||||
|
const [keys, setKeys] = useState([]);
|
||||||
|
const [serverAddress, setServerAddress] = useState('');
|
||||||
|
const [isLoading, setIsLoading] = useState(true);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const loadAllData = async () => {
|
||||||
|
const fetchedKeys = await fetchTokenKeys();
|
||||||
|
if (fetchedKeys.length === 0) {
|
||||||
|
showError('当前没有可用的启用令牌,请确认是否有令牌处于启用状态!');
|
||||||
|
setTimeout(() => {
|
||||||
|
window.location.href = '/token';
|
||||||
|
}, 1500); // 延迟 1.5 秒后跳转
|
||||||
|
}
|
||||||
|
setKeys(fetchedKeys);
|
||||||
|
setIsLoading(false);
|
||||||
|
|
||||||
|
const address = getServerAddress();
|
||||||
|
setServerAddress(address);
|
||||||
|
};
|
||||||
|
|
||||||
|
loadAllData();
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
return { keys, serverAddress, isLoading };
|
||||||
|
}
|
||||||
@@ -16,7 +16,7 @@ import './index.css';
|
|||||||
const root = ReactDOM.createRoot(document.getElementById('root'));
|
const root = ReactDOM.createRoot(document.getElementById('root'));
|
||||||
const { Sider, Content, Header, Footer } = Layout;
|
const { Sider, Content, Header, Footer } = Layout;
|
||||||
root.render(
|
root.render(
|
||||||
// <React.StrictMode>
|
<React.StrictMode>
|
||||||
<StatusProvider>
|
<StatusProvider>
|
||||||
<UserProvider>
|
<UserProvider>
|
||||||
<BrowserRouter>
|
<BrowserRouter>
|
||||||
@@ -28,5 +28,5 @@ root.render(
|
|||||||
</BrowserRouter>
|
</BrowserRouter>
|
||||||
</UserProvider>
|
</UserProvider>
|
||||||
</StatusProvider>
|
</StatusProvider>
|
||||||
// </React.StrictMode>,
|
</React.StrictMode>,
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import React, { useEffect } from 'react';
|
import React, { useEffect } from 'react';
|
||||||
import { useTokenKeys } from '../../components/fetchTokenKeys';
|
import { useTokenKeys } from '../../hooks/useTokenKeys';
|
||||||
import { Banner, Layout } from '@douyinfe/semi-ui';
|
import { Banner, Layout } from '@douyinfe/semi-ui';
|
||||||
import { useParams } from 'react-router-dom';
|
import { useParams } from 'react-router-dom';
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { useTokenKeys } from '../../components/fetchTokenKeys';
|
import { useTokenKeys } from '../../hooks/useTokenKeys';
|
||||||
|
|
||||||
const chat2page = () => {
|
const chat2page = () => {
|
||||||
const { keys, chatLink, serverAddress, isLoading } = useTokenKeys();
|
const { keys, chatLink, serverAddress, isLoading } = useTokenKeys();
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import { StatusContext } from '../../context/Status';
|
|||||||
import { marked } from 'marked';
|
import { marked } from 'marked';
|
||||||
import { useTranslation } from 'react-i18next';
|
import { useTranslation } from 'react-i18next';
|
||||||
import { IconGithubLogo } from '@douyinfe/semi-icons';
|
import { IconGithubLogo } from '@douyinfe/semi-icons';
|
||||||
import exampleImage from '..//example.png';
|
import exampleImage from '/example.png';
|
||||||
import { Link } from 'react-router-dom';
|
import { Link } from 'react-router-dom';
|
||||||
import NoticeModal from '../../components/layout/NoticeModal';
|
import NoticeModal from '../../components/layout/NoticeModal';
|
||||||
import { Moonshot, OpenAI, XAI, Zhipu, Volcengine, Cohere, Claude, Gemini, Suno, Minimax, Wenxin, Spark, Qingyan, DeepSeek, Qwen, Midjourney, Grok, AzureAI, Hunyuan, Xinference } from '@lobehub/icons';
|
import { Moonshot, OpenAI, XAI, Zhipu, Volcengine, Cohere, Claude, Gemini, Suno, Minimax, Wenxin, Spark, Qingyan, DeepSeek, Qwen, Midjourney, Grok, AzureAI, Hunyuan, Xinference } from '@lobehub/icons';
|
||||||
|
|||||||
Reference in New Issue
Block a user