home.tsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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]',
  51. );
  52. const metaDescriptionLight = document.querySelector(
  53. 'meta[name="theme-color"]:not([media])',
  54. );
  55. if (config.theme === "auto") {
  56. metaDescriptionDark?.setAttribute("content", "#151515");
  57. metaDescriptionLight?.setAttribute("content", "#fafafa");
  58. } else {
  59. const themeColor = getCSSVar("--themeColor");
  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. function Screen() {
  73. const config = useAppConfig();
  74. const location = useLocation();
  75. const isHome = location.pathname === Path.Home;
  76. const isMobileScreen = useMobileScreen();
  77. return (
  78. <div
  79. className={
  80. styles.container +
  81. ` ${
  82. config.tightBorder && !isMobileScreen
  83. ? styles["tight-container"]
  84. : styles.container
  85. }`
  86. }
  87. >
  88. <SideBar className={isHome ? styles["sidebar-show"] : ""} />
  89. <div className={styles["window-content"]} id={SlotID.AppBody}>
  90. <Routes>
  91. <Route path={Path.Home} element={<Chat />} />
  92. <Route path={Path.NewChat} element={<NewChat />} />
  93. <Route path={Path.Masks} element={<MaskPage />} />
  94. <Route path={Path.Chat} element={<Chat />} />
  95. <Route path={Path.Settings} element={<Settings />} />
  96. </Routes>
  97. </div>
  98. </div>
  99. );
  100. }
  101. export function Home() {
  102. useSwitchTheme();
  103. if (!useHasHydrated()) {
  104. return <Loading />;
  105. }
  106. return (
  107. <ErrorBoundary>
  108. <Router>
  109. <Screen />
  110. </Router>
  111. </ErrorBoundary>
  112. );
  113. }