You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

app.tsx 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import i18n from '@/locales/config';
  2. import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
  3. import { ReactQueryDevtools } from '@tanstack/react-query-devtools';
  4. import { App, ConfigProvider, ConfigProviderProps } from 'antd';
  5. import enUS from 'antd/locale/en_US';
  6. import zhCN from 'antd/locale/zh_CN';
  7. import zh_HK from 'antd/locale/zh_HK';
  8. import dayjs from 'dayjs';
  9. import advancedFormat from 'dayjs/plugin/advancedFormat';
  10. import customParseFormat from 'dayjs/plugin/customParseFormat';
  11. import localeData from 'dayjs/plugin/localeData';
  12. import weekOfYear from 'dayjs/plugin/weekOfYear';
  13. import weekYear from 'dayjs/plugin/weekYear';
  14. import weekday from 'dayjs/plugin/weekday';
  15. import React, { ReactNode, useEffect, useState } from 'react';
  16. import storage from './utils/authorization-util';
  17. dayjs.extend(customParseFormat);
  18. dayjs.extend(advancedFormat);
  19. dayjs.extend(weekday);
  20. dayjs.extend(localeData);
  21. dayjs.extend(weekOfYear);
  22. dayjs.extend(weekYear);
  23. const AntLanguageMap = {
  24. en: enUS,
  25. zh: zhCN,
  26. 'zh-TRADITIONAL': zh_HK,
  27. };
  28. const queryClient = new QueryClient();
  29. type Locale = ConfigProviderProps['locale'];
  30. const RootProvider = ({ children }: React.PropsWithChildren) => {
  31. const getLocale = (lng: string) =>
  32. AntLanguageMap[lng as keyof typeof AntLanguageMap] ?? enUS;
  33. const [locale, setLocal] = useState<Locale>(getLocale(storage.getLanguage()));
  34. i18n.on('languageChanged', function (lng: string) {
  35. storage.setLanguage(lng);
  36. setLocal(getLocale(lng));
  37. });
  38. useEffect(() => {
  39. // 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.
  40. const lng = storage.getLanguage();
  41. if (lng) {
  42. i18n.changeLanguage(lng);
  43. }
  44. }, []);
  45. return (
  46. <QueryClientProvider client={queryClient}>
  47. <ConfigProvider
  48. theme={{
  49. token: {
  50. fontFamily: 'Inter',
  51. },
  52. }}
  53. locale={locale}
  54. >
  55. <App> {children}</App>
  56. </ConfigProvider>
  57. <ReactQueryDevtools buttonPosition={'top-left'} />
  58. </QueryClientProvider>
  59. );
  60. };
  61. export function rootContainer(container: ReactNode) {
  62. return <RootProvider>{container}</RootProvider>;
  63. }