fix: compute not firing

This commit is contained in:
Ibrahima G. Coulibaly
2025-03-14 17:06:38 +00:00
parent 9a1ecffedd
commit 8de1c51adf
3 changed files with 83 additions and 91 deletions

View File

@@ -1,6 +1,6 @@
import React, { useRef, ReactNode, useState } from 'react';
import React, { ReactNode, useContext } from 'react';
import { Box } from '@mui/material';
import { Formik, FormikProps, FormikValues } from 'formik';
import { Formik, FormikValues, useFormikContext } from 'formik';
import ToolOptions, { GetGroupsType } from '@components/options/ToolOptions';
import ToolInputAndResult from '@components/ToolInputAndResult';
import ToolInfo from '@components/ToolInfo';
@@ -9,6 +9,29 @@ import ToolExamples, {
CardExampleType
} from '@components/examples/ToolExamples';
import { ToolComponentProps } from '@tools/defineTool';
import { CustomSnackBarContext } from '../contexts/CustomSnackBarContext';
const FormikListenerComponent = <T,>({
input,
compute
}: {
input: any;
compute: (optionsValues: T, input: any) => void;
}) => {
const { values } = useFormikContext<T>();
const { showSnackBar } = useContext(CustomSnackBarContext);
React.useEffect(() => {
try {
compute(values, input);
} catch (exception: unknown) {
if (exception instanceof Error) showSnackBar(exception.message, 'error');
else console.error(exception);
}
}, [values, input, showSnackBar]);
return null; // This component doesn't render anything
};
interface ToolContentProps<T, I> extends ToolComponentProps {
// Input/Output components
@@ -75,12 +98,8 @@ export default function ToolContent<T extends FormikValues, I>({
}
result={resultComponent}
/>
<ToolOptions
compute={compute}
getGroups={getGroups}
input={input}
/>
<FormikListenerComponent<T> compute={compute} input={input} />
<ToolOptions getGroups={getGroups} />
{toolInfo && toolInfo.title && toolInfo.description && (
<ToolInfo