import * as React from "react"; import { ColumnDef } from "@tanstack/react-table"; import { Eye, MoreVertical, SquarePen, Trash2, Upload } from "lucide-react"; import { cn, getCookiesDecrypt } from "@/lib/utils"; import { DropdownMenu, DropdownMenuContent, DropdownMenuTrigger, DropdownMenuItem, } from "@/components/ui/dropdown-menu"; import { Button } from "@/components/ui/button"; import { Badge } from "@/components/ui/badge"; import { format } from "date-fns"; import { Link } from "@/components/navigation"; import { useRouter } from "next/navigation"; import { useToast } from "@/components/ui/use-toast"; import { deleteCategory } from "@/service/settings/settings"; import { deleteTaskTa } from "@/service/task"; import { error, loading } from "@/lib/swal"; import withReactContent from "sweetalert2-react-content"; import Swal from "sweetalert2"; import { useTranslations } from "next-intl"; const useTableColumns = ( activeTab: "ta" | "daily" | "special" | "mabes-koor", ) => { const t = useTranslations("Table"); const columns: ColumnDef[] = [ { accessorKey: "no", header: t("no", { defaultValue: "No" }), cell: ({ row }) => {row.getValue("no")}, }, { accessorKey: "title", header: t("title", { defaultValue: "Title" }), cell: ({ row }) => (
{row.getValue("title")} {row.original.isForward && ( )}
), }, { accessorKey: "uniqueCode", header: t("code", { defaultValue: "Code" }), cell: ({ row }) => {row.getValue("uniqueCode")}, }, { accessorKey: "createdAt", header: t("upload-date", { defaultValue: "Upload Date" }), 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}; }, }, { accessorKey: "status", header: "Status", cell: ({ row }) => { const isActive = row.original.isActive; const isDone = row.original.isDone; let statusText = ""; if (isDone) { statusText = "Selesai"; } else if (isActive) { statusText = "Aktif"; } else { statusText = "Nonaktif"; } const statusColors: Record = { Aktif: "bg-primary/20 text-primary", Selesai: "bg-success/20 text-success", Nonaktif: "bg-gray-200 text-gray-500", }; const statusStyles = statusColors[statusText] || "default"; return ( {statusText} ); }, }, { id: "actions", accessorKey: "action", header: t("action", { defaultValue: "Action" }), enableHiding: false, cell: ({ row }) => { const router = useRouter(); const MySwal = withReactContent(Swal); const roleId = Number(getCookiesDecrypt("urie")) || 0; // ❗ jika tab = "special" if (activeTab === "special") { return ( View ); } async function deleteProcess(id: any) { loading(); const resDelete = await deleteTaskTa(id); if (resDelete?.error) { error(resDelete.message); return false; } success(); } function success() { MySwal.fire({ title: "Sukses", icon: "success", confirmButtonColor: "#3085d6", confirmButtonText: "OK", }).then((result) => { if (result.isConfirmed) { window.location.reload(); } }); } const TaskDelete = (id: any) => { MySwal.fire({ title: "Hapus Data", text: "", icon: "warning", showCancelButton: true, cancelButtonColor: "#3085d6", confirmButtonColor: "#d33", confirmButtonText: "Hapus", }).then((result) => { if (result.isConfirmed) { deleteProcess(id); } }); }; return ( {/* {(roleId == 11 || roleId == 12 || roleId == 19) && ( */} {(roleId == 11 || roleId == 12 || roleId == 19 || roleId == 3) && ( View )} {roleId == 11 || (roleId == 3 && ( Edit ))} {(roleId == 12 || roleId == 19) && ( Upload Tugas )} {roleId == 11 || (roleId == 3 && ( TaskDelete(row.original.id)} className="p-2 border-b text-destructive bg-destructive/30 focus:bg-destructive focus:text-destructive-foreground rounded-none" > Delete ))} ); }, }, ]; return columns; }; export default useTableColumns;