home.tsx 4.0 KB

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