settings.tsx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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={clearSessions}
  140. bordered
  141. title={Locale.Settings.Actions.ClearAll}
  142. />
  143. </div>
  144. <div className={styles["window-action-button"]}>
  145. <IconButton
  146. icon={<ResetIcon />}
  147. onClick={resetConfig}
  148. bordered
  149. title={Locale.Settings.Actions.ResetAll}
  150. />
  151. </div>
  152. <div className={styles["window-action-button"]}>
  153. <IconButton
  154. icon={<CloseIcon />}
  155. onClick={props.closeSettings}
  156. bordered
  157. title={Locale.Settings.Actions.Close}
  158. />
  159. </div>
  160. </div>
  161. </div>
  162. <div className={styles["settings"]}>
  163. <List>
  164. <SettingItem title={Locale.Settings.Avatar}>
  165. <Popover
  166. onClose={() => setShowEmojiPicker(false)}
  167. content={
  168. <EmojiPicker
  169. lazyLoadEmojis
  170. theme={EmojiTheme.AUTO}
  171. getEmojiUrl={getEmojiUrl}
  172. onEmojiClick={(e) => {
  173. updateConfig((config) => (config.avatar = e.unified));
  174. setShowEmojiPicker(false);
  175. }}
  176. />
  177. }
  178. open={showEmojiPicker}
  179. >
  180. <div
  181. className={styles.avatar}
  182. onClick={() => setShowEmojiPicker(true)}
  183. >
  184. <Avatar role="user" />
  185. </div>
  186. </Popover>
  187. </SettingItem>
  188. <SettingItem
  189. title={Locale.Settings.Update.Version(currentId)}
  190. subTitle={
  191. checkingUpdate
  192. ? Locale.Settings.Update.IsChecking
  193. : hasNewVersion
  194. ? Locale.Settings.Update.FoundUpdate(remoteId ?? "ERROR")
  195. : Locale.Settings.Update.IsLatest
  196. }
  197. >
  198. {checkingUpdate ? (
  199. <div />
  200. ) : hasNewVersion ? (
  201. <Link href={UPDATE_URL} target="_blank" className="link">
  202. {Locale.Settings.Update.GoToUpdate}
  203. </Link>
  204. ) : (
  205. <IconButton
  206. icon={<ResetIcon></ResetIcon>}
  207. text={Locale.Settings.Update.CheckUpdate}
  208. onClick={() => checkUpdate(true)}
  209. />
  210. )}
  211. </SettingItem>
  212. <SettingItem title={Locale.Settings.SendKey}>
  213. <select
  214. value={config.submitKey}
  215. onChange={(e) => {
  216. updateConfig(
  217. (config) =>
  218. (config.submitKey = e.target.value as any as SubmitKey),
  219. );
  220. }}
  221. >
  222. {Object.values(SubmitKey).map((v) => (
  223. <option value={v} key={v}>
  224. {v}
  225. </option>
  226. ))}
  227. </select>
  228. </SettingItem>
  229. <ListItem>
  230. <div className={styles["settings-title"]}>
  231. {Locale.Settings.Theme}
  232. </div>
  233. <select
  234. value={config.theme}
  235. onChange={(e) => {
  236. updateConfig(
  237. (config) => (config.theme = e.target.value as any as Theme),
  238. );
  239. }}
  240. >
  241. {Object.values(Theme).map((v) => (
  242. <option value={v} key={v}>
  243. {v}
  244. </option>
  245. ))}
  246. </select>
  247. </ListItem>
  248. <SettingItem title={Locale.Settings.Lang.Name}>
  249. <select
  250. value={getLang()}
  251. onChange={(e) => {
  252. changeLang(e.target.value as any);
  253. }}
  254. >
  255. {AllLangs.map((lang) => (
  256. <option value={lang} key={lang}>
  257. {Locale.Settings.Lang.Options[lang]}
  258. </option>
  259. ))}
  260. </select>
  261. </SettingItem>
  262. <SettingItem
  263. title={Locale.Settings.FontSize.Title}
  264. subTitle={Locale.Settings.FontSize.SubTitle}
  265. >
  266. <InputRange
  267. title={`${config.fontSize ?? 14}px`}
  268. value={config.fontSize}
  269. min="12"
  270. max="18"
  271. step="1"
  272. onChange={(e) =>
  273. updateConfig(
  274. (config) =>
  275. (config.fontSize = Number.parseInt(e.currentTarget.value)),
  276. )
  277. }
  278. ></InputRange>
  279. </SettingItem>
  280. <SettingItem title={Locale.Settings.TightBorder}>
  281. <input
  282. type="checkbox"
  283. checked={config.tightBorder}
  284. onChange={(e) =>
  285. updateConfig(
  286. (config) => (config.tightBorder = e.currentTarget.checked),
  287. )
  288. }
  289. ></input>
  290. </SettingItem>
  291. <SettingItem title={Locale.Settings.SendPreviewBubble}>
  292. <input
  293. type="checkbox"
  294. checked={config.sendPreviewBubble}
  295. onChange={(e) =>
  296. updateConfig(
  297. (config) =>
  298. (config.sendPreviewBubble = e.currentTarget.checked),
  299. )
  300. }
  301. ></input>
  302. </SettingItem>
  303. </List>
  304. <List>
  305. <SettingItem
  306. title={Locale.Settings.Prompt.Disable.Title}
  307. subTitle={Locale.Settings.Prompt.Disable.SubTitle}
  308. >
  309. <input
  310. type="checkbox"
  311. checked={config.disablePromptHint}
  312. onChange={(e) =>
  313. updateConfig(
  314. (config) =>
  315. (config.disablePromptHint = e.currentTarget.checked),
  316. )
  317. }
  318. ></input>
  319. </SettingItem>
  320. <SettingItem
  321. title={Locale.Settings.Prompt.List}
  322. subTitle={Locale.Settings.Prompt.ListCount(
  323. builtinCount,
  324. customCount,
  325. )}
  326. >
  327. <IconButton
  328. icon={<EditIcon />}
  329. text={Locale.Settings.Prompt.Edit}
  330. onClick={() => showToast(Locale.WIP)}
  331. />
  332. </SettingItem>
  333. </List>
  334. <List>
  335. {enabledAccessControl ? (
  336. <SettingItem
  337. title={Locale.Settings.AccessCode.Title}
  338. subTitle={Locale.Settings.AccessCode.SubTitle}
  339. >
  340. <PasswordInput
  341. value={accessStore.accessCode}
  342. type="text"
  343. placeholder={Locale.Settings.AccessCode.Placeholder}
  344. onChange={(e) => {
  345. accessStore.updateCode(e.currentTarget.value);
  346. }}
  347. />
  348. </SettingItem>
  349. ) : (
  350. <></>
  351. )}
  352. <SettingItem
  353. title={Locale.Settings.Token.Title}
  354. subTitle={Locale.Settings.Token.SubTitle}
  355. >
  356. <PasswordInput
  357. value={accessStore.token}
  358. type="text"
  359. placeholder={Locale.Settings.Token.Placeholder}
  360. onChange={(e) => {
  361. accessStore.updateToken(e.currentTarget.value);
  362. }}
  363. />
  364. </SettingItem>
  365. <SettingItem
  366. title={Locale.Settings.Usage.Title}
  367. subTitle={
  368. showUsage
  369. ? loadingUsage
  370. ? Locale.Settings.Usage.IsChecking
  371. : Locale.Settings.Usage.SubTitle(
  372. usage?.used ?? "[?]",
  373. usage?.subscription ?? "[?]",
  374. )
  375. : Locale.Settings.Usage.NoAccess
  376. }
  377. >
  378. {!showUsage || loadingUsage ? (
  379. <div />
  380. ) : (
  381. <IconButton
  382. icon={<ResetIcon></ResetIcon>}
  383. text={Locale.Settings.Usage.Check}
  384. onClick={checkUsage}
  385. />
  386. )}
  387. </SettingItem>
  388. <SettingItem
  389. title={Locale.Settings.HistoryCount.Title}
  390. subTitle={Locale.Settings.HistoryCount.SubTitle}
  391. >
  392. <InputRange
  393. title={config.historyMessageCount.toString()}
  394. value={config.historyMessageCount}
  395. min="0"
  396. max="25"
  397. step="1"
  398. onChange={(e) =>
  399. updateConfig(
  400. (config) =>
  401. (config.historyMessageCount = e.target.valueAsNumber),
  402. )
  403. }
  404. ></InputRange>
  405. </SettingItem>
  406. <SettingItem
  407. title={Locale.Settings.CompressThreshold.Title}
  408. subTitle={Locale.Settings.CompressThreshold.SubTitle}
  409. >
  410. <input
  411. type="number"
  412. min={500}
  413. max={4000}
  414. value={config.compressMessageLengthThreshold}
  415. onChange={(e) =>
  416. updateConfig(
  417. (config) =>
  418. (config.compressMessageLengthThreshold =
  419. e.currentTarget.valueAsNumber),
  420. )
  421. }
  422. ></input>
  423. </SettingItem>
  424. </List>
  425. <List>
  426. <SettingItem title={Locale.Settings.Model}>
  427. <select
  428. value={config.modelConfig.model}
  429. onChange={(e) => {
  430. updateConfig(
  431. (config) =>
  432. (config.modelConfig.model = ModalConfigValidator.model(
  433. e.currentTarget.value,
  434. )),
  435. );
  436. }}
  437. >
  438. {ALL_MODELS.map((v) => (
  439. <option value={v.name} key={v.name} disabled={!v.available}>
  440. {v.name}
  441. </option>
  442. ))}
  443. </select>
  444. </SettingItem>
  445. <SettingItem
  446. title={Locale.Settings.Temperature.Title}
  447. subTitle={Locale.Settings.Temperature.SubTitle}
  448. >
  449. <InputRange
  450. value={config.modelConfig.temperature?.toFixed(1)}
  451. min="0"
  452. max="2"
  453. step="0.1"
  454. onChange={(e) => {
  455. updateConfig(
  456. (config) =>
  457. (config.modelConfig.temperature =
  458. ModalConfigValidator.temperature(
  459. e.currentTarget.valueAsNumber,
  460. )),
  461. );
  462. }}
  463. ></InputRange>
  464. </SettingItem>
  465. <SettingItem
  466. title={Locale.Settings.MaxTokens.Title}
  467. subTitle={Locale.Settings.MaxTokens.SubTitle}
  468. >
  469. <input
  470. type="number"
  471. min={100}
  472. max={32000}
  473. value={config.modelConfig.max_tokens}
  474. onChange={(e) =>
  475. updateConfig(
  476. (config) =>
  477. (config.modelConfig.max_tokens =
  478. ModalConfigValidator.max_tokens(
  479. e.currentTarget.valueAsNumber,
  480. )),
  481. )
  482. }
  483. ></input>
  484. </SettingItem>
  485. <SettingItem
  486. title={Locale.Settings.PresencePenlty.Title}
  487. subTitle={Locale.Settings.PresencePenlty.SubTitle}
  488. >
  489. <InputRange
  490. value={config.modelConfig.presence_penalty?.toFixed(1)}
  491. min="-2"
  492. max="2"
  493. step="0.5"
  494. onChange={(e) => {
  495. updateConfig(
  496. (config) =>
  497. (config.modelConfig.presence_penalty =
  498. ModalConfigValidator.presence_penalty(
  499. e.currentTarget.valueAsNumber,
  500. )),
  501. );
  502. }}
  503. ></InputRange>
  504. </SettingItem>
  505. </List>
  506. </div>
  507. </ErrorBoundary>
  508. );
  509. }