forked from QuantumNous/new-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.tsx
More file actions
173 lines (158 loc) · 5.05 KB
/
Copy pathmain.tsx
File metadata and controls
173 lines (158 loc) · 5.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
Copyright (C) 2023-2026 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 {
QueryCache,
QueryClient,
QueryClientProvider,
} from '@tanstack/react-query'
import { RouterProvider, createRouter } from '@tanstack/react-router'
import { AxiosError } from 'axios'
import i18next from 'i18next'
import { StrictMode } from 'react'
import ReactDOM from 'react-dom/client'
import { toast } from 'sonner'
import { getStatus } from '@/lib/api'
import { installBuildMetadata } from '@/lib/build-metadata'
import { applyFaviconToDom } from '@/lib/dom-utils'
import '@/lib/dayjs'
import { initializeFrontendCache } from '@/lib/frontend-cache'
import { handleServerError } from '@/lib/handle-server-error'
import { DirectionProvider } from './context/direction-provider'
import { FontProvider } from './context/font-provider'
import { ThemeProvider } from './context/theme-provider'
import './i18n/config'
// Generated Routes
import { routeTree } from './routeTree.gen'
// Styles
import './styles/index.css'
// Ensure VChart theme is initialized before any chart mounts (prevents white default theme flash)
// VChart theme is driven by our ThemeProvider (html.light/html.dark) via per-chart `theme` prop.
initializeFrontendCache()
installBuildMetadata()
const queryClient = new QueryClient({
defaultOptions: {
queries: {
retry: (failureCount, error) => {
// eslint-disable-next-line no-console
if (import.meta.env.DEV) console.log({ failureCount, error })
if (failureCount >= 0 && import.meta.env.DEV) return false
if (failureCount > 3 && import.meta.env.PROD) return false
return !(
error instanceof AxiosError &&
[401, 403].includes(error.response?.status ?? 0)
)
},
// Keep focused tabs from silently re-running heavy pages like logs.
refetchOnWindowFocus: false,
staleTime: 10 * 1000, // 10s
},
mutations: {
onError: (error) => {
handleServerError(error)
if (error instanceof AxiosError) {
if (error.response?.status === 304) {
toast.error(i18next.t('Content not modified!'))
}
}
},
},
},
queryCache: new QueryCache({
onError: (error) => {
if (error instanceof AxiosError) {
if (error.response?.status === 500) {
toast.error(i18next.t('Internal Server Error!'))
router.navigate({ to: '/500' })
}
}
},
}),
})
// Create a new router instance
const router = createRouter({
routeTree,
context: { queryClient },
defaultPreload: 'intent',
defaultPreloadStaleTime: 0,
})
// Register the router instance for type safety
declare module '@tanstack/react-router' {
interface Register {
router: typeof router
}
}
// Render the app
const rootElement = document.querySelector<HTMLElement>('#root')
if (!rootElement) {
throw new Error('Root element not found')
}
// Set document.title and favicon from cached status, then refresh from network
;(function initSystemBranding() {
try {
if (typeof window === 'undefined' || typeof document === 'undefined') return
const apply = (name: string) => {
document.title = name
const metaTitle = document.querySelector(
'meta[name="title"]'
) as HTMLMetaElement | null
if (metaTitle) metaTitle.setAttribute('content', name)
}
// Cache-first
try {
const saved = localStorage.getItem('status')
if (saved) {
const s = JSON.parse(saved)
if (s?.system_name) apply(s.system_name)
if (s?.logo) applyFaviconToDom(s.logo)
}
} catch {
/* empty */
}
// Background refresh
getStatus()
.then((s) => {
if (s?.system_name) {
apply(s.system_name as string)
try {
localStorage.setItem('status', JSON.stringify(s))
} catch {
/* empty */
}
}
if (s?.logo) applyFaviconToDom(s.logo as string)
})
.catch(() => {
/* empty */
})
} catch {
/* empty */
}
})()
if (!rootElement.innerHTML) {
const root = ReactDOM.createRoot(rootElement)
root.render(
<StrictMode>
<QueryClientProvider client={queryClient}>
<ThemeProvider>
<FontProvider>
<DirectionProvider>
<RouterProvider router={router} />
</DirectionProvider>
</FontProvider>
</ThemeProvider>
</QueryClientProvider>
</StrictMode>
)
}