home.tsx 3.2 KB

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