import { AlertDialog, AlertDialogBody, AlertDialogContent, AlertDialogFooter, AlertDialogHeader, AlertDialogOverlay, Box, Button, Flex, IconButton, Menu, MenuButton, MenuItem, MenuList, Text, useDisclosure, useToast, } from '@chakra-ui/react'; import { useMutation, useQueryClient } from '@tanstack/react-query'; import { __ } from '@wordpress/i18n'; import React, { useEffect, useRef, useState } from 'react'; import { BiDotsHorizontalRounded, BiEdit, BiReset, BiTrash, } from 'react-icons/bi'; import { HiDocumentDuplicate } from 'react-icons/hi'; import { LuCheck, LuPencil } from 'react-icons/lu'; import { useNavigate } from 'react-router-dom'; import API from '../../../../../assets/js/back-end/utils/api'; import { certificateBackendRoutes } from '../utils/routes'; import { certificateAddonUrls } from '../utils/urls'; import { PDFDRAFT_CSS, PX_PER_UNIT } from './pdfdraft-thumb-utils'; interface Certificate { id: number; name: string; status: string; html_content?: string; rendered_html?: string; content_format?: string; date_modified?: string; date_created?: string; } interface Props { certificate: Certificate; bulkSelectMode?: boolean; selected?: boolean; onToggleSelect?: (id: string) => void; } function getLayoutFromCert(certificate: Certificate) { try { const json = JSON.parse(certificate.html_content ?? '{}'); return json?.settings?.layout ?? null; } catch { return null; } } function timeAgo(dateStr?: string): string { if (!dateStr) return ''; const diff = Date.now() - new Date(dateStr).getTime(); const mins = Math.floor(diff / 60000); const hours = Math.floor(mins / 60); const days = Math.floor(hours / 24); const weeks = Math.floor(days / 7); const months = Math.floor(days / 30); if (months > 0) return `${months}mo ago`; if (weeks > 0) return `${weeks}w ago`; if (days > 0) return `${days}d ago`; if (hours > 0) return `${hours}h ago`; if (mins > 0) return `${mins}m ago`; return 'just now'; } const CertificateCardV2: React.FC = ({ certificate, bulkSelectMode = false, selected = false, onToggleSelect, }) => { const navigate = useNavigate(); const toast = useToast(); const queryClient = useQueryClient(); const { isOpen: isDeleteOpen, onOpen: onDeleteOpen, onClose: onDeleteClose, } = useDisclosure(); const cancelRef = useRef(null); const certificateAPI = new API(certificateAddonUrls.certificates); const thumbRef = useRef(null); const [containerWidth, setContainerWidth] = useState(320); useEffect(() => { if (thumbRef.current) { setContainerWidth(thumbRef.current.offsetWidth); } }, []); const layout = getLayoutFromCert(certificate); const orientation = layout?.orientation ?? 'landscape'; const isPublished = certificate.status === 'publish'; const isTrashed = certificate.status === 'trash'; const mult = PX_PER_UNIT[layout?.unit ?? 'in'] ?? 96; const certW = Math.round((layout?.width ?? 11) * mult); const certH = Math.round((layout?.height ?? 8.5) * mult); const innerW = containerWidth - 16; const scale = innerW / certW; const THUMB_HEIGHT = Math.round(containerWidth * (210 / 297)); const thumbSrcdoc = certificate.rendered_html ? `${certificate.rendered_html}` : ''; const ago = timeAgo(certificate.date_modified ?? certificate.date_created); const onActionError = (err: any) => { toast({ title: err?.message || __('Something went wrong', 'learning-management-system'), status: 'error', isClosable: true, }); }; const deleteCertificate = useMutation({ mutationFn: () => certificateAPI.delete(certificate.id, { force: true }), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['certificatesV2List'] }); onDeleteClose(); toast({ title: __('Certificate deleted', 'learning-management-system'), status: 'success', isClosable: true, }); }, onError: onActionError, }); const trashCertificate = useMutation({ mutationFn: () => certificateAPI.delete(certificate.id), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['certificatesV2List'] }); toast({ title: __('Certificate moved to trash', 'learning-management-system'), status: 'success', isClosable: true, }); }, onError: onActionError, }); const restoreCertificate = useMutation({ mutationFn: () => certificateAPI.restore(certificate.id), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['certificatesV2List'] }); toast({ title: __('Certificate restored', 'learning-management-system'), status: 'success', isClosable: true, }); }, onError: onActionError, }); const duplicateCertificate = useMutation({ // Use the server-side clone endpoint, which copies post_content via a // direct $wpdb write (bypassing wp_kses_post). Re-POSTing html_content // through store() runs it through kses and corrupts the PDFDraft JSON, // producing a blank duplicate (MAS-3657). mutationFn: () => certificateAPI.cloneData(certificate.id), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['certificatesV2List'] }); toast({ title: __('Certificate duplicated', 'learning-management-system'), status: 'success', isClosable: true, }); }, onError: (err: any) => { toast({ title: err?.message || __('Something went wrong', 'learning-management-system'), status: 'error', isClosable: true, }); }, }); const handleEdit = () => { if (isTrashed) { return; } navigate( certificateBackendRoutes.certificate.edit.replace( ':certificateId', String(certificate.id), ), { state: { certificate } }, ); }; return ( {/* Thumbnail with hover overlay */} onToggleSelect?.(String(certificate.id)) : handleEdit } > {thumbSrcdoc ? (