settings.tsx 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976
  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 DownloadIcon from "../icons/download.svg";
  12. import UploadIcon from "../icons/upload.svg";
  13. import ConfigIcon from "../icons/config.svg";
  14. import ConfirmIcon from "../icons/confirm.svg";
  15. import ConnectionIcon from "../icons/connection.svg";
  16. import CloudSuccessIcon from "../icons/cloud-success.svg";
  17. import CloudFailIcon from "../icons/cloud-fail.svg";
  18. import {
  19. Input,
  20. List,
  21. ListItem,
  22. Modal,
  23. PasswordInput,
  24. Popover,
  25. Select,
  26. showConfirm,
  27. showToast,
  28. } from "./ui-lib";
  29. import { IconButton } from "./button";
  30. import {
  31. useChatStore,
  32. useUpdateStore,
  33. useAccessStore,
  34. useAppConfig,
  35. LLMProvider,
  36. LLMProviders,
  37. } from "../store";
  38. import Locale, {
  39. AllLangs,
  40. ALL_LANG_OPTIONS,
  41. changeLang,
  42. getLang,
  43. } from "../locales";
  44. import { copyToClipboard } from "../utils";
  45. import Link from "next/link";
  46. import { Path, RELEASE_URL, STORAGE_KEY, UPDATE_URL } from "../constant";
  47. import { Prompt, SearchService, usePromptStore } from "../store/prompt";
  48. import { ErrorBoundary } from "./error";
  49. import { InputRange } from "./input-range";
  50. import { useNavigate } from "react-router-dom";
  51. import { Avatar, AvatarPicker } from "./emoji";
  52. import { getClientConfig } from "../config/client";
  53. import { useSyncStore } from "../store/sync";
  54. import { nanoid } from "nanoid";
  55. import { useMaskStore } from "../store/mask";
  56. import { ProviderType } from "../utils/cloud";
  57. import {
  58. ChatConfigList,
  59. ModelConfigList,
  60. ProviderConfigList,
  61. ProviderSelectItem,
  62. } from "./config";
  63. import { SubmitKey, Theme } from "../typing";
  64. import { deepClone } from "../utils/clone";
  65. function EditPromptModal(props: { id: string; onClose: () => void }) {
  66. const promptStore = usePromptStore();
  67. const prompt = promptStore.get(props.id);
  68. return prompt ? (
  69. <div className="modal-mask">
  70. <Modal
  71. title={Locale.Settings.Prompt.EditModal.Title}
  72. onClose={props.onClose}
  73. actions={[
  74. <IconButton
  75. key=""
  76. onClick={props.onClose}
  77. text={Locale.UI.Confirm}
  78. bordered
  79. />,
  80. ]}
  81. >
  82. <div className={styles["edit-prompt-modal"]}>
  83. <input
  84. type="text"
  85. value={prompt.title}
  86. readOnly={!prompt.isUser}
  87. className={styles["edit-prompt-title"]}
  88. onInput={(e) =>
  89. promptStore.updatePrompt(
  90. props.id,
  91. (prompt) => (prompt.title = e.currentTarget.value),
  92. )
  93. }
  94. ></input>
  95. <Input
  96. value={prompt.content}
  97. readOnly={!prompt.isUser}
  98. className={styles["edit-prompt-content"]}
  99. rows={10}
  100. onInput={(e) =>
  101. promptStore.updatePrompt(
  102. props.id,
  103. (prompt) => (prompt.content = e.currentTarget.value),
  104. )
  105. }
  106. ></Input>
  107. </div>
  108. </Modal>
  109. </div>
  110. ) : null;
  111. }
  112. function UserPromptModal(props: { onClose?: () => void }) {
  113. const promptStore = usePromptStore();
  114. const userPrompts = promptStore.getUserPrompts();
  115. const builtinPrompts = SearchService.builtinPrompts;
  116. const allPrompts = userPrompts.concat(builtinPrompts);
  117. const [searchInput, setSearchInput] = useState("");
  118. const [searchPrompts, setSearchPrompts] = useState<Prompt[]>([]);
  119. const prompts = searchInput.length > 0 ? searchPrompts : allPrompts;
  120. const [editingPromptId, setEditingPromptId] = useState<string>();
  121. useEffect(() => {
  122. if (searchInput.length > 0) {
  123. const searchResult = SearchService.search(searchInput);
  124. setSearchPrompts(searchResult);
  125. } else {
  126. setSearchPrompts([]);
  127. }
  128. }, [searchInput]);
  129. return (
  130. <div className="modal-mask">
  131. <Modal
  132. title={Locale.Settings.Prompt.Modal.Title}
  133. onClose={() => props.onClose?.()}
  134. actions={[
  135. <IconButton
  136. key="add"
  137. onClick={() => {
  138. const promptId = promptStore.add({
  139. id: nanoid(),
  140. createdAt: Date.now(),
  141. title: "Empty Prompt",
  142. content: "Empty Prompt Content",
  143. });
  144. setEditingPromptId(promptId);
  145. }}
  146. icon={<AddIcon />}
  147. bordered
  148. text={Locale.Settings.Prompt.Modal.Add}
  149. />,
  150. ]}
  151. >
  152. <div className={styles["user-prompt-modal"]}>
  153. <input
  154. type="text"
  155. className={styles["user-prompt-search"]}
  156. placeholder={Locale.Settings.Prompt.Modal.Search}
  157. value={searchInput}
  158. onInput={(e) => setSearchInput(e.currentTarget.value)}
  159. ></input>
  160. <div className={styles["user-prompt-list"]}>
  161. {prompts.map((v, _) => (
  162. <div className={styles["user-prompt-item"]} key={v.id ?? v.title}>
  163. <div className={styles["user-prompt-header"]}>
  164. <div className={styles["user-prompt-title"]}>{v.title}</div>
  165. <div className={styles["user-prompt-content"] + " one-line"}>
  166. {v.content}
  167. </div>
  168. </div>
  169. <div className={styles["user-prompt-buttons"]}>
  170. {v.isUser && (
  171. <IconButton
  172. icon={<ClearIcon />}
  173. className={styles["user-prompt-button"]}
  174. onClick={() => promptStore.remove(v.id!)}
  175. />
  176. )}
  177. {v.isUser ? (
  178. <IconButton
  179. icon={<EditIcon />}
  180. className={styles["user-prompt-button"]}
  181. onClick={() => setEditingPromptId(v.id)}
  182. />
  183. ) : (
  184. <IconButton
  185. icon={<EyeIcon />}
  186. className={styles["user-prompt-button"]}
  187. onClick={() => setEditingPromptId(v.id)}
  188. />
  189. )}
  190. <IconButton
  191. icon={<CopyIcon />}
  192. className={styles["user-prompt-button"]}
  193. onClick={() => copyToClipboard(v.content)}
  194. />
  195. </div>
  196. </div>
  197. ))}
  198. </div>
  199. </div>
  200. </Modal>
  201. {editingPromptId !== undefined && (
  202. <EditPromptModal
  203. id={editingPromptId!}
  204. onClose={() => setEditingPromptId(undefined)}
  205. />
  206. )}
  207. </div>
  208. );
  209. }
  210. function DangerItems() {
  211. const chatStore = useChatStore();
  212. const appConfig = useAppConfig();
  213. return (
  214. <List>
  215. <ListItem
  216. title={Locale.Settings.Danger.Reset.Title}
  217. subTitle={Locale.Settings.Danger.Reset.SubTitle}
  218. >
  219. <IconButton
  220. text={Locale.Settings.Danger.Reset.Action}
  221. onClick={async () => {
  222. if (await showConfirm(Locale.Settings.Danger.Reset.Confirm)) {
  223. appConfig.reset();
  224. }
  225. }}
  226. type="danger"
  227. />
  228. </ListItem>
  229. <ListItem
  230. title={Locale.Settings.Danger.Clear.Title}
  231. subTitle={Locale.Settings.Danger.Clear.SubTitle}
  232. >
  233. <IconButton
  234. text={Locale.Settings.Danger.Clear.Action}
  235. onClick={async () => {
  236. if (await showConfirm(Locale.Settings.Danger.Clear.Confirm)) {
  237. chatStore.clearAllData();
  238. }
  239. }}
  240. type="danger"
  241. />
  242. </ListItem>
  243. </List>
  244. );
  245. }
  246. function CheckButton() {
  247. const syncStore = useSyncStore();
  248. const couldCheck = useMemo(() => {
  249. return syncStore.coundSync();
  250. }, [syncStore]);
  251. const [checkState, setCheckState] = useState<
  252. "none" | "checking" | "success" | "failed"
  253. >("none");
  254. async function check() {
  255. setCheckState("checking");
  256. const valid = await syncStore.check();
  257. setCheckState(valid ? "success" : "failed");
  258. }
  259. if (!couldCheck) return null;
  260. return (
  261. <IconButton
  262. text={Locale.Settings.Sync.Config.Modal.Check}
  263. bordered
  264. onClick={check}
  265. icon={
  266. checkState === "none" ? (
  267. <ConnectionIcon />
  268. ) : checkState === "checking" ? (
  269. <LoadingIcon />
  270. ) : checkState === "success" ? (
  271. <CloudSuccessIcon />
  272. ) : checkState === "failed" ? (
  273. <CloudFailIcon />
  274. ) : (
  275. <ConnectionIcon />
  276. )
  277. }
  278. ></IconButton>
  279. );
  280. }
  281. function SyncConfigModal(props: { onClose?: () => void }) {
  282. const syncStore = useSyncStore();
  283. return (
  284. <div className="modal-mask">
  285. <Modal
  286. title={Locale.Settings.Sync.Config.Modal.Title}
  287. onClose={() => props.onClose?.()}
  288. actions={[
  289. <CheckButton key="check" />,
  290. <IconButton
  291. key="confirm"
  292. onClick={props.onClose}
  293. icon={<ConfirmIcon />}
  294. bordered
  295. text={Locale.UI.Confirm}
  296. />,
  297. ]}
  298. >
  299. <List>
  300. <ListItem
  301. title={Locale.Settings.Sync.Config.SyncType.Title}
  302. subTitle={Locale.Settings.Sync.Config.SyncType.SubTitle}
  303. >
  304. <select
  305. value={syncStore.provider}
  306. onChange={(e) => {
  307. syncStore.update(
  308. (config) =>
  309. (config.provider = e.target.value as ProviderType),
  310. );
  311. }}
  312. >
  313. {Object.entries(ProviderType).map(([k, v]) => (
  314. <option value={v} key={k}>
  315. {k}
  316. </option>
  317. ))}
  318. </select>
  319. </ListItem>
  320. <ListItem
  321. title={Locale.Settings.Sync.Config.Proxy.Title}
  322. subTitle={Locale.Settings.Sync.Config.Proxy.SubTitle}
  323. >
  324. <input
  325. type="checkbox"
  326. checked={syncStore.useProxy}
  327. onChange={(e) => {
  328. syncStore.update(
  329. (config) => (config.useProxy = e.currentTarget.checked),
  330. );
  331. }}
  332. ></input>
  333. </ListItem>
  334. {syncStore.useProxy ? (
  335. <ListItem
  336. title={Locale.Settings.Sync.Config.ProxyUrl.Title}
  337. subTitle={Locale.Settings.Sync.Config.ProxyUrl.SubTitle}
  338. >
  339. <input
  340. type="text"
  341. value={syncStore.proxyUrl}
  342. onChange={(e) => {
  343. syncStore.update(
  344. (config) => (config.proxyUrl = e.currentTarget.value),
  345. );
  346. }}
  347. ></input>
  348. </ListItem>
  349. ) : null}
  350. </List>
  351. {syncStore.provider === ProviderType.WebDAV && (
  352. <>
  353. <List>
  354. <ListItem title={Locale.Settings.Sync.Config.WebDav.Endpoint}>
  355. <input
  356. type="text"
  357. value={syncStore.webdav.endpoint}
  358. onChange={(e) => {
  359. syncStore.update(
  360. (config) =>
  361. (config.webdav.endpoint = e.currentTarget.value),
  362. );
  363. }}
  364. ></input>
  365. </ListItem>
  366. <ListItem title={Locale.Settings.Sync.Config.WebDav.UserName}>
  367. <input
  368. type="text"
  369. value={syncStore.webdav.username}
  370. onChange={(e) => {
  371. syncStore.update(
  372. (config) =>
  373. (config.webdav.username = e.currentTarget.value),
  374. );
  375. }}
  376. ></input>
  377. </ListItem>
  378. <ListItem title={Locale.Settings.Sync.Config.WebDav.Password}>
  379. <PasswordInput
  380. value={syncStore.webdav.password}
  381. onChange={(e) => {
  382. syncStore.update(
  383. (config) =>
  384. (config.webdav.password = e.currentTarget.value),
  385. );
  386. }}
  387. ></PasswordInput>
  388. </ListItem>
  389. </List>
  390. </>
  391. )}
  392. {syncStore.provider === ProviderType.UpStash && (
  393. <List>
  394. <ListItem title={Locale.Settings.Sync.Config.UpStash.Endpoint}>
  395. <input
  396. type="text"
  397. value={syncStore.upstash.endpoint}
  398. onChange={(e) => {
  399. syncStore.update(
  400. (config) =>
  401. (config.upstash.endpoint = e.currentTarget.value),
  402. );
  403. }}
  404. ></input>
  405. </ListItem>
  406. <ListItem title={Locale.Settings.Sync.Config.UpStash.UserName}>
  407. <input
  408. type="text"
  409. value={syncStore.upstash.username}
  410. placeholder={STORAGE_KEY}
  411. onChange={(e) => {
  412. syncStore.update(
  413. (config) =>
  414. (config.upstash.username = e.currentTarget.value),
  415. );
  416. }}
  417. ></input>
  418. </ListItem>
  419. <ListItem title={Locale.Settings.Sync.Config.UpStash.Password}>
  420. <PasswordInput
  421. value={syncStore.upstash.apiKey}
  422. onChange={(e) => {
  423. syncStore.update(
  424. (config) => (config.upstash.apiKey = e.currentTarget.value),
  425. );
  426. }}
  427. ></PasswordInput>
  428. </ListItem>
  429. </List>
  430. )}
  431. </Modal>
  432. </div>
  433. );
  434. }
  435. function SyncItems() {
  436. const syncStore = useSyncStore();
  437. const chatStore = useChatStore();
  438. const promptStore = usePromptStore();
  439. const maskStore = useMaskStore();
  440. const couldSync = useMemo(() => {
  441. return syncStore.coundSync();
  442. }, [syncStore]);
  443. const [showSyncConfigModal, setShowSyncConfigModal] = useState(false);
  444. const stateOverview = useMemo(() => {
  445. const sessions = chatStore.sessions;
  446. const messageCount = sessions.reduce((p, c) => p + c.messages.length, 0);
  447. return {
  448. chat: sessions.length,
  449. message: messageCount,
  450. prompt: Object.keys(promptStore.prompts).length,
  451. mask: Object.keys(maskStore.masks).length,
  452. };
  453. }, [chatStore.sessions, maskStore.masks, promptStore.prompts]);
  454. return (
  455. <>
  456. <List>
  457. <ListItem
  458. title={Locale.Settings.Sync.CloudState}
  459. subTitle={
  460. syncStore.lastProvider
  461. ? `${new Date(syncStore.lastSyncTime).toLocaleString()} [${
  462. syncStore.lastProvider
  463. }]`
  464. : Locale.Settings.Sync.NotSyncYet
  465. }
  466. >
  467. <div style={{ display: "flex" }}>
  468. <IconButton
  469. icon={<ConfigIcon />}
  470. text={Locale.UI.Config}
  471. onClick={() => {
  472. setShowSyncConfigModal(true);
  473. }}
  474. />
  475. {couldSync && (
  476. <IconButton
  477. icon={<ResetIcon />}
  478. text={Locale.UI.Sync}
  479. onClick={async () => {
  480. try {
  481. await syncStore.sync();
  482. showToast(Locale.Settings.Sync.Success);
  483. } catch (e) {
  484. showToast(Locale.Settings.Sync.Fail);
  485. console.error("[Sync]", e);
  486. }
  487. }}
  488. />
  489. )}
  490. </div>
  491. </ListItem>
  492. <ListItem
  493. title={Locale.Settings.Sync.LocalState}
  494. subTitle={Locale.Settings.Sync.Overview(stateOverview)}
  495. >
  496. <div style={{ display: "flex" }}>
  497. <IconButton
  498. icon={<UploadIcon />}
  499. text={Locale.UI.Export}
  500. onClick={() => {
  501. syncStore.export();
  502. }}
  503. />
  504. <IconButton
  505. icon={<DownloadIcon />}
  506. text={Locale.UI.Import}
  507. onClick={() => {
  508. syncStore.import();
  509. }}
  510. />
  511. </div>
  512. </ListItem>
  513. </List>
  514. {showSyncConfigModal && (
  515. <SyncConfigModal onClose={() => setShowSyncConfigModal(false)} />
  516. )}
  517. </>
  518. );
  519. }
  520. export function Settings() {
  521. const navigate = useNavigate();
  522. const [showEmojiPicker, setShowEmojiPicker] = useState(false);
  523. const config = useAppConfig();
  524. const updateConfig = config.update;
  525. const updateStore = useUpdateStore();
  526. const [checkingUpdate, setCheckingUpdate] = useState(false);
  527. const currentVersion = updateStore.formatVersion(updateStore.version);
  528. const remoteId = updateStore.formatVersion(updateStore.remoteVersion);
  529. const hasNewVersion = currentVersion !== remoteId;
  530. const updateUrl = getClientConfig()?.isApp ? RELEASE_URL : UPDATE_URL;
  531. function checkUpdate(force = false) {
  532. setCheckingUpdate(true);
  533. updateStore.getLatestVersion(force).then(() => {
  534. setCheckingUpdate(false);
  535. });
  536. console.log("[Update] local version ", updateStore.version);
  537. console.log("[Update] remote version ", updateStore.remoteVersion);
  538. }
  539. const usage = {
  540. used: updateStore.used,
  541. subscription: updateStore.subscription,
  542. };
  543. const [loadingUsage, setLoadingUsage] = useState(false);
  544. function checkUsage(force = false) {
  545. if (accessStore.hideBalanceQuery) {
  546. return;
  547. }
  548. setLoadingUsage(true);
  549. updateStore.updateUsage(force).finally(() => {
  550. setLoadingUsage(false);
  551. });
  552. }
  553. const accessStore = useAccessStore();
  554. const enabledAccessControl = useMemo(
  555. () => accessStore.enabledAccessControl(),
  556. // eslint-disable-next-line react-hooks/exhaustive-deps
  557. [],
  558. );
  559. const promptStore = usePromptStore();
  560. const builtinCount = SearchService.count.builtin;
  561. const customCount = promptStore.getUserPrompts().length ?? 0;
  562. const [shouldShowPromptModal, setShowPromptModal] = useState(false);
  563. const showUsage = accessStore.isAuthorized();
  564. useEffect(() => {
  565. // checks per minutes
  566. checkUpdate();
  567. showUsage && checkUsage();
  568. // eslint-disable-next-line react-hooks/exhaustive-deps
  569. }, []);
  570. useEffect(() => {
  571. const keydownEvent = (e: KeyboardEvent) => {
  572. if (e.key === "Escape") {
  573. navigate(Path.Home);
  574. }
  575. };
  576. document.addEventListener("keydown", keydownEvent);
  577. return () => {
  578. document.removeEventListener("keydown", keydownEvent);
  579. };
  580. // eslint-disable-next-line react-hooks/exhaustive-deps
  581. }, []);
  582. const clientConfig = useMemo(() => getClientConfig(), []);
  583. const showAccessCode = enabledAccessControl && !clientConfig?.isApp;
  584. return (
  585. <ErrorBoundary>
  586. <div className="window-header" data-tauri-drag-region>
  587. <div className="window-header-title">
  588. <div className="window-header-main-title">
  589. {Locale.Settings.Title}
  590. </div>
  591. <div className="window-header-sub-title">
  592. {Locale.Settings.SubTitle}
  593. </div>
  594. </div>
  595. <div className="window-actions">
  596. <div className="window-action-button"></div>
  597. <div className="window-action-button"></div>
  598. <div className="window-action-button">
  599. <IconButton
  600. icon={<CloseIcon />}
  601. onClick={() => navigate(Path.Home)}
  602. bordered
  603. />
  604. </div>
  605. </div>
  606. </div>
  607. <div className={styles["settings"]}>
  608. <List>
  609. <ListItem title={Locale.Settings.Avatar}>
  610. <Popover
  611. onClose={() => setShowEmojiPicker(false)}
  612. content={
  613. <AvatarPicker
  614. onEmojiClick={(avatar: string) => {
  615. updateConfig((config) => (config.avatar = avatar));
  616. setShowEmojiPicker(false);
  617. }}
  618. />
  619. }
  620. open={showEmojiPicker}
  621. >
  622. <div
  623. className={styles.avatar}
  624. onClick={() => setShowEmojiPicker(true)}
  625. >
  626. <Avatar avatar={config.avatar} />
  627. </div>
  628. </Popover>
  629. </ListItem>
  630. <ListItem
  631. title={Locale.Settings.Update.Version(currentVersion ?? "unknown")}
  632. subTitle={
  633. checkingUpdate
  634. ? Locale.Settings.Update.IsChecking
  635. : hasNewVersion
  636. ? Locale.Settings.Update.FoundUpdate(remoteId ?? "ERROR")
  637. : Locale.Settings.Update.IsLatest
  638. }
  639. >
  640. {checkingUpdate ? (
  641. <LoadingIcon />
  642. ) : hasNewVersion ? (
  643. <Link href={updateUrl} target="_blank" className="link">
  644. {Locale.Settings.Update.GoToUpdate}
  645. </Link>
  646. ) : (
  647. <IconButton
  648. icon={<ResetIcon></ResetIcon>}
  649. text={Locale.Settings.Update.CheckUpdate}
  650. onClick={() => checkUpdate(true)}
  651. />
  652. )}
  653. </ListItem>
  654. <ListItem title={Locale.Settings.SendKey}>
  655. <Select
  656. value={config.submitKey}
  657. onChange={(e) => {
  658. updateConfig(
  659. (config) =>
  660. (config.submitKey = e.target.value as any as SubmitKey),
  661. );
  662. }}
  663. >
  664. {Object.values(SubmitKey).map((v) => (
  665. <option value={v} key={v}>
  666. {v}
  667. </option>
  668. ))}
  669. </Select>
  670. </ListItem>
  671. <ListItem title={Locale.Settings.Theme}>
  672. <Select
  673. value={config.theme}
  674. onChange={(e) => {
  675. updateConfig(
  676. (config) => (config.theme = e.target.value as any as Theme),
  677. );
  678. }}
  679. >
  680. {Object.values(Theme).map((v) => (
  681. <option value={v} key={v}>
  682. {v}
  683. </option>
  684. ))}
  685. </Select>
  686. </ListItem>
  687. <ListItem title={Locale.Settings.Lang.Name}>
  688. <Select
  689. value={getLang()}
  690. onChange={(e) => {
  691. changeLang(e.target.value as any);
  692. }}
  693. >
  694. {AllLangs.map((lang) => (
  695. <option value={lang} key={lang}>
  696. {ALL_LANG_OPTIONS[lang]}
  697. </option>
  698. ))}
  699. </Select>
  700. </ListItem>
  701. <ListItem
  702. title={Locale.Settings.FontSize.Title}
  703. subTitle={Locale.Settings.FontSize.SubTitle}
  704. >
  705. <InputRange
  706. title={`${config.fontSize ?? 14}px`}
  707. value={config.fontSize}
  708. min="12"
  709. max="40"
  710. step="1"
  711. onChange={(e) =>
  712. updateConfig(
  713. (config) => (config.fontSize = e.currentTarget.valueAsNumber),
  714. )
  715. }
  716. ></InputRange>
  717. </ListItem>
  718. <ListItem
  719. title={Locale.Settings.AutoGenerateTitle.Title}
  720. subTitle={Locale.Settings.AutoGenerateTitle.SubTitle}
  721. >
  722. <input
  723. type="checkbox"
  724. checked={
  725. config.globalMaskConfig.chatConfig.enableAutoGenerateTitle
  726. }
  727. onChange={(e) =>
  728. updateConfig(
  729. (config) =>
  730. (config.globalMaskConfig.chatConfig.enableAutoGenerateTitle =
  731. e.currentTarget.checked),
  732. )
  733. }
  734. ></input>
  735. </ListItem>
  736. <ListItem
  737. title={Locale.Settings.SendPreviewBubble.Title}
  738. subTitle={Locale.Settings.SendPreviewBubble.SubTitle}
  739. >
  740. <input
  741. type="checkbox"
  742. checked={config.sendPreviewBubble}
  743. onChange={(e) =>
  744. updateConfig(
  745. (config) =>
  746. (config.sendPreviewBubble = e.currentTarget.checked),
  747. )
  748. }
  749. ></input>
  750. </ListItem>
  751. </List>
  752. <SyncItems />
  753. <List>
  754. <ListItem
  755. title={Locale.Settings.Mask.Splash.Title}
  756. subTitle={Locale.Settings.Mask.Splash.SubTitle}
  757. >
  758. <input
  759. type="checkbox"
  760. checked={!config.dontShowMaskSplashScreen}
  761. onChange={(e) =>
  762. updateConfig(
  763. (config) =>
  764. (config.dontShowMaskSplashScreen =
  765. !e.currentTarget.checked),
  766. )
  767. }
  768. ></input>
  769. </ListItem>
  770. <ListItem
  771. title={Locale.Settings.Mask.Builtin.Title}
  772. subTitle={Locale.Settings.Mask.Builtin.SubTitle}
  773. >
  774. <input
  775. type="checkbox"
  776. checked={config.hideBuiltinMasks}
  777. onChange={(e) =>
  778. updateConfig(
  779. (config) =>
  780. (config.hideBuiltinMasks = e.currentTarget.checked),
  781. )
  782. }
  783. ></input>
  784. </ListItem>
  785. </List>
  786. <List>
  787. <ListItem
  788. title={Locale.Settings.Prompt.Disable.Title}
  789. subTitle={Locale.Settings.Prompt.Disable.SubTitle}
  790. >
  791. <input
  792. type="checkbox"
  793. checked={config.disablePromptHint}
  794. onChange={(e) =>
  795. updateConfig(
  796. (config) =>
  797. (config.disablePromptHint = e.currentTarget.checked),
  798. )
  799. }
  800. ></input>
  801. </ListItem>
  802. <ListItem
  803. title={Locale.Settings.Prompt.List}
  804. subTitle={Locale.Settings.Prompt.ListCount(
  805. builtinCount,
  806. customCount,
  807. )}
  808. >
  809. <IconButton
  810. icon={<EditIcon />}
  811. text={Locale.Settings.Prompt.Edit}
  812. onClick={() => setShowPromptModal(true)}
  813. />
  814. </ListItem>
  815. </List>
  816. <List>
  817. {showAccessCode ? (
  818. <ListItem
  819. title={Locale.Settings.AccessCode.Title}
  820. subTitle={Locale.Settings.AccessCode.SubTitle}
  821. >
  822. <PasswordInput
  823. value={accessStore.accessCode}
  824. type="text"
  825. placeholder={Locale.Settings.AccessCode.Placeholder}
  826. onChange={(e) => {
  827. accessStore.update(
  828. (config) => (config.accessCode = e.currentTarget.value),
  829. );
  830. }}
  831. />
  832. </ListItem>
  833. ) : (
  834. <></>
  835. )}
  836. {!accessStore.hideUserApiKey ? <></> : null}
  837. {!accessStore.hideBalanceQuery ? (
  838. <ListItem
  839. title={Locale.Settings.Usage.Title}
  840. subTitle={
  841. showUsage
  842. ? loadingUsage
  843. ? Locale.Settings.Usage.IsChecking
  844. : Locale.Settings.Usage.SubTitle(
  845. usage?.used ?? "[?]",
  846. usage?.subscription ?? "[?]",
  847. )
  848. : Locale.Settings.Usage.NoAccess
  849. }
  850. >
  851. {!showUsage || loadingUsage ? (
  852. <div />
  853. ) : (
  854. <IconButton
  855. icon={<ResetIcon></ResetIcon>}
  856. text={Locale.Settings.Usage.Check}
  857. onClick={() => checkUsage(true)}
  858. />
  859. )}
  860. </ListItem>
  861. ) : null}
  862. </List>
  863. <List>
  864. <ProviderSelectItem
  865. value={config.globalMaskConfig.provider}
  866. update={(value) =>
  867. config.update((_config) => {
  868. _config.globalMaskConfig.provider = value;
  869. })
  870. }
  871. />
  872. <ProviderConfigList
  873. provider={config.globalMaskConfig.provider}
  874. config={config.providerConfig}
  875. updateConfig={(update) => {
  876. config.update((_config) => update(_config.providerConfig));
  877. }}
  878. />
  879. <ModelConfigList
  880. provider={config.globalMaskConfig.provider}
  881. config={config.globalMaskConfig.modelConfig}
  882. updateConfig={(updater) => {
  883. const modelConfig = { ...config.globalMaskConfig.modelConfig };
  884. updater(modelConfig);
  885. config.update(
  886. (config) => (config.globalMaskConfig.modelConfig = modelConfig),
  887. );
  888. }}
  889. />
  890. <ChatConfigList
  891. config={config.globalMaskConfig.chatConfig}
  892. updateConfig={(updater) => {
  893. const chatConfig = deepClone(config.globalMaskConfig.chatConfig);
  894. updater(chatConfig);
  895. config.update(
  896. (config) => (config.globalMaskConfig.chatConfig = chatConfig),
  897. );
  898. }}
  899. />
  900. </List>
  901. {shouldShowPromptModal && (
  902. <UserPromptModal onClose={() => setShowPromptModal(false)} />
  903. )}
  904. <DangerItems />
  905. </div>
  906. </ErrorBoundary>
  907. );
  908. }