import React, { useEffect, useState } from "react"; import { exportToCSV, exportToExcel, printTable, exportToPDF } from "../../utils/tableExportUtils"; const DirectoryPageHeader = ({ searchText, setSearchText, setIsActive, viewType, setViewType, filteredBuckets, tempSelectedBucketIds, handleTempBucketChange, filteredCategories, tempSelectedCategoryIds, handleTempCategoryChange, clearFilter, applyFilter, loading, IsActive, contactsToExport, notesToExport, selectedNoteNames, setSelectedNoteNames, notesForFilter, setFilterAppliedNotes }) => { const [filtered, setFiltered] = useState(0); const [filteredNotes, setFilteredNotes] = useState([]); const [noteCreators, setNoteCreators] = useState([]); const [allCreators, setAllCreators] = useState([]); const [allOrganizations, setAllOrganizations] = useState([]); const [filteredOrganizations, setFilteredOrganizations] = useState([]); const [selectedCreators, setSelectedCreators] = useState([]); const [selectedOrgs, setSelectedOrgs] = useState([]); useEffect(() => { setFiltered(tempSelectedBucketIds?.length + tempSelectedCategoryIds?.length); }, [tempSelectedBucketIds, tempSelectedCategoryIds]); useEffect(() => { if (viewType === "notes") { if (notesToExport && notesToExport.length > 0) { const uniqueNames = [...new Set(notesToExport.map(note => { const firstName = note.createdBy?.firstName || ""; const lastName = note.createdBy?.lastName || ""; return `${firstName} ${lastName}`.trim(); }).filter(name => name !== ""))]; setNoteCreators(uniqueNames.sort()); } else { setNoteCreators([]); } } else { setNoteCreators([]); } }, [notesToExport, viewType]); // Separate effect to clear selection only when switching away from notes useEffect(() => { if (viewType !== "notes" && selectedNoteNames.length > 0) { setSelectedNoteNames([]); } }, [viewType]); useEffect(() => { const creatorsSet = new Set(); const orgsSet = new Set(); notesForFilter.forEach((note) => { const creator = `${note.createdBy?.firstName || ""} ${note.createdBy?.lastName || ""}`.trim(); if (creator) creatorsSet.add(creator); const org = note.organizationName; if (org) orgsSet.add(org); }); setAllCreators([...creatorsSet].sort()); setAllOrganizations([...orgsSet].sort()); setFilteredOrganizations([...orgsSet].sort()); }, [notesForFilter]) const handleToggleNoteName = (name) => { setSelectedNoteNames(prevSelectedNames => { if (prevSelectedNames.includes(name)) { return prevSelectedNames.filter(n => n !== name); } else { return [...prevSelectedNames, name]; } }); }; const updateFilteredOrganizations = () => { if (selectedCreators.length === 0) { setFilteredOrganizations(allOrganizations); return; } const filteredOrgsSet = new Set(); notesForFilter.forEach((note) => { const creator = `${note.createdBy?.firstName || ""} ${note.createdBy?.lastName || ""}`.trim(); if (selectedCreators.includes(creator)) { if (note.organizationName) { filteredOrgsSet.add(note.organizationName); } } }); setFilteredOrganizations([...filteredOrgsSet].sort()); }; const handleToggleCreator = (name) => { const updated = selectedCreators.includes(name) ? selectedCreators.filter((n) => n !== name) : [...selectedCreators, name]; setSelectedCreators(updated); }; const handleToggleOrg = (name) => { const updated = selectedOrgs.includes(name) ? selectedOrgs.filter((n) => n !== name) : [...selectedOrgs, name]; setSelectedOrgs(updated); }; useEffect(() => { updateFilteredOrganizations(); }, [selectedCreators]); const handleExport = (type) => { let dataToExport = []; if (viewType === "notes") { if (!notesToExport || notesToExport.length === 0) { console.warn("No notes to export."); return; } const decodeHtmlEntities = (html) => { const textarea = document.createElement("textarea"); textarea.innerHTML = html; return textarea.value; }; const cleanNoteText = (html) => { if (!html) return ""; const stripped = html.replace(/<[^>]+>/g, ""); const decoded = decodeHtmlEntities(stripped); return decoded.replace(/\u00A0/g, " ").replace(/\s+/g, " ").trim(); }; const cleanName = (name) => { if (!name) return ""; return name.replace(/\u00A0/g, " ").replace(/\s+/g, " ").trim(); }; dataToExport = notesToExport.map(note => ({ "Name": cleanName(`${note.createdBy?.firstName || ""} ${note.createdBy?.lastName || ""}`), "Notes": cleanNoteText(note.note), "Created At": note.createdAt ? new Date(note.createdAt).toLocaleString("en-IN") : "", "Updated At": note.updatedAt ? new Date(note.updatedAt).toLocaleString("en-IN") : "", "Updated By": cleanName( `${note.updatedBy?.firstName || ""} ${note.updatedBy?.lastName || ""}` ), })); } else { if (!contactsToExport || contactsToExport.length === 0) { console.warn("No contacts to export."); return; } dataToExport = contactsToExport.map(contact => ({ Name: contact.name || '', Organization: contact.organization || '', Email: contact.contactEmails?.map(email => email.emailAddress).join(', ') || '', Phone: contact.contactPhones?.map(phone => phone.phoneNumber).join(', ') || '', Category: contact.contactCategory?.name || '', Tags: contact.tags?.map(tag => tag.name).join(', ') || '', })); } const today = new Date(); const formattedDate = `${today.getFullYear()}${String(today.getMonth() + 1).padStart(2, '0')}${String(today.getDate()).padStart(2, '0')}`; const filename = viewType === "notes" ? `Directory_Notes_${formattedDate}` : `Directory_Contacts_${formattedDate}`; switch (type) { case "csv": exportToCSV(dataToExport, filename); break; case "excel": exportToExcel(dataToExport, filename); break; case "pdf": exportToPDF(dataToExport, filename); break; case "print": printTable(dataToExport, filename); break; default: break; } }; const applyCombinedFilter = () => { const lowerSearch = searchText?.toLowerCase() || ""; const filtered = notesForFilter.filter((noteItem) => { const creator = `${noteItem.createdBy?.firstName || ""} ${noteItem.createdBy?.lastName || ""}`.trim(); const org = noteItem.organizationName; const matchesCreator = selectedCreators.length === 0 || selectedCreators.includes(creator); const matchesOrg = selectedOrgs.length === 0 || selectedOrgs.includes(org); const plainNote = noteItem?.note?.replace(/<[^>]+>/g, "").toLowerCase(); const stringValues = []; const extractStrings = (obj) => { for (const key in obj) { const value = obj[key]; if (typeof value === "string") { stringValues.push(value.toLowerCase()); } else if (typeof value === "object" && value !== null) { extractStrings(value); } } }; extractStrings(noteItem); stringValues.push(plainNote, creator.toLowerCase()); const matchesSearch = stringValues.some((val) => val.includes(lowerSearch)); return matchesCreator && matchesOrg && matchesSearch; }); setFilteredNotes(filtered); setFilterAppliedNotes(filtered); }; return ( <>
Created By
{allCreators.map((name, idx) => (Organization
{filteredOrganizations.map((org, idx) => (Filter by
Buckets
Categories