home.tsx 5.1 KB

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