import { useState, useEffect } from 'react'; import BulkInviteModal from './BulkInviteModal'; export default function CollaborationManager({ collectionId, isOwner }) { const [permissions, setPermissions] = useState([]); const [loading, setLoading] = useState(true); const [showInviteModal, setShowInviteModal] = useState(false); const [showBulkInviteModal, setShowBulkInviteModal] = useState(false); const [inviteForm, setInviteForm] = useState({ email: '', role: 'editor', message: '' }); const [inviting, setInviting] = useState(false); useEffect(() => { if (collectionId) { fetchPermissions(); } }, [collectionId]); const fetchPermissions = async () => { try { const response = await fetch(`/api/collections/${collectionId}/permissions`); if (response.ok) { const data = await response.json(); setPermissions(data); } } catch (error) { console.error('Error fetching permissions:', error); } finally { setLoading(false); } }; const handleInviteUser = async (e) => { e.preventDefault(); setInviting(true); try { const response = await fetch(`/api/collections/${collectionId}/permissions`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(inviteForm) }); if (response.ok) { setShowInviteModal(false); setInviteForm({ email: '', role: 'editor', message: '' }); fetchPermissions(); // Refresh the list } else { const error = await response.json(); alert(error.error || 'Failed to send invitation'); } } catch (error) { alert('Network error. Please try again.'); } finally { setInviting(false); } }; const handleUpdatePermission = async (userId, newRole) => { try { const response = await fetch(`/api/collections/${collectionId}/permissions`, { method: 'PUT', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ userId, role: newRole }) }); if (response.ok) { fetchPermissions(); // Refresh the list } else { alert('Failed to update permission'); } } catch (error) { alert('Network error. Please try again.'); } }; const handleRemoveUser = async (userId) => { if (!confirm('Are you sure you want to remove this user from the collection?')) { return; } try { const response = await fetch(`/api/collections/${collectionId}/permissions`, { method: 'DELETE', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ userId }) }); if (response.ok) { fetchPermissions(); // Refresh the list } else { alert('Failed to remove user'); } } catch (error) { alert('Network error. Please try again.'); } }; const getRoleIcon = (role) => { switch (role) { case 'owner': return '👑'; case 'editor': return '✏️'; case 'viewer': return '👁️'; default: return '👤'; } }; const getStatusBadge = (status) => { const styles = { active: { backgroundColor: '#10b981', color: 'white' }, pending: { backgroundColor: '#f59e0b', color: 'white' }, declined: { backgroundColor: '#ef4444', color: 'white' } }; return ( {status} ); }; if (loading) { return (
); } return (

Collaboration ({permissions.length})

{isOwner && (
)}
{permissions.map((permission) => (
{getRoleIcon(permission.role)}
{permission.email} {permission.is_pending && ( (Pending Registration) )}
{permission.role} {getStatusBadge(permission.status)}
{isOwner && permission.role !== 'owner' && (
)}
))} {permissions.length === 0 && (
👥

No collaborators yet. Invite others to work on this collection together!

)}
{/* Invite Modal */} {showInviteModal && (

Invite Collaborator

Invite someone to help manage and add cards to this collection

setInviteForm({ ...inviteForm, email: e.target.value })} className="w-full px-4 py-3 rounded-lg border transition-all duration-200" style={{ backgroundColor: 'var(--bg-primary)', borderColor: 'var(--border)', color: 'var(--text-primary)' }} placeholder="Enter email address" />