settings.tsx 38 KB

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