2024-06-21 20:57:05 +01:00
|
|
|
import { Box, Stack, useTheme } from '@mui/material';
|
|
|
|
|
import SettingsIcon from '@mui/icons-material/Settings';
|
|
|
|
|
import Typography from '@mui/material/Typography';
|
2024-06-26 07:47:17 +01:00
|
|
|
import React, { ReactNode, RefObject, useContext, useEffect } from 'react';
|
|
|
|
|
import { Formik, FormikProps, FormikValues, useFormikContext } from 'formik';
|
|
|
|
|
import ToolOptionGroups, { ToolOptionGroup } from './ToolOptionGroups';
|
|
|
|
|
import { CustomSnackBarContext } from '../../contexts/CustomSnackBarContext';
|
2024-06-21 20:24:00 +01:00
|
|
|
|
2025-02-27 01:26:48 +00:00
|
|
|
export type UpdateField<T> = <Y extends keyof T>(field: Y, value: T[Y]) => void;
|
2024-06-27 21:25:11 +01:00
|
|
|
|
2024-06-27 12:39:38 +01:00
|
|
|
const FormikListenerComponent = <T,>({
|
|
|
|
|
initialValues,
|
|
|
|
|
input,
|
|
|
|
|
compute
|
|
|
|
|
}: {
|
|
|
|
|
initialValues: T;
|
|
|
|
|
input: any;
|
|
|
|
|
compute: (optionsValues: T, input: any) => void;
|
|
|
|
|
}) => {
|
|
|
|
|
const { values } = useFormikContext<typeof initialValues>();
|
|
|
|
|
const { showSnackBar } = useContext(CustomSnackBarContext);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
try {
|
2024-06-27 18:39:55 +01:00
|
|
|
compute(values, input);
|
2024-06-27 12:39:38 +01:00
|
|
|
} catch (exception: unknown) {
|
|
|
|
|
if (exception instanceof Error) showSnackBar(exception.message, 'error');
|
2025-03-08 08:38:35 +00:00
|
|
|
else console.error(exception);
|
2024-06-27 12:39:38 +01:00
|
|
|
}
|
|
|
|
|
}, [values, input]);
|
|
|
|
|
|
|
|
|
|
return null; // This component doesn't render anything
|
|
|
|
|
};
|
2024-06-27 21:52:41 +01:00
|
|
|
|
|
|
|
|
interface FormikHelperProps<T> {
|
|
|
|
|
compute: (optionsValues: T, input: any) => void;
|
|
|
|
|
input: any;
|
|
|
|
|
children?: ReactNode;
|
2025-03-09 01:50:09 +00:00
|
|
|
getGroups:
|
|
|
|
|
| null
|
|
|
|
|
| ((
|
|
|
|
|
formikProps: FormikProps<T> & { updateField: UpdateField<T> }
|
|
|
|
|
) => ToolOptionGroup[]);
|
2024-06-27 21:52:41 +01:00
|
|
|
formikProps: FormikProps<T>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const ToolBody = <T,>({
|
|
|
|
|
compute,
|
|
|
|
|
input,
|
|
|
|
|
children,
|
|
|
|
|
getGroups,
|
|
|
|
|
formikProps
|
|
|
|
|
}: FormikHelperProps<T>) => {
|
|
|
|
|
const { values, setFieldValue } = useFormikContext<T>();
|
|
|
|
|
|
|
|
|
|
const updateField: UpdateField<T> = (field, value) => {
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
setFieldValue(field, value);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Stack direction={'row'} spacing={2}>
|
|
|
|
|
<FormikListenerComponent<T>
|
|
|
|
|
compute={compute}
|
|
|
|
|
input={input}
|
|
|
|
|
initialValues={values}
|
|
|
|
|
/>
|
2025-03-09 01:50:09 +00:00
|
|
|
<ToolOptionGroups
|
|
|
|
|
groups={getGroups?.({ ...formikProps, updateField }) ?? []}
|
|
|
|
|
/>
|
2024-06-27 21:52:41 +01:00
|
|
|
{children}
|
|
|
|
|
</Stack>
|
|
|
|
|
);
|
|
|
|
|
};
|
2025-02-27 01:26:48 +00:00
|
|
|
|
|
|
|
|
export type GetGroupsType<T> = (
|
|
|
|
|
formikProps: FormikProps<T> & { updateField: UpdateField<T> }
|
|
|
|
|
) => ToolOptionGroup[];
|
2024-06-26 07:47:17 +01:00
|
|
|
export default function ToolOptions<T extends FormikValues>({
|
|
|
|
|
children,
|
|
|
|
|
initialValues,
|
|
|
|
|
validationSchema,
|
|
|
|
|
compute,
|
|
|
|
|
input,
|
|
|
|
|
getGroups,
|
|
|
|
|
formRef
|
|
|
|
|
}: {
|
|
|
|
|
children?: ReactNode;
|
|
|
|
|
initialValues: T;
|
2024-07-14 00:48:10 +01:00
|
|
|
validationSchema?: any | (() => any);
|
2024-06-26 07:47:17 +01:00
|
|
|
compute: (optionsValues: T, input: any) => void;
|
2024-06-27 18:39:55 +01:00
|
|
|
input?: any;
|
2025-03-08 06:43:11 +00:00
|
|
|
getGroups: GetGroupsType<T> | null;
|
2024-06-26 07:47:17 +01:00
|
|
|
formRef?: RefObject<FormikProps<T>>;
|
|
|
|
|
}) {
|
2024-06-21 20:57:05 +01:00
|
|
|
const theme = useTheme();
|
2025-03-09 01:50:09 +00:00
|
|
|
return (
|
|
|
|
|
<Box
|
|
|
|
|
sx={{
|
|
|
|
|
mb: 2,
|
|
|
|
|
borderRadius: 2,
|
|
|
|
|
padding: 2,
|
|
|
|
|
backgroundColor: theme.palette.background.default,
|
|
|
|
|
boxShadow: '2',
|
|
|
|
|
display: getGroups ? 'block' : 'none'
|
|
|
|
|
}}
|
|
|
|
|
mt={2}
|
|
|
|
|
>
|
|
|
|
|
<Stack direction={'row'} spacing={1} alignItems={'center'}>
|
|
|
|
|
<SettingsIcon />
|
|
|
|
|
<Typography fontSize={22}>Tool options</Typography>
|
|
|
|
|
</Stack>
|
|
|
|
|
<Box mt={2}>
|
|
|
|
|
<Formik
|
|
|
|
|
innerRef={formRef}
|
|
|
|
|
initialValues={initialValues}
|
|
|
|
|
validationSchema={validationSchema}
|
|
|
|
|
onSubmit={() => {}}
|
|
|
|
|
>
|
|
|
|
|
{(formikProps) => (
|
|
|
|
|
<ToolBody
|
|
|
|
|
compute={compute}
|
|
|
|
|
input={input}
|
|
|
|
|
getGroups={getGroups}
|
|
|
|
|
formikProps={formikProps}
|
|
|
|
|
>
|
|
|
|
|
{children}
|
|
|
|
|
</ToolBody>
|
|
|
|
|
)}
|
|
|
|
|
</Formik>
|
2024-06-26 07:47:17 +01:00
|
|
|
</Box>
|
2025-03-09 01:50:09 +00:00
|
|
|
</Box>
|
|
|
|
|
);
|
2024-06-21 20:24:00 +01:00
|
|
|
}
|