settings.tsx 41 KB

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