settings.tsx 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191
  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.cloudSync();
  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.cloudSync();
  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={() => {
  646. setShowEmojiPicker(!showEmojiPicker);
  647. }}
  648. >
  649. <Avatar avatar={config.avatar} />
  650. </div>
  651. </Popover>
  652. </ListItem>
  653. <ListItem
  654. title={Locale.Settings.Update.Version(currentVersion ?? "unknown")}
  655. subTitle={
  656. checkingUpdate
  657. ? Locale.Settings.Update.IsChecking
  658. : hasNewVersion
  659. ? Locale.Settings.Update.FoundUpdate(remoteId ?? "ERROR")
  660. : Locale.Settings.Update.IsLatest
  661. }
  662. >
  663. {checkingUpdate ? (
  664. <LoadingIcon />
  665. ) : hasNewVersion ? (
  666. <Link href={updateUrl} target="_blank" className="link">
  667. {Locale.Settings.Update.GoToUpdate}
  668. </Link>
  669. ) : (
  670. <IconButton
  671. icon={<ResetIcon></ResetIcon>}
  672. text={Locale.Settings.Update.CheckUpdate}
  673. onClick={() => checkUpdate(true)}
  674. />
  675. )}
  676. </ListItem>
  677. <ListItem title={Locale.Settings.SendKey}>
  678. <Select
  679. value={config.submitKey}
  680. onChange={(e) => {
  681. updateConfig(
  682. (config) =>
  683. (config.submitKey = e.target.value as any as SubmitKey),
  684. );
  685. }}
  686. >
  687. {Object.values(SubmitKey).map((v) => (
  688. <option value={v} key={v}>
  689. {v}
  690. </option>
  691. ))}
  692. </Select>
  693. </ListItem>
  694. <ListItem title={Locale.Settings.Theme}>
  695. <Select
  696. value={config.theme}
  697. onChange={(e) => {
  698. updateConfig(
  699. (config) => (config.theme = e.target.value as any as Theme),
  700. );
  701. }}
  702. >
  703. {Object.values(Theme).map((v) => (
  704. <option value={v} key={v}>
  705. {v}
  706. </option>
  707. ))}
  708. </Select>
  709. </ListItem>
  710. <ListItem title={Locale.Settings.Lang.Name}>
  711. <Select
  712. value={getLang()}
  713. onChange={(e) => {
  714. changeLang(e.target.value as any);
  715. }}
  716. >
  717. {AllLangs.map((lang) => (
  718. <option value={lang} key={lang}>
  719. {ALL_LANG_OPTIONS[lang]}
  720. </option>
  721. ))}
  722. </Select>
  723. </ListItem>
  724. <ListItem
  725. title={Locale.Settings.FontSize.Title}
  726. subTitle={Locale.Settings.FontSize.SubTitle}
  727. >
  728. <InputRange
  729. title={`${config.fontSize ?? 14}px`}
  730. value={config.fontSize}
  731. min="12"
  732. max="40"
  733. step="1"
  734. onChange={(e) =>
  735. updateConfig(
  736. (config) =>
  737. (config.fontSize = Number.parseInt(e.currentTarget.value)),
  738. )
  739. }
  740. ></InputRange>
  741. </ListItem>
  742. <ListItem
  743. title={Locale.Settings.AutoGenerateTitle.Title}
  744. subTitle={Locale.Settings.AutoGenerateTitle.SubTitle}
  745. >
  746. <input
  747. type="checkbox"
  748. checked={config.enableAutoGenerateTitle}
  749. onChange={(e) =>
  750. updateConfig(
  751. (config) =>
  752. (config.enableAutoGenerateTitle = e.currentTarget.checked),
  753. )
  754. }
  755. ></input>
  756. </ListItem>
  757. <ListItem
  758. title={Locale.Settings.SendPreviewBubble.Title}
  759. subTitle={Locale.Settings.SendPreviewBubble.SubTitle}
  760. >
  761. <input
  762. type="checkbox"
  763. checked={config.sendPreviewBubble}
  764. onChange={(e) =>
  765. updateConfig(
  766. (config) =>
  767. (config.sendPreviewBubble = e.currentTarget.checked),
  768. )
  769. }
  770. ></input>
  771. </ListItem>
  772. </List>
  773. <SyncItems />
  774. <List>
  775. <ListItem
  776. title={Locale.Settings.Mask.Splash.Title}
  777. subTitle={Locale.Settings.Mask.Splash.SubTitle}
  778. >
  779. <input
  780. type="checkbox"
  781. checked={!config.dontShowMaskSplashScreen}
  782. onChange={(e) =>
  783. updateConfig(
  784. (config) =>
  785. (config.dontShowMaskSplashScreen =
  786. !e.currentTarget.checked),
  787. )
  788. }
  789. ></input>
  790. </ListItem>
  791. <ListItem
  792. title={Locale.Settings.Mask.Builtin.Title}
  793. subTitle={Locale.Settings.Mask.Builtin.SubTitle}
  794. >
  795. <input
  796. type="checkbox"
  797. checked={config.hideBuiltinMasks}
  798. onChange={(e) =>
  799. updateConfig(
  800. (config) =>
  801. (config.hideBuiltinMasks = e.currentTarget.checked),
  802. )
  803. }
  804. ></input>
  805. </ListItem>
  806. </List>
  807. <List>
  808. <ListItem
  809. title={Locale.Settings.Prompt.Disable.Title}
  810. subTitle={Locale.Settings.Prompt.Disable.SubTitle}
  811. >
  812. <input
  813. type="checkbox"
  814. checked={config.disablePromptHint}
  815. onChange={(e) =>
  816. updateConfig(
  817. (config) =>
  818. (config.disablePromptHint = e.currentTarget.checked),
  819. )
  820. }
  821. ></input>
  822. </ListItem>
  823. <ListItem
  824. title={Locale.Settings.Prompt.List}
  825. subTitle={Locale.Settings.Prompt.ListCount(
  826. builtinCount,
  827. customCount,
  828. )}
  829. >
  830. <IconButton
  831. icon={<EditIcon />}
  832. text={Locale.Settings.Prompt.Edit}
  833. onClick={() => setShowPromptModal(true)}
  834. />
  835. </ListItem>
  836. </List>
  837. <List id={SlotID.CustomModel}>
  838. {showAccessCode && (
  839. <ListItem
  840. title={Locale.Settings.Access.AccessCode.Title}
  841. subTitle={Locale.Settings.Access.AccessCode.SubTitle}
  842. >
  843. <PasswordInput
  844. value={accessStore.accessCode}
  845. type="text"
  846. placeholder={Locale.Settings.Access.AccessCode.Placeholder}
  847. onChange={(e) => {
  848. accessStore.update(
  849. (access) => (access.accessCode = e.currentTarget.value),
  850. );
  851. }}
  852. />
  853. </ListItem>
  854. )}
  855. {!accessStore.hideUserApiKey && (
  856. <>
  857. {
  858. // Conditionally render the following ListItem based on clientConfig.isApp
  859. !clientConfig?.isApp && ( // only show if isApp is false
  860. <ListItem
  861. title={Locale.Settings.Access.CustomEndpoint.Title}
  862. subTitle={Locale.Settings.Access.CustomEndpoint.SubTitle}
  863. >
  864. <input
  865. type="checkbox"
  866. checked={accessStore.useCustomConfig}
  867. onChange={(e) =>
  868. accessStore.update(
  869. (access) =>
  870. (access.useCustomConfig = e.currentTarget.checked),
  871. )
  872. }
  873. ></input>
  874. </ListItem>
  875. )
  876. }
  877. {accessStore.useCustomConfig && (
  878. <>
  879. <ListItem
  880. title={Locale.Settings.Access.Provider.Title}
  881. subTitle={Locale.Settings.Access.Provider.SubTitle}
  882. >
  883. <Select
  884. value={accessStore.provider}
  885. onChange={(e) => {
  886. accessStore.update(
  887. (access) =>
  888. (access.provider = e.target
  889. .value as ServiceProvider),
  890. );
  891. }}
  892. >
  893. {Object.entries(ServiceProvider).map(([k, v]) => (
  894. <option value={v} key={k}>
  895. {k}
  896. </option>
  897. ))}
  898. </Select>
  899. </ListItem>
  900. {accessStore.provider === "OpenAI" ? (
  901. <>
  902. <ListItem
  903. title={Locale.Settings.Access.OpenAI.Endpoint.Title}
  904. subTitle={
  905. Locale.Settings.Access.OpenAI.Endpoint.SubTitle
  906. }
  907. >
  908. <input
  909. type="text"
  910. value={accessStore.openaiUrl}
  911. placeholder={OPENAI_BASE_URL}
  912. onChange={(e) =>
  913. accessStore.update(
  914. (access) =>
  915. (access.openaiUrl = e.currentTarget.value),
  916. )
  917. }
  918. ></input>
  919. </ListItem>
  920. <ListItem
  921. title={Locale.Settings.Access.OpenAI.ApiKey.Title}
  922. subTitle={Locale.Settings.Access.OpenAI.ApiKey.SubTitle}
  923. >
  924. <PasswordInput
  925. value={accessStore.openaiApiKey}
  926. type="text"
  927. placeholder={
  928. Locale.Settings.Access.OpenAI.ApiKey.Placeholder
  929. }
  930. onChange={(e) => {
  931. accessStore.update(
  932. (access) =>
  933. (access.openaiApiKey = e.currentTarget.value),
  934. );
  935. }}
  936. />
  937. </ListItem>
  938. </>
  939. ) : accessStore.provider === "Azure" ? (
  940. <>
  941. <ListItem
  942. title={Locale.Settings.Access.Azure.Endpoint.Title}
  943. subTitle={
  944. Locale.Settings.Access.Azure.Endpoint.SubTitle +
  945. Azure.ExampleEndpoint
  946. }
  947. >
  948. <input
  949. type="text"
  950. value={accessStore.azureUrl}
  951. placeholder={Azure.ExampleEndpoint}
  952. onChange={(e) =>
  953. accessStore.update(
  954. (access) =>
  955. (access.azureUrl = e.currentTarget.value),
  956. )
  957. }
  958. ></input>
  959. </ListItem>
  960. <ListItem
  961. title={Locale.Settings.Access.Azure.ApiKey.Title}
  962. subTitle={Locale.Settings.Access.Azure.ApiKey.SubTitle}
  963. >
  964. <PasswordInput
  965. value={accessStore.azureApiKey}
  966. type="text"
  967. placeholder={
  968. Locale.Settings.Access.Azure.ApiKey.Placeholder
  969. }
  970. onChange={(e) => {
  971. accessStore.update(
  972. (access) =>
  973. (access.azureApiKey = e.currentTarget.value),
  974. );
  975. }}
  976. />
  977. </ListItem>
  978. <ListItem
  979. title={Locale.Settings.Access.Azure.ApiVerion.Title}
  980. subTitle={
  981. Locale.Settings.Access.Azure.ApiVerion.SubTitle
  982. }
  983. >
  984. <input
  985. type="text"
  986. value={accessStore.azureApiVersion}
  987. placeholder="2023-08-01-preview"
  988. onChange={(e) =>
  989. accessStore.update(
  990. (access) =>
  991. (access.azureApiVersion =
  992. e.currentTarget.value),
  993. )
  994. }
  995. ></input>
  996. </ListItem>
  997. </>
  998. ) : accessStore.provider === "Google" ? (
  999. <>
  1000. <ListItem
  1001. title={Locale.Settings.Access.Google.Endpoint.Title}
  1002. subTitle={
  1003. Locale.Settings.Access.Google.Endpoint.SubTitle +
  1004. Google.ExampleEndpoint
  1005. }
  1006. >
  1007. <input
  1008. type="text"
  1009. value={accessStore.googleUrl}
  1010. placeholder={Google.ExampleEndpoint}
  1011. onChange={(e) =>
  1012. accessStore.update(
  1013. (access) =>
  1014. (access.googleUrl = e.currentTarget.value),
  1015. )
  1016. }
  1017. ></input>
  1018. </ListItem>
  1019. <ListItem
  1020. title={Locale.Settings.Access.Google.ApiKey.Title}
  1021. subTitle={Locale.Settings.Access.Google.ApiKey.SubTitle}
  1022. >
  1023. <PasswordInput
  1024. value={accessStore.googleApiKey}
  1025. type="text"
  1026. placeholder={
  1027. Locale.Settings.Access.Google.ApiKey.Placeholder
  1028. }
  1029. onChange={(e) => {
  1030. accessStore.update(
  1031. (access) =>
  1032. (access.googleApiKey = e.currentTarget.value),
  1033. );
  1034. }}
  1035. />
  1036. </ListItem>
  1037. <ListItem
  1038. title={Locale.Settings.Access.Google.ApiVersion.Title}
  1039. subTitle={
  1040. Locale.Settings.Access.Google.ApiVersion.SubTitle
  1041. }
  1042. >
  1043. <input
  1044. type="text"
  1045. value={accessStore.googleApiVersion}
  1046. placeholder="2023-08-01-preview"
  1047. onChange={(e) =>
  1048. accessStore.update(
  1049. (access) =>
  1050. (access.googleApiVersion =
  1051. e.currentTarget.value),
  1052. )
  1053. }
  1054. ></input>
  1055. </ListItem>
  1056. </>
  1057. ) : null}
  1058. </>
  1059. )}
  1060. </>
  1061. )}
  1062. {!shouldHideBalanceQuery && !clientConfig?.isApp ? (
  1063. <ListItem
  1064. title={Locale.Settings.Usage.Title}
  1065. subTitle={
  1066. showUsage
  1067. ? loadingUsage
  1068. ? Locale.Settings.Usage.IsChecking
  1069. : Locale.Settings.Usage.SubTitle(
  1070. usage?.used ?? "[?]",
  1071. usage?.subscription ?? "[?]",
  1072. )
  1073. : Locale.Settings.Usage.NoAccess
  1074. }
  1075. >
  1076. {!showUsage || loadingUsage ? (
  1077. <div />
  1078. ) : (
  1079. <IconButton
  1080. icon={<ResetIcon></ResetIcon>}
  1081. text={Locale.Settings.Usage.Check}
  1082. onClick={() => checkUsage(true)}
  1083. />
  1084. )}
  1085. </ListItem>
  1086. ) : null}
  1087. <ListItem
  1088. title={Locale.Settings.Access.CustomModel.Title}
  1089. subTitle={Locale.Settings.Access.CustomModel.SubTitle}
  1090. >
  1091. <input
  1092. type="text"
  1093. value={config.customModels}
  1094. placeholder="model1,model2,model3"
  1095. onChange={(e) =>
  1096. config.update(
  1097. (config) => (config.customModels = e.currentTarget.value),
  1098. )
  1099. }
  1100. ></input>
  1101. </ListItem>
  1102. </List>
  1103. <List>
  1104. <ModelConfigList
  1105. modelConfig={config.modelConfig}
  1106. updateConfig={(updater) => {
  1107. const modelConfig = { ...config.modelConfig };
  1108. updater(modelConfig);
  1109. config.update((config) => (config.modelConfig = modelConfig));
  1110. }}
  1111. />
  1112. </List>
  1113. {shouldShowPromptModal && (
  1114. <UserPromptModal onClose={() => setShowPromptModal(false)} />
  1115. )}
  1116. <DangerItems />
  1117. </div>
  1118. </ErrorBoundary>
  1119. );
  1120. }