Skip to content

Commit

Permalink
fix: assistant builder form edit bug (#162)
Browse files Browse the repository at this point in the history
  • Loading branch information
PetrBulanek authored Dec 19, 2024
1 parent 436ff00 commit 623863b
Showing 1 changed file with 19 additions and 29 deletions.
48 changes: 19 additions & 29 deletions src/modules/assistants/builder/AssistantBuilderProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ import {
useCallback,
useEffect,
useMemo,
useState,
} from 'react';
import { FormProvider, useForm, UseFormReturn } from 'react-hook-form';
import {
Expand Down Expand Up @@ -97,10 +96,9 @@ interface Props {
}

export function AssistantBuilderProvider({
assistant: assistantProp,
assistant: initialAssistant,
children,
}: Props) {
const [initialAssistant, setInitialAssistant] = useState(assistantProp);
const { addToast } = useToast();
const { project, assistant } = useAppContext();
const { selectAssistant } = useAppApiContext();
Expand All @@ -124,8 +122,8 @@ export function AssistantBuilderProvider({
onSuccess: (result: AssistantResult, isNew: boolean) => {
if (!result) return;
const assistantFromResult = decodeEntityWithMetadata<Assistant>(result);

selectAssistant(assistantFromResult);
setInitialAssistant(assistantFromResult);

if (isMdDown) {
router.push(`/${project.id}/chat/${result.id}`);
Expand All @@ -141,43 +139,37 @@ export function AssistantBuilderProvider({
},
});

const getDefaultValues = useCallback(
() =>
formValuesFromAssistant(
assistantTemplate ? assistantTemplate : (initialAssistant ?? null),
isDuplicate,
),
[assistantTemplate, initialAssistant, isDuplicate],
);

const formReturn = useForm<AssistantFormValues>({
mode: 'onChange',
defaultValues: formValuesFromAssistant(
assistantTemplate ? assistantTemplate : (initialAssistant ?? null),
isDuplicate,
),
defaultValues: getDefaultValues(),
});

const { handleSubmit, reset, formState } = formReturn;

useEffect(() => {
if (isDuplicate || !initialAssistant) {
selectAssistant(null);
reset(getDefaultValues());
} else {
selectAssistant(initialAssistant);
}
}, [isDuplicate, initialAssistant, selectAssistant]);
}, [isDuplicate, initialAssistant, selectAssistant, reset, getDefaultValues]);

useEffect(() => {
if (assistantProp) {
setInitialAssistant(assistantProp);
if (formState.isSubmitSuccessful) {
reset(undefined, { keepValues: true });
router.refresh();
}
}, [assistantProp]);

useEffect(() => {
reset(
formValuesFromAssistant(
assistantTemplate
? assistantTemplate
: ((assistant || initialAssistant) ?? null),
isDuplicate,
),
{
keepValues: false,
},
);
}, [isDuplicate, assistantTemplate, initialAssistant, assistant, reset]);
}, [formState, router, reset]);

useEffect(() => {
if (!isEmpty(formState.dirtyFields))
Expand Down Expand Up @@ -252,10 +244,8 @@ export function AssistantBuilderProvider({
model,
},
});

formReturn.reset({}, { keepValues: true });
},
[assistant, assistantTemplate, formReturn, saveAssistantAsync],
[assistant, assistantTemplate, saveAssistantAsync],
);

const handleError = useCallback(() => {
Expand Down

0 comments on commit 623863b

Please sign in to comment.