home.tsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 { useChatStore } from "../store";
  8. import { getCSSVar, useMobileScreen } from "../utils";
  9. import { Chat } from "./chat";
  10. import dynamic from "next/dynamic";
  11. import { Path } from "../constant";
  12. import { ErrorBoundary } from "./error";
  13. import {
  14. HashRouter as Router,
  15. Routes,
  16. Route,
  17. useLocation,
  18. } from "react-router-dom";
  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 SideBar = dynamic(async () => (await import("./sidebar")).SideBar, {
  31. loading: () => <Loading noLogo />,
  32. });
  33. function useSwitchTheme() {
  34. const config = useChatStore((state) => state.config);
  35. useEffect(() => {
  36. document.body.classList.remove("light");
  37. document.body.classList.remove("dark");
  38. if (config.theme === "dark") {
  39. document.body.classList.add("dark");
  40. } else if (config.theme === "light") {
  41. document.body.classList.add("light");
  42. }
  43. const metaDescriptionDark = document.querySelector(
  44. 'meta[name="theme-color"][media]',
  45. );
  46. const metaDescriptionLight = document.querySelector(
  47. 'meta[name="theme-color"]:not([media])',
  48. );
  49. if (config.theme === "auto") {
  50. metaDescriptionDark?.setAttribute("content", "#151515");
  51. metaDescriptionLight?.setAttribute("content", "#fafafa");
  52. } else {
  53. const themeColor = getCSSVar("--themeColor");
  54. metaDescriptionDark?.setAttribute("content", themeColor);
  55. metaDescriptionLight?.setAttribute("content", themeColor);
  56. }
  57. }, [config.theme]);
  58. }
  59. const useHasHydrated = () => {
  60. const [hasHydrated, setHasHydrated] = useState<boolean>(false);
  61. useEffect(() => {
  62. setHasHydrated(true);
  63. }, []);
  64. return hasHydrated;
  65. };
  66. function WideScreen() {
  67. // setting
  68. const config = useChatStore((state) => state.config);
  69. return (
  70. <div
  71. className={`${
  72. config.tightBorder ? styles["tight-container"] : styles.container
  73. }`}
  74. >
  75. <div className={styles.sidebar}>
  76. <SideBar></SideBar>
  77. </div>
  78. <div className={styles["window-content"]}>
  79. <Routes>
  80. <Route path={Path.Home} element={<Chat />} />
  81. <Route path={Path.Chat} element={<Chat />} />
  82. <Route path={Path.Settings} element={<Settings />} />
  83. </Routes>
  84. </div>
  85. </div>
  86. );
  87. }
  88. function MobileScreen() {
  89. const location = useLocation();
  90. const isHome = location.pathname === Path.Home;
  91. return (
  92. <div className={styles.container}>
  93. <div className={`${styles.sidebar} ${isHome && styles["sidebar-show"]}`}>
  94. <SideBar />
  95. </div>
  96. <div className={styles["window-content"]}>
  97. <Routes>
  98. <Route path={Path.Home} element={null} />
  99. <Route path={Path.Chat} element={<Chat />} />
  100. <Route path={Path.Settings} element={<Settings />} />
  101. </Routes>
  102. </div>
  103. </div>
  104. );
  105. }
  106. export function Home() {
  107. useSwitchTheme();
  108. const isMobileScreen = useMobileScreen();
  109. if (!useHasHydrated()) {
  110. return <Loading />;
  111. }
  112. return (
  113. <ErrorBoundary>
  114. <Router>{isMobileScreen ? <MobileScreen /> : <WideScreen />}</Router>
  115. </ErrorBoundary>
  116. );
  117. }