import { useCallback, useEffect, useState } from 'react'; import { createPortal } from 'react-dom'; import { Input } from '@/components/ui/input'; import { Button } from '@/components/ui/button'; import { KeyHint } from '@/components/key-hint'; import { updateParticleProperties } from '@/lib/firestore-particles'; import { toFirestoreDocPath, type ParticlePath } from '@/lib/particle-path'; import type { Particle } from '@/api/types'; import { useSuspendPlayback } from '@/hooks/use-suspend-playback'; interface RenameStreamOverlayProps { streamPath: ParticlePath; streamParticle: Particle & { type: 'stream' }; onClose: () => void; } export function RenameStreamOverlay({ streamPath, streamParticle, onClose, }: RenameStreamOverlayProps) { useSuspendPlayback(true, 'rename-stream'); const [name, setName] = useState(streamParticle.properties.name); const [saving, setSaving] = useState(false); const trimmed = name.trim(); const canSave = !saving && trimmed.length > 0 && trimmed !== streamParticle.properties.name; const handleSave = useCallback(async () => { if (!canSave) return; setSaving(true); try { const docPath = toFirestoreDocPath(streamPath); await updateParticleProperties<'stream'>(docPath, { name: trimmed }); onClose(); } finally { setSaving(false); } }, [canSave, streamPath, onClose, trimmed]); useEffect(() => { const handler = (e: KeyboardEvent) => { if (e.key === 'Escape') { e.preventDefault(); e.stopPropagation(); onClose(); } }; window.addEventListener('keydown', handler, { capture: true }); return () => window.removeEventListener('keydown', handler, { capture: true }); }, [onClose]); return createPortal(