omni-tools/src/components/ToolContent.tsx

127 lines
3.5 KiB
TypeScript
Raw Normal View History

2025-03-26 05:43:59 +00:00
import React, { ReactNode, useContext, useEffect } from 'react';
2025-03-05 21:53:22 +00:00
import { Box } from '@mui/material';
2025-03-14 17:06:38 +00:00
import { Formik, FormikValues, useFormikContext } from 'formik';
2025-03-10 04:13:10 +00:00
import ToolOptions, { GetGroupsType } from '@components/options/ToolOptions';
2025-03-05 21:53:22 +00:00
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';
2025-03-14 17:06:38 +00:00
import { CustomSnackBarContext } from '../contexts/CustomSnackBarContext';
const FormikListenerComponent = <T,>({
input,
2025-03-26 05:43:59 +00:00
compute,
onValuesChange
2025-03-14 17:06:38 +00:00
}: {
input: any;
compute: (optionsValues: T, input: any) => void;
2025-03-26 05:43:59 +00:00
onValuesChange?: (values: T) => void;
2025-03-14 17:06:38 +00:00
}) => {
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]);
2025-03-26 05:43:59 +00:00
useEffect(() => {
onValuesChange?.(values);
}, [onValuesChange, values]);
2025-03-14 17:06:38 +00:00
return null; // This component doesn't render anything
};
2025-03-05 21:53:22 +00:00
2025-03-08 07:32:20 +00:00
interface ToolContentProps<T, I> extends ToolComponentProps {
2025-03-09 03:58:04 +00:00
inputComponent?: ReactNode;
2025-03-05 21:53:22 +00:00
resultComponent: ReactNode;
2025-03-09 03:58:04 +00:00
renderCustomInput?: (
values: T,
setFieldValue: (fieldName: string, value: any) => void
) => ReactNode;
2025-03-05 21:53:22 +00:00
initialValues: T;
2025-03-08 06:43:11 +00:00
getGroups: GetGroupsType<T> | null;
2025-03-05 22:05:10 +00:00
compute: (optionsValues: T, input: I) => void;
2025-03-05 21:53:22 +00:00
toolInfo?: {
title: string;
2025-03-09 01:22:23 +00:00
description?: string;
2025-03-05 21:53:22 +00:00
};
2025-03-08 07:32:20 +00:00
input?: I;
exampleCards?: CardExampleType<T>[];
setInput?: React.Dispatch<React.SetStateAction<I>>;
2025-03-05 21:53:22 +00:00
validationSchema?: any;
2025-03-26 05:43:59 +00:00
onValuesChange?: (values: T) => void;
2025-03-05 21:53:22 +00:00
}
2025-03-05 22:05:10 +00:00
export default function ToolContent<T extends FormikValues, I>({
2025-03-05 21:53:22 +00:00
title,
inputComponent,
resultComponent,
initialValues,
getGroups,
compute,
toolInfo,
exampleCards,
input,
setInput,
2025-03-09 03:58:04 +00:00
validationSchema,
2025-03-26 05:43:59 +00:00
renderCustomInput,
onValuesChange
2025-03-05 22:05:10 +00:00
}: ToolContentProps<T, I>) {
2025-03-05 21:53:22 +00:00
return (
<Box>
2025-03-10 04:13:10 +00:00
<Formik
2025-03-05 21:53:22 +00:00
initialValues={initialValues}
validationSchema={validationSchema}
2025-03-10 04:13:10 +00:00
onSubmit={() => {}}
>
{({ values, setFieldValue }) => {
return (
<>
<ToolInputAndResult
input={
inputComponent ??
(renderCustomInput &&
renderCustomInput(values, setFieldValue))
}
result={resultComponent}
/>
2025-03-26 05:43:59 +00:00
<FormikListenerComponent<T>
compute={compute}
input={input}
onValuesChange={onValuesChange}
/>
2025-03-14 17:06:38 +00:00
<ToolOptions getGroups={getGroups} />
2025-03-05 21:53:22 +00:00
2025-03-10 04:13:10 +00:00
{toolInfo && toolInfo.title && toolInfo.description && (
<ToolInfo
title={toolInfo.title}
description={toolInfo.description}
/>
)}
2025-03-05 21:53:22 +00:00
2025-03-10 04:13:10 +00:00
{exampleCards && exampleCards.length > 0 && (
<>
<Separator backgroundColor="#5581b5" margin="50px" />
<ToolExamples
title={title}
exampleCards={exampleCards}
getGroups={getGroups}
setInput={setInput}
/>
</>
)}
</>
);
}}
</Formik>
2025-03-05 21:53:22 +00:00
</Box>
);
}