'use client' import { useState, useEffect, useCallback } from 'react' import { getCollaborators, inviteCollaborator, updateCollaboratorRole, removeCollaborator, type Collaborator, } from '@/app/editor/[projectId]/actions' type ShareModalProps = { projectId: string isOwner: boolean onClose: () => void } export default function ShareModal({ projectId, isOwner, onClose }: ShareModalProps) { const [collaborators, setCollaborators] = useState([]) const [loading, setLoading] = useState(true) const [email, setEmail] = useState('') const [role, setRole] = useState<'editor' | 'viewer'>('editor') const [inviting, setInviting] = useState(false) const [error, setError] = useState(null) const [successMessage, setSuccessMessage] = useState(null) const fetchCollaborators = useCallback(async () => { const result = await getCollaborators(projectId) if (result.success && result.data) { setCollaborators(result.data) } setLoading(false) }, [projectId]) useEffect(() => { fetchCollaborators() }, [fetchCollaborators]) const handleInvite = async (e: React.FormEvent) => { e.preventDefault() if (!email.trim()) return setInviting(true) setError(null) setSuccessMessage(null) const result = await inviteCollaborator(projectId, email.trim(), role) if (result.success) { setEmail('') setSuccessMessage('Collaborator invited successfully') fetchCollaborators() } else { setError(result.error || 'Failed to invite collaborator') } setInviting(false) } const handleRoleChange = async (collaboratorId: string, newRole: 'editor' | 'viewer') => { setError(null) const result = await updateCollaboratorRole(projectId, collaboratorId, newRole) if (result.success) { setCollaborators((prev) => prev.map((c) => (c.id === collaboratorId ? { ...c, role: newRole } : c)) ) } else { setError(result.error || 'Failed to update role') } } const handleRemove = async (collaboratorId: string) => { setError(null) const result = await removeCollaborator(projectId, collaboratorId) if (result.success) { setCollaborators((prev) => prev.filter((c) => c.id !== collaboratorId)) } else { setError(result.error || 'Failed to remove collaborator') } } return (
) }