refactor(admin): inline bootstrap.tsx into main.tsx
bootstrap.tsx 只有 3 个小函数,不值得单独文件。 合并到 main.tsx:运行时配置加载 + fallback + createRoot 全在一个文件里。
This commit is contained in:
1 parent
ab95b57139
commit
cda2585f1a
2 files changed
+35
-45
No files matched your search
@@ -1,39 +0,0 @@
|
||||
import {StrictMode, type ReactNode} from 'react';
|
||||
import {createRoot} from 'react-dom/client';
|
||||
import {QueryClient, QueryClientProvider} from '@tanstack/react-query';
|
||||
import dayjs from 'dayjs';
|
||||
import 'dayjs/locale/zh-cn';
|
||||
import './index.css';
|
||||
|
||||
dayjs.locale('zh-cn');
|
||||
|
||||
export const renderApplication = (application: ReactNode) => {
|
||||
const queryClient = new QueryClient({
|
||||
defaultOptions: {queries: {refetchOnWindowFocus: false, retry: 1}},
|
||||
});
|
||||
createRoot(document.getElementById('root')!).render(
|
||||
<StrictMode>
|
||||
<QueryClientProvider client={queryClient}>{application}</QueryClientProvider>
|
||||
</StrictMode>,
|
||||
);
|
||||
};
|
||||
|
||||
export const loadRuntimeConfig = async () => {
|
||||
if (window.PikaRuntime) return;
|
||||
const response = await fetch('/api/config');
|
||||
if (!response.ok) throw new Error('无法加载 Pika 运行时配置');
|
||||
const payload = await response.json();
|
||||
window.PikaRuntime = payload;
|
||||
window.SystemConfig = payload.legacySystemConfig;
|
||||
};
|
||||
|
||||
export const ensureRuntimeFallback = () => {
|
||||
if (window.SystemConfig) return;
|
||||
window.SystemConfig = {
|
||||
SystemNameZh: '皮卡监控',
|
||||
SystemNameEn: 'Pika Monitor',
|
||||
ICPCode: '',
|
||||
DefaultView: 'grid',
|
||||
Version: '',
|
||||
};
|
||||
};
|
||||
+35
-6
@@ -1,9 +1,38 @@
|
||||
import {StrictMode} from 'react';
|
||||
import {createRoot} from 'react-dom/client';
|
||||
import {QueryClient, QueryClientProvider} from '@tanstack/react-query';
|
||||
import dayjs from 'dayjs';
|
||||
import 'dayjs/locale/zh-cn';
|
||||
import AdminApp from './App';
|
||||
import {ensureRuntimeFallback, loadRuntimeConfig, renderApplication} from './bootstrap';
|
||||
import './index.css';
|
||||
|
||||
loadRuntimeConfig()
|
||||
.catch((error) => {
|
||||
console.error(error);
|
||||
ensureRuntimeFallback();
|
||||
dayjs.locale('zh-cn');
|
||||
|
||||
// 异步加载运行时配置,不阻塞渲染
|
||||
fetch('/api/config')
|
||||
.then((res) => res.ok ? res.json() : Promise.reject())
|
||||
.then((payload) => {
|
||||
window.PikaRuntime = payload;
|
||||
window.SystemConfig = payload.legacySystemConfig;
|
||||
})
|
||||
.finally(() => renderApplication(<AdminApp/>));
|
||||
.catch(() => {
|
||||
window.SystemConfig = {
|
||||
SystemNameZh: '皮卡监控',
|
||||
SystemNameEn: 'Pika Monitor',
|
||||
ICPCode: '',
|
||||
DefaultView: 'grid',
|
||||
Version: '',
|
||||
};
|
||||
})
|
||||
.finally(() => {
|
||||
const queryClient = new QueryClient({
|
||||
defaultOptions: {queries: {refetchOnWindowFocus: false, retry: 1}},
|
||||
});
|
||||
createRoot(document.getElementById('root')!).render(
|
||||
<StrictMode>
|
||||
<QueryClientProvider client={queryClient}>
|
||||
<AdminApp/>
|
||||
</QueryClientProvider>
|
||||
</StrictMode>,
|
||||
);
|
||||
});
|
||||
Reference in new issue
Block a user