import { Controller, FormProvider } from 'react-hook-form';
import { SaveAndCancelButtons } from '@/settings/components/SaveAndCancelButtons/SaveAndCancelButtons';
import { SettingsPageContainer } from '@/settings/components/SettingsPageContainer';
import { SettingsSkeletonLoader } from '@/settings/components/SettingsSkeletonLoader';
import { type WebhookFormMode } from '@/settings/developers/constants/WebhookFormMode';
import { useWebhookForm } from '@/settings/developers/hooks/useWebhookForm';
import { SettingsTextInput } from '@/ui/input/components/SettingsTextInput';
import { TextArea } from '@/ui/input/components/TextArea';
import { ConfirmationModal } from '@/ui/layout/modal/components/ConfirmationModal';
import { useModal } from '@/ui/layout/modal/hooks/useModal';
import { SubMenuTopBarContainer } from '@/ui/layout/page/components/SubMenuTopBarContainer';
import { Trans, useLingui } from '@lingui/react/macro';
import { SettingsPath } from 'twenty-shared/types';
import {
getSettingsPath,
getUrlHostnameOrThrow,
isDefined,
isValidUrl,
} from 'twenty-shared/utils';
import { H2Title, IconTrash } from 'twenty-ui/display';
import { Button } from 'twenty-ui/input';
import { Section } from 'twenty-ui/layout';
import { useNavigateSettings } from '~/hooks/useNavigateSettings';
import { SettingsDatabaseEventsForm } from '@/settings/components/SettingsDatabaseEventsForm';
const DELETE_WEBHOOK_MODAL_ID = 'delete-webhook-modal';
type SettingsDevelopersWebhookFormProps = {
webhookId?: string;
mode: WebhookFormMode;
};
export const SettingsDevelopersWebhookForm = ({
webhookId,
mode,
}: SettingsDevelopersWebhookFormProps) => {
const { t } = useLingui();
const navigate = useNavigateSettings();
const { openModal } = useModal();
const {
formConfig,
loading,
canSave,
handleSave,
updateOperation,
removeOperation,
handleDelete,
isCreationMode,
error,
} = useWebhookForm({ webhookId, mode });
const getTitle = () => {
if (isCreationMode) {
return t`New Webhook`;
}
const targetUrl = formConfig.watch('targetUrl');
if (isDefined(targetUrl) && isValidUrl(targetUrl.trim())) {
return getUrlHostnameOrThrow(targetUrl);
}
};
if ((loading && !isCreationMode) || isDefined(error)) {
return ;
}
const descriptionTextAreaId = `${webhookId}-description`;
const targetUrlTextInputId = `${webhookId}-target-url`;
const secretTextInputId = `${webhookId}-secret`;
return (
// eslint-disable-next-line react/jsx-props-no-spreading
navigate(SettingsPath.ApiWebhooks)}
onSave={formConfig.handleSubmit(handleSave)}
/>
}
>
{!isCreationMode && (
)}
{!isCreationMode && (
Please type "yes" to confirm you want to delete this webhook.
}
onConfirmClick={handleDelete}
confirmButtonText={t`Delete`}
/>
)}
);
};