import { useEffect, useState } from 'react' import type { CollaboratorPermission } from '../../data/DiagramCollaboratorRepository' import { SelfCollaboratorError, UnknownUsernameError } from '../../data/DiagramCollaboratorRepository' import { useDiagramCollaboratorStore } from '../../state/diagramCollaboratorStore' import { useDiagramStore } from '../../state/diagramStore' import Modal from '../common/Modal' export default function DiagramSharingModal({ onClose }: { onClose: () => void }) { const diagram = useDiagramStore((s) => s.diagram) const access = useDiagramStore((s) => s.access) const isOwner = access?.myPermission === 'owner' const diagramId = diagram.id const collaborators = useDiagramCollaboratorStore((s) => s.collaborators) const isLoaded = useDiagramCollaboratorStore((s) => s.isLoaded) const load = useDiagramCollaboratorStore((s) => s.load) const add = useDiagramCollaboratorStore((s) => s.add) const updatePermission = useDiagramCollaboratorStore((s) => s.updatePermission) const remove = useDiagramCollaboratorStore((s) => s.remove) const [username, setUsername] = useState('') const [permission, setPermission] = useState('view') const [error, setError] = useState(null) const [busy, setBusy] = useState(false) useEffect(() => { load(diagramId) }, [diagramId, load]) const handleAdd = async () => { const trimmed = username.trim() if (!trimmed) return setError(null) setBusy(true) try { await add(diagramId, trimmed, permission) setUsername('') } catch (err) { setError( err instanceof UnknownUsernameError || err instanceof SelfCollaboratorError ? err.message : 'Could not add that person. Try again.', ) } finally { setBusy(false) } } const handleRemove = (userId: string, name: string) => { const confirmed = window.confirm(`Remove "${name}" from this diagram? They'll immediately lose access.`) if (confirmed) remove(diagramId, userId) } return (
{isOwner && (
setUsername(e.target.value)} onKeyDown={(e) => e.key === 'Enter' && handleAdd()} placeholder="Username" className="w-0 min-w-0 flex-1 rounded border border-slate-300 px-1.5 py-1 text-xs" />
{error &&

{error}

}
)}
{!isLoaded &&

Loading…

} {isLoaded && collaborators.length === 0 && (

Not shared with anyone yet{isOwner ? ' — add someone above.' : '.'}

)} {collaborators.map((c) => (
{c.username} {isOwner ? (
) : ( {c.permission === 'edit' ? 'Can edit' : 'Can view'} )}
))}
) }