home.tsx 5.9 KB

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