import { useState, useEffect, useMemo, HTMLProps, useRef } 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 CopyIcon from "../icons/copy.svg"; import ClearIcon from "../icons/clear.svg"; import EditIcon from "../icons/edit.svg"; import EyeIcon from "../icons/eye.svg"; import EyeOffIcon from "../icons/eye-off.svg"; import { Input, List, ListItem, Modal, Popover } from "./ui-lib"; import { IconButton } from "./button"; import { SubmitKey, useChatStore, Theme, ALL_MODELS, useUpdateStore, useAccessStore, ModalConfigValidator, } from "../store"; import { Avatar } from "./chat"; import Locale, { AllLangs, changeLang, getLang } from "../locales"; import { copyToClipboard, getEmojiUrl } from "../utils"; import Link from "next/link"; import { UPDATE_URL } from "../constant"; import { Prompt, SearchService, usePromptStore } from "../store/prompt"; import { ErrorBoundary } from "./error"; import { InputRange } from "./input-range"; function UserPromptModal(props: { onClose?: () => void }) { const promptStore = usePromptStore(); const userPrompts = promptStore.getUserPrompts(); const builtinPrompts = SearchService.builtinPrompts; const allPrompts = userPrompts.concat(builtinPrompts); const [searchInput, setSearchInput] = useState(""); const [searchPrompts, setSearchPrompts] = useState([]); const prompts = searchInput.length > 0 ? searchPrompts : allPrompts; useEffect(() => { if (searchInput.length > 0) { const searchResult = SearchService.search(searchInput); setSearchPrompts(searchResult); } else { setSearchPrompts([]); } }, [searchInput]); return (
props.onClose?.()} actions={[ promptStore.add({ title: "", content: "" })} icon={} bordered text={Locale.Settings.Prompt.Modal.Add} />, ]} >
setSearchInput(e.currentTarget.value)} >
{prompts.map((v, _) => (
{ if (v.isUser) { promptStore.updateUserPrompts( v.id!, (prompt) => (prompt.title = e.currentTarget.value), ); } }} >
{v.isUser && ( } bordered className={styles["user-prompt-button"]} onClick={() => promptStore.remove(v.id!)} /> )} } bordered className={styles["user-prompt-button"]} onClick={() => copyToClipboard(v.content)} />
{ if (v.isUser) { promptStore.updateUserPrompts( v.id!, (prompt) => (prompt.content = e.currentTarget.value), ); } }} />
))}
); } function SettingItem(props: { title: string; subTitle?: string; children: JSX.Element; }) { return (
{props.title}
{props.subTitle && (
{props.subTitle}
)}
{props.children}
); } function PasswordInput(props: HTMLProps) { const [visible, setVisible] = useState(false); function changeVisibility() { setVisible(!visible); } return (
: } onClick={changeVisibility} className={styles["password-eye"]} />
); } export function Settings(props: { closeSettings: () => void }) { const [showEmojiPicker, setShowEmojiPicker] = useState(false); const [config, updateConfig, resetConfig, clearAllData, clearSessions] = useChatStore((state) => [ state.config, state.updateConfig, state.resetConfig, state.clearAllData, state.clearSessions, ]); const updateStore = useUpdateStore(); const [checkingUpdate, setCheckingUpdate] = useState(false); const currentVersion = updateStore.version; const remoteId = updateStore.remoteVersion; const hasNewVersion = currentVersion !== remoteId; function checkUpdate(force = false) { setCheckingUpdate(true); updateStore.getLatestVersion(force).then(() => { setCheckingUpdate(false); }); } const usage = { used: updateStore.used, subscription: updateStore.subscription, }; const [loadingUsage, setLoadingUsage] = useState(false); function checkUsage() { setLoadingUsage(true); updateStore.updateUsage().finally(() => { setLoadingUsage(false); }); } const accessStore = useAccessStore(); const enabledAccessControl = useMemo( () => accessStore.enabledAccessControl(), // eslint-disable-next-line react-hooks/exhaustive-deps [], ); const promptStore = usePromptStore(); const builtinCount = SearchService.count.builtin; const customCount = promptStore.getUserPrompts().length ?? 0; const [shouldShowPromptModal, setShowPromptModal] = useState(false); const showUsage = accessStore.isAuthorized(); useEffect(() => { // checks per minutes checkUpdate(); showUsage && checkUsage(); // eslint-disable-next-line react-hooks/exhaustive-deps }, []); useEffect(() => { const keydownEvent = (e: KeyboardEvent) => { if (e.key === "Escape") { props.closeSettings(); } }; document.addEventListener("keydown", keydownEvent); return () => { document.removeEventListener("keydown", keydownEvent); }; // eslint-disable-next-line react-hooks/exhaustive-deps }, []); return (
{Locale.Settings.Title}
{Locale.Settings.SubTitle}
} onClick={() => { const confirmed = window.confirm( `${Locale.Settings.Actions.ConfirmClearAll.Confirm}`, ); if (confirmed) { clearSessions(); } }} bordered title={Locale.Settings.Actions.ClearAll} />
} onClick={() => { const confirmed = window.confirm( `${Locale.Settings.Actions.ConfirmResetAll.Confirm}`, ); if (confirmed) { 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)} /> )}
{Locale.Settings.Theme}
updateConfig( (config) => (config.fontSize = Number.parseInt(e.currentTarget.value)), ) } > updateConfig( (config) => (config.tightBorder = e.currentTarget.checked), ) } > updateConfig( (config) => (config.sendPreviewBubble = e.currentTarget.checked), ) } > {enabledAccessControl ? ( { accessStore.updateCode(e.currentTarget.value); }} /> ) : ( <> )} { accessStore.updateToken(e.currentTarget.value); }} /> {!showUsage || loadingUsage ? (
) : ( } text={Locale.Settings.Usage.Check} onClick={checkUsage} /> )} updateConfig( (config) => (config.historyMessageCount = e.target.valueAsNumber), ) } > updateConfig( (config) => (config.compressMessageLengthThreshold = e.currentTarget.valueAsNumber), ) } > updateConfig( (config) => (config.disablePromptHint = e.currentTarget.checked), ) } > } text={Locale.Settings.Prompt.Edit} onClick={() => setShowPromptModal(true)} /> { updateConfig( (config) => (config.modelConfig.temperature = ModalConfigValidator.temperature( e.currentTarget.valueAsNumber, )), ); }} > updateConfig( (config) => (config.modelConfig.max_tokens = ModalConfigValidator.max_tokens( e.currentTarget.valueAsNumber, )), ) } > { updateConfig( (config) => (config.modelConfig.presence_penalty = ModalConfigValidator.presence_penalty( e.currentTarget.valueAsNumber, )), ); }} > {shouldShowPromptModal && ( setShowPromptModal(false)} /> )}
); }