import React, { ReactNode, useContext, useEffect } from 'react'; import { Box } from '@mui/material'; import { Formik, FormikValues, useFormikContext } from 'formik'; import ToolOptions, { GetGroupsType } from '@components/options/ToolOptions'; import ToolInputAndResult from '@components/ToolInputAndResult'; import ToolInfo from '@components/ToolInfo'; import Separator from '@components/Separator'; import ToolExamples, { CardExampleType } from '@components/examples/ToolExamples'; import { ToolComponentProps } from '@tools/defineTool'; import { CustomSnackBarContext } from '../contexts/CustomSnackBarContext'; const FormikListenerComponent = ({ input, compute, onValuesChange }: { input: any; compute: (optionsValues: T, input: any) => void; onValuesChange?: (values: T) => void; }) => { const { values } = useFormikContext(); 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]); useEffect(() => { onValuesChange?.(values); }, [onValuesChange, values]); return null; // This component doesn't render anything }; interface ToolContentProps extends ToolComponentProps { inputComponent?: ReactNode; resultComponent?: ReactNode; renderCustomInput?: ( values: Options, setFieldValue: (fieldName: string, value: any) => void ) => ReactNode; initialValues: Options; getGroups: GetGroupsType | null; compute: (optionsValues: Options, input: Input) => void; toolInfo?: { title: string; description?: string; }; input?: Input; exampleCards?: CardExampleType[]; setInput?: React.Dispatch>; validationSchema?: any; onValuesChange?: (values: Options) => void; verticalGroups?: boolean; } export default function ToolContent({ title, inputComponent, resultComponent, initialValues, getGroups, compute, toolInfo, exampleCards, input, setInput, validationSchema, renderCustomInput, onValuesChange, verticalGroups }: ToolContentProps) { return ( {}} > {({ values, setFieldValue }) => { return ( <> compute={compute} input={input} onValuesChange={onValuesChange} /> {toolInfo && toolInfo.title && toolInfo.description && ( )} {exampleCards && exampleCards.length > 0 && ( <> )} ); }} ); }