settings.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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. <div className="no-mobile">
  211. <SettingItem title={Locale.Settings.TightBorder}>
  212. <input
  213. type="checkbox"
  214. checked={config.tightBorder}
  215. onChange={(e) =>
  216. updateConfig(
  217. (config) => (config.tightBorder = e.currentTarget.checked)
  218. )
  219. }
  220. ></input>
  221. </SettingItem>
  222. </div>
  223. </List>
  224. <List>
  225. <SettingItem
  226. title={Locale.Settings.Prompt.Disable.Title}
  227. subTitle={Locale.Settings.Prompt.Disable.SubTitle}
  228. >
  229. <input
  230. type="checkbox"
  231. checked={config.disablePromptHint}
  232. onChange={(e) =>
  233. updateConfig(
  234. (config) =>
  235. (config.disablePromptHint = e.currentTarget.checked)
  236. )
  237. }
  238. ></input>
  239. </SettingItem>
  240. <SettingItem
  241. title={Locale.Settings.Prompt.List}
  242. subTitle={Locale.Settings.Prompt.ListCount(
  243. builtinCount,
  244. customCount
  245. )}
  246. >
  247. <IconButton
  248. icon={<EditIcon />}
  249. text={Locale.Settings.Prompt.Edit}
  250. onClick={() => showToast(Locale.WIP)}
  251. />
  252. </SettingItem>
  253. </List>
  254. <List>
  255. {enabledAccessControl ? (
  256. <SettingItem
  257. title={Locale.Settings.AccessCode.Title}
  258. subTitle={Locale.Settings.AccessCode.SubTitle}
  259. >
  260. <input
  261. value={accessStore.accessCode}
  262. type="text"
  263. placeholder={Locale.Settings.AccessCode.Placeholder}
  264. onChange={(e) => {
  265. accessStore.updateCode(e.currentTarget.value);
  266. }}
  267. ></input>
  268. </SettingItem>
  269. ) : (
  270. <></>
  271. )}
  272. <SettingItem
  273. title={Locale.Settings.Token.Title}
  274. subTitle={Locale.Settings.Token.SubTitle}
  275. >
  276. <input
  277. value={accessStore.token}
  278. type="text"
  279. placeholder={Locale.Settings.Token.Placeholder}
  280. onChange={(e) => {
  281. accessStore.updateToken(e.currentTarget.value);
  282. }}
  283. ></input>
  284. </SettingItem>
  285. <SettingItem
  286. title={Locale.Settings.HistoryCount.Title}
  287. subTitle={Locale.Settings.HistoryCount.SubTitle}
  288. >
  289. <input
  290. type="range"
  291. title={config.historyMessageCount.toString()}
  292. value={config.historyMessageCount}
  293. min="2"
  294. max="25"
  295. step="2"
  296. onChange={(e) =>
  297. updateConfig(
  298. (config) =>
  299. (config.historyMessageCount = e.target.valueAsNumber)
  300. )
  301. }
  302. ></input>
  303. </SettingItem>
  304. <SettingItem
  305. title={Locale.Settings.CompressThreshold.Title}
  306. subTitle={Locale.Settings.CompressThreshold.SubTitle}
  307. >
  308. <input
  309. type="number"
  310. min={500}
  311. max={4000}
  312. value={config.compressMessageLengthThreshold}
  313. onChange={(e) =>
  314. updateConfig(
  315. (config) =>
  316. (config.compressMessageLengthThreshold =
  317. e.currentTarget.valueAsNumber)
  318. )
  319. }
  320. ></input>
  321. </SettingItem>
  322. </List>
  323. <List>
  324. <SettingItem title={Locale.Settings.Model}>
  325. <select
  326. value={config.modelConfig.model}
  327. onChange={(e) => {
  328. updateConfig(
  329. (config) => (config.modelConfig.model = e.currentTarget.value)
  330. );
  331. }}
  332. >
  333. {ALL_MODELS.map((v) => (
  334. <option value={v.name} key={v.name} disabled={!v.available}>
  335. {v.name}
  336. </option>
  337. ))}
  338. </select>
  339. </SettingItem>
  340. <SettingItem
  341. title={Locale.Settings.Temperature.Title}
  342. subTitle={Locale.Settings.Temperature.SubTitle}
  343. >
  344. <input
  345. type="range"
  346. value={config.modelConfig.temperature.toFixed(1)}
  347. min="0"
  348. max="1"
  349. step="0.1"
  350. onChange={(e) => {
  351. updateConfig(
  352. (config) =>
  353. (config.modelConfig.temperature =
  354. e.currentTarget.valueAsNumber)
  355. );
  356. }}
  357. ></input>
  358. </SettingItem>
  359. <SettingItem
  360. title={Locale.Settings.MaxTokens.Title}
  361. subTitle={Locale.Settings.MaxTokens.SubTitle}
  362. >
  363. <input
  364. type="number"
  365. min={100}
  366. max={4096}
  367. value={config.modelConfig.max_tokens}
  368. onChange={(e) =>
  369. updateConfig(
  370. (config) =>
  371. (config.modelConfig.max_tokens =
  372. e.currentTarget.valueAsNumber)
  373. )
  374. }
  375. ></input>
  376. </SettingItem>
  377. <SettingItem
  378. title={Locale.Settings.PresencePenlty.Title}
  379. subTitle={Locale.Settings.PresencePenlty.SubTitle}
  380. >
  381. <input
  382. type="range"
  383. value={config.modelConfig.presence_penalty.toFixed(1)}
  384. min="-2"
  385. max="2"
  386. step="0.5"
  387. onChange={(e) => {
  388. updateConfig(
  389. (config) =>
  390. (config.modelConfig.presence_penalty =
  391. e.currentTarget.valueAsNumber)
  392. );
  393. }}
  394. ></input>
  395. </SettingItem>
  396. </List>
  397. </div>
  398. </>
  399. );
  400. }