import React, { useEffect, useState } from "react"; import { useContactProfile } from "../../hooks/useDirectory"; import Avatar from "../common/Avatar"; import moment from "moment"; import NotesDirectory from "./NotesDirectory"; const ProfileContactDirectory = ({ contact, setOpen_contact, closeModal }) => { const { contactProfile, loading, refetch } = useContactProfile(contact?.id); const [copiedIndex, setCopiedIndex] = useState(null); const [profileContactState, setProfileContactState] = useState(null); const [expanded, setExpanded] = useState(false); const description = profileContactState?.description || ""; const limit = 500; const toggleReadMore = () => setExpanded(!expanded); const isLong = description.length > limit; const displayText = expanded ? description : description.slice(0, limit) + (isLong ? "..." : ""); useEffect(() => { if (contactProfile) { const names = (contact?.name || "").trim().split(" "); let firstName = ""; let middleName = ""; let lastName = ""; let fullName = contact?.name || ""; // Logic to determine first, middle, and last names if (names.length === 1) { firstName = names[0]; } else if (names.length === 2) { firstName = names[0]; lastName = names[1]; } else if (names.length >= 3) { firstName = names[0]; middleName = names[1]; // This was an error in the original prompt, corrected to names[1] lastName = names[names.length - 1]; // Reconstruct full name to be precise with spacing fullName = `${firstName} ${middleName ? middleName + ' ' : ''}${lastName}`; } else { // Fallback if no names or empty string firstName = "Contact"; fullName = "Contact"; } setProfileContactState({ ...contactProfile, firstName: contactProfile.firstName || firstName, // Adding middleName and lastName to the state for potential future use or more granular access middleName: contactProfile.middleName || middleName, lastName: contactProfile.lastName || lastName, fullName: contactProfile.fullName || fullName, // Prioritize fetched fullName, fallback to derived }); } }, [contactProfile, contact?.name]); const handleCopy = (email, index) => { navigator.clipboard.writeText(email); setCopiedIndex(index); setTimeout(() => setCopiedIndex(null), 2000); }; return (

Contact Profile

{contact?.name} {profileContactState?.designation}
{profileContactState?.contactEmails?.length > 0 && (
Email :
    {profileContactState.contactEmails.map((email, idx) => (
  • {email.emailAddress} handleCopy(email.emailAddress, idx)} >
  • ))}
)} {profileContactState?.contactPhones?.length > 0 && (
Phone :
    {profileContactState.contactPhones.map((phone, idx) => (
  • {phone.phoneNumber} {idx < profileContactState.contactPhones.length - 1 && ","}
  • ))}
)} {profileContactState?.createdAt && (
Created :
{moment(profileContactState.createdAt).format("DD MMMM, YYYY")}
)} {profileContactState?.address && (
Location :
{profileContactState.address}
)}
{profileContactState?.organization && (
Organization :
{profileContactState.organization}
)} {profileContactState?.contactCategory && (
Category :
  • {profileContactState.contactCategory.name}
)} {profileContactState?.tags?.length > 0 && (
Tags :
    {profileContactState.tags.map((tag, index) => (
  • {tag.name}
  • ))}
)} {profileContactState?.buckets?.length > 0 && (
Buckets :
    {profileContactState.buckets.map((bucket) => (
  • {bucket.name}
  • ))}
)}
{profileContactState?.projects?.length > 0 && (
Projects :
    {profileContactState.projects.map((project, index) => (
  • {project.name} {index < profileContactState.projects.length - 1 && ","}
  • ))}
)}
Description :
{displayText} {isLong && ( <>
{expanded ? "Read less" : "Read more"} )}

); }; export default ProfileContactDirectory;