2024-04-12 11:41:00 +08:00
import i18n from '@/locales/config' ;
2024-04-07 17:41:29 +08:00
import { App , ConfigProvider , ConfigProviderProps } from 'antd' ;
import enUS from 'antd/locale/en_US' ;
import zhCN from 'antd/locale/zh_CN' ;
2024-04-12 11:41:00 +08:00
import zh_HK from 'antd/locale/zh_HK' ;
2024-04-07 17:41:29 +08:00
import React , { ReactNode , useEffect , useState } from 'react' ;
import storage from './utils/authorizationUtil' ;
2024-06-05 10:46:06 +08:00
import { QueryClient , QueryClientProvider } from '@tanstack/react-query' ;
2024-04-16 19:06:47 +08:00
import dayjs from 'dayjs' ;
import advancedFormat from 'dayjs/plugin/advancedFormat' ;
import customParseFormat from 'dayjs/plugin/customParseFormat' ;
import localeData from 'dayjs/plugin/localeData' ;
import weekOfYear from 'dayjs/plugin/weekOfYear' ;
import weekYear from 'dayjs/plugin/weekYear' ;
2024-06-05 10:46:06 +08:00
import weekday from 'dayjs/plugin/weekday' ;
2024-04-16 19:06:47 +08:00
dayjs . extend ( customParseFormat ) ;
dayjs . extend ( advancedFormat ) ;
dayjs . extend ( weekday ) ;
dayjs . extend ( localeData ) ;
dayjs . extend ( weekOfYear ) ;
dayjs . extend ( weekYear ) ;
2024-04-12 11:41:00 +08:00
const AntLanguageMap = {
en : enUS ,
zh : zhCN ,
'zh-TRADITIONAL' : zh_HK ,
} ;
2024-06-05 10:46:06 +08:00
const queryClient = new QueryClient ( ) ;
2024-04-07 17:41:29 +08:00
type Locale = ConfigProviderProps [ 'locale' ] ;
const RootProvider = ( { children } : React . PropsWithChildren ) = > {
2024-04-12 11:41:00 +08:00
const getLocale = ( lng : string ) = >
AntLanguageMap [ lng as keyof typeof AntLanguageMap ] ? ? enUS ;
2024-04-07 17:41:29 +08:00
const [ locale , setLocal ] = useState < Locale > ( getLocale ( storage . getLanguage ( ) ) ) ;
2024-04-12 11:41:00 +08:00
i18n . on ( 'languageChanged' , function ( lng : string ) {
2024-04-07 17:41:29 +08:00
storage . setLanguage ( lng ) ;
setLocal ( getLocale ( lng ) ) ;
} ) ;
useEffect ( ( ) = > {
2024-04-08 10:41:03 +08:00
// Because the language is saved in the backend, a token is required to obtain the api. However, the login page cannot obtain the language through the getUserInfo api, so the language needs to be saved in localstorage.
const lng = storage . getLanguage ( ) ;
if ( lng ) {
i18n . changeLanguage ( lng ) ;
}
} , [ ] ) ;
2024-01-29 15:02:27 +08:00
2024-04-02 15:44:09 +08:00
return (
2024-06-05 10:46:06 +08:00
< QueryClientProvider client = { queryClient } >
< ConfigProvider
theme = { {
token : {
fontFamily : 'Inter' ,
} ,
} }
locale = { locale }
>
< App > { children } < / App >
< / ConfigProvider >
< / QueryClientProvider >
2024-03-20 18:20:42 +08:00
) ;
2024-04-07 17:41:29 +08:00
} ;
export function rootContainer ( container : ReactNode ) {
return < RootProvider > { container } < / RootProvider > ;
2024-01-29 15:02:27 +08:00
}