Files
portabase/src/features/dashboard/backup/columns.tsx
T

143 lines
5.6 KiB
TypeScript
Raw Normal View History

2025-05-16 15:54:17 +02:00
"use client";
2024-11-11 14:56:33 +01:00
2025-05-16 15:54:17 +02:00
import { ColumnDef } from "@tanstack/react-table";
2024-11-11 14:56:33 +01:00
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
2025-05-16 15:54:17 +02:00
DropdownMenuLabel,
DropdownMenuSeparator,
DropdownMenuTrigger,
2024-11-11 14:56:33 +01:00
} from "@/components/ui/dropdown-menu";
2025-05-16 15:54:17 +02:00
import { Button } from "@/components/ui/button";
import { Download, MoreHorizontal, Trash2 } from "lucide-react";
import { ReloadIcon } from "@radix-ui/react-icons";
import { Backup } from "@prisma/client";
import { getFileUrlPresignedLocal } from "@/features/upload/private/upload.action";
import { useMutation } from "@tanstack/react-query";
import { createRestorationAction, deleteBackupAction } from "@/features/dashboard/restore/restore.action";
import { toast } from "sonner";
import { useRouter } from "next/navigation";
import { StatusBadge } from "@/components/wrappers/common/status-badge";
import { TooltipCustom } from "@/components/wrappers/common/tooltipCustom/TooltipCustom";
2024-11-11 14:56:33 +01:00
2024-11-11 19:12:34 +01:00
export const backupColumns: ColumnDef<Backup>[] = [
2024-11-11 14:56:33 +01:00
{
accessorKey: "id",
header: "Reference",
},
{
accessorKey: "createdAt",
header: "Created At",
2025-05-16 15:54:17 +02:00
cell: ({ row }) => {
2024-11-11 19:12:34 +01:00
return new Date(row.getValue("createdAt")).toLocaleString("fr-FR");
},
2024-11-11 14:56:33 +01:00
},
{
accessorKey: "status",
header: "Status",
2025-05-16 15:54:17 +02:00
cell: ({ row }) => {
return <StatusBadge status={row.getValue("status")} />;
2024-11-11 14:56:33 +01:00
},
},
{
id: "actions",
2025-05-16 15:54:17 +02:00
cell: ({ row, table }) => {
const status = row.getValue("status");
const rowData: Backup = row.original;
const fileName = rowData.file;
// @ts-ignore
2025-05-16 15:54:17 +02:00
const { extendedProps } = table.options.meta;
2025-05-16 15:54:17 +02:00
const router = useRouter();
const mutationRestore = useMutation({
mutationFn: async () => {
2024-12-14 17:52:10 +01:00
const restoration = await createRestorationAction({
backupId: rowData.id,
2025-05-16 15:54:17 +02:00
databaseId: rowData.databaseId,
});
2024-12-14 17:52:10 +01:00
if (restoration.data.success) {
toast.success(restoration.data.actionSuccess?.message || "Restoration created successfully!");
2025-05-16 15:54:17 +02:00
router.refresh();
} else {
2024-12-14 17:52:10 +01:00
toast.error(restoration.serverError || "Failed to create restoration.");
}
2025-05-16 15:54:17 +02:00
},
});
2025-05-16 15:54:17 +02:00
const mutationDeleteBackup = useMutation({
2024-12-31 12:15:18 +01:00
mutationFn: async () => {
const restoration = await deleteBackupAction({
backupId: rowData.id,
2025-05-16 15:54:17 +02:00
databaseId: rowData.databaseId,
});
2024-12-31 12:15:18 +01:00
if (restoration.data.success) {
toast.success(restoration.data.actionSuccess.message);
2025-05-16 15:54:17 +02:00
router.refresh();
2024-12-31 12:15:18 +01:00
} else {
toast.error(restoration.data.actionError.message);
}
2025-05-16 15:54:17 +02:00
},
});
2024-12-31 12:15:18 +01:00
const handleRestore = async () => {
2025-05-16 15:54:17 +02:00
await mutationRestore.mutateAsync();
};
2024-11-11 14:56:33 +01:00
2024-12-31 12:15:18 +01:00
const handleDelete = async () => {
2025-05-16 15:54:17 +02:00
await mutationDeleteBackup.mutateAsync();
};
2024-12-31 12:15:18 +01:00
const handleDownload = async (fileName: string) => {
2025-05-16 15:54:17 +02:00
const url = await getFileUrlPresignedLocal(fileName);
window.open(url, "_self");
};
2024-12-31 12:15:18 +01:00
2024-11-11 14:56:33 +01:00
return (
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button variant="ghost" className="h-8 w-8 p-0">
<span className="sr-only">Open menu</span>
2025-05-16 15:54:17 +02:00
<MoreHorizontal className="h-4 w-4" />
2024-11-11 14:56:33 +01:00
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end">
<DropdownMenuLabel>Actions</DropdownMenuLabel>
2025-05-16 15:54:17 +02:00
{status == "success" ? (
<>
2024-12-14 17:52:10 +01:00
<TooltipCustom disabled={extendedProps} text="Already a restoration waiting">
2025-05-16 15:54:17 +02:00
<DropdownMenuItem
disabled={mutationRestore.isPending || extendedProps}
onClick={async () => {
await handleRestore();
}}
>
<ReloadIcon /> Restore
</DropdownMenuItem>
</TooltipCustom>
2025-05-16 15:54:17 +02:00
<DropdownMenuItem
onClick={async () => {
await handleDownload(fileName);
}}
>
<Download /> Download
</DropdownMenuItem>
</>
2025-05-16 15:54:17 +02:00
) : null}
<DropdownMenuSeparator />
<DropdownMenuItem
className="text-red-600"
onClick={async () => {
await handleDelete();
}}
>
<Trash2 /> Delete
2024-11-11 20:31:35 +01:00
</DropdownMenuItem>
2024-11-11 14:56:33 +01:00
</DropdownMenuContent>
</DropdownMenu>
2025-05-16 15:54:17 +02:00
);
2024-11-11 14:56:33 +01:00
},
},
2025-05-16 15:54:17 +02:00
];