import { useState, useEffect } from 'react' import { Download, X } from 'lucide-react' import { flowTransferApi } from '@/api/flowTransfer' import { toast } from '@/lib/toast' import { Button } from '@/components/ui/Button' interface ExportFlowModalProps { treeId: string treeName: string onClose: () => void } function slugify(name: string): string { return name.toLowerCase().trim().replace(/[^\w\s-]/g, '').replace(/[-\s]+/g, '-') } export function ExportFlowModal({ treeId, treeName, onClose }: ExportFlowModalProps) { const [isExporting, setIsExporting] = useState(false) useEffect(() => { const handleKeyDown = (e: KeyboardEvent) => { if (e.key === 'Escape') onClose() } document.addEventListener('keydown', handleKeyDown) return () => document.removeEventListener('keydown', handleKeyDown) }, [onClose]) const handleExport = async () => { setIsExporting(true) try { const blob = await flowTransferApi.exportFlow(treeId) const url = URL.createObjectURL(blob) const a = document.createElement('a') a.href = url a.download = `${slugify(treeName)}.rfflow` document.body.appendChild(a) a.click() document.body.removeChild(a) URL.revokeObjectURL(url) toast.success('Flow exported successfully') onClose() } catch (err) { console.error('Export failed:', err) toast.error('Failed to export flow') } finally { setIsExporting(false) } } return (
Export {treeName} as a .rfflow file (JSON format).