home.tsx 6.9 KB

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