home.tsx 3.7 KB

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