import { useState } from 'react'; import { Pressable, Text, TextInput, View } from 'react-native'; import { toast } from 'sonner-native'; import { cn } from '@/lib/utils'; import { toUserMessage } from '@/lib/errors'; import { editTextParticleContent } from '@/lib/firestore-particles'; import { particlePath, toFirestoreDocPath } from '@/lib/particle-path'; import { useSuspendPlayback } from '@/hooks/use-suspend-playback'; import { BottomSheet } from '@/components/BottomSheet'; interface EditParticleSheetProps { open: boolean; onClose: () => void; networkId: string; streamId: string; particleId: string; currentContent: string; } export function EditParticleSheet({ open, onClose, networkId, streamId, particleId, currentContent, }: EditParticleSheetProps) { useSuspendPlayback(open, 'edit-particle'); const [content, setContent] = useState(currentContent); const [saving, setSaving] = useState(false); // Reset the editor each time the sheet opens fresh. const [prevOpen, setPrevOpen] = useState(open); if (open !== prevOpen) { setPrevOpen(open); if (open) { setContent(currentContent); setSaving(false); } } const trimmed = content.trim(); const canSave = !saving && trimmed.length > 0 && trimmed !== currentContent; const handleSave = async () => { if (!canSave) return; setSaving(true); try { const docPath = toFirestoreDocPath( particlePath(networkId, [streamId, particleId]), ); await editTextParticleContent(docPath, trimmed); onClose(); } catch (err) { toast.error(toUserMessage(err)); setSaving(false); } }; return ( Cancel Edit {saving ? 'Saving...' : 'Save'} ); }