Files
monkey-ai-web/src/index.jsx
T
2026-10-15 16:33:46 +08:00

124 lines
4.7 KiB
React
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*
Copyright (C) 2025 QuantumNous
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
For commercial licensing, please contact support@quantumnous.com
*/
import React from 'react';
import ReactDOM from 'react-dom/client';
import { BrowserRouter } from 'react-router-dom';
import '~/@douyinfe/semi-ui/dist/css/semi.css';
import { UserProvider } from './context/User';
import 'react-toastify/dist/ReactToastify.css';
import { StatusProvider } from './context/Status';
import { ThemeProvider } from './context/Theme';
import PageLayout from './components/layout/PageLayout';
import i18n, { loadLanguage } from './i18n/i18n';
import { bootstrapLanguage, createBrowserEnv } from './i18n/bootstrap';
import { API } from './helpers';
import './styles/tokens.css';
import './index.css';
import './styles/org.css';
import { applyThemeFromCache } from './helpers/theme';
import { installChunkReloadHandlers } from './helpers/chunkReload';
import ChunkErrorBoundary from './components/common/ChunkErrorBoundary';
// 在 React 渲染之前同步注入上次缓存的主题,避免首屏 FOUC
applyThemeFromCache();
installChunkReloadHandlers();
import { LocaleProvider } from '@douyinfe/semi-ui';
import { useTranslation } from 'react-i18next';
import zh_CN from '@douyinfe/semi-ui/lib/es/locale/source/zh_CN';
import en_GB from '@douyinfe/semi-ui/lib/es/locale/source/en_GB';
import zh_TW from '@douyinfe/semi-ui/lib/es/locale/source/zh_TW';
import ja_JP from '@douyinfe/semi-ui/lib/es/locale/source/ja_JP';
import ko_KR from '@douyinfe/semi-ui/lib/es/locale/source/ko_KR';
import vi_VN from '@douyinfe/semi-ui/lib/es/locale/source/vi_VN';
import th_TH from '@douyinfe/semi-ui/lib/es/locale/source/th_TH';
import { normalizeLanguage } from './i18n/language';
// 控制台欢迎信息改为后端可配置(ConsoleWelcomeTitle / ConsoleWelcomeLink),
// 在 PageLayout 拉取 /api/status 后打印
function SemiLocaleWrapper({ children }) {
const { i18n } = useTranslation();
// i18n.language 可能带地区码(如 zh-CN / ko-KR),先归一化再查表,
// 否则一律落回 zh_CN,Semi 自带文案(分页、日期选择器、空态)不会跟着切
const semiLocale = React.useMemo(
() =>
({
zh: zh_CN,
'zh-TW': zh_TW,
en: en_GB,
ja: ja_JP,
ko: ko_KR,
vi: vi_VN,
th: th_TH,
})[normalizeLanguage(i18n.language)] || zh_CN,
[i18n.language],
);
return <LocaleProvider locale={semiLocale}>{children}</LocaleProvider>;
}
// initialization
console.log('[App] Initializing React application...');
const root = ReactDOM.createRoot(document.getElementById('root'));
const renderApp = () => {
console.log('[App] Rendering root component...');
root.render(
<React.StrictMode>
<StatusProvider>
<UserProvider>
<BrowserRouter
future={{
v7_startTransition: true,
v7_relativeSplatPath: true,
}}
>
<ThemeProvider>
<SemiLocaleWrapper>
<ChunkErrorBoundary>
<PageLayout />
</ChunkErrorBoundary>
</SemiLocaleWrapper>
</ThemeProvider>
</BrowserRouter>
</UserProvider>
</StatusProvider>
</React.StrictMode>,
);
};
// 界面语言在渲染前判定:有本地缓存零等待,仅首访等待 /api/i18n/locale(超时回退),
// 避免先渲染回退语言再闪切。判定异常也照常渲染。
// 用户选择与浏览器语言走请求头而非 query:gin 访问日志会把完整 query 连同客户端 IP 落盘,
// 放在 URL 里等于每次首屏持久化一条"IP + 浏览器语言列表"指纹(R83 中-1)
const fetchLocale = (params) =>
API.get('/api/i18n/locale', {
headers: {
'X-UI-Language-Choice': params.user_lang || '',
'X-UI-Languages': params.browser || '',
},
skipErrorHandler: true,
disableDuplicate: true,
}).then((res) => (res.data?.success ? res.data.data : null));
bootstrapLanguage(i18n, createBrowserEnv({ fetchLocale, loadLanguage }))
.catch((error) => console.error('[App] Language bootstrap failed:', error))
.then(renderApp);