import React, { useState } from "react"; import { motion, AnimatePresence } from "framer-motion"; import { X, Mail, Send, Loader2 } from "lucide-react"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; export default function ShareModal({ isOpen, onClose, onShare, extractionId }) { const [email, setEmail] = useState(""); const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(""); const [success, setSuccess] = useState(false); const [successMessage, setSuccessMessage] = useState(""); const handleSubmit = async (e) => { e.preventDefault(); setError(""); setSuccess(false); // Parse and validate multiple emails (comma or semicolon separated) if (!email.trim()) { setError("Please enter at least one recipient email address"); return; } // Split by comma or semicolon, trim each email, and filter out empty strings const emailList = email .split(/[,;]/) .map((e) => e.trim()) .filter((e) => e.length > 0); if (emailList.length === 0) { setError("Please enter at least one recipient email address"); return; } // Validate each email const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; const invalidEmails = emailList.filter((e) => !emailRegex.test(e)); if (invalidEmails.length > 0) { setError(`Invalid email address(es): ${invalidEmails.join(", ")}`); return; } setIsLoading(true); try { const result = await onShare(extractionId, emailList); setSuccessMessage(result?.message || `Successfully shared with ${emailList.length} recipient(s)`); setSuccess(true); setEmail(""); // Close modal after 2 seconds setTimeout(() => { setSuccess(false); setSuccessMessage(""); onClose(); }, 2000); } catch (err) { setError(err.message || "Failed to share extraction. Please try again."); } finally { setIsLoading(false); } }; const handleClose = () => { if (!isLoading) { setEmail(""); setError(""); setSuccess(false); onClose(); } }; if (!isOpen) return null; return (
{/* Backdrop */} {/* Modal */} e.stopPropagation()} > {/* Header */}

Share Output

{/* Content */}
{success ? (

Share Sent Successfully!

{successMessage || "The recipient(s) will receive an email with a link to view the extraction."}

) : (

Separate multiple emails with commas or semicolons

setEmail(e.target.value)} placeholder="Enter email addresses (comma or semicolon separated)" className="pl-10 h-12 rounded-xl border-slate-200 focus:border-indigo-500 focus:ring-indigo-500" disabled={isLoading} autoFocus />
{error && ( {error} )}
)}
); }