settings.tsx 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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.Lang.Name}>
  139. <div className="">
  140. <select
  141. value={getLang()}
  142. onChange={(e) => {
  143. changeLang(e.target.value as any);
  144. }}
  145. >
  146. <option value="en" key="en">
  147. {Locale.Settings.Lang.Options.en}
  148. </option>
  149. <option value="cn" key="cn">
  150. {Locale.Settings.Lang.Options.cn}
  151. </option>
  152. </select>
  153. </div>
  154. </SettingItem>
  155. <div className="no-mobile">
  156. <SettingItem title={Locale.Settings.TightBorder}>
  157. <input
  158. type="checkbox"
  159. checked={config.tightBorder}
  160. onChange={(e) =>
  161. updateConfig(
  162. (config) => (config.tightBorder = e.currentTarget.checked)
  163. )
  164. }
  165. ></input>
  166. </SettingItem>
  167. </div>
  168. </List>
  169. <List>
  170. <SettingItem
  171. title={Locale.Settings.HistoryCount.Title}
  172. subTitle={Locale.Settings.HistoryCount.SubTitle}
  173. >
  174. <input
  175. type="range"
  176. title={config.historyMessageCount.toString()}
  177. value={config.historyMessageCount}
  178. min="2"
  179. max="25"
  180. step="2"
  181. onChange={(e) =>
  182. updateConfig(
  183. (config) =>
  184. (config.historyMessageCount = e.target.valueAsNumber)
  185. )
  186. }
  187. ></input>
  188. </SettingItem>
  189. <SettingItem
  190. title={Locale.Settings.CompressThreshold.Title}
  191. subTitle={Locale.Settings.CompressThreshold.SubTitle}
  192. >
  193. <input
  194. type="number"
  195. min={500}
  196. max={4000}
  197. value={config.compressMessageLengthThreshold}
  198. onChange={(e) =>
  199. updateConfig(
  200. (config) =>
  201. (config.compressMessageLengthThreshold =
  202. e.currentTarget.valueAsNumber)
  203. )
  204. }
  205. ></input>
  206. </SettingItem>
  207. </List>
  208. <List>
  209. <SettingItem title={Locale.Settings.Model}>
  210. <select
  211. value={config.modelConfig.model}
  212. onChange={(e) => {
  213. updateConfig(
  214. (config) => (config.modelConfig.model = e.currentTarget.value)
  215. );
  216. }}
  217. >
  218. {ALL_MODELS.map((v) => (
  219. <option value={v.name} key={v.name} disabled={!v.available}>
  220. {v.name}
  221. </option>
  222. ))}
  223. </select>
  224. </SettingItem>
  225. <SettingItem
  226. title={Locale.Settings.Temperature.Title}
  227. subTitle={Locale.Settings.Temperature.SubTitle}
  228. >
  229. <input
  230. type="range"
  231. value={config.modelConfig.temperature.toFixed(1)}
  232. min="0"
  233. max="1"
  234. step="0.1"
  235. onChange={(e) => {
  236. updateConfig(
  237. (config) =>
  238. (config.modelConfig.temperature =
  239. e.currentTarget.valueAsNumber)
  240. );
  241. }}
  242. ></input>
  243. </SettingItem>
  244. <SettingItem
  245. title={Locale.Settings.MaxTokens.Title}
  246. subTitle={Locale.Settings.MaxTokens.SubTitle}
  247. >
  248. <input
  249. type="number"
  250. min={100}
  251. max={4000}
  252. value={config.modelConfig.max_tokens}
  253. onChange={(e) =>
  254. updateConfig(
  255. (config) =>
  256. (config.modelConfig.max_tokens =
  257. e.currentTarget.valueAsNumber)
  258. )
  259. }
  260. ></input>
  261. </SettingItem>
  262. <SettingItem
  263. title={Locale.Settings.PresencePenlty.Title}
  264. subTitle={Locale.Settings.PresencePenlty.SubTitle}
  265. >
  266. <input
  267. type="range"
  268. value={config.modelConfig.presence_penalty.toFixed(1)}
  269. min="-2"
  270. max="2"
  271. step="0.5"
  272. onChange={(e) => {
  273. updateConfig(
  274. (config) =>
  275. (config.modelConfig.presence_penalty =
  276. e.currentTarget.valueAsNumber)
  277. );
  278. }}
  279. ></input>
  280. </SettingItem>
  281. </List>
  282. </div>
  283. </>
  284. );
  285. }