home.tsx 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. "use client";
  2. require("../polyfill");
  3. import { useState, useEffect } 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 } 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 NewChat = dynamic(
  115. async () => {
  116. await delayer();
  117. return (await import("./new-chat")).NewChat
  118. },
  119. {
  120. loading: () => <Loading />,
  121. }
  122. );
  123. const MaskPage = dynamic(
  124. async () => {
  125. await delayer();
  126. return (await import("./mask")).MaskPage
  127. },
  128. {
  129. loading: () => <Loading />,
  130. }
  131. );
  132. const Sd = dynamic(
  133. async () => {
  134. await delayer();
  135. return (await import("./sd")).Sd
  136. },
  137. {
  138. loading: () => <Loading />,
  139. }
  140. );
  141. export function useSwitchTheme() {
  142. const config = useAppConfig();
  143. useEffect(() => {
  144. document.body.classList.remove("light");
  145. document.body.classList.remove("dark");
  146. if (config.theme === "dark") {
  147. document.body.classList.add("dark");
  148. } else if (config.theme === "light") {
  149. document.body.classList.add("light");
  150. }
  151. const metaDescriptionDark = document.querySelector(
  152. 'meta[name="theme-color"][media*="dark"]',
  153. );
  154. const metaDescriptionLight = document.querySelector(
  155. 'meta[name="theme-color"][media*="light"]',
  156. );
  157. if (config.theme === "auto") {
  158. metaDescriptionDark?.setAttribute("content", "#151515");
  159. metaDescriptionLight?.setAttribute("content", "#fafafa");
  160. } else {
  161. const themeColor = getCSSVar("--theme-color");
  162. metaDescriptionDark?.setAttribute("content", themeColor);
  163. metaDescriptionLight?.setAttribute("content", themeColor);
  164. }
  165. }, [config.theme]);
  166. }
  167. function useHtmlLang() {
  168. useEffect(() => {
  169. const lang = getISOLang();
  170. const htmlLang = document.documentElement.lang;
  171. if (lang !== htmlLang) {
  172. document.documentElement.lang = lang;
  173. }
  174. }, []);
  175. }
  176. const useHasHydrated = () => {
  177. const [hasHydrated, setHasHydrated] = useState<boolean>(false);
  178. useEffect(() => {
  179. setHasHydrated(true);
  180. }, []);
  181. return hasHydrated;
  182. };
  183. const loadAsyncGoogleFont = () => {
  184. const linkEl = document.createElement("link");
  185. const proxyFontUrl = "/google-fonts";
  186. const remoteFontUrl = "https://fonts.googleapis.com";
  187. const googleFontUrl =
  188. getClientConfig()?.buildMode === "export" ? remoteFontUrl : proxyFontUrl;
  189. linkEl.rel = "stylesheet";
  190. linkEl.href =
  191. googleFontUrl +
  192. "/css2?family=" +
  193. encodeURIComponent("Noto Sans:wght@300;400;700;900") +
  194. "&display=swap";
  195. document.head.appendChild(linkEl);
  196. };
  197. export function WindowContent(props: { children: React.ReactNode }) {
  198. return (
  199. <div className={styles["window-content"]} id={SlotID.AppBody}>
  200. {props?.children}
  201. </div>
  202. );
  203. }
  204. function Screen() {
  205. const config = useAppConfig();
  206. const location = useLocation();
  207. const isArtifact = location.pathname.includes(Path.Artifacts);
  208. const isHome = location.pathname === Path.Home;
  209. const isAuth = location.pathname === Path.Auth;
  210. const isSd = location.pathname === Path.Sd;
  211. const isSdNew = location.pathname === Path.SdNew;
  212. const isMobileScreen = useMobileScreen();
  213. const shouldTightBorder =
  214. getClientConfig()?.isApp || (config.tightBorder && !isMobileScreen);
  215. useEffect(() => {
  216. loadAsyncGoogleFont();
  217. }, []);
  218. if (isArtifact) {
  219. return (
  220. <Routes>
  221. <Route path="/artifacts/:id" element={<Artifacts />} />
  222. </Routes>
  223. );
  224. }
  225. const renderContent = () => {
  226. if (isAuth) return <AuthPage />;
  227. if (isSd) return <Sd />;
  228. if (isSdNew) return <Sd />;
  229. return (
  230. <>
  231. <SideBar className={isHome ? styles["sidebar-show"] : ""} />
  232. <WindowContent>
  233. <Routes>
  234. <Route path={Path.Home} element={<Chat />} />
  235. {/* <Route path={'/record'} element={<Record />} /> */}
  236. <Route path={Path.NewChat} element={<NewChat />} />
  237. {/* <Route path={Path.Masks} element={<MaskPage />} /> */}
  238. <Route path={Path.Chat} element={<Chat />} />
  239. {/* <Route path={Path.Settings} element={<Settings />} /> */}
  240. </Routes>
  241. </WindowContent>
  242. </>
  243. );
  244. };
  245. return (
  246. <div
  247. className={`${styles.container} ${shouldTightBorder ? styles["tight-container"] : styles.container
  248. } ${getLang() === "ar" ? styles["rtl-screen"] : ""}`}
  249. >
  250. {renderContent()}
  251. </div>
  252. );
  253. }
  254. export function useLoadData() {
  255. const config = useAppConfig();
  256. const api: ClientApi = getClientApi(config.modelConfig.providerName);
  257. useEffect(() => {
  258. (async () => {
  259. const models = await api.llm.models();
  260. config.mergeModels(models);
  261. })();
  262. // eslint-disable-next-line react-hooks/exhaustive-deps
  263. }, []);
  264. }
  265. export function Home() {
  266. useSwitchTheme();
  267. useLoadData();
  268. useHtmlLang();
  269. useEffect(() => {
  270. // console.log("[Config] got config from build time", getClientConfig());
  271. useAccessStore.getState().fetch();
  272. }, []);
  273. if (!useHasHydrated()) {
  274. return <Loading />;
  275. }
  276. return (
  277. <ErrorBoundary>
  278. <Router>
  279. <Screen />
  280. </Router>
  281. </ErrorBoundary>
  282. );
  283. }