import React, { useState, useEffect } from 'react'; import { Send, Download, Loader2, CheckCircle2, AlertCircle, Settings, RefreshCw } from 'lucide-react'; import { Button } from "@/components/ui/button"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"; import { motion } from 'framer-motion'; export default function SmartleadPanel({ uploadedFile, sequencesCount, onPushComplete }) { const [selectedCampaignId, setSelectedCampaignId] = useState(''); const [campaigns, setCampaigns] = useState([]); const [isLoadingCampaigns, setIsLoadingCampaigns] = useState(false); const [dryRun, setDryRun] = useState(false); const [isPushing, setIsPushing] = useState(false); const [pushResult, setPushResult] = useState(null); const [error, setError] = useState(null); // Fetch campaigns on component mount useEffect(() => { fetchCampaigns(); }, []); const fetchCampaigns = async () => { setIsLoadingCampaigns(true); setError(null); try { const response = await fetch('/api/smartlead-campaigns'); const data = await response.json(); if (response.ok) { setCampaigns(data.campaigns || []); } else { setError('Failed to fetch campaigns: ' + (data.detail || 'Unknown error')); } } catch (err) { setError('Network error: ' + err.message); } finally { setIsLoadingCampaigns(false); } }; const handlePushToSmartlead = async () => { if (!uploadedFile?.fileId) { setError('No file uploaded'); return; } if (!selectedCampaignId) { setError('Please select a campaign'); return; } setIsPushing(true); setError(null); setPushResult(null); try { const response = await fetch('/api/push-to-smartlead', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ file_id: uploadedFile.fileId, campaign_id: selectedCampaignId, dry_run: dryRun }) }); const data = await response.json(); if (response.ok) { setPushResult(data); onPushComplete?.(data); } else { setError(data.detail || 'Failed to push to Smartlead'); } } catch (err) { setError('Network error: ' + err.message); } finally { setIsPushing(false); } }; const handleDownloadCSV = async () => { try { const response = await fetch(`/api/download-sequences?file_id=${uploadedFile.fileId}`); if (response.ok) { const blob = await response.blob(); const url = URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download = 'email_sequences_fixed.csv'; a.click(); URL.revokeObjectURL(url); } else { alert('Failed to download CSV. Please try again.'); } } catch (error) { console.error('Download error:', error); alert('Error downloading CSV. Please try again.'); } }; return (

Send via Smartlead

Push sequences to Smartlead campaign

{/* Campaign Selector */}
{isLoadingCampaigns ? (
Loading campaigns...
) : campaigns.length === 0 ? (

No campaigns found. Please create a campaign in Smartlead first.

) : ( )}

Select an existing campaign to add leads to. Create campaigns manually in Smartlead using the CSV download.

{/* Dry Run Toggle */}
setDryRun(e.target.checked)} className="rounded border-slate-300" />
{/* Error Display */} {error && (

Error

{error}

)} {/* Success Result */} {pushResult && (

{pushResult.status === 'dry_run_completed' ? 'Dry Run Completed' : 'Push Successful'}

{pushResult.campaign_id && (

Campaign ID: {pushResult.campaign_id}

)} {pushResult.campaign_name && (

Campaign: {pushResult.campaign_name}

)}
Total: {pushResult.total}
Added: {pushResult.added}
{pushResult.skipped > 0 && (
Skipped: {pushResult.skipped}
)} {pushResult.failed > 0 && (
Failed: {pushResult.failed}
)}
{pushResult.errors && pushResult.errors.length > 0 && (

Errors:

{pushResult.errors.slice(0, 5).map((err, idx) => (

{err.email}: {err.error}

))}
)}
)} {/* Actions */}
); }