home.tsx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. "use client";
  2. import { Sd } from "@/app/components/sd";
  3. require("../polyfill");
  4. import { useState, useEffect } from "react";
  5. import styles from "./home.module.scss";
  6. import BotIcon from "../icons/bot.svg";
  7. import LoadingIcon from "../icons/three-dots.svg";
  8. import { getCSSVar, useMobileScreen } from "../utils";
  9. import dynamic from "next/dynamic";
  10. import { ModelProvider, Path, SlotID } from "../constant";
  11. import { ErrorBoundary } from "./error";
  12. import { getISOLang, getLang } from "../locales";
  13. import {
  14. HashRouter as Router,
  15. Routes,
  16. Route,
  17. useLocation,
  18. } from "react-router-dom";
  19. import { SideBar } from "./sidebar";
  20. import { useAppConfig } from "../store/config";
  21. import { AuthPage } from "./auth";
  22. import { getClientConfig } from "../config/client";
  23. import { ClientApi } from "../client/api";
  24. import { useAccessStore } from "../store";
  25. import { identifyDefaultClaudeModel } from "../utils/checkers";
  26. export function Loading(props: { noLogo?: boolean }) {
  27. return (
  28. <div className={styles["loading-content"] + " no-dark"}>
  29. {!props.noLogo && <BotIcon />}
  30. <LoadingIcon />
  31. </div>
  32. );
  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. export function useSwitchTheme() {
  47. const config = useAppConfig();
  48. useEffect(() => {
  49. document.body.classList.remove("light");
  50. document.body.classList.remove("dark");
  51. if (config.theme === "dark") {
  52. document.body.classList.add("dark");
  53. } else if (config.theme === "light") {
  54. document.body.classList.add("light");
  55. }
  56. const metaDescriptionDark = document.querySelector(
  57. 'meta[name="theme-color"][media*="dark"]',
  58. );
  59. const metaDescriptionLight = document.querySelector(
  60. 'meta[name="theme-color"][media*="light"]',
  61. );
  62. if (config.theme === "auto") {
  63. metaDescriptionDark?.setAttribute("content", "#151515");
  64. metaDescriptionLight?.setAttribute("content", "#fafafa");
  65. } else {
  66. const themeColor = getCSSVar("--theme-color");
  67. metaDescriptionDark?.setAttribute("content", themeColor);
  68. metaDescriptionLight?.setAttribute("content", themeColor);
  69. }
  70. }, [config.theme]);
  71. }
  72. function useHtmlLang() {
  73. useEffect(() => {
  74. const lang = getISOLang();
  75. const htmlLang = document.documentElement.lang;
  76. if (lang !== htmlLang) {
  77. document.documentElement.lang = lang;
  78. }
  79. }, []);
  80. }
  81. const useHasHydrated = () => {
  82. const [hasHydrated, setHasHydrated] = useState<boolean>(false);
  83. useEffect(() => {
  84. setHasHydrated(true);
  85. }, []);
  86. return hasHydrated;
  87. };
  88. const loadAsyncGoogleFont = () => {
  89. const linkEl = document.createElement("link");
  90. const proxyFontUrl = "/google-fonts";
  91. const remoteFontUrl = "https://fonts.googleapis.com";
  92. const googleFontUrl =
  93. getClientConfig()?.buildMode === "export" ? remoteFontUrl : proxyFontUrl;
  94. linkEl.rel = "stylesheet";
  95. linkEl.href =
  96. googleFontUrl +
  97. "/css2?family=" +
  98. encodeURIComponent("Noto Sans:wght@300;400;700;900") +
  99. "&display=swap";
  100. document.head.appendChild(linkEl);
  101. };
  102. function Screen() {
  103. const config = useAppConfig();
  104. const location = useLocation();
  105. const isHome = location.pathname === Path.Home;
  106. const isAuth = location.pathname === Path.Auth;
  107. const isMobileScreen = useMobileScreen();
  108. const shouldTightBorder =
  109. getClientConfig()?.isApp || (config.tightBorder && !isMobileScreen);
  110. useEffect(() => {
  111. loadAsyncGoogleFont();
  112. }, []);
  113. return (
  114. <div
  115. className={
  116. styles.container +
  117. ` ${shouldTightBorder ? styles["tight-container"] : styles.container} ${
  118. getLang() === "ar" ? styles["rtl-screen"] : ""
  119. }`
  120. }
  121. >
  122. {isAuth ? (
  123. <>
  124. <AuthPage />
  125. </>
  126. ) : (
  127. <>
  128. <SideBar className={isHome ? styles["sidebar-show"] : ""} />
  129. <div className={styles["window-content"]} id={SlotID.AppBody}>
  130. <Routes>
  131. <Route path={Path.Home} element={<Chat />} />
  132. <Route path={Path.NewChat} element={<NewChat />} />
  133. <Route path={Path.Masks} element={<MaskPage />} />
  134. <Route path={Path.Chat} element={<Chat />} />
  135. <Route path={Path.Sd} element={<Sd />} />
  136. <Route path={Path.Settings} element={<Settings />} />
  137. </Routes>
  138. </div>
  139. </>
  140. )}
  141. </div>
  142. );
  143. }
  144. export function useLoadData() {
  145. const config = useAppConfig();
  146. var api: ClientApi;
  147. if (config.modelConfig.model.startsWith("gemini")) {
  148. api = new ClientApi(ModelProvider.GeminiPro);
  149. } else if (identifyDefaultClaudeModel(config.modelConfig.model)) {
  150. api = new ClientApi(ModelProvider.Claude);
  151. } else {
  152. api = new ClientApi(ModelProvider.GPT);
  153. }
  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. }