home.tsx 5.0 KB

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