home.tsx 4.3 KB

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