settings.tsx 12 KB

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