settings.tsx 12 KB

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