"use client"; import {useRouter} from "next/navigation"; import {useMutation} from "@tanstack/react-query"; import {Form, FormControl, FormField, FormItem, FormLabel, FormMessage, useZodForm} from "@/components/ui/form"; import {ButtonWithLoading} from "@/components/common/button-with-loading"; import {Input} from "@/components/ui/input"; import { addStorageChannelAction, updateStorageChannelAction } from "@/features/channel/components/storages/channel.action"; import {toast} from "sonner"; import {OrganizationWithMembers} from "@/db/schema/03_organization"; import {Button} from "@/components/ui/button"; import {NotificationChannelWith} from "@/db/schema/09_notification-channel"; import {useEffect} from "react"; import {cn} from "@/lib/utils"; import {Card} from "@/components/ui/card"; import {ArrowLeft} from "lucide-react"; import {StorageChannelWith} from "@/db/schema/12_storage-channel"; import { NotificationChannelFormSchema, NotificationChannelFormType, StorageChannelFormSchema, StorageChannelFormType } from "@/features/channel/schemas/channel-form.schema"; import { ChannelKind, renderChannelForm } from "@/features/channel/components/channels-helpers"; import {storageProviders} from "@/features/channel/components/channels-storage-helper"; import {notificationProviders} from "@/features/channel/components/channels-notification-helper"; import { ChannelTestButton } from "@/features/channel/components/channel-test-button"; import { addNotificationChannelAction, updateNotificationChannelAction } from "@/features/channel/components/notifications/channel.action"; type NotifierFormProps = { onSuccessAction?: () => void; organization?: OrganizationWithMembers; defaultValues?: NotificationChannelWith | StorageChannelWith adminView?: boolean kind: ChannelKind }; export const ChannelForm = ({onSuccessAction, organization, defaultValues, kind}: NotifierFormProps) => { const router = useRouter(); const isCreate = !Boolean(defaultValues); const form = useZodForm({ schema: kind == "notification" ? NotificationChannelFormSchema : StorageChannelFormSchema, // @ts-expect-error — defaultValues type is a union of notification/storage channel types defaultValues: {...defaultValues}, }); useEffect(() => { form.reset(defaultValues ? {...defaultValues} : {enabled: true}); }, [defaultValues]); const mutationAddNotificationChannel = useMutation({ mutationFn: async (values: NotificationChannelFormType | StorageChannelFormType) => { const payload = { data: values, ...(organization && {organizationId: organization.id}), ...((defaultValues && {id: defaultValues.id})) }; let result: any; if (kind === "notification") { // @ts-expect-error — payload type varies between notification and storage result = isCreate ? await addNotificationChannelAction(payload) : await updateNotificationChannelAction(payload); } else if (kind === "storage") { // @ts-expect-error — payload type varies between notification and storage result = isCreate ? await addStorageChannelAction(payload) : await updateStorageChannelAction(payload); } else { toast.error("An error occurred"); return; } const inner = result?.data; if (inner?.success) { toast.success(inner.actionSuccess?.message); isCreate && onSuccessAction?.(); router.refresh(); } else { toast.error(inner?.actionError?.message); isCreate && onSuccessAction?.(); } } }); const provider = form.watch("provider"); const channelTypes = kind == "notification" ? notificationProviders : storageProviders const selectedProviderDetails = channelTypes.find(t => t.value === provider); if (isCreate && !provider) { return (