settings.tsx 22 KB

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