settings.tsx 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127
  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. if (clientConfig?.isApp) { // Force to set custom endpoint to true if it's app
  591. accessStore.update((state) => {
  592. state.useCustomConfig = true;
  593. });
  594. }
  595. document.addEventListener("keydown", keydownEvent);
  596. return () => {
  597. document.removeEventListener("keydown", keydownEvent);
  598. };
  599. // eslint-disable-next-line react-hooks/exhaustive-deps
  600. }, []);
  601. const clientConfig = useMemo(() => getClientConfig(), []);
  602. const showAccessCode = enabledAccessControl && !clientConfig?.isApp;
  603. return (
  604. <ErrorBoundary>
  605. <div className="window-header" data-tauri-drag-region>
  606. <div className="window-header-title">
  607. <div className="window-header-main-title">
  608. {Locale.Settings.Title}
  609. </div>
  610. <div className="window-header-sub-title">
  611. {Locale.Settings.SubTitle}
  612. </div>
  613. </div>
  614. <div className="window-actions">
  615. <div className="window-action-button"></div>
  616. <div className="window-action-button"></div>
  617. <div className="window-action-button">
  618. <IconButton
  619. icon={<CloseIcon />}
  620. onClick={() => navigate(Path.Home)}
  621. bordered
  622. />
  623. </div>
  624. </div>
  625. </div>
  626. <div className={styles["settings"]}>
  627. <List>
  628. <ListItem title={Locale.Settings.Avatar}>
  629. <Popover
  630. onClose={() => setShowEmojiPicker(false)}
  631. content={
  632. <AvatarPicker
  633. onEmojiClick={(avatar: string) => {
  634. updateConfig((config) => (config.avatar = avatar));
  635. setShowEmojiPicker(false);
  636. }}
  637. />
  638. }
  639. open={showEmojiPicker}
  640. >
  641. <div
  642. className={styles.avatar}
  643. onClick={() => setShowEmojiPicker(true)}
  644. >
  645. <Avatar avatar={config.avatar} />
  646. </div>
  647. </Popover>
  648. </ListItem>
  649. <ListItem
  650. title={Locale.Settings.Update.Version(currentVersion ?? "unknown")}
  651. subTitle={
  652. checkingUpdate
  653. ? Locale.Settings.Update.IsChecking
  654. : hasNewVersion
  655. ? Locale.Settings.Update.FoundUpdate(remoteId ?? "ERROR")
  656. : Locale.Settings.Update.IsLatest
  657. }
  658. >
  659. {checkingUpdate ? (
  660. <LoadingIcon />
  661. ) : hasNewVersion ? (
  662. <Link href={updateUrl} target="_blank" className="link">
  663. {Locale.Settings.Update.GoToUpdate}
  664. </Link>
  665. ) : (
  666. <IconButton
  667. icon={<ResetIcon></ResetIcon>}
  668. text={Locale.Settings.Update.CheckUpdate}
  669. onClick={() => checkUpdate(true)}
  670. />
  671. )}
  672. </ListItem>
  673. <ListItem title={Locale.Settings.SendKey}>
  674. <Select
  675. value={config.submitKey}
  676. onChange={(e) => {
  677. updateConfig(
  678. (config) =>
  679. (config.submitKey = e.target.value as any as SubmitKey),
  680. );
  681. }}
  682. >
  683. {Object.values(SubmitKey).map((v) => (
  684. <option value={v} key={v}>
  685. {v}
  686. </option>
  687. ))}
  688. </Select>
  689. </ListItem>
  690. <ListItem title={Locale.Settings.Theme}>
  691. <Select
  692. value={config.theme}
  693. onChange={(e) => {
  694. updateConfig(
  695. (config) => (config.theme = e.target.value as any as Theme),
  696. );
  697. }}
  698. >
  699. {Object.values(Theme).map((v) => (
  700. <option value={v} key={v}>
  701. {v}
  702. </option>
  703. ))}
  704. </Select>
  705. </ListItem>
  706. <ListItem title={Locale.Settings.Lang.Name}>
  707. <Select
  708. value={getLang()}
  709. onChange={(e) => {
  710. changeLang(e.target.value as any);
  711. }}
  712. >
  713. {AllLangs.map((lang) => (
  714. <option value={lang} key={lang}>
  715. {ALL_LANG_OPTIONS[lang]}
  716. </option>
  717. ))}
  718. </Select>
  719. </ListItem>
  720. <ListItem
  721. title={Locale.Settings.FontSize.Title}
  722. subTitle={Locale.Settings.FontSize.SubTitle}
  723. >
  724. <InputRange
  725. title={`${config.fontSize ?? 14}px`}
  726. value={config.fontSize}
  727. min="12"
  728. max="40"
  729. step="1"
  730. onChange={(e) =>
  731. updateConfig(
  732. (config) =>
  733. (config.fontSize = Number.parseInt(e.currentTarget.value)),
  734. )
  735. }
  736. ></InputRange>
  737. </ListItem>
  738. <ListItem
  739. title={Locale.Settings.AutoGenerateTitle.Title}
  740. subTitle={Locale.Settings.AutoGenerateTitle.SubTitle}
  741. >
  742. <input
  743. type="checkbox"
  744. checked={config.enableAutoGenerateTitle}
  745. onChange={(e) =>
  746. updateConfig(
  747. (config) =>
  748. (config.enableAutoGenerateTitle = e.currentTarget.checked),
  749. )
  750. }
  751. ></input>
  752. </ListItem>
  753. <ListItem
  754. title={Locale.Settings.SendPreviewBubble.Title}
  755. subTitle={Locale.Settings.SendPreviewBubble.SubTitle}
  756. >
  757. <input
  758. type="checkbox"
  759. checked={config.sendPreviewBubble}
  760. onChange={(e) =>
  761. updateConfig(
  762. (config) =>
  763. (config.sendPreviewBubble = e.currentTarget.checked),
  764. )
  765. }
  766. ></input>
  767. </ListItem>
  768. </List>
  769. <SyncItems />
  770. <List>
  771. <ListItem
  772. title={Locale.Settings.Mask.Splash.Title}
  773. subTitle={Locale.Settings.Mask.Splash.SubTitle}
  774. >
  775. <input
  776. type="checkbox"
  777. checked={!config.dontShowMaskSplashScreen}
  778. onChange={(e) =>
  779. updateConfig(
  780. (config) =>
  781. (config.dontShowMaskSplashScreen =
  782. !e.currentTarget.checked),
  783. )
  784. }
  785. ></input>
  786. </ListItem>
  787. <ListItem
  788. title={Locale.Settings.Mask.Builtin.Title}
  789. subTitle={Locale.Settings.Mask.Builtin.SubTitle}
  790. >
  791. <input
  792. type="checkbox"
  793. checked={config.hideBuiltinMasks}
  794. onChange={(e) =>
  795. updateConfig(
  796. (config) =>
  797. (config.hideBuiltinMasks = e.currentTarget.checked),
  798. )
  799. }
  800. ></input>
  801. </ListItem>
  802. </List>
  803. <List>
  804. <ListItem
  805. title={Locale.Settings.Prompt.Disable.Title}
  806. subTitle={Locale.Settings.Prompt.Disable.SubTitle}
  807. >
  808. <input
  809. type="checkbox"
  810. checked={config.disablePromptHint}
  811. onChange={(e) =>
  812. updateConfig(
  813. (config) =>
  814. (config.disablePromptHint = e.currentTarget.checked),
  815. )
  816. }
  817. ></input>
  818. </ListItem>
  819. <ListItem
  820. title={Locale.Settings.Prompt.List}
  821. subTitle={Locale.Settings.Prompt.ListCount(
  822. builtinCount,
  823. customCount,
  824. )}
  825. >
  826. <IconButton
  827. icon={<EditIcon />}
  828. text={Locale.Settings.Prompt.Edit}
  829. onClick={() => setShowPromptModal(true)}
  830. />
  831. </ListItem>
  832. </List>
  833. <List id={SlotID.CustomModel}>
  834. {showAccessCode && (
  835. <ListItem
  836. title={Locale.Settings.Access.AccessCode.Title}
  837. subTitle={Locale.Settings.Access.AccessCode.SubTitle}
  838. >
  839. <PasswordInput
  840. value={accessStore.accessCode}
  841. type="text"
  842. placeholder={Locale.Settings.Access.AccessCode.Placeholder}
  843. onChange={(e) => {
  844. accessStore.update(
  845. (access) => (access.accessCode = e.currentTarget.value),
  846. );
  847. }}
  848. />
  849. </ListItem>
  850. )}
  851. {!accessStore.hideUserApiKey && (
  852. <>
  853. {
  854. // Conditionally render the following ListItem based on clientConfig.isApp
  855. !clientConfig?.isApp && ( // only show if isApp is false
  856. <ListItem
  857. title={Locale.Settings.Access.CustomEndpoint.Title}
  858. subTitle={Locale.Settings.Access.CustomEndpoint.SubTitle}
  859. >
  860. <input
  861. type="checkbox"
  862. checked={accessStore.useCustomConfig}
  863. onChange={(e) =>
  864. accessStore.update(
  865. (access) =>
  866. (access.useCustomConfig = e.currentTarget.checked),
  867. )
  868. }
  869. ></input>
  870. </ListItem>
  871. )
  872. }
  873. {accessStore.useCustomConfig && (
  874. <>
  875. <ListItem
  876. title={Locale.Settings.Access.Provider.Title}
  877. subTitle={Locale.Settings.Access.Provider.SubTitle}
  878. >
  879. <Select
  880. value={accessStore.provider}
  881. onChange={(e) => {
  882. accessStore.update(
  883. (access) =>
  884. (access.provider = e.target
  885. .value as ServiceProvider),
  886. );
  887. }}
  888. >
  889. {Object.entries(ServiceProvider).map(([k, v]) => (
  890. <option value={v} key={k}>
  891. {k}
  892. </option>
  893. ))}
  894. </Select>
  895. </ListItem>
  896. {accessStore.provider === "OpenAI" ? (
  897. <>
  898. <ListItem
  899. title={Locale.Settings.Access.OpenAI.Endpoint.Title}
  900. subTitle={
  901. Locale.Settings.Access.OpenAI.Endpoint.SubTitle
  902. }
  903. >
  904. <input
  905. type="text"
  906. value={accessStore.openaiUrl}
  907. placeholder={OPENAI_BASE_URL}
  908. onChange={(e) =>
  909. accessStore.update(
  910. (access) =>
  911. (access.openaiUrl = e.currentTarget.value),
  912. )
  913. }
  914. ></input>
  915. </ListItem>
  916. <ListItem
  917. title={Locale.Settings.Access.OpenAI.ApiKey.Title}
  918. subTitle={Locale.Settings.Access.OpenAI.ApiKey.SubTitle}
  919. >
  920. <PasswordInput
  921. value={accessStore.openaiApiKey}
  922. type="text"
  923. placeholder={
  924. Locale.Settings.Access.OpenAI.ApiKey.Placeholder
  925. }
  926. onChange={(e) => {
  927. accessStore.update(
  928. (access) =>
  929. (access.openaiApiKey = e.currentTarget.value),
  930. );
  931. }}
  932. />
  933. </ListItem>
  934. </>
  935. ) : (
  936. <>
  937. <ListItem
  938. title={Locale.Settings.Access.Azure.Endpoint.Title}
  939. subTitle={
  940. Locale.Settings.Access.Azure.Endpoint.SubTitle +
  941. Azure.ExampleEndpoint
  942. }
  943. >
  944. <input
  945. type="text"
  946. value={accessStore.azureUrl}
  947. placeholder={Azure.ExampleEndpoint}
  948. onChange={(e) =>
  949. accessStore.update(
  950. (access) =>
  951. (access.azureUrl = e.currentTarget.value),
  952. )
  953. }
  954. ></input>
  955. </ListItem>
  956. <ListItem
  957. title={Locale.Settings.Access.Azure.ApiKey.Title}
  958. subTitle={Locale.Settings.Access.Azure.ApiKey.SubTitle}
  959. >
  960. <PasswordInput
  961. value={accessStore.azureApiKey}
  962. type="text"
  963. placeholder={
  964. Locale.Settings.Access.Azure.ApiKey.Placeholder
  965. }
  966. onChange={(e) => {
  967. accessStore.update(
  968. (access) =>
  969. (access.azureApiKey = e.currentTarget.value),
  970. );
  971. }}
  972. />
  973. </ListItem>
  974. <ListItem
  975. title={Locale.Settings.Access.Azure.ApiVerion.Title}
  976. subTitle={
  977. Locale.Settings.Access.Azure.ApiVerion.SubTitle
  978. }
  979. >
  980. <input
  981. type="text"
  982. value={accessStore.azureApiVersion}
  983. placeholder="2023-08-01-preview"
  984. onChange={(e) =>
  985. accessStore.update(
  986. (access) =>
  987. (access.azureApiVersion =
  988. e.currentTarget.value),
  989. )
  990. }
  991. ></input>
  992. </ListItem>
  993. </>
  994. )}
  995. </>
  996. )}
  997. </>
  998. )}
  999. {!shouldHideBalanceQuery && !clientConfig?.isApp ? (
  1000. <ListItem
  1001. title={Locale.Settings.Usage.Title}
  1002. subTitle={
  1003. showUsage
  1004. ? loadingUsage
  1005. ? Locale.Settings.Usage.IsChecking
  1006. : Locale.Settings.Usage.SubTitle(
  1007. usage?.used ?? "[?]",
  1008. usage?.subscription ?? "[?]",
  1009. )
  1010. : Locale.Settings.Usage.NoAccess
  1011. }
  1012. >
  1013. {!showUsage || loadingUsage ? (
  1014. <div />
  1015. ) : (
  1016. <IconButton
  1017. icon={<ResetIcon></ResetIcon>}
  1018. text={Locale.Settings.Usage.Check}
  1019. onClick={() => checkUsage(true)}
  1020. />
  1021. )}
  1022. </ListItem>
  1023. ) : null}
  1024. <ListItem
  1025. title={Locale.Settings.Access.CustomModel.Title}
  1026. subTitle={Locale.Settings.Access.CustomModel.SubTitle}
  1027. >
  1028. <input
  1029. type="text"
  1030. value={config.customModels}
  1031. placeholder="model1,model2,model3"
  1032. onChange={(e) =>
  1033. config.update(
  1034. (config) => (config.customModels = e.currentTarget.value),
  1035. )
  1036. }
  1037. ></input>
  1038. </ListItem>
  1039. </List>
  1040. <List>
  1041. <ModelConfigList
  1042. modelConfig={config.modelConfig}
  1043. updateConfig={(updater) => {
  1044. const modelConfig = { ...config.modelConfig };
  1045. updater(modelConfig);
  1046. config.update((config) => (config.modelConfig = modelConfig));
  1047. }}
  1048. />
  1049. </List>
  1050. {shouldShowPromptModal && (
  1051. <UserPromptModal onClose={() => setShowPromptModal(false)} />
  1052. )}
  1053. <DangerItems />
  1054. </div>
  1055. </ErrorBoundary>
  1056. );
  1057. }