settings.tsx 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. import { useState, useEffect, useMemo } 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 LoadingIcon from "../icons/three-dots.svg";
  9. import EditIcon from "../icons/edit.svg";
  10. import EyeIcon from "../icons/eye.svg";
  11. import {
  12. Input,
  13. List,
  14. ListItem,
  15. Modal,
  16. PasswordInput,
  17. Popover,
  18. Select,
  19. showConfirm,
  20. } from "./ui-lib";
  21. import { ModelConfigList } from "./model-config";
  22. import { IconButton } from "./button";
  23. import {
  24. SubmitKey,
  25. useChatStore,
  26. Theme,
  27. useUpdateStore,
  28. useAccessStore,
  29. useAppConfig,
  30. } from "../store";
  31. import Locale, {
  32. AllLangs,
  33. ALL_LANG_OPTIONS,
  34. changeLang,
  35. getLang,
  36. } from "../locales";
  37. import { copyToClipboard } from "../utils";
  38. import Link from "next/link";
  39. import { Path, RELEASE_URL, UPDATE_URL } from "../constant";
  40. import { Prompt, SearchService, usePromptStore } from "../store/prompt";
  41. import { ErrorBoundary } from "./error";
  42. import { InputRange } from "./input-range";
  43. import { useNavigate } from "react-router-dom";
  44. import { Avatar, AvatarPicker } from "./emoji";
  45. import { getClientConfig } from "../config/client";
  46. import { useSyncStore } from "../store/sync";
  47. function EditPromptModal(props: { id: number; onClose: () => void }) {
  48. const promptStore = usePromptStore();
  49. const prompt = promptStore.get(props.id);
  50. return prompt ? (
  51. <div className="modal-mask">
  52. <Modal
  53. title={Locale.Settings.Prompt.EditModal.Title}
  54. onClose={props.onClose}
  55. actions={[
  56. <IconButton
  57. key=""
  58. onClick={props.onClose}
  59. text={Locale.UI.Confirm}
  60. bordered
  61. />,
  62. ]}
  63. >
  64. <div className={styles["edit-prompt-modal"]}>
  65. <input
  66. type="text"
  67. value={prompt.title}
  68. readOnly={!prompt.isUser}
  69. className={styles["edit-prompt-title"]}
  70. onInput={(e) =>
  71. promptStore.update(
  72. props.id,
  73. (prompt) => (prompt.title = e.currentTarget.value),
  74. )
  75. }
  76. ></input>
  77. <Input
  78. value={prompt.content}
  79. readOnly={!prompt.isUser}
  80. className={styles["edit-prompt-content"]}
  81. rows={10}
  82. onInput={(e) =>
  83. promptStore.update(
  84. props.id,
  85. (prompt) => (prompt.content = e.currentTarget.value),
  86. )
  87. }
  88. ></Input>
  89. </div>
  90. </Modal>
  91. </div>
  92. ) : null;
  93. }
  94. function UserPromptModal(props: { onClose?: () => void }) {
  95. const promptStore = usePromptStore();
  96. const userPrompts = promptStore.getUserPrompts();
  97. const builtinPrompts = SearchService.builtinPrompts;
  98. const allPrompts = userPrompts.concat(builtinPrompts);
  99. const [searchInput, setSearchInput] = useState("");
  100. const [searchPrompts, setSearchPrompts] = useState<Prompt[]>([]);
  101. const prompts = searchInput.length > 0 ? searchPrompts : allPrompts;
  102. const [editingPromptId, setEditingPromptId] = useState<number>();
  103. useEffect(() => {
  104. if (searchInput.length > 0) {
  105. const searchResult = SearchService.search(searchInput);
  106. setSearchPrompts(searchResult);
  107. } else {
  108. setSearchPrompts([]);
  109. }
  110. }, [searchInput]);
  111. return (
  112. <div className="modal-mask">
  113. <Modal
  114. title={Locale.Settings.Prompt.Modal.Title}
  115. onClose={() => props.onClose?.()}
  116. actions={[
  117. <IconButton
  118. key="add"
  119. onClick={() =>
  120. promptStore.add({
  121. title: "Empty Prompt",
  122. content: "Empty Prompt Content",
  123. })
  124. }
  125. icon={<AddIcon />}
  126. bordered
  127. text={Locale.Settings.Prompt.Modal.Add}
  128. />,
  129. ]}
  130. >
  131. <div className={styles["user-prompt-modal"]}>
  132. <input
  133. type="text"
  134. className={styles["user-prompt-search"]}
  135. placeholder={Locale.Settings.Prompt.Modal.Search}
  136. value={searchInput}
  137. onInput={(e) => setSearchInput(e.currentTarget.value)}
  138. ></input>
  139. <div className={styles["user-prompt-list"]}>
  140. {prompts.map((v, _) => (
  141. <div className={styles["user-prompt-item"]} key={v.id ?? v.title}>
  142. <div className={styles["user-prompt-header"]}>
  143. <div className={styles["user-prompt-title"]}>{v.title}</div>
  144. <div className={styles["user-prompt-content"] + " one-line"}>
  145. {v.content}
  146. </div>
  147. </div>
  148. <div className={styles["user-prompt-buttons"]}>
  149. {v.isUser && (
  150. <IconButton
  151. icon={<ClearIcon />}
  152. className={styles["user-prompt-button"]}
  153. onClick={() => promptStore.remove(v.id!)}
  154. />
  155. )}
  156. {v.isUser ? (
  157. <IconButton
  158. icon={<EditIcon />}
  159. className={styles["user-prompt-button"]}
  160. onClick={() => setEditingPromptId(v.id)}
  161. />
  162. ) : (
  163. <IconButton
  164. icon={<EyeIcon />}
  165. className={styles["user-prompt-button"]}
  166. onClick={() => setEditingPromptId(v.id)}
  167. />
  168. )}
  169. <IconButton
  170. icon={<CopyIcon />}
  171. className={styles["user-prompt-button"]}
  172. onClick={() => copyToClipboard(v.content)}
  173. />
  174. </div>
  175. </div>
  176. ))}
  177. </div>
  178. </div>
  179. </Modal>
  180. {editingPromptId !== undefined && (
  181. <EditPromptModal
  182. id={editingPromptId!}
  183. onClose={() => setEditingPromptId(undefined)}
  184. />
  185. )}
  186. </div>
  187. );
  188. }
  189. function DangerItems() {
  190. const chatStore = useChatStore();
  191. const appConfig = useAppConfig();
  192. return (
  193. <List>
  194. <ListItem
  195. title={Locale.Settings.Danger.Reset.Title}
  196. subTitle={Locale.Settings.Danger.Reset.SubTitle}
  197. >
  198. <IconButton
  199. text={Locale.Settings.Danger.Reset.Action}
  200. onClick={async () => {
  201. if (await showConfirm(Locale.Settings.Danger.Reset.Confirm)) {
  202. appConfig.reset();
  203. }
  204. }}
  205. type="danger"
  206. />
  207. </ListItem>
  208. <ListItem
  209. title={Locale.Settings.Danger.Clear.Title}
  210. subTitle={Locale.Settings.Danger.Clear.SubTitle}
  211. >
  212. <IconButton
  213. text={Locale.Settings.Danger.Clear.Action}
  214. onClick={async () => {
  215. if (await showConfirm(Locale.Settings.Danger.Clear.Confirm)) {
  216. chatStore.clearAllData();
  217. }
  218. }}
  219. type="danger"
  220. />
  221. </ListItem>
  222. </List>
  223. );
  224. }
  225. function SyncItems() {
  226. const syncStore = useSyncStore();
  227. const webdav = syncStore.webDavConfig;
  228. // not ready: https://github.com/Yidadaa/ChatGPT-Next-Web/issues/920#issuecomment-1609866332
  229. return null;
  230. return (
  231. <List>
  232. <ListItem
  233. title={"上次同步:" + new Date().toLocaleString()}
  234. subTitle={"20 次对话,100 条消息,200 提示词,20 面具"}
  235. >
  236. <IconButton
  237. icon={<ResetIcon />}
  238. text="同步"
  239. onClick={() => {
  240. syncStore.check().then(console.log);
  241. }}
  242. />
  243. </ListItem>
  244. <ListItem
  245. title={"本地备份"}
  246. subTitle={"20 次对话,100 条消息,200 提示词,20 面具"}
  247. ></ListItem>
  248. <ListItem
  249. title={"Web Dav Server"}
  250. subTitle={Locale.Settings.AccessCode.SubTitle}
  251. >
  252. <input
  253. value={webdav.server}
  254. type="text"
  255. placeholder={"https://example.com"}
  256. onChange={(e) => {
  257. syncStore.update(
  258. (config) => (config.server = e.currentTarget.value),
  259. );
  260. }}
  261. />
  262. </ListItem>
  263. <ListItem title="Web Dav User Name" subTitle="user name here">
  264. <input
  265. value={webdav.username}
  266. type="text"
  267. placeholder={"username"}
  268. onChange={(e) => {
  269. syncStore.update(
  270. (config) => (config.username = e.currentTarget.value),
  271. );
  272. }}
  273. />
  274. </ListItem>
  275. <ListItem title="Web Dav Password" subTitle="password here">
  276. <input
  277. value={webdav.password}
  278. type="text"
  279. placeholder={"password"}
  280. onChange={(e) => {
  281. syncStore.update(
  282. (config) => (config.password = e.currentTarget.value),
  283. );
  284. }}
  285. />
  286. </ListItem>
  287. </List>
  288. );
  289. }
  290. export function Settings() {
  291. const navigate = useNavigate();
  292. const [showEmojiPicker, setShowEmojiPicker] = useState(false);
  293. const config = useAppConfig();
  294. const updateConfig = config.update;
  295. const chatStore = useChatStore();
  296. const updateStore = useUpdateStore();
  297. const [checkingUpdate, setCheckingUpdate] = useState(false);
  298. const currentVersion = updateStore.formatVersion(updateStore.version);
  299. const remoteId = updateStore.formatVersion(updateStore.remoteVersion);
  300. const hasNewVersion = currentVersion !== remoteId;
  301. const updateUrl = getClientConfig()?.isApp ? RELEASE_URL : UPDATE_URL;
  302. function checkUpdate(force = false) {
  303. setCheckingUpdate(true);
  304. updateStore.getLatestVersion(force).then(() => {
  305. setCheckingUpdate(false);
  306. });
  307. console.log("[Update] local version ", updateStore.version);
  308. console.log("[Update] remote version ", updateStore.remoteVersion);
  309. }
  310. const usage = {
  311. used: updateStore.used,
  312. subscription: updateStore.subscription,
  313. };
  314. const [loadingUsage, setLoadingUsage] = useState(false);
  315. function checkUsage(force = false) {
  316. if (accessStore.hideBalanceQuery) {
  317. return;
  318. }
  319. setLoadingUsage(true);
  320. updateStore.updateUsage(force).finally(() => {
  321. setLoadingUsage(false);
  322. });
  323. }
  324. const accessStore = useAccessStore();
  325. const enabledAccessControl = useMemo(
  326. () => accessStore.enabledAccessControl(),
  327. // eslint-disable-next-line react-hooks/exhaustive-deps
  328. [],
  329. );
  330. const promptStore = usePromptStore();
  331. const builtinCount = SearchService.count.builtin;
  332. const customCount = promptStore.getUserPrompts().length ?? 0;
  333. const [shouldShowPromptModal, setShowPromptModal] = useState(false);
  334. const showUsage = accessStore.isAuthorized();
  335. useEffect(() => {
  336. // checks per minutes
  337. checkUpdate();
  338. showUsage && checkUsage();
  339. // eslint-disable-next-line react-hooks/exhaustive-deps
  340. }, []);
  341. useEffect(() => {
  342. const keydownEvent = (e: KeyboardEvent) => {
  343. if (e.key === "Escape") {
  344. navigate(Path.Home);
  345. }
  346. };
  347. document.addEventListener("keydown", keydownEvent);
  348. return () => {
  349. document.removeEventListener("keydown", keydownEvent);
  350. };
  351. // eslint-disable-next-line react-hooks/exhaustive-deps
  352. }, []);
  353. const clientConfig = useMemo(() => getClientConfig(), []);
  354. const showAccessCode = enabledAccessControl && !clientConfig?.isApp;
  355. return (
  356. <ErrorBoundary>
  357. <div className="window-header" data-tauri-drag-region>
  358. <div className="window-header-title">
  359. <div className="window-header-main-title">
  360. {Locale.Settings.Title}
  361. </div>
  362. <div className="window-header-sub-title">
  363. {Locale.Settings.SubTitle}
  364. </div>
  365. </div>
  366. <div className="window-actions">
  367. <div className="window-action-button"></div>
  368. <div className="window-action-button"></div>
  369. <div className="window-action-button">
  370. <IconButton
  371. icon={<CloseIcon />}
  372. onClick={() => navigate(Path.Home)}
  373. bordered
  374. />
  375. </div>
  376. </div>
  377. </div>
  378. <div className={styles["settings"]}>
  379. <List>
  380. <ListItem title={Locale.Settings.Avatar}>
  381. <Popover
  382. onClose={() => setShowEmojiPicker(false)}
  383. content={
  384. <AvatarPicker
  385. onEmojiClick={(avatar: string) => {
  386. updateConfig((config) => (config.avatar = avatar));
  387. setShowEmojiPicker(false);
  388. }}
  389. />
  390. }
  391. open={showEmojiPicker}
  392. >
  393. <div
  394. className={styles.avatar}
  395. onClick={() => setShowEmojiPicker(true)}
  396. >
  397. <Avatar avatar={config.avatar} />
  398. </div>
  399. </Popover>
  400. </ListItem>
  401. <ListItem
  402. title={Locale.Settings.Update.Version(currentVersion ?? "unknown")}
  403. subTitle={
  404. checkingUpdate
  405. ? Locale.Settings.Update.IsChecking
  406. : hasNewVersion
  407. ? Locale.Settings.Update.FoundUpdate(remoteId ?? "ERROR")
  408. : Locale.Settings.Update.IsLatest
  409. }
  410. >
  411. {checkingUpdate ? (
  412. <LoadingIcon />
  413. ) : hasNewVersion ? (
  414. <Link href={updateUrl} target="_blank" className="link">
  415. {Locale.Settings.Update.GoToUpdate}
  416. </Link>
  417. ) : (
  418. <IconButton
  419. icon={<ResetIcon></ResetIcon>}
  420. text={Locale.Settings.Update.CheckUpdate}
  421. onClick={() => checkUpdate(true)}
  422. />
  423. )}
  424. </ListItem>
  425. <ListItem title={Locale.Settings.SendKey}>
  426. <Select
  427. value={config.submitKey}
  428. onChange={(e) => {
  429. updateConfig(
  430. (config) =>
  431. (config.submitKey = e.target.value as any as SubmitKey),
  432. );
  433. }}
  434. >
  435. {Object.values(SubmitKey).map((v) => (
  436. <option value={v} key={v}>
  437. {v}
  438. </option>
  439. ))}
  440. </Select>
  441. </ListItem>
  442. <ListItem title={Locale.Settings.Theme}>
  443. <Select
  444. value={config.theme}
  445. onChange={(e) => {
  446. updateConfig(
  447. (config) => (config.theme = e.target.value as any as Theme),
  448. );
  449. }}
  450. >
  451. {Object.values(Theme).map((v) => (
  452. <option value={v} key={v}>
  453. {v}
  454. </option>
  455. ))}
  456. </Select>
  457. </ListItem>
  458. <ListItem title={Locale.Settings.Lang.Name}>
  459. <Select
  460. value={getLang()}
  461. onChange={(e) => {
  462. changeLang(e.target.value as any);
  463. }}
  464. >
  465. {AllLangs.map((lang) => (
  466. <option value={lang} key={lang}>
  467. {ALL_LANG_OPTIONS[lang]}
  468. </option>
  469. ))}
  470. </Select>
  471. </ListItem>
  472. <ListItem
  473. title={Locale.Settings.FontSize.Title}
  474. subTitle={Locale.Settings.FontSize.SubTitle}
  475. >
  476. <InputRange
  477. title={`${config.fontSize ?? 14}px`}
  478. value={config.fontSize}
  479. min="12"
  480. max="18"
  481. step="1"
  482. onChange={(e) =>
  483. updateConfig(
  484. (config) =>
  485. (config.fontSize = Number.parseInt(e.currentTarget.value)),
  486. )
  487. }
  488. ></InputRange>
  489. </ListItem>
  490. <ListItem
  491. title={Locale.Settings.SendPreviewBubble.Title}
  492. subTitle={Locale.Settings.SendPreviewBubble.SubTitle}
  493. >
  494. <input
  495. type="checkbox"
  496. checked={config.sendPreviewBubble}
  497. onChange={(e) =>
  498. updateConfig(
  499. (config) =>
  500. (config.sendPreviewBubble = e.currentTarget.checked),
  501. )
  502. }
  503. ></input>
  504. </ListItem>
  505. </List>
  506. <List>
  507. <ListItem
  508. title={Locale.Settings.Mask.Splash.Title}
  509. subTitle={Locale.Settings.Mask.Splash.SubTitle}
  510. >
  511. <input
  512. type="checkbox"
  513. checked={!config.dontShowMaskSplashScreen}
  514. onChange={(e) =>
  515. updateConfig(
  516. (config) =>
  517. (config.dontShowMaskSplashScreen =
  518. !e.currentTarget.checked),
  519. )
  520. }
  521. ></input>
  522. </ListItem>
  523. <ListItem
  524. title={Locale.Settings.Mask.Builtin.Title}
  525. subTitle={Locale.Settings.Mask.Builtin.SubTitle}
  526. >
  527. <input
  528. type="checkbox"
  529. checked={config.hideBuiltinMasks}
  530. onChange={(e) =>
  531. updateConfig(
  532. (config) =>
  533. (config.hideBuiltinMasks = e.currentTarget.checked),
  534. )
  535. }
  536. ></input>
  537. </ListItem>
  538. </List>
  539. <List>
  540. {showAccessCode ? (
  541. <ListItem
  542. title={Locale.Settings.AccessCode.Title}
  543. subTitle={Locale.Settings.AccessCode.SubTitle}
  544. >
  545. <PasswordInput
  546. value={accessStore.accessCode}
  547. type="text"
  548. placeholder={Locale.Settings.AccessCode.Placeholder}
  549. onChange={(e) => {
  550. accessStore.updateCode(e.currentTarget.value);
  551. }}
  552. />
  553. </ListItem>
  554. ) : (
  555. <></>
  556. )}
  557. {!accessStore.hideUserApiKey ? (
  558. <>
  559. <ListItem
  560. title={Locale.Settings.Endpoint.Title}
  561. subTitle={Locale.Settings.Endpoint.SubTitle}
  562. >
  563. <input
  564. type="text"
  565. value={accessStore.openaiUrl}
  566. placeholder="https://api.openai.com/"
  567. onChange={(e) =>
  568. accessStore.updateOpenAiUrl(e.currentTarget.value)
  569. }
  570. ></input>
  571. </ListItem>
  572. <ListItem
  573. title={Locale.Settings.Token.Title}
  574. subTitle={Locale.Settings.Token.SubTitle}
  575. >
  576. <PasswordInput
  577. value={accessStore.token}
  578. type="text"
  579. placeholder={Locale.Settings.Token.Placeholder}
  580. onChange={(e) => {
  581. accessStore.updateToken(e.currentTarget.value);
  582. }}
  583. />
  584. </ListItem>
  585. </>
  586. ) : null}
  587. {!accessStore.hideBalanceQuery ? (
  588. <ListItem
  589. title={Locale.Settings.Usage.Title}
  590. subTitle={
  591. showUsage
  592. ? loadingUsage
  593. ? Locale.Settings.Usage.IsChecking
  594. : Locale.Settings.Usage.SubTitle(
  595. usage?.used ?? "[?]",
  596. usage?.subscription ?? "[?]",
  597. )
  598. : Locale.Settings.Usage.NoAccess
  599. }
  600. >
  601. {!showUsage || loadingUsage ? (
  602. <div />
  603. ) : (
  604. <IconButton
  605. icon={<ResetIcon></ResetIcon>}
  606. text={Locale.Settings.Usage.Check}
  607. onClick={() => checkUsage(true)}
  608. />
  609. )}
  610. </ListItem>
  611. ) : null}
  612. </List>
  613. <List>
  614. <ListItem
  615. title={Locale.Settings.Prompt.Disable.Title}
  616. subTitle={Locale.Settings.Prompt.Disable.SubTitle}
  617. >
  618. <input
  619. type="checkbox"
  620. checked={config.disablePromptHint}
  621. onChange={(e) =>
  622. updateConfig(
  623. (config) =>
  624. (config.disablePromptHint = e.currentTarget.checked),
  625. )
  626. }
  627. ></input>
  628. </ListItem>
  629. <ListItem
  630. title={Locale.Settings.Prompt.List}
  631. subTitle={Locale.Settings.Prompt.ListCount(
  632. builtinCount,
  633. customCount,
  634. )}
  635. >
  636. <IconButton
  637. icon={<EditIcon />}
  638. text={Locale.Settings.Prompt.Edit}
  639. onClick={() => setShowPromptModal(true)}
  640. />
  641. </ListItem>
  642. </List>
  643. <SyncItems />
  644. <List>
  645. <ModelConfigList
  646. modelConfig={config.modelConfig}
  647. updateConfig={(updater) => {
  648. const modelConfig = { ...config.modelConfig };
  649. updater(modelConfig);
  650. config.update((config) => (config.modelConfig = modelConfig));
  651. }}
  652. />
  653. </List>
  654. {shouldShowPromptModal && (
  655. <UserPromptModal onClose={() => setShowPromptModal(false)} />
  656. )}
  657. <DangerItems />
  658. </div>
  659. </ErrorBoundary>
  660. );
  661. }