home.tsx 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. return (
  26. <div className={styles["loading-content"] + " no-dark"}>
  27. <img src={loadingIcon.src} />
  28. </div>
  29. );
  30. }
  31. // 延时器
  32. export const delayer = (): Promise<any> => {
  33. // 延时时间-秒
  34. const time: number = 0.5;
  35. return new Promise((resolve, reject) => {
  36. setTimeout(() => {
  37. resolve({});
  38. }, time * 1000);
  39. });
  40. }
  41. const Artifacts = dynamic(
  42. async () => {
  43. await delayer();
  44. return (await import("./artifacts")).Artifacts
  45. },
  46. {
  47. loading: () => <Loading />,
  48. }
  49. );
  50. const Settings = dynamic(
  51. async () => {
  52. await delayer();
  53. return (await import("./settings")).Settings
  54. },
  55. {
  56. loading: () => <Loading />,
  57. }
  58. );
  59. const Chat = dynamic(
  60. async () => {
  61. await delayer();
  62. return (await import("./chat")).Chat
  63. },
  64. {
  65. loading: () => <Loading />,
  66. }
  67. );
  68. const Record = dynamic(
  69. async () => {
  70. await delayer();
  71. return (await import("./Record"))
  72. },
  73. {
  74. loading: () => <Loading />,
  75. }
  76. );
  77. const NewChat = dynamic(
  78. async () => {
  79. await delayer();
  80. return (await import("./new-chat")).NewChat
  81. },
  82. {
  83. loading: () => <Loading />,
  84. }
  85. );
  86. const MaskPage = dynamic(
  87. async () => {
  88. await delayer();
  89. return (await import("./mask")).MaskPage
  90. },
  91. {
  92. loading: () => <Loading />,
  93. }
  94. );
  95. const Sd = dynamic(
  96. async () => {
  97. await delayer();
  98. return (await import("./sd")).Sd
  99. },
  100. {
  101. loading: () => <Loading />,
  102. }
  103. );
  104. export function useSwitchTheme() {
  105. const config = useAppConfig();
  106. useEffect(() => {
  107. document.body.classList.remove("light");
  108. document.body.classList.remove("dark");
  109. if (config.theme === "dark") {
  110. document.body.classList.add("dark");
  111. } else if (config.theme === "light") {
  112. document.body.classList.add("light");
  113. }
  114. const metaDescriptionDark = document.querySelector(
  115. 'meta[name="theme-color"][media*="dark"]',
  116. );
  117. const metaDescriptionLight = document.querySelector(
  118. 'meta[name="theme-color"][media*="light"]',
  119. );
  120. if (config.theme === "auto") {
  121. metaDescriptionDark?.setAttribute("content", "#151515");
  122. metaDescriptionLight?.setAttribute("content", "#fafafa");
  123. } else {
  124. const themeColor = getCSSVar("--theme-color");
  125. metaDescriptionDark?.setAttribute("content", themeColor);
  126. metaDescriptionLight?.setAttribute("content", themeColor);
  127. }
  128. }, [config.theme]);
  129. }
  130. function useHtmlLang() {
  131. useEffect(() => {
  132. const lang = getISOLang();
  133. const htmlLang = document.documentElement.lang;
  134. if (lang !== htmlLang) {
  135. document.documentElement.lang = lang;
  136. }
  137. }, []);
  138. }
  139. const useHasHydrated = () => {
  140. const [hasHydrated, setHasHydrated] = useState<boolean>(false);
  141. useEffect(() => {
  142. setHasHydrated(true);
  143. }, []);
  144. return hasHydrated;
  145. };
  146. const loadAsyncGoogleFont = () => {
  147. const linkEl = document.createElement("link");
  148. const proxyFontUrl = "/google-fonts";
  149. const remoteFontUrl = "https://fonts.googleapis.com";
  150. const googleFontUrl =
  151. getClientConfig()?.buildMode === "export" ? remoteFontUrl : proxyFontUrl;
  152. linkEl.rel = "stylesheet";
  153. linkEl.href =
  154. googleFontUrl +
  155. "/css2?family=" +
  156. encodeURIComponent("Noto Sans:wght@300;400;700;900") +
  157. "&display=swap";
  158. document.head.appendChild(linkEl);
  159. };
  160. export function WindowContent(props: { children: React.ReactNode }) {
  161. return (
  162. <div className={styles["window-content"]} id={SlotID.AppBody}>
  163. {props?.children}
  164. </div>
  165. );
  166. }
  167. function Screen() {
  168. const config = useAppConfig();
  169. const location = useLocation();
  170. const isArtifact = location.pathname.includes(Path.Artifacts);
  171. const isHome = location.pathname === Path.Home;
  172. const isAuth = location.pathname === Path.Auth;
  173. const isSd = location.pathname === Path.Sd;
  174. const isSdNew = location.pathname === Path.SdNew;
  175. const isMobileScreen = useMobileScreen();
  176. const shouldTightBorder =
  177. getClientConfig()?.isApp || (config.tightBorder && !isMobileScreen);
  178. useEffect(() => {
  179. loadAsyncGoogleFont();
  180. }, []);
  181. if (isArtifact) {
  182. return (
  183. <Routes>
  184. <Route path="/artifacts/:id" element={<Artifacts />} />
  185. </Routes>
  186. );
  187. }
  188. const renderContent = () => {
  189. if (isAuth) return <AuthPage />;
  190. if (isSd) return <Sd />;
  191. if (isSdNew) return <Sd />;
  192. return (
  193. <>
  194. {/* <SideBar className={isHome ? styles["sidebar-show"] : ""} /> */}
  195. <WindowContent>
  196. <Routes>
  197. <Route path={Path.Home} element={<Chat />} />
  198. <Route path={'/record'} element={<Record />} />
  199. {/* <Route path={Path.NewChat} element={<NewChat />} /> */}
  200. {/* <Route path={Path.Masks} element={<MaskPage />} /> */}
  201. {/* <Route path={Path.Chat} element={<Chat />} /> */}
  202. {/* <Route path={Path.Settings} element={<Settings />} /> */}
  203. </Routes>
  204. </WindowContent>
  205. </>
  206. );
  207. };
  208. return (
  209. <div
  210. className={`${styles.container} ${shouldTightBorder ? styles["tight-container"] : styles.container
  211. } ${getLang() === "ar" ? styles["rtl-screen"] : ""}`}
  212. >
  213. {renderContent()}
  214. </div>
  215. );
  216. }
  217. export function useLoadData() {
  218. const config = useAppConfig();
  219. const api: ClientApi = getClientApi(config.modelConfig.providerName);
  220. useEffect(() => {
  221. (async () => {
  222. const models = await api.llm.models();
  223. config.mergeModels(models);
  224. })();
  225. // eslint-disable-next-line react-hooks/exhaustive-deps
  226. }, []);
  227. }
  228. export function Home() {
  229. useSwitchTheme();
  230. useLoadData();
  231. useHtmlLang();
  232. useEffect(() => {
  233. // console.log("[Config] got config from build time", getClientConfig());
  234. useAccessStore.getState().fetch();
  235. }, []);
  236. if (!useHasHydrated()) {
  237. return <Loading />;
  238. }
  239. return (
  240. <ErrorBoundary>
  241. <Router>
  242. <Screen />
  243. </Router>
  244. </ErrorBoundary>
  245. );
  246. }