home.tsx 6.1 KB

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