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 } 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. export function useSwitchTheme() {
  34. const config = useAppConfig();
  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. const config = useAppConfig();
  68. return (
  69. <div
  70. className={`${
  71. config.tightBorder ? styles["tight-container"] : styles.container
  72. }`}
  73. >
  74. <SideBar />
  75. <div className={styles["window-content"]}>
  76. <Routes>
  77. <Route path={Path.Home} element={<Chat />} />
  78. <Route path={Path.Chat} element={<Chat />} />
  79. <Route path={Path.Settings} element={<Settings />} />
  80. </Routes>
  81. </div>
  82. </div>
  83. );
  84. }
  85. function MobileScreen() {
  86. const location = useLocation();
  87. const isHome = location.pathname === Path.Home;
  88. return (
  89. <div className={styles.container}>
  90. <SideBar className={isHome ? styles["sidebar-show"] : ""} />
  91. <div className={styles["window-content"]}>
  92. <Routes>
  93. <Route path={Path.Home} element={null} />
  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. const isMobileScreen = useMobileScreen();
  103. useSwitchTheme();
  104. if (!useHasHydrated()) {
  105. return <Loading />;
  106. }
  107. return (
  108. <ErrorBoundary>
  109. <Router>{isMobileScreen ? <MobileScreen /> : <WideScreen />}</Router>
  110. </ErrorBoundary>
  111. );
  112. }