home.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. export function useSwitchTheme() {
  37. const config = useAppConfig();
  38. useEffect(() => {
  39. document.body.classList.remove("light");
  40. document.body.classList.remove("dark");
  41. if (config.theme === "dark") {
  42. document.body.classList.add("dark");
  43. } else if (config.theme === "light") {
  44. document.body.classList.add("light");
  45. }
  46. const metaDescriptionDark = document.querySelector(
  47. 'meta[name="theme-color"][media]',
  48. );
  49. const metaDescriptionLight = document.querySelector(
  50. 'meta[name="theme-color"]:not([media])',
  51. );
  52. if (config.theme === "auto") {
  53. metaDescriptionDark?.setAttribute("content", "#151515");
  54. metaDescriptionLight?.setAttribute("content", "#fafafa");
  55. } else {
  56. const themeColor = getCSSVar("--themeColor");
  57. metaDescriptionDark?.setAttribute("content", themeColor);
  58. metaDescriptionLight?.setAttribute("content", themeColor);
  59. }
  60. }, [config.theme]);
  61. }
  62. const useHasHydrated = () => {
  63. const [hasHydrated, setHasHydrated] = useState<boolean>(false);
  64. useEffect(() => {
  65. setHasHydrated(true);
  66. }, []);
  67. return hasHydrated;
  68. };
  69. function Screen() {
  70. const config = useAppConfig();
  71. const location = useLocation();
  72. const isHome = location.pathname === Path.Home;
  73. const isMobileScreen = useMobileScreen();
  74. return (
  75. <div
  76. className={
  77. styles.container +
  78. ` ${
  79. config.tightBorder && !isMobileScreen
  80. ? styles["tight-container"]
  81. : styles.container
  82. }`
  83. }
  84. >
  85. <SideBar className={isHome ? styles["sidebar-show"] : ""} />
  86. <div className={styles["window-content"]} id={SlotID.AppBody}>
  87. <Routes>
  88. <Route path={Path.Home} element={<Chat />} />
  89. <Route path={Path.NewChat} element={<NewChat />} />
  90. <Route path={Path.Chat} element={<Chat />} />
  91. <Route path={Path.Settings} element={<Settings />} />
  92. </Routes>
  93. </div>
  94. </div>
  95. );
  96. }
  97. export function Home() {
  98. useSwitchTheme();
  99. if (!useHasHydrated()) {
  100. return <Loading />;
  101. }
  102. return (
  103. <ErrorBoundary>
  104. <Router>
  105. <Screen />
  106. </Router>
  107. </ErrorBoundary>
  108. );
  109. }