home.tsx 4.4 KB

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