settings.tsx 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319
  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. Baidu,
  51. Google,
  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. </>
  1061. )}
  1062. {accessStore.provider === ServiceProvider.Anthropic && (
  1063. <>
  1064. <ListItem
  1065. title={Locale.Settings.Access.Anthropic.Endpoint.Title}
  1066. subTitle={
  1067. Locale.Settings.Access.Anthropic.Endpoint.SubTitle +
  1068. Anthropic.ExampleEndpoint
  1069. }
  1070. >
  1071. <input
  1072. type="text"
  1073. value={accessStore.anthropicUrl}
  1074. placeholder={Anthropic.ExampleEndpoint}
  1075. onChange={(e) =>
  1076. accessStore.update(
  1077. (access) =>
  1078. (access.anthropicUrl = e.currentTarget.value),
  1079. )
  1080. }
  1081. ></input>
  1082. </ListItem>
  1083. <ListItem
  1084. title={Locale.Settings.Access.Anthropic.ApiKey.Title}
  1085. subTitle={
  1086. Locale.Settings.Access.Anthropic.ApiKey.SubTitle
  1087. }
  1088. >
  1089. <PasswordInput
  1090. value={accessStore.anthropicApiKey}
  1091. type="text"
  1092. placeholder={
  1093. Locale.Settings.Access.Anthropic.ApiKey.Placeholder
  1094. }
  1095. onChange={(e) => {
  1096. accessStore.update(
  1097. (access) =>
  1098. (access.anthropicApiKey =
  1099. e.currentTarget.value),
  1100. );
  1101. }}
  1102. />
  1103. </ListItem>
  1104. <ListItem
  1105. title={Locale.Settings.Access.Anthropic.ApiVerion.Title}
  1106. subTitle={
  1107. Locale.Settings.Access.Anthropic.ApiVerion.SubTitle
  1108. }
  1109. >
  1110. <input
  1111. type="text"
  1112. value={accessStore.anthropicApiVersion}
  1113. placeholder={Anthropic.Vision}
  1114. onChange={(e) =>
  1115. accessStore.update(
  1116. (access) =>
  1117. (access.anthropicApiVersion =
  1118. e.currentTarget.value),
  1119. )
  1120. }
  1121. ></input>
  1122. </ListItem>
  1123. </>
  1124. )}
  1125. {accessStore.provider === ServiceProvider.Baidu && (
  1126. <>
  1127. <ListItem
  1128. title={Locale.Settings.Access.Baidu.Endpoint.Title}
  1129. subTitle={
  1130. Locale.Settings.Access.Anthropic.Endpoint.SubTitle +
  1131. Baidu.ExampleEndpoint
  1132. }
  1133. >
  1134. <input
  1135. type="text"
  1136. value={accessStore.baiduUrl}
  1137. placeholder={Baidu.ExampleEndpoint}
  1138. onChange={(e) =>
  1139. accessStore.update(
  1140. (access) =>
  1141. (access.baiduUrl = e.currentTarget.value),
  1142. )
  1143. }
  1144. ></input>
  1145. </ListItem>
  1146. <ListItem
  1147. title={Locale.Settings.Access.Baidu.ApiKey.Title}
  1148. subTitle={Locale.Settings.Access.Baidu.ApiKey.SubTitle}
  1149. >
  1150. <PasswordInput
  1151. value={accessStore.baiduApiKey}
  1152. type="text"
  1153. placeholder={
  1154. Locale.Settings.Access.Baidu.ApiKey.Placeholder
  1155. }
  1156. onChange={(e) => {
  1157. accessStore.update(
  1158. (access) =>
  1159. (access.baiduApiKey = e.currentTarget.value),
  1160. );
  1161. }}
  1162. />
  1163. </ListItem>
  1164. <ListItem
  1165. title={Locale.Settings.Access.Baidu.SecretKey.Title}
  1166. subTitle={
  1167. Locale.Settings.Access.Baidu.SecretKey.SubTitle
  1168. }
  1169. >
  1170. <PasswordInput
  1171. value={accessStore.baiduSecretKey}
  1172. type="text"
  1173. placeholder={
  1174. Locale.Settings.Access.Baidu.SecretKey.Placeholder
  1175. }
  1176. onChange={(e) => {
  1177. accessStore.update(
  1178. (access) =>
  1179. (access.baiduSecretKey = e.currentTarget.value),
  1180. );
  1181. }}
  1182. />
  1183. </ListItem>
  1184. </>
  1185. )}
  1186. </>
  1187. )}
  1188. </>
  1189. )}
  1190. {!shouldHideBalanceQuery && !clientConfig?.isApp ? (
  1191. <ListItem
  1192. title={Locale.Settings.Usage.Title}
  1193. subTitle={
  1194. showUsage
  1195. ? loadingUsage
  1196. ? Locale.Settings.Usage.IsChecking
  1197. : Locale.Settings.Usage.SubTitle(
  1198. usage?.used ?? "[?]",
  1199. usage?.subscription ?? "[?]",
  1200. )
  1201. : Locale.Settings.Usage.NoAccess
  1202. }
  1203. >
  1204. {!showUsage || loadingUsage ? (
  1205. <div />
  1206. ) : (
  1207. <IconButton
  1208. icon={<ResetIcon></ResetIcon>}
  1209. text={Locale.Settings.Usage.Check}
  1210. onClick={() => checkUsage(true)}
  1211. />
  1212. )}
  1213. </ListItem>
  1214. ) : null}
  1215. <ListItem
  1216. title={Locale.Settings.Access.CustomModel.Title}
  1217. subTitle={Locale.Settings.Access.CustomModel.SubTitle}
  1218. >
  1219. <input
  1220. type="text"
  1221. value={config.customModels}
  1222. placeholder="model1,model2,model3"
  1223. onChange={(e) =>
  1224. config.update(
  1225. (config) => (config.customModels = e.currentTarget.value),
  1226. )
  1227. }
  1228. ></input>
  1229. </ListItem>
  1230. </List>
  1231. <List>
  1232. <ModelConfigList
  1233. modelConfig={config.modelConfig}
  1234. updateConfig={(updater) => {
  1235. const modelConfig = { ...config.modelConfig };
  1236. updater(modelConfig);
  1237. config.update((config) => (config.modelConfig = modelConfig));
  1238. }}
  1239. />
  1240. </List>
  1241. {shouldShowPromptModal && (
  1242. <UserPromptModal onClose={() => setShowPromptModal(false)} />
  1243. )}
  1244. <DangerItems />
  1245. </div>
  1246. </ErrorBoundary>
  1247. );
  1248. }