settings.tsx 8.9 KB

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