home.tsx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. "use client";
  2. require("../polyfill");
  3. import { useState, useEffect } from "react";
  4. import { IconButton } from "./button";
  5. import styles from "./home.module.scss";
  6. import SettingsIcon from "../icons/settings.svg";
  7. import GithubIcon from "../icons/github.svg";
  8. import ChatGptIcon from "../icons/chatgpt.svg";
  9. import BotIcon from "../icons/bot.svg";
  10. import AddIcon from "../icons/add.svg";
  11. import LoadingIcon from "../icons/three-dots.svg";
  12. import CloseIcon from "../icons/close.svg";
  13. import { useChatStore } from "../store";
  14. import { isMobileScreen } from "../utils";
  15. import Locale from "../locales";
  16. import { Chat } from "./chat";
  17. import dynamic from "next/dynamic";
  18. import { REPO_URL } from "../constant";
  19. import { ErrorBoundary } from "./error";
  20. export function Loading(props: { noLogo?: boolean }) {
  21. return (
  22. <div className={styles["loading-content"]}>
  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 ChatList = dynamic(async () => (await import("./chat-list")).ChatList, {
  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 = getComputedStyle(document.body)
  55. .getPropertyValue("--theme-color")
  56. .trim();
  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 _Home() {
  70. const [createNewSession, currentIndex, removeSession] = useChatStore(
  71. (state) => [
  72. state.newSession,
  73. state.currentSessionIndex,
  74. state.removeSession,
  75. ],
  76. );
  77. const loading = !useHasHydrated();
  78. const [showSideBar, setShowSideBar] = useState(true);
  79. // setting
  80. const [openSettings, setOpenSettings] = useState(false);
  81. const config = useChatStore((state) => state.config);
  82. useSwitchTheme();
  83. if (loading) {
  84. return <Loading />;
  85. }
  86. return (
  87. <div
  88. className={`${
  89. config.tightBorder && !isMobileScreen()
  90. ? styles["tight-container"]
  91. : styles.container
  92. }`}
  93. >
  94. <div
  95. className={styles.sidebar + ` ${showSideBar && styles["sidebar-show"]}`}
  96. >
  97. <div className={styles["sidebar-header"]}>
  98. <div className={styles["sidebar-title"]}>ChatGPT Next</div>
  99. <div className={styles["sidebar-sub-title"]}>
  100. Build your own AI assistant.
  101. </div>
  102. <div className={styles["sidebar-logo"]}>
  103. <ChatGptIcon />
  104. </div>
  105. </div>
  106. <div
  107. className={styles["sidebar-body"]}
  108. onClick={() => {
  109. setOpenSettings(false);
  110. setShowSideBar(false);
  111. }}
  112. >
  113. <ChatList />
  114. </div>
  115. <div className={styles["sidebar-tail"]}>
  116. <div className={styles["sidebar-actions"]}>
  117. <div className={styles["sidebar-action"] + " " + styles.mobile}>
  118. <IconButton
  119. icon={<CloseIcon />}
  120. onClick={() => {
  121. if (confirm(Locale.Home.DeleteChat)) {
  122. removeSession(currentIndex);
  123. }
  124. }}
  125. />
  126. </div>
  127. <div className={styles["sidebar-action"]}>
  128. <IconButton
  129. icon={<SettingsIcon />}
  130. onClick={() => {
  131. setOpenSettings(true);
  132. setShowSideBar(false);
  133. }}
  134. shadow
  135. />
  136. </div>
  137. <div className={styles["sidebar-action"]}>
  138. <a href={REPO_URL} target="_blank">
  139. <IconButton icon={<GithubIcon />} shadow />
  140. </a>
  141. </div>
  142. </div>
  143. <div>
  144. <IconButton
  145. icon={<AddIcon />}
  146. text={Locale.Home.NewChat}
  147. onClick={() => {
  148. createNewSession();
  149. setShowSideBar(false);
  150. }}
  151. shadow
  152. />
  153. </div>
  154. </div>
  155. </div>
  156. <div className={styles["window-content"]}>
  157. {openSettings ? (
  158. <Settings
  159. closeSettings={() => {
  160. setOpenSettings(false);
  161. setShowSideBar(true);
  162. }}
  163. />
  164. ) : (
  165. <Chat
  166. key="chat"
  167. showSideBar={() => setShowSideBar(true)}
  168. sideBarShowing={showSideBar}
  169. />
  170. )}
  171. </div>
  172. </div>
  173. );
  174. }
  175. export function Home() {
  176. return (
  177. <ErrorBoundary>
  178. <_Home></_Home>
  179. </ErrorBoundary>
  180. );
  181. }