home.tsx 7.7 KB

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