home.tsx 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. "use client";
  2. require("../polyfill");
  3. import { useState, useEffect } from "react";
  4. import {
  5. HashRouter as Router,
  6. Routes,
  7. Route,
  8. useLocation,
  9. } from "react-router-dom";
  10. import styles from "./home.module.scss";
  11. import BotIcon from "../icons/bot.svg";
  12. import loadingIcon from "../icons/loading.gif";
  13. import { getCSSVar, useMobileScreen } from "../utils";
  14. import dynamic from "next/dynamic";
  15. import { Path, SlotID } from "../constant";
  16. import { ErrorBoundary } from "./error";
  17. import { getISOLang, getLang } from "../locales";
  18. import { SideBar } from "./sidebar";
  19. import { useAppConfig } from "../store/config";
  20. import { AuthPage } from "./auth";
  21. import { getClientConfig } from "../config/client";
  22. import { type ClientApi, getClientApi } from "../client/api";
  23. import { useAccessStore } from "../store";
  24. import HomeApp from "./DeekSeekHome";
  25. import { Chat } from "./chat";
  26. import { Chat as DeepSeekChat } from "./DeepSeekChat";
  27. export function Loading() {
  28. /** second版本注释掉进度条 */
  29. // const [progress, setProgress] = useState(1);
  30. //
  31. // useEffect(() => {
  32. // let isMounted = true;
  33. //
  34. // const intervalId = setInterval(() => {
  35. // if (isMounted && progress < 100) {
  36. // // 每隔一段时间增加1%进度
  37. // setProgress(prevProgress => prevProgress + 1);
  38. // }
  39. // }, 30);// 每10毫秒更新1%进度
  40. //
  41. // return () => {
  42. // isMounted = false;
  43. // clearInterval(intervalId);
  44. // };
  45. // }, [progress]);
  46. return (
  47. <div className={styles["loading-content"] + " no-dark"}>
  48. <img src={loadingIcon.src} />
  49. {/* seceond版本注释掉进度条 */}
  50. {/* <div style={{ width: '60%', height: 15, background: '#F5F6F9', borderRadius: 8, marginTop: '20%', position: 'relative' }}> */}
  51. {/* <div */}
  52. {/* style={{ */}
  53. {/* width: `${progress}%`, */}
  54. {/* height: 15, */}
  55. {/* background: '#265C7D', */}
  56. {/* borderRadius: 8, */}
  57. {/* display: 'flex', */}
  58. {/* justifyContent: 'flex-end', */}
  59. {/* alignItems: 'center', */}
  60. {/* position: 'absolute' */}
  61. {/* }} */}
  62. {/* > */}
  63. {/* <div style={{ color: '#FFFFFF', fontSize: 12, lineHeight: 12, marginRight: 4 }}> */}
  64. {/* {progress}% */}
  65. {/* </div> */}
  66. {/* </div> */}
  67. {/* </div> */}
  68. </div>
  69. );
  70. }
  71. // 延时器
  72. export const delayer = (): Promise<any> => {
  73. // 延时时间-秒
  74. const time: number = 0.1;
  75. return new Promise((resolve, reject) => {
  76. setTimeout(() => {
  77. resolve({});
  78. }, time * 1000);
  79. });
  80. }
  81. const Artifacts = dynamic(
  82. async () => {
  83. await delayer();
  84. return (await import("./artifacts")).Artifacts
  85. },
  86. {
  87. loading: () => <Loading />,
  88. }
  89. );
  90. const Settings = dynamic(
  91. async () => {
  92. await delayer();
  93. return (await import("./settings")).Settings
  94. },
  95. {
  96. loading: () => <Loading />,
  97. }
  98. );
  99. // const Chat = dynamic(
  100. // async () => {
  101. // await delayer();
  102. // return (await import("./chat")).Chat
  103. // },
  104. // {
  105. // loading: () => <Loading />,
  106. // }
  107. // );
  108. // const DeepSeekChat = dynamic(
  109. // async () => {
  110. // await delayer();
  111. // return (await import("./DeepSeekChat")).Chat
  112. // },
  113. // {
  114. // loading: () => <Loading />,
  115. // }
  116. // );
  117. const Record = dynamic(
  118. async () => {
  119. await delayer();
  120. return (await import("./Record"))
  121. },
  122. {
  123. loading: () => <Loading />,
  124. }
  125. );
  126. // const HomeApp = dynamic(
  127. // async () => {
  128. // return (await import("./DeekSeekHome"))
  129. // },
  130. // {
  131. // loading: () => <Loading />,
  132. // }
  133. // );
  134. const NewChat = dynamic(
  135. async () => {
  136. await delayer();
  137. return (await import("./new-chat")).NewChat
  138. },
  139. {
  140. loading: () => <Loading />,
  141. }
  142. );
  143. const MaskPage = dynamic(
  144. async () => {
  145. await delayer();
  146. return (await import("./mask")).MaskPage
  147. },
  148. {
  149. loading: () => <Loading />,
  150. }
  151. );
  152. export function useSwitchTheme() {
  153. const config = useAppConfig();
  154. useEffect(() => {
  155. document.body.classList.remove("light");
  156. document.body.classList.remove("dark");
  157. if (config.theme === "dark") {
  158. document.body.classList.add("dark");
  159. } else if (config.theme === "light") {
  160. document.body.classList.add("light");
  161. }
  162. const metaDescriptionDark = document.querySelector(
  163. 'meta[name="theme-color"][media*="dark"]',
  164. );
  165. const metaDescriptionLight = document.querySelector(
  166. 'meta[name="theme-color"][media*="light"]',
  167. );
  168. if (config.theme === "auto") {
  169. metaDescriptionDark?.setAttribute("content", "#151515");
  170. metaDescriptionLight?.setAttribute("content", "#fafafa");
  171. } else {
  172. const themeColor = getCSSVar("--theme-color");
  173. metaDescriptionDark?.setAttribute("content", themeColor);
  174. metaDescriptionLight?.setAttribute("content", themeColor);
  175. }
  176. }, [config.theme]);
  177. }
  178. function useHtmlLang() {
  179. useEffect(() => {
  180. const lang = getISOLang();
  181. const htmlLang = document.documentElement.lang;
  182. if (lang !== htmlLang) {
  183. document.documentElement.lang = lang;
  184. }
  185. }, []);
  186. }
  187. const useHasHydrated = () => {
  188. const [hasHydrated, setHasHydrated] = useState<boolean>(false);
  189. useEffect(() => {
  190. setHasHydrated(true);
  191. }, []);
  192. return hasHydrated;
  193. };
  194. const loadAsyncGoogleFont = () => {
  195. const linkEl = document.createElement("link");
  196. const proxyFontUrl = "/google-fonts";
  197. const remoteFontUrl = "https://fonts.googleapis.com";
  198. const googleFontUrl =
  199. getClientConfig()?.buildMode === "export" ? remoteFontUrl : proxyFontUrl;
  200. linkEl.rel = "stylesheet";
  201. linkEl.href =
  202. googleFontUrl +
  203. "/css2?family=" +
  204. encodeURIComponent("Noto Sans:wght@300;400;700;900") +
  205. "&display=swap";
  206. document.head.appendChild(linkEl);
  207. };
  208. export function WindowContent(props: { children: React.ReactNode }) {
  209. return (
  210. <div className={styles["window-content"]} id={SlotID.AppBody}>
  211. {props?.children}
  212. </div>
  213. );
  214. }
  215. function Screen() {
  216. const config = useAppConfig();
  217. const location = useLocation();
  218. const isArtifact = location.pathname.includes(Path.Artifacts);
  219. const isAuth = location.pathname === Path.Auth;
  220. const isMobileScreen = useMobileScreen();
  221. const shouldTightBorder =
  222. getClientConfig()?.isApp || (config.tightBorder && !isMobileScreen);
  223. useEffect(() => {
  224. loadAsyncGoogleFont();
  225. }, []);
  226. if (isArtifact) {
  227. return (
  228. <Routes>
  229. <Route path="/artifacts/:id" element={<Artifacts />} />
  230. </Routes>
  231. );
  232. }
  233. const renderContent = () => {
  234. if (isAuth) return <AuthPage />;
  235. return (
  236. <>
  237. {
  238. location.pathname !== '/' &&
  239. <SideBar className={(location.pathname === '/knowledgeChat' || location.pathname === '/deepseekChat') ? styles["sidebar-show"] : ""} />
  240. }
  241. <WindowContent>
  242. <Routes>
  243. <Route path='/' element={<HomeApp />} />
  244. <Route path='/knowledgeChat' element={<Chat />} />
  245. <Route path='/newChat' element={<Chat />} />
  246. <Route path='/deepseekChat' element={<DeepSeekChat />} />
  247. <Route path='/newDeepseekChat' element={<DeepSeekChat />} />
  248. </Routes>
  249. </WindowContent>
  250. </>
  251. );
  252. };
  253. return (
  254. <div
  255. className={`${styles.container} ${shouldTightBorder ? styles["tight-container"] : styles.container
  256. }`}
  257. >
  258. {renderContent()}
  259. </div>
  260. );
  261. }
  262. export function useLoadData() {
  263. const config = useAppConfig();
  264. const api: ClientApi = getClientApi(config.modelConfig.providerName);
  265. useEffect(() => {
  266. (async () => {
  267. const models = await api.llm.models();
  268. config.mergeModels(models);
  269. })();
  270. // eslint-disable-next-line react-hooks/exhaustive-deps
  271. }, []);
  272. }
  273. export function Home() {
  274. useSwitchTheme();
  275. useLoadData();
  276. useHtmlLang();
  277. useEffect(() => {
  278. // console.log("[Config] got config from build time", getClientConfig());
  279. useAccessStore.getState().fetch();
  280. }, []);
  281. if (!useHasHydrated()) {
  282. return <Loading />;
  283. }
  284. return (
  285. <ErrorBoundary>
  286. <Router>
  287. <Screen />
  288. </Router>
  289. </ErrorBoundary>
  290. );
  291. }