home.tsx 5.5 KB

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