import { useState, useEffect } from 'react'; export default function CollaboratorFacepile({ collectionId, creatorEmail }) { const [collaborators, setCollaborators] = useState([]); const [loading, setLoading] = useState(true); useEffect(() => { if (collectionId) { fetchCollaborators(); } }, [collectionId]); const fetchCollaborators = async () => { try { const response = await fetch(`/api/collections/${collectionId}/permissions`, { headers: { 'Authorization': `Bearer ${localStorage.getItem('auth_token')}` } }); if (response.ok) { const data = await response.json(); setCollaborators(data.permissions || []); } else { console.error('Failed to fetch collaborators:', response.status); setCollaborators([]); // Fallback to empty array } } catch (error) { console.error('Error fetching collaborators:', error); setCollaborators([]); // Fallback to empty array } finally { setLoading(false); } }; if (loading) { return (
Crafted by {creatorEmail}
); } // Include creator and active collaborators const allUsers = [ { email: creatorEmail, role: 'owner', status: 'active' }, ...collaborators.filter(c => c.status === 'active') ]; const visibleUsers = allUsers.slice(0, 4); // Show max 4 faces const remainingCount = Math.max(0, allUsers.length - 4); const getRoleColor = (role) => { switch (role) { case 'owner': return 'bg-purple-600'; case 'editor': return 'bg-blue-600'; case 'viewer': return 'bg-green-600'; default: return 'bg-gray-600'; } }; const getRoleLabel = (role) => { switch (role) { case 'owner': return 'Owner'; case 'editor': return 'Collaborator'; case 'viewer': return 'Viewer'; default: return 'Member'; } }; // Debug: Log removed - functionality working return (
Crafted by {creatorEmail} {allUsers.length > 1 && ( & {allUsers.length - 1} other{allUsers.length > 2 ? 's' : ''} )} {allUsers.length > 1 && (
{visibleUsers.map((user, index) => (
{user.email ? user.email.charAt(0).toUpperCase() : '?'}
{/* Tooltip */}
{user.email}
{getRoleLabel(user.role)}
{/* Arrow */}
))} {remainingCount > 0 && (
+{remainingCount}
{/* Tooltip for remaining users */}
{remainingCount} more collaborator{remainingCount > 1 ? 's' : ''}
{allUsers.slice(4).map((user, index) => (
{user.email} ({getRoleLabel(user.role)})
))} {/* Arrow */}
)}
)}
); }