omni-tools/src/components/ToolContent.tsx

88 lines
2.2 KiB
TypeScript
Raw Normal View History

2025-03-05 21:53:22 +00:00
import React, { useRef, useState, ReactNode } from 'react';
import { Box } from '@mui/material';
import { FormikProps, FormikValues } 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';
2025-03-08 07:32:20 +00:00
interface ToolContentProps<T, I> extends ToolComponentProps {
2025-03-05 21:53:22 +00:00
// Input/Output components
inputComponent: ReactNode;
resultComponent: ReactNode;
// Tool options
initialValues: T;
2025-03-08 06:43:11 +00:00
getGroups: GetGroupsType<T> | null;
2025-03-05 21:53:22 +00:00
// Computation function
2025-03-05 22:05:10 +00:00
compute: (optionsValues: T, input: I) => void;
2025-03-05 21:53:22 +00:00
// Tool info (optional)
toolInfo?: {
title: string;
2025-03-09 01:22:23 +00:00
description?: string;
2025-03-05 21:53:22 +00:00
};
// Input value to pass to the compute function
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
// Validation schema (optional)
validationSchema?: any;
}
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,
validationSchema
2025-03-05 22:05:10 +00:00
}: ToolContentProps<T, I>) {
2025-03-05 21:53:22 +00:00
const formRef = useRef<FormikProps<T>>(null);
return (
<Box>
<ToolInputAndResult input={inputComponent} result={resultComponent} />
<ToolOptions
formRef={formRef}
compute={compute}
getGroups={getGroups}
initialValues={initialValues}
input={input}
validationSchema={validationSchema}
/>
2025-03-09 01:22:23 +00:00
{toolInfo && toolInfo.title && toolInfo.description && (
2025-03-05 21:53:22 +00:00
<ToolInfo title={toolInfo.title} description={toolInfo.description} />
)}
{exampleCards && exampleCards.length > 0 && (
<>
<Separator backgroundColor="#5581b5" margin="50px" />
<ToolExamples
title={title}
exampleCards={exampleCards}
getGroups={getGroups}
formRef={formRef}
setInput={setInput}
/>
</>
)}
</Box>
);
}