home.tsx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 chatStore = useChatStore();
  78. const loading = !useHasHydrated();
  79. const [showSideBar, setShowSideBar] = useState(true);
  80. // setting
  81. const [openSettings, setOpenSettings] = useState(false);
  82. const config = useChatStore((state) => state.config);
  83. useSwitchTheme();
  84. if (loading) {
  85. return <Loading />;
  86. }
  87. return (
  88. <div
  89. className={`${
  90. config.tightBorder && !isMobileScreen()
  91. ? styles["tight-container"]
  92. : styles.container
  93. }`}
  94. >
  95. <div
  96. className={styles.sidebar + ` ${showSideBar && styles["sidebar-show"]}`}
  97. >
  98. <div className={styles["sidebar-header"]}>
  99. <div className={styles["sidebar-title"]}>ChatGPT Next</div>
  100. <div className={styles["sidebar-sub-title"]}>
  101. Build your own AI assistant.
  102. </div>
  103. <div className={styles["sidebar-logo"]}>
  104. <ChatGptIcon />
  105. </div>
  106. </div>
  107. <div
  108. className={styles["sidebar-body"]}
  109. onClick={() => {
  110. setOpenSettings(false);
  111. setShowSideBar(false);
  112. }}
  113. >
  114. <ChatList />
  115. </div>
  116. <div className={styles["sidebar-tail"]}>
  117. <div className={styles["sidebar-actions"]}>
  118. <div className={styles["sidebar-action"] + " " + styles.mobile}>
  119. <IconButton
  120. icon={<CloseIcon />}
  121. onClick={chatStore.deleteSession}
  122. />
  123. </div>
  124. <div className={styles["sidebar-action"]}>
  125. <IconButton
  126. icon={<SettingsIcon />}
  127. onClick={() => {
  128. setOpenSettings(true);
  129. setShowSideBar(false);
  130. }}
  131. shadow
  132. />
  133. </div>
  134. <div className={styles["sidebar-action"]}>
  135. <a href={REPO_URL} target="_blank">
  136. <IconButton icon={<GithubIcon />} shadow />
  137. </a>
  138. </div>
  139. </div>
  140. <div>
  141. <IconButton
  142. icon={<AddIcon />}
  143. text={Locale.Home.NewChat}
  144. onClick={() => {
  145. createNewSession();
  146. setShowSideBar(false);
  147. }}
  148. shadow
  149. />
  150. </div>
  151. </div>
  152. </div>
  153. <div className={styles["window-content"]}>
  154. {openSettings ? (
  155. <Settings
  156. closeSettings={() => {
  157. setOpenSettings(false);
  158. setShowSideBar(true);
  159. }}
  160. />
  161. ) : (
  162. <Chat
  163. key="chat"
  164. showSideBar={() => setShowSideBar(true)}
  165. sideBarShowing={showSideBar}
  166. />
  167. )}
  168. </div>
  169. </div>
  170. );
  171. }
  172. export function Home() {
  173. return (
  174. <ErrorBoundary>
  175. <_Home></_Home>
  176. </ErrorBoundary>
  177. );
  178. }