settings.tsx 17 KB

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