settings.tsx 42 KB

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