import { useState, useEffect } from "react"; import EmojiPicker, { Theme as EmojiTheme } from "emoji-picker-react"; import styles from "./settings.module.scss"; import ResetIcon from "../icons/reload.svg"; import CloseIcon from "../icons/close.svg"; import ClearIcon from "../icons/clear.svg"; import { List, ListItem, Popover } from "./ui-lib"; import { IconButton } from "./button"; import { SubmitKey, useChatStore, Theme, ALL_MODELS } from "../store"; import { Avatar } from "./home"; import Locale, { changeLang, getLang } from "../locales"; import { checkUpstreamLatestCommitId, getCurrentCommitId } from "../utils"; import Link from "next/link"; import { UPDATE_URL } from "../constant"; function SettingItem(props: { title: string; subTitle?: string; children: JSX.Element; }) { return ( {props.title} {props.subTitle && ( {props.subTitle} )} {props.children} ); } export function Settings(props: { closeSettings: () => void }) { const [showEmojiPicker, setShowEmojiPicker] = useState(false); const [config, updateConfig, resetConfig, clearAllData] = useChatStore( (state) => [ state.config, state.updateConfig, state.resetConfig, state.clearAllData, ] ); const currentId = getCurrentCommitId(); const [checkingUpdate, setCheckingUpdate] = useState(false); const [remoteId, setRemoteId] = useState(); const hasNewVersion = currentId !== remoteId; function checkUpdate(force = false) { setCheckingUpdate(true); checkUpstreamLatestCommitId(force).then((id) => { setRemoteId(id); setCheckingUpdate(false); }); } useEffect(() => { checkUpdate(); }, []); return ( <> {Locale.Settings.Title} {Locale.Settings.SubTitle} } onClick={clearAllData} bordered title={Locale.Settings.Actions.ClearAll} /> } onClick={resetConfig} bordered title={Locale.Settings.Actions.ResetAll} /> } onClick={props.closeSettings} bordered title={Locale.Settings.Actions.Close} /> setShowEmojiPicker(false)} content={ { updateConfig((config) => (config.avatar = e.unified)); setShowEmojiPicker(false); }} /> } open={showEmojiPicker} > setShowEmojiPicker(true)} > {checkingUpdate ? ( ) : hasNewVersion ? ( {Locale.Settings.Update.GoToUpdate} ) : ( } text={Locale.Settings.Update.CheckUpdate} onClick={() => checkUpdate(true)} /> )} { updateConfig( (config) => (config.submitKey = e.target.value as any as SubmitKey) ); }} > {Object.values(SubmitKey).map((v) => ( {v} ))} {Locale.Settings.Theme} { updateConfig( (config) => (config.theme = e.target.value as any as Theme) ); }} > {Object.values(Theme).map((v) => ( {v} ))} { changeLang(e.target.value as any); }} > {Locale.Settings.Lang.Options.en} {Locale.Settings.Lang.Options.cn} updateConfig( (config) => (config.tightBorder = e.currentTarget.checked) ) } > updateConfig( (config) => (config.historyMessageCount = e.target.valueAsNumber) ) } > updateConfig( (config) => (config.compressMessageLengthThreshold = e.currentTarget.valueAsNumber) ) } > { updateConfig( (config) => (config.modelConfig.model = e.currentTarget.value) ); }} > {ALL_MODELS.map((v) => ( {v.name} ))} { updateConfig( (config) => (config.modelConfig.temperature = e.currentTarget.valueAsNumber) ); }} > updateConfig( (config) => (config.modelConfig.max_tokens = e.currentTarget.valueAsNumber) ) } > { updateConfig( (config) => (config.modelConfig.presence_penalty = e.currentTarget.valueAsNumber) ); }} > > ); }