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