File size: 12,559 Bytes
f485041 c356b87 f485041 c356b87 f485041 c356b87 f485041 c356b87 f485041 c356b87 952e292 c356b87 f485041 c356b87 f485041 c356b87 f485041 c356b87 f485041 c356b87 f485041 c356b87 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 | 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 (
<div className="w-full">
<div className="rounded-2xl border border-slate-200 bg-white p-6">
<div className="flex items-center gap-3 mb-6">
<div className="rounded-xl bg-blue-100 p-3">
<Settings className="h-6 w-6 text-blue-600" />
</div>
<div>
<h3 className="font-semibold text-slate-800">Send via Smartlead</h3>
<p className="text-sm text-slate-500">Push sequences to Smartlead campaign</p>
</div>
</div>
{/* Campaign Selector */}
<div className="mb-6">
<div className="flex items-center justify-between mb-2">
<label className="text-sm font-medium text-slate-700">
Select Campaign
</label>
<Button
type="button"
variant="ghost"
size="sm"
onClick={fetchCampaigns}
disabled={isLoadingCampaigns}
className="h-7 text-xs"
>
<RefreshCw className={`h-3 w-3 mr-1 ${isLoadingCampaigns ? 'animate-spin' : ''}`} />
Refresh
</Button>
</div>
{isLoadingCampaigns ? (
<div className="flex items-center justify-center p-4 border border-slate-200 rounded-lg">
<Loader2 className="h-4 w-4 animate-spin text-slate-400 mr-2" />
<span className="text-sm text-slate-500">Loading campaigns...</span>
</div>
) : campaigns.length === 0 ? (
<div className="p-4 border border-slate-200 rounded-lg bg-slate-50">
<p className="text-sm text-slate-600 text-center">
No campaigns found. Please create a campaign in Smartlead first.
</p>
</div>
) : (
<Select value={selectedCampaignId} onValueChange={setSelectedCampaignId}>
<SelectTrigger className="w-full">
<SelectValue placeholder="Select a campaign" />
</SelectTrigger>
<SelectContent>
{campaigns.map((campaign) => (
<SelectItem key={campaign.id} value={campaign.id}>
{campaign.name} ({campaign.id})
</SelectItem>
))}
</SelectContent>
</Select>
)}
<p className="text-xs text-slate-500 mt-1">
Select an existing campaign to add leads to. Create campaigns manually in Smartlead using the CSV download.
</p>
</div>
{/* Dry Run Toggle */}
<div className="mb-6 flex items-center gap-2">
<input
type="checkbox"
id="dry-run"
checked={dryRun}
onChange={(e) => setDryRun(e.target.checked)}
className="rounded border-slate-300"
/>
<label htmlFor="dry-run" className="text-sm text-slate-700">
Dry run (generate but do not call Smartlead)
</label>
</div>
{/* Error Display */}
{error && (
<motion.div
initial={{ opacity: 0, y: -10 }}
animate={{ opacity: 1, y: 0 }}
className="mb-4 p-3 rounded-lg bg-red-50 border border-red-200 flex items-start gap-2"
>
<AlertCircle className="h-5 w-5 text-red-600 mt-0.5" />
<div className="flex-1">
<p className="text-sm font-medium text-red-800">Error</p>
<p className="text-sm text-red-600">{error}</p>
</div>
</motion.div>
)}
{/* Success Result */}
{pushResult && (
<motion.div
initial={{ opacity: 0, y: -10 }}
animate={{ opacity: 1, y: 0 }}
className="mb-4 p-4 rounded-lg bg-green-50 border border-green-200"
>
<div className="flex items-start gap-2 mb-3">
<CheckCircle2 className="h-5 w-5 text-green-600 mt-0.5" />
<div className="flex-1">
<p className="text-sm font-medium text-green-800 mb-2">
{pushResult.status === 'dry_run_completed' ? 'Dry Run Completed' : 'Push Successful'}
</p>
{pushResult.campaign_id && (
<p className="text-xs text-green-700 mb-1">
Campaign ID: <span className="font-mono">{pushResult.campaign_id}</span>
</p>
)}
{pushResult.campaign_name && (
<p className="text-xs text-green-700 mb-1">
Campaign: {pushResult.campaign_name}
</p>
)}
</div>
</div>
<div className="grid grid-cols-2 gap-2 text-xs">
<div>
<span className="text-green-600">Total:</span> {pushResult.total}
</div>
<div>
<span className="text-green-600">Added:</span> {pushResult.added}
</div>
{pushResult.skipped > 0 && (
<div>
<span className="text-green-600">Skipped:</span> {pushResult.skipped}
</div>
)}
{pushResult.failed > 0 && (
<div>
<span className="text-red-600">Failed:</span> {pushResult.failed}
</div>
)}
</div>
{pushResult.errors && pushResult.errors.length > 0 && (
<div className="mt-2 pt-2 border-t border-green-200">
<p className="text-xs font-medium text-green-800 mb-1">Errors:</p>
<div className="max-h-32 overflow-y-auto">
{pushResult.errors.slice(0, 5).map((err, idx) => (
<p key={idx} className="text-xs text-red-600">
{err.email}: {err.error}
</p>
))}
</div>
</div>
)}
</motion.div>
)}
{/* Actions */}
<div className="flex gap-3">
<Button
onClick={handlePushToSmartlead}
disabled={isPushing || !uploadedFile?.fileId}
className="flex-1 bg-blue-600 hover:bg-blue-700"
>
{isPushing ? (
<>
<Loader2 className="h-4 w-4 mr-2 animate-spin" />
Pushing...
</>
) : (
<>
<Send className="h-4 w-4 mr-2" />
Generate + Push to Smartlead
</>
)}
</Button>
<Button
onClick={handleDownloadCSV}
variant="outline"
className="flex-1"
>
<Download className="h-4 w-4 mr-2" />
Download CSV
</Button>
</div>
</div>
</div>
);
}
|