- {/* Created By */}
-
-
Created By
- {allCreators.map((name, idx) => (
-
- handleToggleCreator(name)}
- />
-
-
- ))}
-
+ 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);
+ }
+ }
+ });
- {/* Organization */}
-
-
Organization
- {filteredOrganizations.map((org, idx) => (
-
- handleToggleOrg(org)}
- />
-
-
- ))}
-
+ 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);
+ };
+
+ 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 (
+ <>
+
+
+
+ -
+
+
+ -
+
+
+
-
- {/* Buttons */}
-
-
-
-
-
-