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 { ChatList } from "./chat-list";
  17. import { Chat } from "./chat";
  18. import dynamic from "next/dynamic";
  19. import { REPO_URL } from "../constant";
  20. import { ErrorBoundary } from "./error";
  21. export function Loading(props: { noLogo?: boolean }) {
  22. return (
  23. <div className={styles["loading-content"]}>
  24. {!props.noLogo && <BotIcon />}
  25. <LoadingIcon />
  26. </div>
  27. );
  28. }
  29. const Settings = dynamic(async () => (await import("./settings")).Settings, {
  30. loading: () => <Loading noLogo />,
  31. });
  32. function useSwitchTheme() {
  33. const config = useChatStore((state) => state.config);
  34. useEffect(() => {
  35. document.body.classList.remove("light");
  36. document.body.classList.remove("dark");
  37. if (config.theme === "dark") {
  38. document.body.classList.add("dark");
  39. } else if (config.theme === "light") {
  40. document.body.classList.add("light");
  41. }
  42. const metaDescriptionDark = document.querySelector(
  43. 'meta[name="theme-color"][media]',
  44. );
  45. const metaDescriptionLight = document.querySelector(
  46. 'meta[name="theme-color"]:not([media])',
  47. );
  48. if (config.theme === "auto") {
  49. metaDescriptionDark?.setAttribute("content", "#151515");
  50. metaDescriptionLight?.setAttribute("content", "#fafafa");
  51. } else {
  52. const themeColor = getComputedStyle(document.body)
  53. .getPropertyValue("--theme-color")
  54. .trim();
  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 _Home() {
  68. const [createNewSession, currentIndex, removeSession] = useChatStore(
  69. (state) => [
  70. state.newSession,
  71. state.currentSessionIndex,
  72. state.removeSession,
  73. ],
  74. );
  75. const loading = !useHasHydrated();
  76. const [showSideBar, setShowSideBar] = useState(true);
  77. // setting
  78. const [openSettings, setOpenSettings] = useState(false);
  79. const config = useChatStore((state) => state.config);
  80. useSwitchTheme();
  81. if (loading) {
  82. return <Loading />;
  83. }
  84. return (
  85. <div
  86. className={`${
  87. config.tightBorder && !isMobileScreen()
  88. ? styles["tight-container"]
  89. : styles.container
  90. }`}
  91. >
  92. <div
  93. className={styles.sidebar + ` ${showSideBar && styles["sidebar-show"]}`}
  94. >
  95. <div className={styles["sidebar-header"]}>
  96. <div className={styles["sidebar-title"]}>ChatGPT Next</div>
  97. <div className={styles["sidebar-sub-title"]}>
  98. Build your own AI assistant.
  99. </div>
  100. <div className={styles["sidebar-logo"]}>
  101. <ChatGptIcon />
  102. </div>
  103. </div>
  104. <div
  105. className={styles["sidebar-body"]}
  106. onClick={() => {
  107. setOpenSettings(false);
  108. setShowSideBar(false);
  109. }}
  110. >
  111. <ChatList />
  112. </div>
  113. <div className={styles["sidebar-tail"]}>
  114. <div className={styles["sidebar-actions"]}>
  115. <div className={styles["sidebar-action"] + " " + styles.mobile}>
  116. <IconButton
  117. icon={<CloseIcon />}
  118. onClick={() => {
  119. if (confirm(Locale.Home.DeleteChat)) {
  120. removeSession(currentIndex);
  121. }
  122. }}
  123. />
  124. </div>
  125. <div className={styles["sidebar-action"]}>
  126. <IconButton
  127. icon={<SettingsIcon />}
  128. onClick={() => {
  129. setOpenSettings(true);
  130. setShowSideBar(false);
  131. }}
  132. shadow
  133. />
  134. </div>
  135. <div className={styles["sidebar-action"]}>
  136. <a href={REPO_URL} target="_blank">
  137. <IconButton icon={<GithubIcon />} shadow />
  138. </a>
  139. </div>
  140. </div>
  141. <div>
  142. <IconButton
  143. icon={<AddIcon />}
  144. text={Locale.Home.NewChat}
  145. onClick={() => {
  146. createNewSession();
  147. setShowSideBar(false);
  148. }}
  149. shadow
  150. />
  151. </div>
  152. </div>
  153. </div>
  154. <div className={styles["window-content"]}>
  155. {openSettings ? (
  156. <Settings
  157. closeSettings={() => {
  158. setOpenSettings(false);
  159. setShowSideBar(true);
  160. }}
  161. />
  162. ) : (
  163. <Chat
  164. key="chat"
  165. showSideBar={() => setShowSideBar(true)}
  166. sideBarShowing={showSideBar}
  167. />
  168. )}
  169. </div>
  170. </div>
  171. );
  172. }
  173. export function Home() {
  174. return (
  175. <ErrorBoundary>
  176. <_Home></_Home>
  177. </ErrorBoundary>
  178. );
  179. }