home.tsx 5.6 KB

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