settings.tsx 21 KB

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