288f0919db
Currently, when a server query or mutation from the front-end fails, the error message defined server-side is displayed in a snackbar in the front-end. These error messages usually contain technical details that don't belong to the user interface, such as "ObjectMetadataCollection not found" or "invalid ENUM value for ...". **BE** In addition to the original error message that is still needed (for the request response, debugging, sentry monitoring etc.), we add a `displayedErrorMessage` that will be used in the snackbars. It's only relevant to add it for the messages that will reach the FE (ie. not in jobs or in rest api for instance) and if it can help the user sort out / fix things (ie. we do add displayedErrorMessage for "Cannot create multiple draft versions for the same workflow" or "Cannot delete [field], please update the label identifier field first", but not "Object metadata does not exist"), even if in practice in the FE users should not be able to perform an action that will not work (ie should not be able to save creation of multiple draft versions of the same workflows). **FE** To ease the usage we replaced enqueueSnackBar with enqueueErrorSnackBar and enqueueSuccessSnackBar with an api that only requires to pass on the error. If no displayedErrorMessage is specified then the default error message is `An error occured.`
261 lines
7.5 KiB
TypeScript
261 lines
7.5 KiB
TypeScript
import { zodResolver } from '@hookform/resolvers/zod';
|
|
import { useForm } from 'react-hook-form';
|
|
|
|
import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular';
|
|
import { useCreateOneRecord } from '@/object-record/hooks/useCreateOneRecord';
|
|
import { useDeleteOneRecord } from '@/object-record/hooks/useDeleteOneRecord';
|
|
import { useFindOneRecord } from '@/object-record/hooks/useFindOneRecord';
|
|
import { useUpdateOneRecord } from '@/object-record/hooks/useUpdateOneRecord';
|
|
import { WebhookFormMode } from '@/settings/developers/constants/WebhookFormMode';
|
|
import { Webhook } from '@/settings/developers/types/webhook/Webhook';
|
|
import {
|
|
webhookFormSchema,
|
|
WebhookFormValues,
|
|
} from '@/settings/developers/validation-schemas/webhookFormSchema';
|
|
import { SettingsPath } from '@/types/SettingsPath';
|
|
import { useSnackBar } from '@/ui/feedback/snack-bar-manager/hooks/useSnackBar';
|
|
import { ApolloError } from '@apollo/client';
|
|
import { t } from '@lingui/core/macro';
|
|
import { isDefined } from 'twenty-shared/utils';
|
|
import { v4 } from 'uuid';
|
|
import { useNavigateSettings } from '~/hooks/useNavigateSettings';
|
|
import { WEBHOOK_EMPTY_OPERATION } from '~/pages/settings/developers/webhooks/constants/WebhookEmptyOperation';
|
|
import { WebhookOperationType } from '~/pages/settings/developers/webhooks/types/WebhookOperationsType';
|
|
|
|
type UseWebhookFormProps = {
|
|
webhookId?: string;
|
|
mode: WebhookFormMode;
|
|
};
|
|
|
|
export const useWebhookForm = ({ webhookId, mode }: UseWebhookFormProps) => {
|
|
const navigate = useNavigateSettings();
|
|
const { enqueueSuccessSnackBar, enqueueErrorSnackBar } = useSnackBar();
|
|
|
|
const isCreationMode = mode === WebhookFormMode.Create;
|
|
|
|
const { createOneRecord } = useCreateOneRecord<Webhook>({
|
|
objectNameSingular: CoreObjectNameSingular.Webhook,
|
|
});
|
|
|
|
const { updateOneRecord } = useUpdateOneRecord<Webhook>({
|
|
objectNameSingular: CoreObjectNameSingular.Webhook,
|
|
});
|
|
|
|
const { deleteOneRecord: deleteOneWebhook } = useDeleteOneRecord({
|
|
objectNameSingular: CoreObjectNameSingular.Webhook,
|
|
});
|
|
|
|
const formConfig = useForm<WebhookFormValues>({
|
|
mode: isCreationMode ? 'onSubmit' : 'onTouched',
|
|
resolver: zodResolver(webhookFormSchema),
|
|
defaultValues: {
|
|
targetUrl: '',
|
|
description: '',
|
|
operations: [
|
|
{
|
|
object: '*',
|
|
action: '*',
|
|
},
|
|
],
|
|
secret: '',
|
|
},
|
|
});
|
|
|
|
const addEmptyOperationIfNecessary = (
|
|
newOperations: WebhookOperationType[],
|
|
): WebhookOperationType[] => {
|
|
if (
|
|
!newOperations.some((op) => op.object === '*' && op.action === '*') &&
|
|
!newOperations.some((op) => op.object === null)
|
|
) {
|
|
return [...newOperations, WEBHOOK_EMPTY_OPERATION];
|
|
}
|
|
return newOperations;
|
|
};
|
|
|
|
const cleanAndFormatOperations = (operations: WebhookOperationType[]) => {
|
|
return Array.from(
|
|
new Set(
|
|
operations
|
|
.filter((op) => isDefined(op.object) && isDefined(op.action))
|
|
.map((op) => `${op.object}.${op.action}`),
|
|
),
|
|
);
|
|
};
|
|
|
|
const { loading, error } = useFindOneRecord({
|
|
skip: isCreationMode,
|
|
objectNameSingular: CoreObjectNameSingular.Webhook,
|
|
objectRecordId: webhookId || '',
|
|
onCompleted: (data) => {
|
|
if (!data) return;
|
|
|
|
const baseOperations = data?.operations
|
|
? data.operations.map((op: string) => {
|
|
const [object, action] = op.split('.');
|
|
return { object, action };
|
|
})
|
|
: data?.operation
|
|
? [
|
|
{
|
|
object: data.operation.split('.')[0],
|
|
action: data.operation.split('.')[1],
|
|
},
|
|
]
|
|
: [];
|
|
const operations = addEmptyOperationIfNecessary(baseOperations);
|
|
|
|
formConfig.reset({
|
|
targetUrl: data.targetUrl || '',
|
|
description: data.description || '',
|
|
operations,
|
|
secret: data.secret || '',
|
|
});
|
|
},
|
|
});
|
|
|
|
const { isDirty, isValid, isSubmitting } = formConfig.formState;
|
|
const canSave = isCreationMode
|
|
? isValid && !isSubmitting
|
|
: isDirty && isValid && !isSubmitting;
|
|
|
|
const handleCreate = async (formValues: WebhookFormValues) => {
|
|
try {
|
|
const cleanedOperations = cleanAndFormatOperations(formValues.operations);
|
|
|
|
const webhookData = {
|
|
targetUrl: formValues.targetUrl.trim(),
|
|
operations: cleanedOperations,
|
|
description: formValues.description,
|
|
secret: formValues.secret,
|
|
};
|
|
|
|
const createdWebhook = await createOneRecord({
|
|
id: v4(),
|
|
...webhookData,
|
|
});
|
|
|
|
const targetUrl = createdWebhook?.targetUrl
|
|
? `${createdWebhook?.targetUrl}`
|
|
: '';
|
|
|
|
enqueueSuccessSnackBar({
|
|
message: t`Webhook ${targetUrl} created successfully`,
|
|
});
|
|
|
|
navigate(
|
|
createdWebhook ? SettingsPath.WebhookDetail : SettingsPath.Webhooks,
|
|
createdWebhook ? { webhookId: createdWebhook.id } : undefined,
|
|
);
|
|
} catch (error) {
|
|
enqueueErrorSnackBar({
|
|
apolloError: error instanceof ApolloError ? error : undefined,
|
|
});
|
|
}
|
|
};
|
|
|
|
const handleUpdate = async (formValues: WebhookFormValues) => {
|
|
if (!webhookId) {
|
|
enqueueErrorSnackBar({
|
|
message: t`Webhook ID is required for updates`,
|
|
});
|
|
return;
|
|
}
|
|
|
|
try {
|
|
const cleanedOperations = cleanAndFormatOperations(formValues.operations);
|
|
|
|
const webhookData = {
|
|
targetUrl: formValues.targetUrl.trim(),
|
|
operations: cleanedOperations,
|
|
description: formValues.description,
|
|
secret: formValues.secret,
|
|
};
|
|
|
|
await updateOneRecord({
|
|
idToUpdate: webhookId,
|
|
updateOneRecordInput: webhookData,
|
|
});
|
|
|
|
formConfig.reset(formValues);
|
|
|
|
const targetUrl = webhookData.targetUrl ? `${webhookData.targetUrl}` : '';
|
|
|
|
enqueueSuccessSnackBar({
|
|
message: t`Webhook ${targetUrl} updated successfully`,
|
|
});
|
|
} catch (error) {
|
|
enqueueErrorSnackBar({
|
|
apolloError: error instanceof ApolloError ? error : undefined,
|
|
});
|
|
}
|
|
};
|
|
|
|
const handleSave = isCreationMode ? handleCreate : handleUpdate;
|
|
|
|
const updateOperation = (
|
|
index: number,
|
|
field: 'object' | 'action',
|
|
value: string | null,
|
|
) => {
|
|
const currentOperations = formConfig.getValues('operations');
|
|
const newOperations = [...currentOperations];
|
|
|
|
newOperations[index] = {
|
|
...newOperations[index],
|
|
[field]: value,
|
|
};
|
|
|
|
formConfig.setValue(
|
|
'operations',
|
|
addEmptyOperationIfNecessary(newOperations),
|
|
{ shouldDirty: true, shouldValidate: true },
|
|
);
|
|
};
|
|
|
|
const removeOperation = (index: number) => {
|
|
const currentOperations = formConfig.getValues('operations');
|
|
const newOperations = currentOperations.filter((_, i) => i !== index);
|
|
|
|
formConfig.setValue(
|
|
'operations',
|
|
addEmptyOperationIfNecessary(newOperations),
|
|
{ shouldDirty: true, shouldValidate: true },
|
|
);
|
|
};
|
|
|
|
const deleteWebhook = async () => {
|
|
if (!webhookId) {
|
|
enqueueErrorSnackBar({
|
|
message: t`Webhook ID is required for deletion`,
|
|
});
|
|
return;
|
|
}
|
|
|
|
try {
|
|
await deleteOneWebhook(webhookId);
|
|
enqueueSuccessSnackBar({
|
|
message: t`Webhook deleted successfully`,
|
|
});
|
|
|
|
navigate(SettingsPath.Webhooks);
|
|
} catch (error) {
|
|
enqueueErrorSnackBar({
|
|
apolloError: error instanceof ApolloError ? error : undefined,
|
|
});
|
|
}
|
|
};
|
|
|
|
return {
|
|
formConfig,
|
|
loading,
|
|
canSave,
|
|
handleSave,
|
|
updateOperation,
|
|
removeOperation,
|
|
deleteWebhook,
|
|
isCreationMode,
|
|
error,
|
|
};
|
|
};
|