home.tsx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 themeColor = getComputedStyle(document.body)
  43. .getPropertyValue("--theme-color")
  44. .trim();
  45. const metaDescription = document.querySelector('meta[name="theme-color"]');
  46. metaDescription?.setAttribute("content", themeColor);
  47. }, [config.theme]);
  48. }
  49. const useHasHydrated = () => {
  50. const [hasHydrated, setHasHydrated] = useState<boolean>(false);
  51. useEffect(() => {
  52. setHasHydrated(true);
  53. }, []);
  54. return hasHydrated;
  55. };
  56. function _Home() {
  57. const [createNewSession, currentIndex, removeSession] = useChatStore(
  58. (state) => [
  59. state.newSession,
  60. state.currentSessionIndex,
  61. state.removeSession,
  62. ],
  63. );
  64. const loading = !useHasHydrated();
  65. const [showSideBar, setShowSideBar] = useState(true);
  66. // setting
  67. const [openSettings, setOpenSettings] = useState(false);
  68. const config = useChatStore((state) => state.config);
  69. useSwitchTheme();
  70. if (loading) {
  71. return <Loading />;
  72. }
  73. return (
  74. <div
  75. className={`${
  76. config.tightBorder && !isMobileScreen()
  77. ? styles["tight-container"]
  78. : styles.container
  79. }`}
  80. >
  81. <div
  82. className={styles.sidebar + ` ${showSideBar && styles["sidebar-show"]}`}
  83. >
  84. <div className={styles["sidebar-header"]}>
  85. <div className={styles["sidebar-title"]}>ChatGPT Next</div>
  86. <div className={styles["sidebar-sub-title"]}>
  87. Build your own AI assistant.
  88. </div>
  89. <div className={styles["sidebar-logo"]}>
  90. <ChatGptIcon />
  91. </div>
  92. </div>
  93. <div
  94. className={styles["sidebar-body"]}
  95. onClick={() => {
  96. setOpenSettings(false);
  97. setShowSideBar(false);
  98. }}
  99. >
  100. <ChatList />
  101. </div>
  102. <div className={styles["sidebar-tail"]}>
  103. <div className={styles["sidebar-actions"]}>
  104. <div className={styles["sidebar-action"] + " " + styles.mobile}>
  105. <IconButton
  106. icon={<CloseIcon />}
  107. onClick={() => {
  108. if (confirm(Locale.Home.DeleteChat)) {
  109. removeSession(currentIndex);
  110. }
  111. }}
  112. />
  113. </div>
  114. <div className={styles["sidebar-action"]}>
  115. <IconButton
  116. icon={<SettingsIcon />}
  117. onClick={() => {
  118. setOpenSettings(true);
  119. setShowSideBar(false);
  120. }}
  121. shadow
  122. />
  123. </div>
  124. <div className={styles["sidebar-action"]}>
  125. <a href={REPO_URL} target="_blank">
  126. <IconButton icon={<GithubIcon />} shadow />
  127. </a>
  128. </div>
  129. </div>
  130. <div>
  131. <IconButton
  132. icon={<AddIcon />}
  133. text={Locale.Home.NewChat}
  134. onClick={() => {
  135. createNewSession();
  136. setShowSideBar(false);
  137. }}
  138. shadow
  139. />
  140. </div>
  141. </div>
  142. </div>
  143. <div className={styles["window-content"]}>
  144. {openSettings ? (
  145. <Settings
  146. closeSettings={() => {
  147. setOpenSettings(false);
  148. setShowSideBar(true);
  149. }}
  150. />
  151. ) : (
  152. <Chat
  153. key="chat"
  154. showSideBar={() => setShowSideBar(true)}
  155. sideBarShowing={showSideBar}
  156. />
  157. )}
  158. </div>
  159. </div>
  160. );
  161. }
  162. export function Home() {
  163. return (
  164. <ErrorBoundary>
  165. <_Home></_Home>
  166. </ErrorBoundary>
  167. );
  168. }