settings.tsx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. import { useState, useEffect, useMemo, HTMLProps } from "react";
  2. import EmojiPicker, { Theme as EmojiTheme } from "emoji-picker-react";
  3. import styles from "./settings.module.scss";
  4. import ResetIcon from "../icons/reload.svg";
  5. import CloseIcon from "../icons/close.svg";
  6. import ClearIcon from "../icons/clear.svg";
  7. import EditIcon from "../icons/edit.svg";
  8. import EyeIcon from "../icons/eye.svg";
  9. import EyeOffIcon from "../icons/eye-off.svg";
  10. import { List, ListItem, Popover, showToast } from "./ui-lib";
  11. import { IconButton } from "./button";
  12. import {
  13. SubmitKey,
  14. useChatStore,
  15. Theme,
  16. ALL_MODELS,
  17. useUpdateStore,
  18. useAccessStore,
  19. ModalConfigValidator,
  20. } from "../store";
  21. import { Avatar } from "./chat";
  22. import Locale, { AllLangs, changeLang, getLang } from "../locales";
  23. import { getEmojiUrl } from "../utils";
  24. import Link from "next/link";
  25. import { UPDATE_URL } from "../constant";
  26. import { SearchService, usePromptStore } from "../store/prompt";
  27. import { requestUsage } from "../requests";
  28. import { ErrorBoundary } from "./error";
  29. import { InputRange } from "./input-range";
  30. import { getClientSideConfig } from "../config/client";
  31. function SettingItem(props: {
  32. title: string;
  33. subTitle?: string;
  34. children: JSX.Element;
  35. }) {
  36. return (
  37. <ListItem>
  38. <div className={styles["settings-title"]}>
  39. <div>{props.title}</div>
  40. {props.subTitle && (
  41. <div className={styles["settings-sub-title"]}>{props.subTitle}</div>
  42. )}
  43. </div>
  44. {props.children}
  45. </ListItem>
  46. );
  47. }
  48. function PasswordInput(props: HTMLProps<HTMLInputElement>) {
  49. const [visible, setVisible] = useState(false);
  50. function changeVisibility() {
  51. setVisible(!visible);
  52. }
  53. return (
  54. <div className={styles["password-input-container"]}>
  55. <IconButton
  56. icon={visible ? <EyeIcon /> : <EyeOffIcon />}
  57. onClick={changeVisibility}
  58. className={styles["password-eye"]}
  59. />
  60. <input
  61. {...props}
  62. type={visible ? "text" : "password"}
  63. className={styles["password-input"]}
  64. />
  65. </div>
  66. );
  67. }
  68. export function Settings(props: { closeSettings: () => void }) {
  69. const [showEmojiPicker, setShowEmojiPicker] = useState(false);
  70. const [config, updateConfig, resetConfig, clearAllData, clearSessions] =
  71. useChatStore((state) => [
  72. state.config,
  73. state.updateConfig,
  74. state.resetConfig,
  75. state.clearAllData,
  76. state.clearSessions,
  77. ]);
  78. const updateStore = useUpdateStore();
  79. const [checkingUpdate, setCheckingUpdate] = useState(false);
  80. const currentVersion = getClientSideConfig()?.version;
  81. const remoteId = updateStore.remoteId;
  82. const hasNewVersion = currentVersion !== remoteId;
  83. function checkUpdate(force = false) {
  84. setCheckingUpdate(true);
  85. updateStore.getLatestCommitId(force).then(() => {
  86. setCheckingUpdate(false);
  87. });
  88. }
  89. const [usage, setUsage] = useState<{
  90. used?: number;
  91. subscription?: number;
  92. }>();
  93. const [loadingUsage, setLoadingUsage] = useState(false);
  94. function checkUsage() {
  95. setLoadingUsage(true);
  96. requestUsage()
  97. .then((res) => setUsage(res))
  98. .finally(() => {
  99. setLoadingUsage(false);
  100. });
  101. }
  102. const accessStore = useAccessStore();
  103. const enabledAccessControl = useMemo(
  104. () => accessStore.enabledAccessControl(),
  105. // eslint-disable-next-line react-hooks/exhaustive-deps
  106. [],
  107. );
  108. const promptStore = usePromptStore();
  109. const builtinCount = SearchService.count.builtin;
  110. const customCount = promptStore.prompts.size ?? 0;
  111. const showUsage = accessStore.isAuthorized();
  112. useEffect(() => {
  113. checkUpdate();
  114. showUsage && checkUsage();
  115. // eslint-disable-next-line react-hooks/exhaustive-deps
  116. }, []);
  117. useEffect(() => {
  118. const keydownEvent = (e: KeyboardEvent) => {
  119. if (e.key === "Escape") {
  120. props.closeSettings();
  121. }
  122. };
  123. document.addEventListener("keydown", keydownEvent);
  124. return () => {
  125. document.removeEventListener("keydown", keydownEvent);
  126. };
  127. // eslint-disable-next-line react-hooks/exhaustive-deps
  128. }, []);
  129. return (
  130. <ErrorBoundary>
  131. <div className={styles["window-header"]}>
  132. <div className={styles["window-header-title"]}>
  133. <div className={styles["window-header-main-title"]}>
  134. {Locale.Settings.Title}
  135. </div>
  136. <div className={styles["window-header-sub-title"]}>
  137. {Locale.Settings.SubTitle}
  138. </div>
  139. </div>
  140. <div className={styles["window-actions"]}>
  141. <div className={styles["window-action-button"]}>
  142. <IconButton
  143. icon={<ClearIcon />}
  144. onClick={() => {
  145. const confirmed = window.confirm(
  146. `${Locale.Settings.Actions.ConfirmClearAll.Confirm}`,
  147. );
  148. if (confirmed) {
  149. clearSessions();
  150. }
  151. }}
  152. bordered
  153. title={Locale.Settings.Actions.ClearAll}
  154. />
  155. </div>
  156. <div className={styles["window-action-button"]}>
  157. <IconButton
  158. icon={<ResetIcon />}
  159. onClick={() => {
  160. const confirmed = window.confirm(
  161. `${Locale.Settings.Actions.ConfirmResetAll.Confirm}`,
  162. );
  163. if (confirmed) {
  164. resetConfig();
  165. }
  166. }}
  167. bordered
  168. title={Locale.Settings.Actions.ResetAll}
  169. />
  170. </div>
  171. <div className={styles["window-action-button"]}>
  172. <IconButton
  173. icon={<CloseIcon />}
  174. onClick={props.closeSettings}
  175. bordered
  176. title={Locale.Settings.Actions.Close}
  177. />
  178. </div>
  179. </div>
  180. </div>
  181. <div className={styles["settings"]}>
  182. <List>
  183. <SettingItem title={Locale.Settings.Avatar}>
  184. <Popover
  185. onClose={() => setShowEmojiPicker(false)}
  186. content={
  187. <EmojiPicker
  188. lazyLoadEmojis
  189. theme={EmojiTheme.AUTO}
  190. getEmojiUrl={getEmojiUrl}
  191. onEmojiClick={(e) => {
  192. updateConfig((config) => (config.avatar = e.unified));
  193. setShowEmojiPicker(false);
  194. }}
  195. />
  196. }
  197. open={showEmojiPicker}
  198. >
  199. <div
  200. className={styles.avatar}
  201. onClick={() => setShowEmojiPicker(true)}
  202. >
  203. <Avatar role="user" />
  204. </div>
  205. </Popover>
  206. </SettingItem>
  207. <SettingItem
  208. title={Locale.Settings.Update.Version(currentVersion ?? "unknown")}
  209. subTitle={
  210. checkingUpdate
  211. ? Locale.Settings.Update.IsChecking
  212. : hasNewVersion
  213. ? Locale.Settings.Update.FoundUpdate(remoteId ?? "ERROR")
  214. : Locale.Settings.Update.IsLatest
  215. }
  216. >
  217. {checkingUpdate ? (
  218. <div />
  219. ) : hasNewVersion ? (
  220. <Link href={UPDATE_URL} target="_blank" className="link">
  221. {Locale.Settings.Update.GoToUpdate}
  222. </Link>
  223. ) : (
  224. <IconButton
  225. icon={<ResetIcon></ResetIcon>}
  226. text={Locale.Settings.Update.CheckUpdate}
  227. onClick={() => checkUpdate(true)}
  228. />
  229. )}
  230. </SettingItem>
  231. <SettingItem title={Locale.Settings.SendKey}>
  232. <select
  233. value={config.submitKey}
  234. onChange={(e) => {
  235. updateConfig(
  236. (config) =>
  237. (config.submitKey = e.target.value as any as SubmitKey),
  238. );
  239. }}
  240. >
  241. {Object.values(SubmitKey).map((v) => (
  242. <option value={v} key={v}>
  243. {v}
  244. </option>
  245. ))}
  246. </select>
  247. </SettingItem>
  248. <ListItem>
  249. <div className={styles["settings-title"]}>
  250. {Locale.Settings.Theme}
  251. </div>
  252. <select
  253. value={config.theme}
  254. onChange={(e) => {
  255. updateConfig(
  256. (config) => (config.theme = e.target.value as any as Theme),
  257. );
  258. }}
  259. >
  260. {Object.values(Theme).map((v) => (
  261. <option value={v} key={v}>
  262. {v}
  263. </option>
  264. ))}
  265. </select>
  266. </ListItem>
  267. <SettingItem title={Locale.Settings.Lang.Name}>
  268. <select
  269. value={getLang()}
  270. onChange={(e) => {
  271. changeLang(e.target.value as any);
  272. }}
  273. >
  274. {AllLangs.map((lang) => (
  275. <option value={lang} key={lang}>
  276. {Locale.Settings.Lang.Options[lang]}
  277. </option>
  278. ))}
  279. </select>
  280. </SettingItem>
  281. <SettingItem
  282. title={Locale.Settings.FontSize.Title}
  283. subTitle={Locale.Settings.FontSize.SubTitle}
  284. >
  285. <InputRange
  286. title={`${config.fontSize ?? 14}px`}
  287. value={config.fontSize}
  288. min="12"
  289. max="18"
  290. step="1"
  291. onChange={(e) =>
  292. updateConfig(
  293. (config) =>
  294. (config.fontSize = Number.parseInt(e.currentTarget.value)),
  295. )
  296. }
  297. ></InputRange>
  298. </SettingItem>
  299. <SettingItem title={Locale.Settings.TightBorder}>
  300. <input
  301. type="checkbox"
  302. checked={config.tightBorder}
  303. onChange={(e) =>
  304. updateConfig(
  305. (config) => (config.tightBorder = e.currentTarget.checked),
  306. )
  307. }
  308. ></input>
  309. </SettingItem>
  310. <SettingItem title={Locale.Settings.SendPreviewBubble}>
  311. <input
  312. type="checkbox"
  313. checked={config.sendPreviewBubble}
  314. onChange={(e) =>
  315. updateConfig(
  316. (config) =>
  317. (config.sendPreviewBubble = e.currentTarget.checked),
  318. )
  319. }
  320. ></input>
  321. </SettingItem>
  322. </List>
  323. <List>
  324. {enabledAccessControl ? (
  325. <SettingItem
  326. title={Locale.Settings.AccessCode.Title}
  327. subTitle={Locale.Settings.AccessCode.SubTitle}
  328. >
  329. <PasswordInput
  330. value={accessStore.accessCode}
  331. type="text"
  332. placeholder={Locale.Settings.AccessCode.Placeholder}
  333. onChange={(e) => {
  334. accessStore.updateCode(e.currentTarget.value);
  335. }}
  336. />
  337. </SettingItem>
  338. ) : (
  339. <></>
  340. )}
  341. <SettingItem
  342. title={Locale.Settings.Token.Title}
  343. subTitle={Locale.Settings.Token.SubTitle}
  344. >
  345. <PasswordInput
  346. value={accessStore.token}
  347. type="text"
  348. placeholder={Locale.Settings.Token.Placeholder}
  349. onChange={(e) => {
  350. accessStore.updateToken(e.currentTarget.value);
  351. }}
  352. />
  353. </SettingItem>
  354. <SettingItem
  355. title={Locale.Settings.Usage.Title}
  356. subTitle={
  357. showUsage
  358. ? loadingUsage
  359. ? Locale.Settings.Usage.IsChecking
  360. : Locale.Settings.Usage.SubTitle(
  361. usage?.used ?? "[?]",
  362. usage?.subscription ?? "[?]",
  363. )
  364. : Locale.Settings.Usage.NoAccess
  365. }
  366. >
  367. {!showUsage || loadingUsage ? (
  368. <div />
  369. ) : (
  370. <IconButton
  371. icon={<ResetIcon></ResetIcon>}
  372. text={Locale.Settings.Usage.Check}
  373. onClick={checkUsage}
  374. />
  375. )}
  376. </SettingItem>
  377. <SettingItem
  378. title={Locale.Settings.HistoryCount.Title}
  379. subTitle={Locale.Settings.HistoryCount.SubTitle}
  380. >
  381. <InputRange
  382. title={config.historyMessageCount.toString()}
  383. value={config.historyMessageCount}
  384. min="0"
  385. max="25"
  386. step="1"
  387. onChange={(e) =>
  388. updateConfig(
  389. (config) =>
  390. (config.historyMessageCount = e.target.valueAsNumber),
  391. )
  392. }
  393. ></InputRange>
  394. </SettingItem>
  395. <SettingItem
  396. title={Locale.Settings.CompressThreshold.Title}
  397. subTitle={Locale.Settings.CompressThreshold.SubTitle}
  398. >
  399. <input
  400. type="number"
  401. min={500}
  402. max={4000}
  403. value={config.compressMessageLengthThreshold}
  404. onChange={(e) =>
  405. updateConfig(
  406. (config) =>
  407. (config.compressMessageLengthThreshold =
  408. e.currentTarget.valueAsNumber),
  409. )
  410. }
  411. ></input>
  412. </SettingItem>
  413. </List>
  414. <List>
  415. <SettingItem
  416. title={Locale.Settings.Prompt.Disable.Title}
  417. subTitle={Locale.Settings.Prompt.Disable.SubTitle}
  418. >
  419. <input
  420. type="checkbox"
  421. checked={config.disablePromptHint}
  422. onChange={(e) =>
  423. updateConfig(
  424. (config) =>
  425. (config.disablePromptHint = e.currentTarget.checked),
  426. )
  427. }
  428. ></input>
  429. </SettingItem>
  430. <SettingItem
  431. title={Locale.Settings.Prompt.List}
  432. subTitle={Locale.Settings.Prompt.ListCount(
  433. builtinCount,
  434. customCount,
  435. )}
  436. >
  437. <IconButton
  438. icon={<EditIcon />}
  439. text={Locale.Settings.Prompt.Edit}
  440. onClick={() => showToast(Locale.WIP)}
  441. />
  442. </SettingItem>
  443. </List>
  444. <List>
  445. <SettingItem title={Locale.Settings.Model}>
  446. <select
  447. value={config.modelConfig.model}
  448. onChange={(e) => {
  449. updateConfig(
  450. (config) =>
  451. (config.modelConfig.model = ModalConfigValidator.model(
  452. e.currentTarget.value,
  453. )),
  454. );
  455. }}
  456. >
  457. {ALL_MODELS.map((v) => (
  458. <option value={v.name} key={v.name} disabled={!v.available}>
  459. {v.name}
  460. </option>
  461. ))}
  462. </select>
  463. </SettingItem>
  464. <SettingItem
  465. title={Locale.Settings.Temperature.Title}
  466. subTitle={Locale.Settings.Temperature.SubTitle}
  467. >
  468. <InputRange
  469. value={config.modelConfig.temperature?.toFixed(1)}
  470. min="0"
  471. max="2"
  472. step="0.1"
  473. onChange={(e) => {
  474. updateConfig(
  475. (config) =>
  476. (config.modelConfig.temperature =
  477. ModalConfigValidator.temperature(
  478. e.currentTarget.valueAsNumber,
  479. )),
  480. );
  481. }}
  482. ></InputRange>
  483. </SettingItem>
  484. <SettingItem
  485. title={Locale.Settings.MaxTokens.Title}
  486. subTitle={Locale.Settings.MaxTokens.SubTitle}
  487. >
  488. <input
  489. type="number"
  490. min={100}
  491. max={32000}
  492. value={config.modelConfig.max_tokens}
  493. onChange={(e) =>
  494. updateConfig(
  495. (config) =>
  496. (config.modelConfig.max_tokens =
  497. ModalConfigValidator.max_tokens(
  498. e.currentTarget.valueAsNumber,
  499. )),
  500. )
  501. }
  502. ></input>
  503. </SettingItem>
  504. <SettingItem
  505. title={Locale.Settings.PresencePenlty.Title}
  506. subTitle={Locale.Settings.PresencePenlty.SubTitle}
  507. >
  508. <InputRange
  509. value={config.modelConfig.presence_penalty?.toFixed(1)}
  510. min="-2"
  511. max="2"
  512. step="0.5"
  513. onChange={(e) => {
  514. updateConfig(
  515. (config) =>
  516. (config.modelConfig.presence_penalty =
  517. ModalConfigValidator.presence_penalty(
  518. e.currentTarget.valueAsNumber,
  519. )),
  520. );
  521. }}
  522. ></InputRange>
  523. </SettingItem>
  524. </List>
  525. </div>
  526. </ErrorBoundary>
  527. );
  528. }