_app.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import { ChatContext, ChatContextProvider } from '@/app/chat-context';
  2. import SideBar from '@/components/layout/side-bar';
  3. import FloatHelper from '@/new-components/layout/FloatHelper';
  4. import { STORAGE_LANG_KEY, STORAGE_USERINFO_KEY, STORAGE_USERINFO_VALID_TIME_KEY } from '@/utils/constants/index';
  5. import { App, ConfigProvider, MappingAlgorithm, theme } from 'antd';
  6. import enUS from 'antd/locale/en_US';
  7. import zhCN from 'antd/locale/zh_CN';
  8. import classNames from 'classnames';
  9. import type { AppProps } from 'next/app';
  10. import Head from 'next/head';
  11. import { useRouter } from 'next/router';
  12. import React, { useContext, useEffect, useState } from 'react';
  13. import { useTranslation } from 'react-i18next';
  14. import '../app/i18n';
  15. import '../nprogress.css';
  16. import '../styles/globals.css';
  17. // import TopProgressBar from '@/components/layout/top-progress-bar';
  18. const antdDarkTheme: MappingAlgorithm = (seedToken, mapToken) => {
  19. return {
  20. ...theme.darkAlgorithm(seedToken, mapToken),
  21. colorBgBase: '#232734',
  22. colorBorder: '#828282',
  23. colorBgContainer: '#232734',
  24. };
  25. };
  26. function CssWrapper({ children }: { children: React.ReactElement }) {
  27. const { mode } = useContext(ChatContext);
  28. const { i18n } = useTranslation();
  29. useEffect(() => {
  30. if (mode) {
  31. document.body?.classList?.add(mode);
  32. if (mode === 'light') {
  33. document.body?.classList?.remove('dark');
  34. } else {
  35. document.body?.classList?.remove('light');
  36. }
  37. }
  38. }, [mode]);
  39. useEffect(() => {
  40. i18n.changeLanguage?.(window.localStorage.getItem(STORAGE_LANG_KEY) || 'zh');
  41. }, [i18n]);
  42. return (
  43. <div>
  44. {/* <TopProgressBar /> */}
  45. {children}
  46. </div>
  47. );
  48. }
  49. function LayoutWrapper({ children }: { children: React.ReactNode }) {
  50. const { isMenuExpand, mode } = useContext(ChatContext);
  51. const { i18n } = useTranslation();
  52. const [isLogin, setIsLogin] = useState(false);
  53. const router = useRouter();
  54. // 登录检测
  55. const handleAuth = async () => {
  56. setIsLogin(false);
  57. // 如果已有登录信息,直接展示首页
  58. // if (localStorage.getItem(STORAGE_USERINFO_KEY)) {
  59. // setIsLogin(true);
  60. // return;
  61. // }
  62. // MOCK User info
  63. const user = {
  64. user_channel: `dbgpt`,
  65. user_no: `001`,
  66. nick_name: `dbgpt`,
  67. };
  68. if (user) {
  69. localStorage.setItem(STORAGE_USERINFO_KEY, JSON.stringify(user));
  70. localStorage.setItem(STORAGE_USERINFO_VALID_TIME_KEY, Date.now().toString());
  71. setIsLogin(true);
  72. }
  73. };
  74. useEffect(() => {
  75. handleAuth();
  76. }, []);
  77. if (!isLogin) {
  78. return null;
  79. }
  80. const renderContent = () => {
  81. if (router.pathname.includes('mobile')) {
  82. return <>{children}</>;
  83. }
  84. return (
  85. <div className='flex w-screen h-screen overflow-hidden'>
  86. <Head>
  87. <meta name='viewport' content='initial-scale=1.0, width=device-width, maximum-scale=1' />
  88. </Head>
  89. {router.pathname !== '/construct/app/extra' && (
  90. <div className={classNames('transition-[width]', isMenuExpand ? 'w-60' : 'w-20', 'hidden', 'md:block')}>
  91. <SideBar />
  92. </div>
  93. )}
  94. <div className='flex flex-col flex-1 relative overflow-hidden'>{children}</div>
  95. <FloatHelper />
  96. </div>
  97. );
  98. };
  99. return (
  100. <ConfigProvider
  101. locale={i18n.language === 'en' ? enUS : zhCN}
  102. theme={{
  103. token: {
  104. colorPrimary: '#0C75FC',
  105. borderRadius: 4,
  106. },
  107. algorithm: mode === 'dark' ? antdDarkTheme : undefined,
  108. }}
  109. >
  110. <App>{renderContent()}</App>
  111. </ConfigProvider>
  112. );
  113. }
  114. function MyApp({ Component, pageProps }: AppProps) {
  115. return (
  116. <ChatContextProvider>
  117. <CssWrapper>
  118. <LayoutWrapper>
  119. <Component {...pageProps} />
  120. </LayoutWrapper>
  121. </CssWrapper>
  122. </ChatContextProvider>
  123. );
  124. }
  125. export default MyApp;