settings.tsx 35 KB

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