home.tsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. useNavigation,
  18. useLocation,
  19. } from "react-router-dom";
  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 SideBar = dynamic(async () => (await import("./sidebar")).SideBar, {
  32. loading: () => <Loading noLogo />,
  33. });
  34. function useSwitchTheme() {
  35. const config = useChatStore((state) => state.config);
  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 WideScreen() {
  68. // setting
  69. const config = useChatStore((state) => state.config);
  70. return (
  71. <div
  72. className={`${
  73. config.tightBorder ? styles["tight-container"] : styles.container
  74. }`}
  75. >
  76. <div className={styles.sidebar}>
  77. <SideBar></SideBar>
  78. </div>
  79. <div className={styles["window-content"]}>
  80. <Routes>
  81. <Route path={Path.Home} element={<Chat />} />
  82. <Route path={Path.Chat} element={<Chat />} />
  83. <Route path={Path.Settings} element={<Settings />} />
  84. </Routes>
  85. </div>
  86. </div>
  87. );
  88. }
  89. function MobileScreen() {
  90. const location = useLocation();
  91. const isHome = location.pathname === Path.Home;
  92. return (
  93. <div className={styles.container}>
  94. <div className={`${styles.sidebar} ${isHome && styles["sidebar-show"]}`}>
  95. <SideBar />
  96. </div>
  97. <div className={styles["window-content"]}>
  98. <Routes>
  99. <Route path={Path.Home} element={null} />
  100. <Route path={Path.Chat} element={<Chat />} />
  101. <Route path={Path.Settings} element={<Settings />} />
  102. </Routes>
  103. </div>
  104. </div>
  105. );
  106. }
  107. export function Home() {
  108. useSwitchTheme();
  109. const isMobileScreen = useMobileScreen();
  110. if (!useHasHydrated()) {
  111. return <Loading />;
  112. }
  113. return (
  114. <ErrorBoundary>
  115. <Router>{isMobileScreen ? <MobileScreen /> : <WideScreen />}</Router>
  116. </ErrorBoundary>
  117. );
  118. }