settings.tsx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. import { useState, useEffect, useMemo, HTMLProps, useRef } from "react";
  2. import styles from "./settings.module.scss";
  3. import ResetIcon from "../icons/reload.svg";
  4. import CloseIcon from "../icons/close.svg";
  5. import CopyIcon from "../icons/copy.svg";
  6. import ClearIcon from "../icons/clear.svg";
  7. import EditIcon from "../icons/edit.svg";
  8. import { Input, List, ListItem, Modal, PasswordInput, Popover } from "./ui-lib";
  9. import { ModelConfigList } from "./model-config";
  10. import { IconButton } from "./button";
  11. import {
  12. SubmitKey,
  13. useChatStore,
  14. Theme,
  15. useUpdateStore,
  16. useAccessStore,
  17. useAppConfig,
  18. } from "../store";
  19. import Locale, { AllLangs, changeLang, getLang } from "../locales";
  20. import { copyToClipboard } from "../utils";
  21. import Link from "next/link";
  22. import { Path, UPDATE_URL } from "../constant";
  23. import { Prompt, SearchService, usePromptStore } from "../store/prompt";
  24. import { ErrorBoundary } from "./error";
  25. import { InputRange } from "./input-range";
  26. import { useNavigate } from "react-router-dom";
  27. import { Avatar, AvatarPicker } from "./emoji";
  28. function UserPromptModal(props: { onClose?: () => void }) {
  29. const promptStore = usePromptStore();
  30. const userPrompts = promptStore.getUserPrompts();
  31. const builtinPrompts = SearchService.builtinPrompts;
  32. const allPrompts = userPrompts.concat(builtinPrompts);
  33. const [searchInput, setSearchInput] = useState("");
  34. const [searchPrompts, setSearchPrompts] = useState<Prompt[]>([]);
  35. const prompts = searchInput.length > 0 ? searchPrompts : allPrompts;
  36. useEffect(() => {
  37. if (searchInput.length > 0) {
  38. const searchResult = SearchService.search(searchInput);
  39. setSearchPrompts(searchResult);
  40. } else {
  41. setSearchPrompts([]);
  42. }
  43. }, [searchInput]);
  44. return (
  45. <div className="modal-mask">
  46. <Modal
  47. title={Locale.Settings.Prompt.Modal.Title}
  48. onClose={() => props.onClose?.()}
  49. actions={[
  50. <IconButton
  51. key="add"
  52. onClick={() => promptStore.add({ title: "", content: "" })}
  53. icon={<ClearIcon />}
  54. bordered
  55. text={Locale.Settings.Prompt.Modal.Add}
  56. />,
  57. ]}
  58. >
  59. <div className={styles["user-prompt-modal"]}>
  60. <input
  61. type="text"
  62. className={styles["user-prompt-search"]}
  63. placeholder={Locale.Settings.Prompt.Modal.Search}
  64. value={searchInput}
  65. onInput={(e) => setSearchInput(e.currentTarget.value)}
  66. ></input>
  67. <div className={styles["user-prompt-list"]}>
  68. {prompts.map((v, _) => (
  69. <div className={styles["user-prompt-item"]} key={v.id ?? v.title}>
  70. <div className={styles["user-prompt-header"]}>
  71. <input
  72. type="text"
  73. className={styles["user-prompt-title"]}
  74. value={v.title}
  75. readOnly={!v.isUser}
  76. onChange={(e) => {
  77. if (v.isUser) {
  78. promptStore.updateUserPrompts(
  79. v.id!,
  80. (prompt) => (prompt.title = e.currentTarget.value),
  81. );
  82. }
  83. }}
  84. ></input>
  85. <div className={styles["user-prompt-buttons"]}>
  86. {v.isUser && (
  87. <IconButton
  88. icon={<ClearIcon />}
  89. bordered
  90. className={styles["user-prompt-button"]}
  91. onClick={() => promptStore.remove(v.id!)}
  92. />
  93. )}
  94. <IconButton
  95. icon={<CopyIcon />}
  96. bordered
  97. className={styles["user-prompt-button"]}
  98. onClick={() => copyToClipboard(v.content)}
  99. />
  100. </div>
  101. </div>
  102. <Input
  103. rows={2}
  104. value={v.content}
  105. className={styles["user-prompt-content"]}
  106. readOnly={!v.isUser}
  107. onChange={(e) => {
  108. if (v.isUser) {
  109. promptStore.updateUserPrompts(
  110. v.id!,
  111. (prompt) => (prompt.content = e.currentTarget.value),
  112. );
  113. }
  114. }}
  115. />
  116. </div>
  117. ))}
  118. </div>
  119. </div>
  120. </Modal>
  121. </div>
  122. );
  123. }
  124. export function Settings() {
  125. const navigate = useNavigate();
  126. const [showEmojiPicker, setShowEmojiPicker] = useState(false);
  127. const config = useAppConfig();
  128. const updateConfig = config.update;
  129. const resetConfig = config.reset;
  130. const [clearAllData, clearSessions] = useChatStore((state) => [
  131. state.clearAllData,
  132. state.clearSessions,
  133. ]);
  134. const updateStore = useUpdateStore();
  135. const [checkingUpdate, setCheckingUpdate] = useState(false);
  136. const currentVersion = updateStore.version;
  137. const remoteId = updateStore.remoteVersion;
  138. const hasNewVersion = currentVersion !== remoteId;
  139. function checkUpdate(force = false) {
  140. setCheckingUpdate(true);
  141. updateStore.getLatestVersion(force).then(() => {
  142. setCheckingUpdate(false);
  143. });
  144. }
  145. const usage = {
  146. used: updateStore.used,
  147. subscription: updateStore.subscription,
  148. };
  149. const [loadingUsage, setLoadingUsage] = useState(false);
  150. function checkUsage() {
  151. setLoadingUsage(true);
  152. updateStore.updateUsage().finally(() => {
  153. setLoadingUsage(false);
  154. });
  155. }
  156. const accessStore = useAccessStore();
  157. const enabledAccessControl = useMemo(
  158. () => accessStore.enabledAccessControl(),
  159. // eslint-disable-next-line react-hooks/exhaustive-deps
  160. [],
  161. );
  162. const promptStore = usePromptStore();
  163. const builtinCount = SearchService.count.builtin;
  164. const customCount = promptStore.getUserPrompts().length ?? 0;
  165. const [shouldShowPromptModal, setShowPromptModal] = useState(false);
  166. const showUsage = accessStore.isAuthorized();
  167. useEffect(() => {
  168. // checks per minutes
  169. checkUpdate();
  170. showUsage && checkUsage();
  171. // eslint-disable-next-line react-hooks/exhaustive-deps
  172. }, []);
  173. useEffect(() => {
  174. const keydownEvent = (e: KeyboardEvent) => {
  175. if (e.key === "Escape") {
  176. navigate(Path.Home);
  177. }
  178. };
  179. document.addEventListener("keydown", keydownEvent);
  180. return () => {
  181. document.removeEventListener("keydown", keydownEvent);
  182. };
  183. // eslint-disable-next-line react-hooks/exhaustive-deps
  184. }, []);
  185. return (
  186. <ErrorBoundary>
  187. <div className={styles["window-header"]}>
  188. <div className={styles["window-header-title"]}>
  189. <div className={styles["window-header-main-title"]}>
  190. {Locale.Settings.Title}
  191. </div>
  192. <div className={styles["window-header-sub-title"]}>
  193. {Locale.Settings.SubTitle}
  194. </div>
  195. </div>
  196. <div className={styles["window-actions"]}>
  197. <div className={styles["window-action-button"]}>
  198. <IconButton
  199. icon={<ClearIcon />}
  200. onClick={() => {
  201. const confirmed = window.confirm(
  202. `${Locale.Settings.Actions.ConfirmClearAll.Confirm}`,
  203. );
  204. if (confirmed) {
  205. clearSessions();
  206. }
  207. }}
  208. bordered
  209. title={Locale.Settings.Actions.ClearAll}
  210. />
  211. </div>
  212. <div className={styles["window-action-button"]}>
  213. <IconButton
  214. icon={<ResetIcon />}
  215. onClick={() => {
  216. const confirmed = window.confirm(
  217. `${Locale.Settings.Actions.ConfirmResetAll.Confirm}`,
  218. );
  219. if (confirmed) {
  220. resetConfig();
  221. }
  222. }}
  223. bordered
  224. title={Locale.Settings.Actions.ResetAll}
  225. />
  226. </div>
  227. <div className={styles["window-action-button"]}>
  228. <IconButton
  229. icon={<CloseIcon />}
  230. onClick={() => navigate(Path.Home)}
  231. bordered
  232. title={Locale.Settings.Actions.Close}
  233. />
  234. </div>
  235. </div>
  236. </div>
  237. <div className={styles["settings"]}>
  238. <List>
  239. <ListItem title={Locale.Settings.Avatar}>
  240. <Popover
  241. onClose={() => setShowEmojiPicker(false)}
  242. content={
  243. <AvatarPicker
  244. onEmojiClick={(avatar: string) => {
  245. updateConfig((config) => (config.avatar = avatar));
  246. setShowEmojiPicker(false);
  247. }}
  248. />
  249. }
  250. open={showEmojiPicker}
  251. >
  252. <div
  253. className={styles.avatar}
  254. onClick={() => setShowEmojiPicker(true)}
  255. >
  256. <Avatar avatar={config.avatar} />
  257. </div>
  258. </Popover>
  259. </ListItem>
  260. <ListItem
  261. title={Locale.Settings.Update.Version(currentVersion ?? "unknown")}
  262. subTitle={
  263. checkingUpdate
  264. ? Locale.Settings.Update.IsChecking
  265. : hasNewVersion
  266. ? Locale.Settings.Update.FoundUpdate(remoteId ?? "ERROR")
  267. : Locale.Settings.Update.IsLatest
  268. }
  269. >
  270. {checkingUpdate ? (
  271. <div />
  272. ) : hasNewVersion ? (
  273. <Link href={UPDATE_URL} target="_blank" className="link">
  274. {Locale.Settings.Update.GoToUpdate}
  275. </Link>
  276. ) : (
  277. <IconButton
  278. icon={<ResetIcon></ResetIcon>}
  279. text={Locale.Settings.Update.CheckUpdate}
  280. onClick={() => checkUpdate(true)}
  281. />
  282. )}
  283. </ListItem>
  284. <ListItem title={Locale.Settings.SendKey}>
  285. <select
  286. value={config.submitKey}
  287. onChange={(e) => {
  288. updateConfig(
  289. (config) =>
  290. (config.submitKey = e.target.value as any as SubmitKey),
  291. );
  292. }}
  293. >
  294. {Object.values(SubmitKey).map((v) => (
  295. <option value={v} key={v}>
  296. {v}
  297. </option>
  298. ))}
  299. </select>
  300. </ListItem>
  301. <ListItem title={Locale.Settings.Theme}>
  302. <select
  303. value={config.theme}
  304. onChange={(e) => {
  305. updateConfig(
  306. (config) => (config.theme = e.target.value as any as Theme),
  307. );
  308. }}
  309. >
  310. {Object.values(Theme).map((v) => (
  311. <option value={v} key={v}>
  312. {v}
  313. </option>
  314. ))}
  315. </select>
  316. </ListItem>
  317. <ListItem title={Locale.Settings.Lang.Name}>
  318. <select
  319. value={getLang()}
  320. onChange={(e) => {
  321. changeLang(e.target.value as any);
  322. }}
  323. >
  324. {AllLangs.map((lang) => (
  325. <option value={lang} key={lang}>
  326. {Locale.Settings.Lang.Options[lang]}
  327. </option>
  328. ))}
  329. </select>
  330. </ListItem>
  331. <ListItem
  332. title={Locale.Settings.FontSize.Title}
  333. subTitle={Locale.Settings.FontSize.SubTitle}
  334. >
  335. <InputRange
  336. title={`${config.fontSize ?? 14}px`}
  337. value={config.fontSize}
  338. min="12"
  339. max="18"
  340. step="1"
  341. onChange={(e) =>
  342. updateConfig(
  343. (config) =>
  344. (config.fontSize = Number.parseInt(e.currentTarget.value)),
  345. )
  346. }
  347. ></InputRange>
  348. </ListItem>
  349. <ListItem title={Locale.Settings.TightBorder}>
  350. <input
  351. type="checkbox"
  352. checked={config.tightBorder}
  353. onChange={(e) =>
  354. updateConfig(
  355. (config) => (config.tightBorder = e.currentTarget.checked),
  356. )
  357. }
  358. ></input>
  359. </ListItem>
  360. <ListItem title={Locale.Settings.SendPreviewBubble}>
  361. <input
  362. type="checkbox"
  363. checked={config.sendPreviewBubble}
  364. onChange={(e) =>
  365. updateConfig(
  366. (config) =>
  367. (config.sendPreviewBubble = e.currentTarget.checked),
  368. )
  369. }
  370. ></input>
  371. </ListItem>
  372. </List>
  373. <List>
  374. {enabledAccessControl ? (
  375. <ListItem
  376. title={Locale.Settings.AccessCode.Title}
  377. subTitle={Locale.Settings.AccessCode.SubTitle}
  378. >
  379. <PasswordInput
  380. value={accessStore.accessCode}
  381. type="text"
  382. placeholder={Locale.Settings.AccessCode.Placeholder}
  383. onChange={(e) => {
  384. accessStore.updateCode(e.currentTarget.value);
  385. }}
  386. />
  387. </ListItem>
  388. ) : (
  389. <></>
  390. )}
  391. <ListItem
  392. title={Locale.Settings.Token.Title}
  393. subTitle={Locale.Settings.Token.SubTitle}
  394. >
  395. <PasswordInput
  396. value={accessStore.token}
  397. type="text"
  398. placeholder={Locale.Settings.Token.Placeholder}
  399. onChange={(e) => {
  400. accessStore.updateToken(e.currentTarget.value);
  401. }}
  402. />
  403. </ListItem>
  404. <ListItem
  405. title={Locale.Settings.Usage.Title}
  406. subTitle={
  407. showUsage
  408. ? loadingUsage
  409. ? Locale.Settings.Usage.IsChecking
  410. : Locale.Settings.Usage.SubTitle(
  411. usage?.used ?? "[?]",
  412. usage?.subscription ?? "[?]",
  413. )
  414. : Locale.Settings.Usage.NoAccess
  415. }
  416. >
  417. {!showUsage || loadingUsage ? (
  418. <div />
  419. ) : (
  420. <IconButton
  421. icon={<ResetIcon></ResetIcon>}
  422. text={Locale.Settings.Usage.Check}
  423. onClick={checkUsage}
  424. />
  425. )}
  426. </ListItem>
  427. </List>
  428. <List>
  429. <ListItem
  430. title={Locale.Settings.Prompt.Disable.Title}
  431. subTitle={Locale.Settings.Prompt.Disable.SubTitle}
  432. >
  433. <input
  434. type="checkbox"
  435. checked={config.disablePromptHint}
  436. onChange={(e) =>
  437. updateConfig(
  438. (config) =>
  439. (config.disablePromptHint = e.currentTarget.checked),
  440. )
  441. }
  442. ></input>
  443. </ListItem>
  444. <ListItem
  445. title={Locale.Settings.Prompt.List}
  446. subTitle={Locale.Settings.Prompt.ListCount(
  447. builtinCount,
  448. customCount,
  449. )}
  450. >
  451. <IconButton
  452. icon={<EditIcon />}
  453. text={Locale.Settings.Prompt.Edit}
  454. onClick={() => setShowPromptModal(true)}
  455. />
  456. </ListItem>
  457. </List>
  458. <ModelConfigList
  459. modelConfig={config.modelConfig}
  460. updateConfig={(upater) => {
  461. const modelConfig = { ...config.modelConfig };
  462. upater(modelConfig);
  463. config.update((config) => (config.modelConfig = modelConfig));
  464. }}
  465. />
  466. {shouldShowPromptModal && (
  467. <UserPromptModal onClose={() => setShowPromptModal(false)} />
  468. )}
  469. </div>
  470. </ErrorBoundary>
  471. );
  472. }