home.tsx 6.4 KB

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