home.tsx 6.1 KB

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