settings.tsx 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  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 updateStore = useUpdateStore();
  296. const [checkingUpdate, setCheckingUpdate] = useState(false);
  297. const currentVersion = updateStore.formatVersion(updateStore.version);
  298. const remoteId = updateStore.formatVersion(updateStore.remoteVersion);
  299. const hasNewVersion = currentVersion !== remoteId;
  300. const updateUrl = getClientConfig()?.isApp ? RELEASE_URL : UPDATE_URL;
  301. function checkUpdate(force = false) {
  302. setCheckingUpdate(true);
  303. updateStore.getLatestVersion(force).then(() => {
  304. setCheckingUpdate(false);
  305. });
  306. console.log("[Update] local version ", updateStore.version);
  307. console.log("[Update] remote version ", updateStore.remoteVersion);
  308. }
  309. const usage = {
  310. used: updateStore.used,
  311. subscription: updateStore.subscription,
  312. };
  313. const [loadingUsage, setLoadingUsage] = useState(false);
  314. function checkUsage(force = false) {
  315. if (accessStore.hideBalanceQuery) {
  316. return;
  317. }
  318. setLoadingUsage(true);
  319. updateStore.updateUsage(force).finally(() => {
  320. setLoadingUsage(false);
  321. });
  322. }
  323. const accessStore = useAccessStore();
  324. const enabledAccessControl = useMemo(
  325. () => accessStore.enabledAccessControl(),
  326. // eslint-disable-next-line react-hooks/exhaustive-deps
  327. [],
  328. );
  329. const promptStore = usePromptStore();
  330. const builtinCount = SearchService.count.builtin;
  331. const customCount = promptStore.getUserPrompts().length ?? 0;
  332. const [shouldShowPromptModal, setShowPromptModal] = useState(false);
  333. const showUsage = accessStore.isAuthorized();
  334. useEffect(() => {
  335. // checks per minutes
  336. checkUpdate();
  337. showUsage && checkUsage();
  338. // eslint-disable-next-line react-hooks/exhaustive-deps
  339. }, []);
  340. useEffect(() => {
  341. const keydownEvent = (e: KeyboardEvent) => {
  342. if (e.key === "Escape") {
  343. navigate(Path.Home);
  344. }
  345. };
  346. document.addEventListener("keydown", keydownEvent);
  347. return () => {
  348. document.removeEventListener("keydown", keydownEvent);
  349. };
  350. // eslint-disable-next-line react-hooks/exhaustive-deps
  351. }, []);
  352. const clientConfig = useMemo(() => getClientConfig(), []);
  353. const showAccessCode = enabledAccessControl && !clientConfig?.isApp;
  354. return (
  355. <ErrorBoundary>
  356. <div className="window-header" data-tauri-drag-region>
  357. <div className="window-header-title">
  358. <div className="window-header-main-title">
  359. {Locale.Settings.Title}
  360. </div>
  361. <div className="window-header-sub-title">
  362. {Locale.Settings.SubTitle}
  363. </div>
  364. </div>
  365. <div className="window-actions">
  366. <div className="window-action-button"></div>
  367. <div className="window-action-button"></div>
  368. <div className="window-action-button">
  369. <IconButton
  370. icon={<CloseIcon />}
  371. onClick={() => navigate(Path.Home)}
  372. bordered
  373. />
  374. </div>
  375. </div>
  376. </div>
  377. <div className={styles["settings"]}>
  378. <List>
  379. <ListItem title={Locale.Settings.Avatar}>
  380. <Popover
  381. onClose={() => setShowEmojiPicker(false)}
  382. content={
  383. <AvatarPicker
  384. onEmojiClick={(avatar: string) => {
  385. updateConfig((config) => (config.avatar = avatar));
  386. setShowEmojiPicker(false);
  387. }}
  388. />
  389. }
  390. open={showEmojiPicker}
  391. >
  392. <div
  393. className={styles.avatar}
  394. onClick={() => setShowEmojiPicker(true)}
  395. >
  396. <Avatar avatar={config.avatar} />
  397. </div>
  398. </Popover>
  399. </ListItem>
  400. <ListItem
  401. title={Locale.Settings.Update.Version(currentVersion ?? "unknown")}
  402. subTitle={
  403. checkingUpdate
  404. ? Locale.Settings.Update.IsChecking
  405. : hasNewVersion
  406. ? Locale.Settings.Update.FoundUpdate(remoteId ?? "ERROR")
  407. : Locale.Settings.Update.IsLatest
  408. }
  409. >
  410. {checkingUpdate ? (
  411. <LoadingIcon />
  412. ) : hasNewVersion ? (
  413. <Link href={updateUrl} target="_blank" className="link">
  414. {Locale.Settings.Update.GoToUpdate}
  415. </Link>
  416. ) : (
  417. <IconButton
  418. icon={<ResetIcon></ResetIcon>}
  419. text={Locale.Settings.Update.CheckUpdate}
  420. onClick={() => checkUpdate(true)}
  421. />
  422. )}
  423. </ListItem>
  424. <ListItem title={Locale.Settings.SendKey}>
  425. <Select
  426. value={config.submitKey}
  427. onChange={(e) => {
  428. updateConfig(
  429. (config) =>
  430. (config.submitKey = e.target.value as any as SubmitKey),
  431. );
  432. }}
  433. >
  434. {Object.values(SubmitKey).map((v) => (
  435. <option value={v} key={v}>
  436. {v}
  437. </option>
  438. ))}
  439. </Select>
  440. </ListItem>
  441. <ListItem title={Locale.Settings.Theme}>
  442. <Select
  443. value={config.theme}
  444. onChange={(e) => {
  445. updateConfig(
  446. (config) => (config.theme = e.target.value as any as Theme),
  447. );
  448. }}
  449. >
  450. {Object.values(Theme).map((v) => (
  451. <option value={v} key={v}>
  452. {v}
  453. </option>
  454. ))}
  455. </Select>
  456. </ListItem>
  457. <ListItem title={Locale.Settings.Lang.Name}>
  458. <Select
  459. value={getLang()}
  460. onChange={(e) => {
  461. changeLang(e.target.value as any);
  462. }}
  463. >
  464. {AllLangs.map((lang) => (
  465. <option value={lang} key={lang}>
  466. {ALL_LANG_OPTIONS[lang]}
  467. </option>
  468. ))}
  469. </Select>
  470. </ListItem>
  471. <ListItem
  472. title={Locale.Settings.FontSize.Title}
  473. subTitle={Locale.Settings.FontSize.SubTitle}
  474. >
  475. <InputRange
  476. title={`${config.fontSize ?? 14}px`}
  477. value={config.fontSize}
  478. min="12"
  479. max="18"
  480. step="1"
  481. onChange={(e) =>
  482. updateConfig(
  483. (config) =>
  484. (config.fontSize = Number.parseInt(e.currentTarget.value)),
  485. )
  486. }
  487. ></InputRange>
  488. </ListItem>
  489. <ListItem
  490. title={Locale.Settings.SendPreviewBubble.Title}
  491. subTitle={Locale.Settings.SendPreviewBubble.SubTitle}
  492. >
  493. <input
  494. type="checkbox"
  495. checked={config.sendPreviewBubble}
  496. onChange={(e) =>
  497. updateConfig(
  498. (config) =>
  499. (config.sendPreviewBubble = e.currentTarget.checked),
  500. )
  501. }
  502. ></input>
  503. </ListItem>
  504. </List>
  505. <List>
  506. <ListItem
  507. title={Locale.Settings.Mask.Splash.Title}
  508. subTitle={Locale.Settings.Mask.Splash.SubTitle}
  509. >
  510. <input
  511. type="checkbox"
  512. checked={!config.dontShowMaskSplashScreen}
  513. onChange={(e) =>
  514. updateConfig(
  515. (config) =>
  516. (config.dontShowMaskSplashScreen =
  517. !e.currentTarget.checked),
  518. )
  519. }
  520. ></input>
  521. </ListItem>
  522. <ListItem
  523. title={Locale.Settings.Mask.Builtin.Title}
  524. subTitle={Locale.Settings.Mask.Builtin.SubTitle}
  525. >
  526. <input
  527. type="checkbox"
  528. checked={config.hideBuiltinMasks}
  529. onChange={(e) =>
  530. updateConfig(
  531. (config) =>
  532. (config.hideBuiltinMasks = e.currentTarget.checked),
  533. )
  534. }
  535. ></input>
  536. </ListItem>
  537. </List>
  538. <List>
  539. <ListItem
  540. title={Locale.Settings.Prompt.Disable.Title}
  541. subTitle={Locale.Settings.Prompt.Disable.SubTitle}
  542. >
  543. <input
  544. type="checkbox"
  545. checked={config.disablePromptHint}
  546. onChange={(e) =>
  547. updateConfig(
  548. (config) =>
  549. (config.disablePromptHint = e.currentTarget.checked),
  550. )
  551. }
  552. ></input>
  553. </ListItem>
  554. <ListItem
  555. title={Locale.Settings.Prompt.List}
  556. subTitle={Locale.Settings.Prompt.ListCount(
  557. builtinCount,
  558. customCount,
  559. )}
  560. >
  561. <IconButton
  562. icon={<EditIcon />}
  563. text={Locale.Settings.Prompt.Edit}
  564. onClick={() => setShowPromptModal(true)}
  565. />
  566. </ListItem>
  567. </List>
  568. <List>
  569. {showAccessCode ? (
  570. <ListItem
  571. title={Locale.Settings.AccessCode.Title}
  572. subTitle={Locale.Settings.AccessCode.SubTitle}
  573. >
  574. <PasswordInput
  575. value={accessStore.accessCode}
  576. type="text"
  577. placeholder={Locale.Settings.AccessCode.Placeholder}
  578. onChange={(e) => {
  579. accessStore.updateCode(e.currentTarget.value);
  580. }}
  581. />
  582. </ListItem>
  583. ) : (
  584. <></>
  585. )}
  586. {!accessStore.hideUserApiKey ? (
  587. <>
  588. <ListItem
  589. title={Locale.Settings.Endpoint.Title}
  590. subTitle={Locale.Settings.Endpoint.SubTitle}
  591. >
  592. <input
  593. type="text"
  594. value={accessStore.openaiUrl}
  595. placeholder="https://api.openai.com/"
  596. onChange={(e) =>
  597. accessStore.updateOpenAiUrl(e.currentTarget.value)
  598. }
  599. ></input>
  600. </ListItem>
  601. <ListItem
  602. title={Locale.Settings.Token.Title}
  603. subTitle={Locale.Settings.Token.SubTitle}
  604. >
  605. <PasswordInput
  606. value={accessStore.token}
  607. type="text"
  608. placeholder={Locale.Settings.Token.Placeholder}
  609. onChange={(e) => {
  610. accessStore.updateToken(e.currentTarget.value);
  611. }}
  612. />
  613. </ListItem>
  614. </>
  615. ) : null}
  616. {!accessStore.hideBalanceQuery ? (
  617. <ListItem
  618. title={Locale.Settings.Usage.Title}
  619. subTitle={
  620. showUsage
  621. ? loadingUsage
  622. ? Locale.Settings.Usage.IsChecking
  623. : Locale.Settings.Usage.SubTitle(
  624. usage?.used ?? "[?]",
  625. usage?.subscription ?? "[?]",
  626. )
  627. : Locale.Settings.Usage.NoAccess
  628. }
  629. >
  630. {!showUsage || loadingUsage ? (
  631. <div />
  632. ) : (
  633. <IconButton
  634. icon={<ResetIcon></ResetIcon>}
  635. text={Locale.Settings.Usage.Check}
  636. onClick={() => checkUsage(true)}
  637. />
  638. )}
  639. </ListItem>
  640. ) : null}
  641. <ListItem
  642. title={Locale.Settings.CustomModel.Title}
  643. subTitle={Locale.Settings.CustomModel.SubTitle}
  644. >
  645. <input
  646. type="text"
  647. value={config.customModels}
  648. placeholder="model1,model2,model3"
  649. onChange={(e) =>
  650. config.update(
  651. (config) => (config.customModels = e.currentTarget.value),
  652. )
  653. }
  654. ></input>
  655. </ListItem>
  656. </List>
  657. <SyncItems />
  658. <List>
  659. <ModelConfigList
  660. modelConfig={config.modelConfig}
  661. updateConfig={(updater) => {
  662. const modelConfig = { ...config.modelConfig };
  663. updater(modelConfig);
  664. config.update((config) => (config.modelConfig = modelConfig));
  665. }}
  666. />
  667. </List>
  668. {shouldShowPromptModal && (
  669. <UserPromptModal onClose={() => setShowPromptModal(false)} />
  670. )}
  671. <DangerItems />
  672. </div>
  673. </ErrorBoundary>
  674. );
  675. }