"use client"; import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert"; import { Badge } from "@/components/ui/badge"; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage, useZodForm, } from "@/components/ui/form"; import { TooltipProvider } from "@/components/ui/tooltip"; import { ButtonWithLoading } from "@/components/common/button-with-loading"; import { getChannelIcon } from "@/features/channel/components/channels-helpers"; import { getStatusColor, getStatusIcon, } from "@/features/notifications/components/notification-log-columns"; import { createRestorationBackupAction, deleteBackupAction, deleteBackupStorageAction, downloadBackupAction, } from "@/features/database/actions/backup-actions.action"; import { BackupActionsSchema, BackupActionsType, } from "@/features/database/schemas/backup-actions.schema"; import { DatabaseActionKind, useBackupModal, } from "@/features/database/components/backup-modal-context"; import { Backup, BackupWith, Restoration } from "@/db/schema/07_database"; import { BackupStorageWith } from "@/db/schema/14_storage-backup"; import { ServerActionResult } from "@/types/action-type"; import { useMutation, useQueryClient } from "@tanstack/react-query"; import { AlertCircleIcon } from "lucide-react"; import { SafeActionResult } from "next-safe-action"; import { useRouter } from "next/navigation"; import { toast } from "sonner"; import { ZodString } from "zod"; type BackupActionsFormProps = { backup: BackupWith; action: DatabaseActionKind; }; export const BackupActionsForm = ({ backup, action, }: BackupActionsFormProps) => { const filteredBackupStorages = backup.storages?.filter((storage) => storage.deletedAt === null) ?? []; const { closeModal } = useBackupModal(); const queryClient = useQueryClient(); const router = useRouter(); const form = useZodForm({ schema: BackupActionsSchema, }); const mutation = useMutation({ mutationFn: async (values: BackupActionsType) => { let result: | SafeActionResult< string, ZodString, readonly [], { _errors?: string[] | undefined; }, readonly [], ServerActionResult, object > | undefined; if (action === "download") { result = await downloadBackupAction({ backupStorageId: values.backupStorageId, }); } else if (action === "restore") { result = await createRestorationBackupAction({ databaseId: backup.databaseId, backupStorageId: values.backupStorageId, backupId: backup.id, }); } else if (action === "delete") { result = await deleteBackupStorageAction({ databaseId: backup.databaseId, backupStorageId: values.backupStorageId, backupId: backup.id, }); } const inner = result?.data; if (inner?.success) { toast.success(inner.actionSuccess?.message); queryClient.invalidateQueries({ queryKey: ["database-data", backup.databaseId], }); router.refresh(); if (action === "download") { const url = inner.value; if (typeof url === "string") { window.open(url, "_self"); } closeModal(); } else if (action === "restore") { closeModal(); } else if (action === "delete") { closeModal(); } else { closeModal(); } } else { if (action === "delete") { toast.success("Backup deleted successfully."); queryClient.invalidateQueries({ queryKey: ["database-data", backup.databaseId], }); router.refresh(); closeModal(); } else { toast.error(inner?.actionError?.message ?? "An error occurred."); } } }, }); const mutationDeleteEntireBackup = useMutation({ mutationFn: async () => { const result = await deleteBackupAction({ databaseId: backup.databaseId, backupId: backup.id, }); const inner = result?.data; if (inner?.success) { toast.success(inner.actionSuccess?.message); queryClient.invalidateQueries({ queryKey: ["database-data", backup.databaseId], }); router.refresh(); closeModal(); } else { toast.error(inner?.actionError?.message); } }, }); return (
{ await mutation.mutateAsync(values); }} > {filteredBackupStorages.length > 0 ? ( ( Choose a storage backup
{filteredBackupStorages.map( (storage: BackupStorageWith) => ( ), ) ??

No storages available

}
)} /> ) : ( Backup does not have files

You can safely delete the entire backup; no files seem to be related. Maybe an error occurred.

)}
{action === "delete" && ( mutationDeleteEntireBackup.mutateAsync()} isPending={mutationDeleteEntireBackup.isPending} disabled={mutationDeleteEntireBackup.isPending} > Delete entire backup )} {filteredBackupStorages.length > 0 && ( Confirm )}
); };