home.tsx 6.6 KB

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