diff --git a/src/hooks/useLeads.js b/src/hooks/useLeads.js index a1de8bb..23e1f1b 100644 --- a/src/hooks/useLeads.js +++ b/src/hooks/useLeads.js @@ -19,10 +19,12 @@ async function listAll(collection, queries = []) { return out } -/** Haengt an jeden Lead die WOID seines Akquise-Tickets: - * Lead -> Kunde (per E-Mail) -> Akquise-Workorder -> woid. Best effort; - * am Lead selbst ist keine WOID gespeichert, daher live gejoint. */ -async function attachWoids(leads) { +/** Haengt an jeden Lead Verweise, die nicht am Lead selbst gespeichert sind, + * sondern ueber den Kunden verknuepft werden — via Lead -> Kunde (per E-Mail): + * - woid: WOID des Akquise-Tickets (workorders type=Akquise) + * - previewUrl: URL der erstellten, deployten Website (websiteProjects) + * Best effort; Fehler brechen die Lead-Liste nicht. */ +async function attachLeadLinks(leads) { const norm = (l) => (l.email || l.portalLogin || '').trim().toLowerCase() const emails = [...new Set(leads.map(norm).filter(Boolean))] if (!emails.length) return leads @@ -57,10 +59,29 @@ async function attachWoids(leads) { } } - // 3) an die Leads haengen + // 3) deployte Preview je Kunde -> URL der erstellten Website. + // Die websiteProjects-Collection ist klein -> komplett laden + lokal zuordnen + // (die Preview haengt am selben Kunden, aber an einem anderen Repo als lead.repoUrl). + const customerToPreview = {} + const idSet = new Set(customerIds) + for (const p of await listAll(COLLECTIONS.WEBSITE_PROJECTS)) { + if (!idSet.has(p.customerId) || customerToPreview[p.customerId]) continue + const ready = p.status === 'deployed' || p.status === 'ready' || p.provisioningStatus === 'ready' + const url = p.previewUrl || (p.subdomain ? `https://${p.subdomain}.project.webklar.com` : '') + if (ready && url) customerToPreview[p.customerId] = url + } + + // 4) an die Leads haengen return leads.map((l) => { - const hit = customerToWoid[emailToCustomer[norm(l)]] - return hit ? { ...l, woid: hit.woid, ticketId: hit.ticketId } : l + const cid = emailToCustomer[norm(l)] + const t = customerToWoid[cid] + const previewUrl = customerToPreview[cid] + if (!t && !previewUrl) return l + return { + ...l, + ...(t ? { woid: t.woid, ticketId: t.ticketId } : {}), + ...(previewUrl ? { previewUrl } : {}), + } }) } @@ -77,7 +98,7 @@ export function useLeads() { try { const out = await listAll(COLLECTIONS.LEADS, [Query.orderDesc('leadScore')]) let enriched = out - try { enriched = await attachWoids(out) } catch { /* WOID-Join best effort */ } + try { enriched = await attachLeadLinks(out) } catch { /* Live-Join best effort */ } setLeads(enriched) setError(null) } catch (err) { diff --git a/src/pages/LeadsPage.jsx b/src/pages/LeadsPage.jsx index 2907a1c..64b0c83 100644 --- a/src/pages/LeadsPage.jsx +++ b/src/pages/LeadsPage.jsx @@ -17,6 +17,7 @@ import { FaArrowsRotate, FaCheck, FaCopy, + FaEye, FaHashtag, FaTriangleExclamation, } from 'react-icons/fa6' @@ -428,6 +429,11 @@ function LeadCard({ lead, pipeline, onUpdate }) { )} + {lead.previewUrl && ( + + )} {lead.woid && (