home.tsx 6.3 KB

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