app.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import { PropsWithChildren, useState, useEffect } from 'react';
  2. import { useLaunch } from '@tarojs/taro';
  3. import { IntlProvider } from 'react-intl';
  4. import { ConfigProvider, Button } from 'antd';
  5. import { Provider } from 'react-redux';
  6. import store from './states/store';
  7. import { initializeProductState } from './states/productSlice';
  8. import './app.css';
  9. import { lightTheme, darkTheme } from './themes';
  10. import ProductSelector from './components/ProductSelector';
  11. import QuotaAlertModal from './pages/security/QuotaAlertModal';
  12. const locale = (window.navigator.language || 'en').toLowerCase().split('-')[0]; // Get locale from browser or default to 'en'
  13. import messages_en from './assets/i18n/messages/en';
  14. import messages_zh from './assets/i18n/messages/zh';
  15. import AcquisitionTracer from './pages/exam/components/acquisitionTracer';
  16. const messages = locale === 'zh' ? messages_zh : messages_en;
  17. console.log(`process.env.USE_MSW: ${process.env.USE_MSW}`);
  18. console.log(`process.env.NODE_ENV: ${process.env.NODE_ENV}`);
  19. if (process.env.NODE_ENV === 'development' && process.env.USE_MSW === 'true') {
  20. import('../mocks/server')
  21. .then(({ server }) => {
  22. server.start({
  23. onUnhandledRequest: 'error', // 未处理的请求触发网络错误
  24. });
  25. console.log(`启动了MSW`);
  26. })
  27. .catch((err) => {
  28. console.warn('Mock server module not found:', err);
  29. });
  30. }
  31. function App({ children }: PropsWithChildren<React.ReactNode>) {
  32. useLaunch(() => {
  33. console.log('App launched.');
  34. });
  35. const [currentTheme, setCurrentTheme] = useState(lightTheme); // 默认使用 light 主题
  36. const changeTheme = (themeConfig: typeof lightTheme) => {
  37. setCurrentTheme(themeConfig);
  38. };
  39. useEffect(() => {
  40. store.dispatch(initializeProductState());
  41. }, []);
  42. // children 是将要会渲染的页面
  43. // return children
  44. return (
  45. <Provider store={store}>
  46. <ConfigProvider theme={currentTheme}>
  47. <IntlProvider locale={locale} messages={messages}>
  48. <style>
  49. {/*把theme中的colorPrimary转换成变量--color-primary,变量被tailwindcss使用*/}
  50. {`:root {
  51. --color-primary: ${currentTheme.token.colorPrimary};
  52. }`}
  53. </style>
  54. <div
  55. style={{
  56. backgroundColor: currentTheme.token.colorBgLayout,
  57. color: currentTheme.token.colorText,
  58. borderRadius: '8px',
  59. boxShadow: '0 2px 8px rgba(0,0,0,0.1)',
  60. }}
  61. >
  62. <AcquisitionTracer />
  63. <QuotaAlertModal />
  64. {children}
  65. {process.env.NODE_ENV === 'development' && <ProductSelector />}
  66. <div
  67. style={{
  68. position: 'fixed',
  69. top: '50%',
  70. right: 16,
  71. transform: 'translateY(-50%)',
  72. display: 'flex',
  73. flexDirection: 'column',
  74. gap: 8, // 按钮间距
  75. zIndex: 1000,
  76. }}
  77. >
  78. <Button
  79. type="primary"
  80. shape="circle"
  81. size="large"
  82. onClick={() => changeTheme(darkTheme)}
  83. title="Switch to Dark Theme"
  84. >
  85. 🌙
  86. </Button>
  87. <Button
  88. type="primary"
  89. shape="circle"
  90. size="large"
  91. onClick={() => changeTheme(lightTheme)}
  92. title="Switch to Light Theme"
  93. >
  94. ☀️
  95. </Button>
  96. </div>
  97. </div>
  98. </IntlProvider>
  99. </ConfigProvider>
  100. </Provider>
  101. );
  102. }
  103. export default App;