settings.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. import { useState, useEffect, useRef, useMemo } 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 { List, ListItem, Popover, showToast } from "./ui-lib";
  9. import { IconButton } from "./button";
  10. import {
  11. SubmitKey,
  12. useChatStore,
  13. Theme,
  14. ALL_MODELS,
  15. useUpdateStore,
  16. useAccessStore,
  17. } from "../store";
  18. import { Avatar, PromptHints } from "./home";
  19. import Locale, { AllLangs, changeLang, getLang } from "../locales";
  20. import { getCurrentCommitId } from "../utils";
  21. import Link from "next/link";
  22. import { UPDATE_URL } from "../constant";
  23. import { SearchService, usePromptStore } from "../store/prompt";
  24. function SettingItem(props: {
  25. title: string;
  26. subTitle?: string;
  27. children: JSX.Element;
  28. }) {
  29. return (
  30. <ListItem>
  31. <div className={styles["settings-title"]}>
  32. <div>{props.title}</div>
  33. {props.subTitle && (
  34. <div className={styles["settings-sub-title"]}>{props.subTitle}</div>
  35. )}
  36. </div>
  37. {props.children}
  38. </ListItem>
  39. );
  40. }
  41. export function Settings(props: { closeSettings: () => void }) {
  42. const [showEmojiPicker, setShowEmojiPicker] = useState(false);
  43. const [config, updateConfig, resetConfig, clearAllData] = useChatStore(
  44. (state) => [
  45. state.config,
  46. state.updateConfig,
  47. state.resetConfig,
  48. state.clearAllData,
  49. ]
  50. );
  51. const updateStore = useUpdateStore();
  52. const [checkingUpdate, setCheckingUpdate] = useState(false);
  53. const currentId = getCurrentCommitId();
  54. const remoteId = updateStore.remoteId;
  55. const hasNewVersion = currentId !== remoteId;
  56. function checkUpdate(force = false) {
  57. setCheckingUpdate(true);
  58. updateStore.getLatestCommitId(force).then(() => {
  59. setCheckingUpdate(false);
  60. });
  61. }
  62. useEffect(() => {
  63. checkUpdate();
  64. }, []);
  65. const accessStore = useAccessStore();
  66. const enabledAccessControl = useMemo(
  67. () => accessStore.enabledAccessControl(),
  68. []
  69. );
  70. const promptStore = usePromptStore();
  71. const builtinCount = SearchService.count.builtin;
  72. const customCount = promptStore.prompts.size ?? 0;
  73. return (
  74. <>
  75. <div className={styles["window-header"]}>
  76. <div className={styles["window-header-title"]}>
  77. <div className={styles["window-header-main-title"]}>
  78. {Locale.Settings.Title}
  79. </div>
  80. <div className={styles["window-header-sub-title"]}>
  81. {Locale.Settings.SubTitle}
  82. </div>
  83. </div>
  84. <div className={styles["window-actions"]}>
  85. <div className={styles["window-action-button"]}>
  86. <IconButton
  87. icon={<ClearIcon />}
  88. onClick={clearAllData}
  89. bordered
  90. title={Locale.Settings.Actions.ClearAll}
  91. />
  92. </div>
  93. <div className={styles["window-action-button"]}>
  94. <IconButton
  95. icon={<ResetIcon />}
  96. onClick={resetConfig}
  97. bordered
  98. title={Locale.Settings.Actions.ResetAll}
  99. />
  100. </div>
  101. <div className={styles["window-action-button"]}>
  102. <IconButton
  103. icon={<CloseIcon />}
  104. onClick={props.closeSettings}
  105. bordered
  106. title={Locale.Settings.Actions.Close}
  107. />
  108. </div>
  109. </div>
  110. </div>
  111. <div className={styles["settings"]}>
  112. <List>
  113. <SettingItem title={Locale.Settings.Avatar}>
  114. <Popover
  115. onClose={() => setShowEmojiPicker(false)}
  116. content={
  117. <EmojiPicker
  118. lazyLoadEmojis
  119. theme={EmojiTheme.AUTO}
  120. onEmojiClick={(e) => {
  121. updateConfig((config) => (config.avatar = e.unified));
  122. setShowEmojiPicker(false);
  123. }}
  124. />
  125. }
  126. open={showEmojiPicker}
  127. >
  128. <div
  129. className={styles.avatar}
  130. onClick={() => setShowEmojiPicker(true)}
  131. >
  132. <Avatar role="user" />
  133. </div>
  134. </Popover>
  135. </SettingItem>
  136. <SettingItem
  137. title={Locale.Settings.Update.Version(currentId)}
  138. subTitle={
  139. checkingUpdate
  140. ? Locale.Settings.Update.IsChecking
  141. : hasNewVersion
  142. ? Locale.Settings.Update.FoundUpdate(remoteId ?? "ERROR")
  143. : Locale.Settings.Update.IsLatest
  144. }
  145. >
  146. {checkingUpdate ? (
  147. <div />
  148. ) : hasNewVersion ? (
  149. <Link href={UPDATE_URL} target="_blank" className="link">
  150. {Locale.Settings.Update.GoToUpdate}
  151. </Link>
  152. ) : (
  153. <IconButton
  154. icon={<ResetIcon></ResetIcon>}
  155. text={Locale.Settings.Update.CheckUpdate}
  156. onClick={() => checkUpdate(true)}
  157. />
  158. )}
  159. </SettingItem>
  160. <SettingItem title={Locale.Settings.SendKey}>
  161. <select
  162. value={config.submitKey}
  163. onChange={(e) => {
  164. updateConfig(
  165. (config) =>
  166. (config.submitKey = e.target.value as any as SubmitKey)
  167. );
  168. }}
  169. >
  170. {Object.values(SubmitKey).map((v) => (
  171. <option value={v} key={v}>
  172. {v}
  173. </option>
  174. ))}
  175. </select>
  176. </SettingItem>
  177. <ListItem>
  178. <div className={styles["settings-title"]}>
  179. {Locale.Settings.Theme}
  180. </div>
  181. <select
  182. value={config.theme}
  183. onChange={(e) => {
  184. updateConfig(
  185. (config) => (config.theme = e.target.value as any as Theme)
  186. );
  187. }}
  188. >
  189. {Object.values(Theme).map((v) => (
  190. <option value={v} key={v}>
  191. {v}
  192. </option>
  193. ))}
  194. </select>
  195. </ListItem>
  196. <SettingItem title={Locale.Settings.Lang.Name}>
  197. <select
  198. value={getLang()}
  199. onChange={(e) => {
  200. changeLang(e.target.value as any);
  201. }}
  202. >
  203. {AllLangs.map((lang) => (
  204. <option value={lang} key={lang}>
  205. {Locale.Settings.Lang.Options[lang]}
  206. </option>
  207. ))}
  208. </select>
  209. </SettingItem>
  210. <SettingItem
  211. title={Locale.Settings.FontSize.Title}
  212. subTitle={Locale.Settings.FontSize.SubTitle}
  213. >
  214. <input
  215. type="range"
  216. title={config.fontSize.toString() + 'px'}
  217. value={config.fontSize}
  218. min="12"
  219. max="18"
  220. step="1"
  221. onChange={(e) =>
  222. updateConfig(
  223. (config) =>
  224. (config.fontSize = Number.parseInt(e.currentTarget.value))
  225. )
  226. }
  227. ></input>
  228. </SettingItem>
  229. <div className="no-mobile">
  230. <SettingItem title={Locale.Settings.TightBorder}>
  231. <input
  232. type="checkbox"
  233. checked={config.tightBorder}
  234. onChange={(e) =>
  235. updateConfig(
  236. (config) => (config.tightBorder = e.currentTarget.checked)
  237. )
  238. }
  239. ></input>
  240. </SettingItem>
  241. </div>
  242. </List>
  243. <List>
  244. <SettingItem
  245. title={Locale.Settings.Prompt.Disable.Title}
  246. subTitle={Locale.Settings.Prompt.Disable.SubTitle}
  247. >
  248. <input
  249. type="checkbox"
  250. checked={config.disablePromptHint}
  251. onChange={(e) =>
  252. updateConfig(
  253. (config) =>
  254. (config.disablePromptHint = e.currentTarget.checked)
  255. )
  256. }
  257. ></input>
  258. </SettingItem>
  259. <SettingItem
  260. title={Locale.Settings.Prompt.List}
  261. subTitle={Locale.Settings.Prompt.ListCount(
  262. builtinCount,
  263. customCount
  264. )}
  265. >
  266. <IconButton
  267. icon={<EditIcon />}
  268. text={Locale.Settings.Prompt.Edit}
  269. onClick={() => showToast(Locale.WIP)}
  270. />
  271. </SettingItem>
  272. </List>
  273. <List>
  274. {enabledAccessControl ? (
  275. <SettingItem
  276. title={Locale.Settings.AccessCode.Title}
  277. subTitle={Locale.Settings.AccessCode.SubTitle}
  278. >
  279. <input
  280. value={accessStore.accessCode}
  281. type="text"
  282. placeholder={Locale.Settings.AccessCode.Placeholder}
  283. onChange={(e) => {
  284. accessStore.updateCode(e.currentTarget.value);
  285. }}
  286. ></input>
  287. </SettingItem>
  288. ) : (
  289. <></>
  290. )}
  291. <SettingItem
  292. title={Locale.Settings.Token.Title}
  293. subTitle={Locale.Settings.Token.SubTitle}
  294. >
  295. <input
  296. value={accessStore.token}
  297. type="text"
  298. placeholder={Locale.Settings.Token.Placeholder}
  299. onChange={(e) => {
  300. accessStore.updateToken(e.currentTarget.value);
  301. }}
  302. ></input>
  303. </SettingItem>
  304. <SettingItem
  305. title={Locale.Settings.HistoryCount.Title}
  306. subTitle={Locale.Settings.HistoryCount.SubTitle}
  307. >
  308. <input
  309. type="range"
  310. title={config.historyMessageCount.toString()}
  311. value={config.historyMessageCount}
  312. min="2"
  313. max="25"
  314. step="2"
  315. onChange={(e) =>
  316. updateConfig(
  317. (config) =>
  318. (config.historyMessageCount = e.target.valueAsNumber)
  319. )
  320. }
  321. ></input>
  322. </SettingItem>
  323. <SettingItem
  324. title={Locale.Settings.CompressThreshold.Title}
  325. subTitle={Locale.Settings.CompressThreshold.SubTitle}
  326. >
  327. <input
  328. type="number"
  329. min={500}
  330. max={4000}
  331. value={config.compressMessageLengthThreshold}
  332. onChange={(e) =>
  333. updateConfig(
  334. (config) =>
  335. (config.compressMessageLengthThreshold =
  336. e.currentTarget.valueAsNumber)
  337. )
  338. }
  339. ></input>
  340. </SettingItem>
  341. </List>
  342. <List>
  343. <SettingItem title={Locale.Settings.Model}>
  344. <select
  345. value={config.modelConfig.model}
  346. onChange={(e) => {
  347. updateConfig(
  348. (config) => (config.modelConfig.model = e.currentTarget.value)
  349. );
  350. }}
  351. >
  352. {ALL_MODELS.map((v) => (
  353. <option value={v.name} key={v.name} disabled={!v.available}>
  354. {v.name}
  355. </option>
  356. ))}
  357. </select>
  358. </SettingItem>
  359. <SettingItem
  360. title={Locale.Settings.Temperature.Title}
  361. subTitle={Locale.Settings.Temperature.SubTitle}
  362. >
  363. <input
  364. type="range"
  365. value={config.modelConfig.temperature.toFixed(1)}
  366. min="0"
  367. max="1"
  368. step="0.1"
  369. onChange={(e) => {
  370. updateConfig(
  371. (config) =>
  372. (config.modelConfig.temperature =
  373. e.currentTarget.valueAsNumber)
  374. );
  375. }}
  376. ></input>
  377. </SettingItem>
  378. <SettingItem
  379. title={Locale.Settings.MaxTokens.Title}
  380. subTitle={Locale.Settings.MaxTokens.SubTitle}
  381. >
  382. <input
  383. type="number"
  384. min={100}
  385. max={4096}
  386. value={config.modelConfig.max_tokens}
  387. onChange={(e) =>
  388. updateConfig(
  389. (config) =>
  390. (config.modelConfig.max_tokens =
  391. e.currentTarget.valueAsNumber)
  392. )
  393. }
  394. ></input>
  395. </SettingItem>
  396. <SettingItem
  397. title={Locale.Settings.PresencePenlty.Title}
  398. subTitle={Locale.Settings.PresencePenlty.SubTitle}
  399. >
  400. <input
  401. type="range"
  402. value={config.modelConfig.presence_penalty.toFixed(1)}
  403. min="-2"
  404. max="2"
  405. step="0.5"
  406. onChange={(e) => {
  407. updateConfig(
  408. (config) =>
  409. (config.modelConfig.presence_penalty =
  410. e.currentTarget.valueAsNumber)
  411. );
  412. }}
  413. ></input>
  414. </SettingItem>
  415. </List>
  416. </div>
  417. </>
  418. );
  419. }