"use client"; import * as React from "react"; import { ColumnDef } from "@tanstack/react-table"; import { Eye, MoreVertical, SquarePen, Trash2 } 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 { useRouter } from "next/navigation"; import { deleteMedia } from "@/service/content/content"; import { error } from "@/lib/swal"; import Swal from "sweetalert2"; import withReactContent from "sweetalert2-react-content"; import Link from "next/link"; const useTableColumns = () => { const MySwal = withReactContent(Swal); const userLevelId = getCookiesDecrypt("ulie"); const columns: ColumnDef[] = [ { accessorKey: "no", header: "No", cell: ({ row }) => (

{row.getValue("no")}

), }, { accessorKey: "aliasName", header: "Name", cell: ({ row }) => (

{row.getValue("aliasName")}

), }, { accessorKey: "group", header: "Group", cell: ({ row }) => (

{row.getValue("group")}

), }, { accessorKey: "parentLevelName", header: "Parent Level", cell: ({ row }) => (

{row.getValue("parentLevelName")}

), }, // { // accessorKey: "createdAt", // header: "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: "creatorName", // header: "Creator Group", // cell: ({ row }) => ( // // {row.original.creatorName || row.original.createdByName || "-"} // // ), // }, // { // accessorKey: "creatorGroupLevelName", // header: "Source", // cell: ({ row }) => ( // // {row.getValue("creatorGroupLevelName") || "-"} // // ), // }, // { // accessorKey: "publishedOn", // header: "Published", // cell: ({ row }) => { // const isPublish = row.original.isPublish; // const isPublishOnPolda = row.original.isPublishOnPolda; // const creatorGroupParentLevelId = row.original.creatorGroupParentLevelId; // let displayText = "-"; // if (isPublish && !isPublishOnPolda) { // displayText = "Mabes"; // } else if (isPublish && isPublishOnPolda) { // if (Number(creatorGroupParentLevelId) === 761) { // displayText = "Mabes & Satker"; // } else { // displayText = "Mabes & Polda"; // } // } else if (!isPublish && isPublishOnPolda) { // if (Number(creatorGroupParentLevelId) === 761) { // displayText = "Satker"; // } else { // displayText = "Polda"; // } // } // return ( //
// {displayText} //
// ); // }, // }, // { // accessorKey: "statusName", // header: "Status", // cell: ({ row }) => { // const statusId = Number(row.original?.statusId); // const reviewedAtLevel = row.original?.reviewedAtLevel || ""; // const creatorGroupLevelId = Number(row.original?.creatorGroupLevelId); // const needApprovalFromLevel = Number(row.original?.needApprovalFromLevel); // const userHasReviewed = reviewedAtLevel.includes(`:${userLevelId}:`); // const isCreator = creatorGroupLevelId === Number(userLevelId); // const isWaitingForReview = statusId === 2 && !userHasReviewed && !isCreator; // const isApprovalNeeded = statusId === 1 && needApprovalFromLevel === Number(userLevelId); // const label = // isWaitingForReview || isApprovalNeeded // ? "Menunggu Review" // : statusId === 2 // ? "Diterima" // : row.original?.statusName; // const colors: Record = { // "Menunggu Review": "bg-orange-100 text-orange-600", // Diterima: "bg-green-100 text-green-600", // default: "bg-red-200 text-red-600", // }; // const statusStyles = colors[label] || colors.default; // return ( // // {label} // // ); // }, // }, { id: "actions", accessorKey: "action", header: "Action", enableHiding: false, cell: ({ row }) => { const router = useRouter(); const MySwal = withReactContent(Swal); async function doDelete(id: any) { const data = { id }; const response = await deleteMedia(data); if (response?.error) { error(response.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 handleDeleteMedia = (id: any) => { MySwal.fire({ title: "Hapus Data", icon: "warning", showCancelButton: true, cancelButtonColor: "#3085d6", confirmButtonColor: "#d33", confirmButtonText: "Hapus", }).then((result) => { if (result.isConfirmed) { doDelete(id); } }); }; const [isMabesApprover, setIsMabesApprover] = React.useState(false); const userId = getCookiesDecrypt("uie"); const userLevelId = getCookiesDecrypt("ulie"); const roleId = getCookiesDecrypt("urie"); React.useEffect(() => { if (userLevelId !== undefined && roleId !== undefined) { setIsMabesApprover(Number(userLevelId) === 216 && Number(roleId) === 3); } }, [userLevelId, roleId]); return ( View {/* {(Number(row.original.uploadedById) === Number(userId) || isMabesApprover) && ( */} Edit {/* )} */} handleDeleteMedia(row.original.id)} className="p-2 border-b text-destructive bg-destructive/30 focus:bg-destructive focus:text-white rounded-none" > Delete ); }, }, ]; return columns; }; export default useTableColumns;