From 8202d9d613bebbaec582bea72cefc6e4f619e504 Mon Sep 17 00:00:00 2001 From: yorkeking <34937020+yorkeking@users.noreply.github.com> Date: Tue, 4 Apr 2023 00:40:27 +0800 Subject: [PATCH] Update settings.tsx --- app/components/settings.tsx | 487 ++++-------------------------------- 1 file changed, 44 insertions(+), 443 deletions(-) diff --git a/app/components/settings.tsx b/app/components/settings.tsx index 5d3879b26..3bf9434d5 100644 --- a/app/components/settings.tsx +++ b/app/components/settings.tsx @@ -1,498 +1,99 @@ -import { useState, useEffect, useRef, useMemo } from "react"; +import { useState, useEffect, useMemo, HTMLProps } from "react"; import EmojiPicker, { Theme as EmojiTheme } from "emoji-picker-react"; -import styles from "./settings.module.scss"; - -import ResetIcon from "../icons/reload.svg"; + @@ -8,6 +8,8 @@ import ResetIcon from "../icons/reload.svg"; import CloseIcon from "../icons/close.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 { List, ListItem, Popover, showToast } from "./ui-lib"; -import { IconButton } from "./button"; -import { - SubmitKey, - useChatStore, - Theme, - ALL_MODELS, - useUpdateStore, - useAccessStore, -} from "../store"; -import { Avatar, PromptHints } from "./home"; + @@ -47,6 +49,28 @@ function SettingItem(props: { + ); +} -import Locale, { AllLangs, changeLang, getLang } from "../locales"; -import { getCurrentVersion } from "../utils"; -import Link from "next/link"; -import { UPDATE_URL } from "../constant"; -import { SearchService, usePromptStore } from "../store/prompt"; -import { requestUsage } from "../requests"; +function PasswordInput(props: HTMLProps) { + const [visible, setVisible] = useState(false); + + function changeVisibility() { + setVisible(!visible); + } -function SettingItem(props: { - title: string; - subTitle?: string; - children: JSX.Element; -}) { return ( - -
-
{props.title}
- {props.subTitle && ( -
{props.subTitle}
- )} -
- {props.children} -
+ + + : } + onClick={changeVisibility} + /> + ); } 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 currentId = getCurrentVersion(); - const remoteId = updateStore.remoteId; - const hasNewVersion = currentId !== remoteId; - - function checkUpdate(force = false) { - setCheckingUpdate(true); - updateStore.getLatestCommitId(force).then(() => { - setCheckingUpdate(false); - }); - } - - const [usage, setUsage] = useState<{ - granted?: number; - used?: number; - }>(); - const [loadingUsage, setLoadingUsage] = useState(false); - function checkUsage() { - setLoadingUsage(true); - requestUsage() - .then((res) => - setUsage({ - granted: res?.total_granted, - used: res?.total_used, - }), - ) - .finally(() => { - setLoadingUsage(false); - }); - } - - useEffect(() => { - checkUpdate(); - checkUsage(); - }, []); - - const accessStore = useAccessStore(); - const enabledAccessControl = useMemo( - () => accessStore.enabledAccessControl(), - [], - ); - - const promptStore = usePromptStore(); + @@ -103,6 +127,13 @@ export function Settings(props: { closeSettings: () => void }) { const builtinCount = SearchService.count.builtin; const customCount = promptStore.prompts.size ?? 0; + const showUsage = accessStore.token !== ""; + useEffect(() => { + if (showUsage) { + checkUsage(); + } + }, [showUsage]); + return ( <>
-
-
- {Locale.Settings.Title} -
-
- {Locale.Settings.SubTitle} -
-
-
-
- } - onClick={clearSessions} - 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)} - /> - )} - - - - - - - -
- {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), - ) - } - > - - - - - - updateConfig( - (config) => - (config.disablePromptHint = e.currentTarget.checked), - ) - } - > - - - - } - text={Locale.Settings.Prompt.Edit} - onClick={() => showToast(Locale.WIP)} - /> - - - - {enabledAccessControl ? ( - void }) { title={Locale.Settings.AccessCode.Title} subTitle={Locale.Settings.AccessCode.SubTitle} > - { accessStore.updateCode(e.currentTarget.value); }} - > + /> ) : ( <> - )} - - void }) { title={Locale.Settings.Token.Title} subTitle={Locale.Settings.Token.SubTitle} > - { accessStore.updateToken(e.currentTarget.value); }} - > + /> - {loadingUsage ? ( + {!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.modelConfig.temperature = - e.currentTarget.valueAsNumber), - ); - }} - > - - - - updateConfig( - (config) => - (config.modelConfig.max_tokens = - e.currentTarget.valueAsNumber), - ) - } - > - - - { - updateConfig( - (config) => - (config.modelConfig.presence_penalty = - e.currentTarget.valueAsNumber), - ); - }} - > - - -
- - ); -}