app.tsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. const locale = (window.navigator.language || 'en').toLowerCase().split('-')[0]; // Get locale from browser or default to 'en'
  12. import messages_en from './assets/i18n/messages/en';
  13. import messages_zh from './assets/i18n/messages/zh';
  14. const messages = locale === 'zh' ? messages_zh : messages_en;
  15. console.log(`process.env.USE_MSW: ${process.env.USE_MSW}`);
  16. console.log(`process.env.NODE_ENV: ${process.env.NODE_ENV}`);
  17. if (process.env.NODE_ENV === 'development' && process.env.USE_MSW === 'true') {
  18. import('../mocks/server')
  19. .then(({ server }) => {
  20. server.start({
  21. onUnhandledRequest: 'bypass', // Ignore unhandled requests
  22. });
  23. })
  24. .catch((err) => {
  25. console.warn('Mock server module not found:', err);
  26. });
  27. }
  28. function App({ children }: PropsWithChildren<React.ReactNode>) {
  29. useLaunch(() => {
  30. console.log('App launched.');
  31. });
  32. const [currentTheme, setCurrentTheme] = useState(lightTheme); // 默认使用 light 主题
  33. const changeTheme = (themeConfig: typeof lightTheme) => {
  34. setCurrentTheme(themeConfig);
  35. };
  36. useEffect(() => {
  37. store.dispatch(initializeProductState());
  38. }, []);
  39. // children 是将要会渲染的页面
  40. // return children
  41. return (
  42. <Provider store={store}>
  43. <ConfigProvider theme={currentTheme}>
  44. <IntlProvider locale={locale} messages={messages}>
  45. <div
  46. style={{
  47. backgroundColor: currentTheme.token.colorBgLayout,
  48. color: currentTheme.token.colorText,
  49. borderRadius: '8px',
  50. boxShadow: '0 2px 8px rgba(0,0,0,0.1)',
  51. }}
  52. >
  53. {children}
  54. {process.env.NODE_ENV === 'development' && <ProductSelector />}
  55. <div
  56. style={{
  57. position: 'fixed',
  58. top: '50%',
  59. right: 16,
  60. transform: 'translateY(-50%)',
  61. display: 'flex',
  62. flexDirection: 'column',
  63. gap: 8, // 按钮间距
  64. zIndex: 1000,
  65. }}
  66. >
  67. <Button
  68. type="primary"
  69. shape="circle"
  70. size="large"
  71. onClick={() => changeTheme(darkTheme)}
  72. title="Switch to Dark Theme"
  73. >
  74. 🌙
  75. </Button>
  76. <Button
  77. type="primary"
  78. shape="circle"
  79. size="large"
  80. onClick={() => changeTheme(lightTheme)}
  81. title="Switch to Light Theme"
  82. >
  83. ☀️
  84. </Button>
  85. </div>
  86. </div>
  87. </IntlProvider>
  88. </ConfigProvider>
  89. </Provider>
  90. );
  91. }
  92. export default App;