settings.tsx 10 KB

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