home.tsx 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. "use client";
  2. require("../polyfill");
  3. import { useState, useEffect, FC } from "react";
  4. import styles from "./home.module.scss";
  5. import BotIcon from "../icons/bot.svg";
  6. import loadingIcon from "../icons/loading.gif";
  7. import { getCSSVar, useMobileScreen } from "../utils";
  8. import dynamic from "next/dynamic";
  9. import { Path, SlotID } from "../constant";
  10. import { ErrorBoundary } from "./error";
  11. import { getISOLang, getLang } from "../locales";
  12. import {
  13. HashRouter as Router,
  14. Routes,
  15. Route,
  16. useLocation,
  17. } from "react-router-dom";
  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, useChatStore } 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 Record = dynamic(
  106. async () => {
  107. await delayer();
  108. return (await import("./Record"))
  109. },
  110. {
  111. loading: () => <Loading />,
  112. }
  113. );
  114. const DeekSeek = dynamic(
  115. async () => {
  116. return (await import("./DeekSeek"))
  117. },
  118. {
  119. loading: () => <Loading />,
  120. }
  121. );
  122. const NewChat = dynamic(
  123. async () => {
  124. await delayer();
  125. return (await import("./new-chat")).NewChat
  126. },
  127. {
  128. loading: () => <Loading />,
  129. }
  130. );
  131. const MaskPage = dynamic(
  132. async () => {
  133. await delayer();
  134. return (await import("./mask")).MaskPage
  135. },
  136. {
  137. loading: () => <Loading />,
  138. }
  139. );
  140. const Sd = dynamic(
  141. async () => {
  142. await delayer();
  143. return (await import("./sd")).Sd
  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 isHome = location.pathname === Path.Home;
  217. const isAuth = location.pathname === Path.Auth;
  218. const isSd = location.pathname === Path.Sd;
  219. const isSdNew = location.pathname === Path.SdNew;
  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 DeepSeekApp: FC = () => {
  234. const chatStore = useChatStore();
  235. useEffect(() => {
  236. chatStore.setModel('DeepSeek');
  237. }, [])
  238. return (
  239. <>
  240. <WindowContent>
  241. <Routes>
  242. <Route path='/deepSeek' element={<DeekSeek />} />
  243. </Routes>
  244. </WindowContent>
  245. </>
  246. )
  247. }
  248. const BigModelApp: FC = () => {
  249. const chatStore = useChatStore();
  250. useEffect(() => {
  251. chatStore.setModel('BigModel');
  252. }, [])
  253. return (
  254. <>
  255. <SideBar className={isHome ? styles["sidebar-show"] : ""} />
  256. <WindowContent>
  257. <Routes>
  258. <Route path={Path.Home} element={<Chat />} />
  259. {/* <Route path={'/record'} element={<Record />} /> */}
  260. <Route path={Path.Chat} element={<Chat />} />
  261. </Routes>
  262. </WindowContent>
  263. </>
  264. )
  265. }
  266. const renderContent = () => {
  267. if (isAuth) return <AuthPage />;
  268. if (isSd) return <Sd />;
  269. if (isSdNew) return <Sd />;
  270. return (
  271. <>
  272. {location.pathname === '/deepSeek' ? <DeepSeekApp /> : <BigModelApp />}
  273. </>
  274. );
  275. };
  276. return (
  277. <div
  278. className={`${styles.container} ${shouldTightBorder ? styles["tight-container"] : styles.container
  279. } ${getLang() === "ar" ? styles["rtl-screen"] : ""}`}
  280. >
  281. {renderContent()}
  282. </div>
  283. );
  284. }
  285. export function useLoadData() {
  286. const config = useAppConfig();
  287. const api: ClientApi = getClientApi(config.modelConfig.providerName);
  288. useEffect(() => {
  289. (async () => {
  290. const models = await api.llm.models();
  291. config.mergeModels(models);
  292. })();
  293. // eslint-disable-next-line react-hooks/exhaustive-deps
  294. }, []);
  295. }
  296. export function Home() {
  297. useSwitchTheme();
  298. useLoadData();
  299. useHtmlLang();
  300. useEffect(() => {
  301. // console.log("[Config] got config from build time", getClientConfig());
  302. useAccessStore.getState().fetch();
  303. }, []);
  304. if (!useHasHydrated()) {
  305. return <Loading />;
  306. }
  307. return (
  308. <ErrorBoundary>
  309. <Router>
  310. <Screen />
  311. </Router>
  312. </ErrorBoundary>
  313. );
  314. }