home.tsx 5.9 KB

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