import * as React from "react"; import { ColumnDef } from "@tanstack/react-table"; import { Eye, MoreVertical, SquarePen, Trash2 } from "lucide-react"; import { DropdownMenu, DropdownMenuContent, DropdownMenuTrigger, DropdownMenuItem, } from "@/components/ui/dropdown-menu"; import { Button } from "@/components/ui/button"; import { format } from "date-fns"; import { Link } from "@/components/navigation"; import { deleteTicketInternal } from "@/service/communication/communication"; import withReactContent from "sweetalert2-react-content"; import Swal from "sweetalert2"; import { useTranslations } from "next-intl"; const MySwal = withReactContent(Swal); const useTableColumns = (onDeleteSuccess?: () => void) => { const t = useTranslations("Table"); const handleDelete = async (id: any) => { MySwal.fire({ title: "Delete Data?", text: "Apakah Anda yakin ingin menghapus data ini?", icon: "warning", showCancelButton: true, cancelButtonColor: "#d33", confirmButtonColor: "#3085d6", confirmButtonText: "Ya, hapus", }).then(async (result) => { if (result.isConfirmed) { try { await deleteTicketInternal(id); MySwal.fire("Sukses", "Data berhasil dihapus!", "success"); if (onDeleteSuccess) onDeleteSuccess(); } catch (error) { console.log(error); MySwal.fire("Gagal", "Terjadi kesalahan saat menghapus", "error"); } } }); }; const columns: ColumnDef[] = [ { accessorKey: "no", header: t("no", { defaultValue: "No" }), cell: ({ row }) => {row.getValue("no")}, }, { accessorKey: "title", header: t("question", { defaultValue: "Question" }), cell: ({ row }) => ( {row.getValue("title")} ), }, { accessorKey: "createdBy", header: t("sender", { defaultValue: "Sender" }), cell: ({ row }) => { const createdBy = row.original.createdBy; // Akses properti category return ( {createdBy?.fullname || "N/A"} ); }, }, { accessorKey: "sendTo", header: t("sendto", { defaultValue: "Sendto" }), cell: ({ row }) => { const sendTo = row.original.sendTo; // Akses properti category return {sendTo?.fullname || "N/A"}; }, }, { accessorKey: "createdAt", header: t("time", { defaultValue: "Time" }), cell: ({ row }) => { const createdAt = row.getValue("createdAt") as | string | number | undefined; const formattedDate = createdAt && !isNaN(new Date(createdAt).getTime()) ? format(new Date(createdAt), "dd-MM-yyyy HH:mm:ss") : "-"; return {formattedDate}; }, }, { id: "actions", header: t("action"), cell: ({ row }) => ( {/* View */} View {/* Edit */} Edit {/* Delete */} handleDelete(row.original.id)} > Delete ), }, ]; return columns; }; export default useTableColumns;