import React, { useEffect, useState } from 'react' import { RootState } from '../../../../store/store'; import { useSelector } from 'react-redux'; import { DroppedItemsObj } from '../DocumentContainer'; import { MdKeyboardArrowUp, MdMoreVert } from 'react-icons/md'; import { PiTagSimpleFill } from 'react-icons/pi'; const DocumentPages: React.FC<{ isPages: boolean; droppedItems: DroppedItemsObj; setIsPages: React.Dispatch>; }> = ({ isPages, droppedItems, setIsPages }) => { type RecipientPageTagInfo = { color: string; recipientIndex: number; recipientTagCount?: number; }; type DocumentTagsMapping = { [documentId: string]: { [pageIndex: string]: RecipientPageTagInfo[]; }; }; const selectedDocuments = useSelector((state: RootState) => state.documents.documents) const [documentPagesTagsMapping, setDocumentPagesTagsMapping] = useState({}); const recipients = useSelector((state: RootState) => state.selectedRecipients.recipients); const [openDocs, setOpenDocs] = useState>( () => Object.fromEntries(selectedDocuments.map((d, i) => [d.id, i === 0])) ) const [activePage, setActivePage] = useState( selectedDocuments[0] ? `${selectedDocuments[0].id}_0` : null ) const toggleDoc = (docId: string) => { setOpenDocs(prev => ({ ...prev, [docId]: !prev[docId] })) } const goToPage = (docId: string, pageIndex: number) => { const key = `${docId}_${pageIndex}` setActivePage(key) window.location.hash = docId + "" + pageIndex } // useEffect(() => { // setTimeout(() => { // let documentPagesTagsMappingLocal: { // [documentId: string]: { // [pageIndex: number]: [number, string]; // }; // }[] = []; // recipients && recipients.length && recipients.forEach((selectedRecipient, selectedRecipientIndx) => { // const selectedRecipientColor = selectedRecipient.color // let activeRecipientTagsCount = 0; // droppedItems && Object.keys(droppedItems).length > 0 && Object.keys(droppedItems).forEach(key => { // activeRecipientTagsCount = (droppedItems[key] && droppedItems[key].length) > 0 ? droppedItems[key].reduce((acc, item) => !item.isDeleted && item.recipientIndx == selectedRecipientIndx ? acc + 1 : acc, 0) : 0; // if (activeRecipientTagsCount == 0) return; // const [documentId, pageIndex] = key.split("_"); // if (!documentPagesTagsMappingLocal[documentId]) { // documentPagesTagsMappingLocal[documentId] = {}; // } // documentPagesTagsMappingLocal.push({ [documentId]: { [pageIndex]: [activeRecipientTagsCount, selectedRecipientColor] } }) // }) // }) // setDocumentPagesTagsMapping(documentPagesTagsMappingLocal) // }, 300) // // }, [droppedItems, selectedRecipientIndx]) // }, [droppedItems]) useEffect(() => { setTimeout(() => { let documentTagsMappingLocal: DocumentTagsMapping = {}; recipients && recipients.length && recipients.forEach((selectedRecipient, selectedRecipientIndx) => { const selectedRecipientColor = selectedRecipient.color; droppedItems && Object.keys(droppedItems).length > 0 && Object.keys(droppedItems).forEach(key => { const activeRecipientTagsCount = (droppedItems[key] && droppedItems[key].length) > 0 ? droppedItems[key].reduce((acc, item) => { return !item.isDeleted && item.recipientIndx == selectedRecipientIndx ? acc + 1 : acc; }, 0) : 0; if (activeRecipientTagsCount === 0) return; const [documentId, pageIndex] = key.split("_"); // Create document object if (!documentTagsMappingLocal[documentId]) { documentTagsMappingLocal[documentId] = {}; } // Create page array if (!documentTagsMappingLocal[documentId][pageIndex]) { documentTagsMappingLocal[documentId][pageIndex] = []; } // Push recipient info documentTagsMappingLocal[documentId][pageIndex].push({ color: selectedRecipientColor, recipientIndex: selectedRecipientIndx, recipientTagCount: activeRecipientTagsCount, }); }); }); setDocumentPagesTagsMapping(documentTagsMappingLocal); }, 300); }, [droppedItems]); useEffect(() => { setTimeout(() => { if (!isPages) return const docPagesCon = document.getElementById("document-pages"); if (docPagesCon) { docPagesCon.focus(); } }, 0) }, [isPages]); return (
window.innerWidth <= 638 && setIsPages(false)} className={`h-[100vh] absolute ${isPages || window.innerWidth > 638 ? "" : "hidden"} top-0 left-0 sm:static max-w-max bg-slate-100 z-50 sm:col-span-2 flex flex-col w-full overflow-y-auto`}> {selectedDocuments.map((document, indx) => (
{/* Section label (if any) */} {document.section && (

{document.section}

)} {/* Document Header */}
toggleDoc(document.id)} className="flex items-start justify-between px-3 py-2.5 cursor-pointer hover:bg-slate-50 transition-colors" >

{document.name}

{document.pages?.length} {document.pages?.length === 1 ? 'page' : 'pages'}

{/* Page Thumbnails */} {openDocs[document.id] && (
{document.pages?.map((_: any, pageIndex: number) => { const key = `${document.id}_${pageIndex}` const isActive = activePage === key return (
goToPage(document.id, pageIndex)} className="flex flex-col items-center py-2 px-1 rounded-md cursor-pointer hover:bg-slate-50 transition-colors" > {/* Big Thumbnail */}
{document.thumbnail ? ( {`Page ) : (
Document
)} {/* Page number + dots menu */}
{pageIndex + 1} { // <> // // }
{documentPagesTagsMapping[document.id]?.[pageIndex]?.length > 0 && ( <> {documentPagesTagsMapping[document.id][pageIndex].map((tagInfo, indx) => (
{/* Count */} {/* Optional */} {/* {tagInfo.recipientTagCount} */} {/* Tag */}
))} )}
) })}
)}
))}
) } export default DocumentPages