diff --git a/app/suche/page.tsx b/app/suche/page.tsx index edb0202..1cf8a32 100644 --- a/app/suche/page.tsx +++ b/app/suche/page.tsx @@ -53,33 +53,16 @@ export default function SuchePage() { } } - const handleDone = useCallback(async (result: LeadResult[], warning?: string) => { + const handleDone = useCallback((result: LeadResult[], warning?: string) => { setLoading(false); setLeads(result); setSearchDone(true); - - // Auto-delete existing (non-new) leads from vault if "Nur neue speichern" is active - const existingIds = result.filter(l => !l.isNew).map(l => l.id); - if (saveOnlyNew && existingIds.length > 0) { - try { - await fetch("/api/leads/delete-from-results", { - method: "POST", - headers: { "Content-Type": "application/json" }, - body: JSON.stringify({ resultIds: existingIds }), - }); - // Mark them as deleted in local state (isNew stays false, they remain visible but are gone from vault) - } catch { /* silent — vault cleanup best-effort */ } - } - - const newCount = result.filter(l => l.isNew).length; if (warning) { toast.warning(`${result.length} Unternehmen gefunden — E-Mail-Anreicherung fehlgeschlagen: ${warning}`, { duration: 6000 }); - } else if (saveOnlyNew && existingIds.length > 0) { - toast.success(`✓ ${newCount} neue Leads gespeichert, ${existingIds.length} bereits vorhandene verworfen`, { duration: 5000 }); } else { toast.success(`✓ ${result.length} Leads gefunden und im Leadspeicher gespeichert`, { duration: 4000 }); } - }, [saveOnlyNew]); + }, []); async function handleDelete(ids: string[]) { if (!ids.length || deleting) return; @@ -104,6 +87,26 @@ export default function SuchePage() { } } + async function handleSaveOnlyNew() { + const existingIds = leads.filter(l => !l.isNew).map(l => l.id); + if (!existingIds.length || deleting) return; + setDeleting(true); + try { + await fetch("/api/leads/delete-from-results", { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ resultIds: existingIds }), + }); + setSaveOnlyNew(true); + const newCount = leads.filter(l => l.isNew).length; + toast.success(`✓ ${newCount} neue Leads behalten, ${existingIds.length} vorhandene aus Leadspeicher entfernt`, { duration: 5000 }); + } catch { + toast.error("Fehler beim Bereinigen"); + } finally { + setDeleting(false); + } + } + const handleError = useCallback((message: string) => { setLoading(false); setJobId(null); @@ -146,28 +149,6 @@ export default function SuchePage() { onSubmit={handleSubmit} /> - {/* Save option */} - {!loading && !searchDone && ( - - )} {/* Loading Card */} {loading && jobId && ( @@ -282,7 +263,7 @@ export default function SuchePage() { -