settings.tsx 17 KB

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