diff --git a/web/containers/Checkbox/index.tsx b/web/containers/Checkbox/index.tsx index f9f7c17e8..8cb2f5d70 100644 --- a/web/containers/Checkbox/index.tsx +++ b/web/containers/Checkbox/index.tsx @@ -1,6 +1,6 @@ -/* eslint-disable @typescript-eslint/no-explicit-any */ -import { useEffect, useState } from 'react' +import { FieldValues, UseFormRegister } from 'react-hook-form' +import { ModelRuntimeParams } from '@janhq/core' import { Switch } from '@janhq/uikit' import { useAtomValue } from 'jotai' @@ -16,44 +16,32 @@ type Props = { name: string title: string checked: boolean - register: any + register: UseFormRegister } const Checkbox: React.FC = ({ name, title, checked, register }) => { - const [currentChecked, setCurrentChecked] = useState(checked) const { updateModelParameter } = useUpdateModelParameters() const threadId = useAtomValue(getActiveThreadIdAtom) const activeModelParams = useAtomValue(getActiveThreadModelRuntimeParamsAtom) - useEffect(() => { - setCurrentChecked(checked) - }, [checked]) + const onCheckedChange = (checked: boolean) => { + if (!threadId || !activeModelParams) return - useEffect(() => { - updateSetting() - // eslint-disable-next-line react-hooks/exhaustive-deps - }, [currentChecked]) - - const updateValue = [name].reduce((accumulator, value) => { - return { ...accumulator, [value]: currentChecked } - }, {}) - - const updateSetting = () => { - return updateModelParameter(String(threadId), { + const updatedModelParams: ModelRuntimeParams = { ...activeModelParams, - ...updateValue, - }) + [name]: checked, + } + + updateModelParameter(threadId, updatedModelParams) } return (
{ - setCurrentChecked(e) - }} + onCheckedChange={onCheckedChange} />
) diff --git a/web/containers/Slider/index.tsx b/web/containers/Slider/index.tsx index c8daa17fb..8f37aed59 100644 --- a/web/containers/Slider/index.tsx +++ b/web/containers/Slider/index.tsx @@ -1,6 +1,6 @@ -/* eslint-disable @typescript-eslint/no-explicit-any */ -import { useEffect, useState } from 'react' +import { FieldValues, UseFormRegister } from 'react-hook-form' +import { ModelRuntimeParams } from '@janhq/core' import { Slider, Input } from '@janhq/uikit' import { useAtomValue } from 'jotai' @@ -18,7 +18,7 @@ type Props = { max: number step: number value: number - register: any + register: UseFormRegister } const SliderRightPanel: React.FC = ({ @@ -30,30 +30,19 @@ const SliderRightPanel: React.FC = ({ value, register, }) => { - const [currentValue, setCurrentValue] = useState(value) const { updateModelParameter } = useUpdateModelParameters() const threadId = useAtomValue(getActiveThreadIdAtom) const activeModelParams = useAtomValue(getActiveThreadModelRuntimeParamsAtom) - useEffect(() => { - setCurrentValue(value) - // eslint-disable-next-line react-hooks/exhaustive-deps - }, [value]) + const onValueChanged = (e: number[]) => { + if (!threadId || !activeModelParams) return - useEffect(() => { - updateSetting() - // eslint-disable-next-line react-hooks/exhaustive-deps - }, [currentValue]) - - const updateValue = [name].reduce((accumulator, value) => { - return { ...accumulator, [value]: currentValue } - }, {}) - - const updateSetting = () => { - return updateModelParameter(String(threadId), { + const updatedModelParams: ModelRuntimeParams = { ...activeModelParams, - ...updateValue, - }) + [name]: Number(e[0]), + } + + updateModelParameter(threadId, updatedModelParams) } return ( @@ -63,14 +52,10 @@ const SliderRightPanel: React.FC = ({
parseInt(v), + setValueAs: (v: string) => parseInt(v), })} - value={[currentValue]} - onValueChange={async (e) => { - setCurrentValue(Number(e[0])) - await updateSetting() - }} - type="range" + value={[value]} + onValueChange={onValueChanged} min={min} max={max} step={step} @@ -87,11 +72,8 @@ const SliderRightPanel: React.FC = ({ className="-mt-4 h-8 w-16" min={min} max={max} - value={String(currentValue)} - onChange={async (e) => { - setCurrentValue(Number(e.target.value)) - await updateSetting() - }} + value={String(value)} + onChange={(e) => onValueChanged([Number(e.target.value)])} />
diff --git a/web/screens/Chat/ModelSetting/index.tsx b/web/screens/Chat/ModelSetting/index.tsx index e8c9b2453..dbd4c7892 100644 --- a/web/screens/Chat/ModelSetting/index.tsx +++ b/web/screens/Chat/ModelSetting/index.tsx @@ -1,5 +1,3 @@ -import { useEffect, useState } from 'react' - import { useForm } from 'react-hook-form' import { ModelRuntimeParams } from '@janhq/core' @@ -11,42 +9,31 @@ import settingComponentBuilder, { SettingComponentData, } from './settingComponentBuilder' -import { - getActiveThreadIdAtom, - getActiveThreadModelRuntimeParamsAtom, -} from '@/helpers/atoms/Thread.atom' +import { getActiveThreadModelRuntimeParamsAtom } from '@/helpers/atoms/Thread.atom' export default function ModelSetting() { - const threadId = useAtomValue(getActiveThreadIdAtom) - const activeModelParams = useAtomValue(getActiveThreadModelRuntimeParamsAtom) - const [modelParams, setModelParams] = useState< - ModelRuntimeParams | undefined - >(activeModelParams) - const { register } = useForm() + const activeModelParams = useAtomValue(getActiveThreadModelRuntimeParamsAtom) - useEffect(() => { - setModelParams(activeModelParams) - // eslint-disable-next-line react-hooks/exhaustive-deps - }, [threadId]) - - if (!modelParams) { - return
This thread has no model parameters
+ if (!activeModelParams) { + return null } const componentData: SettingComponentData[] = [] - Object.keys(modelParams).forEach((key) => { + Object.keys(activeModelParams).forEach((key) => { const componentSetting = presetConfiguration[key] if (componentSetting) { if ('value' in componentSetting.controllerData) { componentSetting.controllerData.value = Number( - modelParams[key as keyof ModelRuntimeParams] + activeModelParams[key as keyof ModelRuntimeParams] ) } else if ('checked' in componentSetting.controllerData) { - componentSetting.controllerData.checked = modelParams[ + const checked = activeModelParams[ key as keyof ModelRuntimeParams ] as boolean + + componentSetting.controllerData.checked = checked } componentData.push(componentSetting) } diff --git a/web/screens/Chat/ModelSetting/settingComponentBuilder.tsx b/web/screens/Chat/ModelSetting/settingComponentBuilder.tsx index 604e70773..761704241 100644 --- a/web/screens/Chat/ModelSetting/settingComponentBuilder.tsx +++ b/web/screens/Chat/ModelSetting/settingComponentBuilder.tsx @@ -1,5 +1,5 @@ /* eslint-disable no-case-declarations */ -/* eslint-disable @typescript-eslint/no-explicit-any */ +import { FieldValues, UseFormRegister } from 'react-hook-form' import Checkbox from '@/containers/Checkbox' import Slider from '@/containers/Slider' @@ -27,7 +27,7 @@ type CheckboxData = { const settingComponentBuilder = ( componentData: SettingComponentData[], - register: any + register: UseFormRegister ) => { const components = componentData.map((data) => { switch (data.controllerType) {