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 withReactContent from "sweetalert2-react-content"; import { deleteMedia } from "@/service/content/content"; import { error } from "@/lib/swal"; import Swal from "sweetalert2"; 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: "title", header: "Title", cell: ({ row }: { row: { getValue: (key: string) => string } }) => { const title: string = row.getValue("title"); return ( {title.length > 50 ? `${title.slice(0, 30)}...` : title} ); }, }, { accessorKey: "categoryName", header: "Category Name", cell: ({ row }) => { const categoryName = row.getValue("categoryName"); const categories = row.original.categories; // Handle new API structure with categories array const displayName = categoryName || (categories && categories.length > 0 ? categories[0].title : "-"); return ( {displayName} ); }, }, { 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.getValue("creatorName") || row.getValue("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 statusColors: Record = { diterima: "bg-green-100 text-green-600", "menunggu review": "bg-orange-100 text-orange-600", }; const colors = [ "bg-orange-100 text-orange-600", "bg-orange-100 text-orange-600", "bg-green-100 text-green-600", "bg-blue-100 text-blue-600", "bg-red-200 text-red-600", ]; const status = Number(row.original?.statusId) == 2 && row.original?.reviewedAtLevel !== null && !row.original?.reviewedAtLevel?.includes(`:${userLevelId}:`) && Number(row.original?.creatorGroupLevelId) != Number(userLevelId) ? "1" : row.original?.statusId; const statusStyles = colors[Number(status)] || "bg-red-200 text-red-600"; // const statusStyles = statusColors[status] || "bg-red-200 text-red-600"; return ( {(Number(row.original?.statusId) == 2 && !row.original?.reviewedAtLevel !== null && !row.original?.reviewedAtLevel?.includes( `:${Number(userLevelId)}:` ) && Number(row.original?.creatorGroupLevelId) != Number(userLevelId)) || (Number(row.original?.statusId) == 1 && Number(row.original?.needApprovalFromLevel) == Number(userLevelId)) ? "Menunggu Review" : row.original?.statusName}{" "} ); }, }, { id: "actions", accessorKey: "action", header: "Action", enableHiding: false, cell: ({ row }) => { const MySwal = withReactContent(Swal); async function doDelete(id: any) { // loading(); 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", text: "", 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 {/* Edit */} {(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 {/* {(row.original.uploadedById === userId || isMabesApprover) && ( handleDeleteMedia(row.original.id)} className="p-2 border-b text-destructive bg-destructive/30 focus:bg-destructive focus:text-destructive-foreground rounded-none" > Hapus )} */} ); }, }, ]; return columns; }; export default useTableColumns;