settings.tsx 17 KB

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