1import{z as f,D as p,E as g,Q as de,au as ce,e as u,r as Q,f as S,K as F,al as o,j as e,C as I,a as T,b as A,k as C,d as P,cQ as O,cR as R,B as x,I as M,am as me,an as he,ao as ue,ap as xe,aq as fe,h as c,ab as j,ac as y,ad as v,ae as _,af as d,ay as pe,aE as ge,ak as X,as as je}from"./index-DHwBXtP2.js";import{l as ye}from"./meta-lead-forms.functions-CxK6jyFh.js";const ve=f({method:"POST"}).middleware([p]).handler(g("src_lib_meta-leadgen_functions_ts--getLeadFormSetup_createServerFn_handler")),_e=f({method:"POST"}).middleware([p]).handler(g("src_lib_meta-leadgen_functions_ts--enableLeadFormDelivery_createServerFn_handler")),be=f({method:"POST"}).middleware([p]).handler(g("src_lib_meta-leadgen_functions_ts--syncLeadFormsNow_createServerFn_handler")),Ne=f({method:"POST"}).middleware([p]).handler(g("src_lib_meta-leadgen_functions_ts--saveLeadFormConnection_createServerFn_handler")),Fe=f({method:"POST"}).middleware([p]).handler(g("src_lib_meta-leadgen_functions_ts--deleteLeadFormConnection_createServerFn_handler")),Ce=f({method:"POST"}).middleware([p]).handler(g("src_lib_meta-leadgen_functions_ts--getLeadFormQuestions_createServerFn_handler")),we=f({method:"POST"}).middleware([p]).handler(g("src_lib_meta-leadgen_functions_ts--backfillLeadForm_createServerFn_handler")),ke=f({method:"POST"}).middleware([p]).handler(g("src_lib_meta-leadgen_functions_ts--getLeadFormActivity_createServerFn_handler")),Se=f({method:"POST"}).middleware([p]).handler(g("src_lib_meta-leadgen_functions_ts--saveLeadFormNotifyEmails_createServerFn_handler")),Ie=[{value:"name",label:"Full name"},{value:"first_name",label:"First name"},{value:"last_name",label:"Last name"},{value:"email",label:"Email"},{value:"phone",label:"Phone"},{value:"company",label:"Company"},{value:"answer",label:"Keep as an answer for the AI"},{value:"ignore",label:"Ignore"}],r="__none__";function J(n){return n.split(/[\s,;]+/).map(i=>i.trim().toLowerCase()).filter(i=>i.includes("@"))}function Pe(){return{formId:"",formName:null,status:"live",offerId:null,pipelineId:null,stageId:null,sdrPool:[],hosPool:[],instantCall:!0,callDelaySeconds:120,extraCallQuestions:"",consentQuestion:null,consentChannels:["email","sms"],fieldMap:{},notifyEmails:""}}function Le(){const n=de(),i=ce(),L=u(ve),E=u(ye),Y=u(ke),Z=u(Ce),ee=u(Ne),se=u(Fe),ae=u(_e),ne=u(we),te=u(Se),le=u(be),[a,t]=Q.useState(null),[W,oe]=Q.useState(null),m=S({queryKey:["lead-form-setup",n],enabled:!!n,queryFn:()=>L({data:{workspaceId:n}})}),w=S({queryKey:["meta-lead-forms",n],enabled:!!n,queryFn:()=>E({data:{workspaceId:n}})}),H=S({queryKey:["lead-form-activity",n],enabled:!!n,refetchInterval:3e4,queryFn:()=>Y({data:{workspaceId:n}})}),l=w.data?.pageId??null,b=S({queryKey:["lead-form-questions",n,a?.formId,l],enabled:!!(n&&l&&a?.formId),queryFn:()=>Z({data:{workspaceId:n,pageId:l,formId:a.formId}})}),N=()=>{i.invalidateQueries({queryKey:["lead-form-setup",n]}),i.invalidateQueries({queryKey:["lead-form-activity",n]})},K=F({mutationFn:async s=>{if(!n||!l)throw new Error("No Facebook Page connected");return ee({data:{workspaceId:n,pageId:l,id:s.id,formId:s.formId,formName:s.formName,status:s.status,offerId:s.offerId,pipelineId:s.pipelineId,stageId:s.stageId,sdrPool:s.sdrPool,hosPool:s.hosPool,instantCall:s.instantCall,callDelaySeconds:s.callDelaySeconds,extraCallQuestions:s.extraCallQuestions.split(` 2`).map(h=>h.trim()).filter(Boolean),consentQuestion:s.consentQuestion,consentChannels:s.consentChannels,fieldMap:s.fieldMap,notifyEmails:J(s.notifyEmails)}})},onSuccess:()=>{o.success("Lead form connection saved"),t(null),N()},onError:s=>o.error(s.message)}),V=F({mutationFn:async s=>te({data:{workspaceId:n,emails:J(s)}}),onSuccess:s=>{o.success(s.emails.length?`Lead alerts will go to ${s.emails.length} address(es)`:"Lead alert emails switched off"),N()},onError:s=>o.error(s.message)}),$=F({mutationFn:async()=>{if(!n||!l)throw new Error("No Facebook Page connected");return ae({data:{workspaceId:n,pageId:l}})},onSuccess:s=>{s.ok?o.success("Facebook confirmed it will push new leads to Brain"):o.warning(s.error??"Facebook did not confirm instant push â Brain will keep pulling leads every few minutes instead"),N()},onError:s=>o.error(s.message)}),z=F({mutationFn:async s=>{if(!n||!l)throw new Error("No Facebook Page connected");return ne({data:{workspaceId:n,pageId:l,formId:s,days:30}})},onSuccess:s=>{s.error?o.error(s.error):o.success(`Pulled ${s.queued} past lead(s) from Facebook`),N()},onError:s=>o.error(s.message)}),D=F({mutationFn:async()=>le({data:{workspaceId:n}}),onSuccess:s=>{s.error?o.error(s.error):o.success(s.queued?`Brought in ${s.queued} lead(s) from Facebook`:"Already up to date â nothing new on Facebook"),N()},onError:s=>o.error(s.message)}),re=F({mutationFn:async s=>se({data:{workspaceId:n,id:s}}),onSuccess:()=>{o.success("Connection removed"),N()}}),ie=Q.useMemo(()=>(m.data?.stages??[]).filter(s=>!a?.pipelineId||s.pipeline_id===a.pipelineId),[m.data?.stages,a?.pipelineId]),q=m.data?.connections??[],k=m.data?.deliveryOk===!0,G=m.data?.deliveryError??null;return e.jsxs("div",{className:"space-y-6",children:[e.jsxs(I,{children:[e.jsxs(T,{className:"flex-row items-center justify-between space-y-0",children:[e.jsx(A,{className:"text-base",children:"Facebook lead delivery"}),e.jsx(C,{variant:k?"default":"secondary",children:k?"Instant + backup sync":"Backup sync every 2 minutes"})]}),e.jsxs(P,{className:"space-y-3 text-sm",children:[e.jsxs("p",{className:"text-muted-foreground",children:[l?e.jsxs(e.Fragment,{children:["Page ",e.jsx("span",{className:"font-mono",children:l}),"."," "]}):null,"Brain checks every live form for new leads every couple of minutes, so leads arrive even if Facebook is not pushing them."," ",k?"Facebook is also pushing each submission instantly.":"Instant push is not confirmed yet, so expect up to a two-minute delay."]}),l?null:e.jsx(O,{children:e.jsx(R,{children:w.data?.error??"Connect Meta and pick a Facebook Page to add new forms â lead forms live on the Page. Forms already connected keep syncing."})}),G?e.jsx(O,{children:e.jsxs(R,{className:"text-xs",children:["Last message from Facebook: ",G]})}):null,e.jsxs("div",{className:"flex flex-wrap gap-2",children:[l?e.jsx(x,{size:"sm",onClick:()=>$.mutate(),disabled:$.isPending,children:k?"Re-check instant push":"Try instant push again"}):null,e.jsx(x,{size:"sm",variant:"outline",onClick:()=>D.mutate(),disabled:D.isPending||!q.length,children:D.isPending?"Syncingâ¦":"Sync leads now"})]})]})]}),e.jsxs(I,{children:[e.jsx(T,{children:e.jsx(A,{className:"text-base",children:"Email notifications"})}),e.jsxs(P,{className:"space-y-3 text-sm",children:[e.jsx("p",{className:"text-muted-foreground",children:"Brain emails these addresses every time a lead form is submitted â Facebook Instant Forms and Brain-hosted funnel pages â with every field the lead filled in, plus who it was assigned to. Separate several addresses with commas."}),e.jsxs("div",{className:"flex flex-col gap-2 sm:flex-row",children:[e.jsx(M,{value:W??(m.data?.workspaceNotifyEmails??[]).join(", "),placeholder:"[email protected], [email protected]",onChange:s=>oe(s.target.value)}),e.jsx(x,{onClick:()=>V.mutate(W??(m.data?.workspaceNotifyEmails??[]).join(", ")),disabled:V.isPending||!n,children:"Save recipients"})]}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Leave it empty to switch the emails off. Each connected form can add its own extra recipients when you edit it."})]})]}),e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx("h3",{className:"text-sm font-semibold",children:"Connected forms"}),e.jsx(x,{size:"sm",variant:"outline",disabled:!l,onClick:()=>t(Pe()),children:"Connect a form"})]}),q.length===0?e.jsx("p",{className:"text-sm text-muted-foreground",children:"No forms connected yet. Connect one and every lead from it gets an offer, a pipeline card, an SDR, a Head of Strategy, and an instant AI call."}):e.jsx("div",{className:"grid gap-3",children:q.map(s=>e.jsx(I,{children:e.jsxs(P,{className:"space-y-2 pt-4 text-sm",children:[e.jsxs("div",{className:"flex flex-wrap items-center gap-2",children:[e.jsx("span",{className:"font-medium",children:s.form_name??s.form_id}),e.jsx(C,{variant:s.status==="live"?"default":"secondary",children:s.status==="live"?"Live":"Paused"}),s.instant_call?e.jsxs(C,{variant:"outline",children:["AI calls after ",Math.round(s.call_delay_seconds/60)," min"]}):e.jsx(C,{variant:"outline",children:"No instant call"}),e.jsxs("span",{className:"ml-auto text-xs text-muted-foreground",children:[s.leads_total," lead(s)",s.last_lead_at?` · last ${new Date(s.last_lead_at).toLocaleStr
2ing()}`:""]})]}),e.jsxs("p",{className:"text-xs text-muted-foreground",children:[s.last_synced_at?`Last checked with Facebook ${new Date(s.last_synced_at).toLocaleString()}`:"Not checked with Facebook yet",s.last_sync_error?` · ${s.last_sync_error}`:""]}),e.jsxs("p",{className:"text-xs text-muted-foreground",children:[s.sdr_pool.length?`${s.sdr_pool.length} SDR(s) in rotation`:"Whole SDR rotation"," · ",s.hos_pool.length?`${s.hos_pool.length} Head(s) of Strategy in rotation`:"Whole Head of Strategy rotation"]}),s.last_error?e.jsx(O,{variant:"destructive",children:e.jsx(R,{className:"text-xs",children:s.last_error})}):null,e.jsxs("div",{className:"flex gap-2 pt-1",children:[e.jsx(x,{size:"sm",variant:"outline",onClick:()=>t({id:s.id,formId:s.form_id,formName:s.form_name,status:s.status==="live"?"live":"paused",offerId:s.offer_id,pipelineId:s.pipeline_id,stageId:s.stage_id,sdrPool:s.sdr_pool,hosPool:s.hos_pool,instantCall:s.instant_call,callDelaySeconds:s.call_delay_seconds,extraCallQuestions:(s.extra_call_questions??[]).join(` 3`),consentQuestion:s.consent_question,consentChannels:s.consent_channels?.length?s.consent_channels:["email"],fieldMap:s.field_map??{},notifyEmails:(s.notify_emails??[]).join(", ")}),children:"Edit"}),e.jsx(x,{size:"sm",variant:"outline",disabled:z.isPending,onClick:()=>z.mutate(s.form_id),children:"Pull last 30 days"}),e.jsx(x,{size:"sm",variant:"ghost",onClick:()=>re.mutate(s.id),children:"Remove"})]})]})},s.id))}),e.jsxs(I,{children:[e.jsx(T,{children:e.jsx(A,{className:"text-base",children:"Recent deliveries"})}),e.jsx(P,{className:"space-y-1 text-xs",children:(H.data?.events??[]).length===0?e.jsx("p",{className:"text-muted-foreground",children:"Nothing received yet."}):(H.data?.events??[]).map(s=>e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("span",{className:"text-muted-foreground",children:new Date(s.received_at).toLocaleString()}),e.jsx("span",{className:"font-mono",children:s.leadgen_id}),s.lead_event_id?e.jsx(C,{variant:"outline",children:"In Brain"}):s.processing_error?e.jsx("span",{className:"text-destructive",children:s.processing_error}):e.jsx(C,{variant:"secondary",children:"Waiting"})]},s.id))})]}),e.jsx(me,{open:!!a,onOpenChange:s=>!s&&t(null),children:e.jsxs(he,{className:"max-h-[85vh] max-w-2xl overflow-y-auto",children:[e.jsxs(ue,{children:[e.jsx(xe,{children:"Lead form connection"}),e.jsx(fe,{children:"What Brain does the moment someone submits this Facebook form."})]}),a?e.jsxs("div",{className:"space-y-4 text-sm",children:[e.jsxs("div",{className:"space-y-1",children:[e.jsx(c,{children:"Facebook form"}),e.jsxs(j,{value:a.formId||r,onValueChange:s=>{const h=(w.data?.forms??[]).find(B=>B.id===s);t({...a,formId:s,formName:h?.name??null})},children:[e.jsx(y,{children:e.jsx(v,{placeholder:"Pick a form"})}),e.jsx(_,{children:(w.data?.forms??[]).map(s=>e.jsx(d,{value:s.id,children:s.name},s.id))})]})]}),e.jsxs("div",{className:"grid gap-3 sm:grid-cols-2",children:[e.jsxs("div",{className:"space-y-1",children:[e.jsx(c,{children:"Offer"}),e.jsxs(j,{value:a.offerId??r,onValueChange:s=>t({...a,offerId:s===r?null:s}),children:[e.jsx(y,{children:e.jsx(v,{placeholder:"Pick an offer"})}),e.jsxs(_,{children:[e.jsx(d,{value:r,children:"Work it out automatically"}),(m.data?.offers??[]).map(s=>e.jsx(d,{value:s.id,children:s.name},s.id))]})]})]}),e.jsxs("div",{className:"space-y-1",children:[e.jsx(c,{children:"Pipeline"}),e.jsxs(j,{value:a.pipelineId??r,onValueChange:s=>t({...a,pipelineId:s===r?null:s,stageId:null}),children:[e.jsx(y,{children:e.jsx(v,{placeholder:"Pick a pipeline"})}),e.jsxs(_,{children:[e.jsx(d,{value:r,children:"Follow the offer"}),(m.data?.pipelines??[]).map(s=>e.jsx(d,{value:s.id,children:s.name},s.id))]})]})]}),e.jsxs("div",{className:"space-y-1",children:[e.jsx(c,{children:"Starting stage"}),e.jsxs(j,{value:a.stageId??r,onValueChange:s=>t({...a,stageId:s===r?null:s}),children:[e.jsx(y,{children:e.jsx(v,{placeholder:"First stage"})}),e.jsxs(_,{children:[e.jsx(d,{value:r,children:"First stage"}),ie.map(s=>e.jsx(d,{value:s.id,children:s.name},s.id))]})]})]}),e.jsxs("div",{className:"space-y-1",children:[e.jsx(c,{children:"Status"}),e.jsxs(j,{value:a.status,onValueChange:s=>t({...a,status:s}),children:[e.jsx(y,{children:e.jsx(v,{})}),e.jsxs(_,{children:[e.jsx(d,{value:"live",children:"Live â accept leads"}),e.jsx(d,{value:"paused",children:"Paused â hold leads"})]})]})]})]}),e.jsxs("div",{className:"grid gap-3 sm:grid-cols-2",children:[e.jsx(U,{label:"SDR pool",hint:"Leave empty to use the whole SDR rotation.",people:m.data?.sdrs??[],selected:a.sdrPool,onCh
3ange:s=>t({...a,sdrPool:s})}),e.jsx(U,{label:"Head of Strategy pool",hint:"Leave empty to use the whole Head of Strategy rotation.",people:m.data?.heads??[],selected:a.hosPool,onChange:s=>t({...a,hosPool:s})})]}),e.jsxs("div",{className:"space-y-2 rounded-md border p-3",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsx(c,{children:"Call the lead automatically"}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"The AI rings them and tries to book the appointment."})]}),e.jsx(pe,{checked:a.instantCall,onCheckedChange:s=>t({...a,instantCall:s})})]}),a.instantCall?e.jsxs("div",{className:"space-y-1",children:[e.jsx(c,{children:"Wait before calling (seconds)"}),e.jsx(M,{type:"number",min:0,max:3600,value:a.callDelaySeconds,onChange:s=>t({...a,callDelaySeconds:Number(s.target.value)||0})})]}):null,e.jsxs("div",{className:"space-y-1",children:[e.jsx(c,{children:"Extra questions to ask on the call"}),e.jsx(ge,{rows:3,placeholder:`One per line, e.g. 4What's your monthly ad budget?`,value:a.extraCallQuestions,onChange:s=>t({...a,extraCallQuestions:s.target.value})}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Anything they already answered on the form is not asked again."})]})]}),e.jsxs("div",{className:"space-y-2 rounded-md border p-3",children:[e.jsx(c,{children:"Permission to contact"}),e.jsxs(j,{value:a.consentQuestion??r,onValueChange:s=>t({...a,consentQuestion:s===r?null:s}),children:[e.jsx(y,{children:e.jsx(v,{placeholder:"Which form question is the opt-in?"})}),e.jsxs(_,{children:[e.jsx(d,{value:r,children:"No opt-in question â treat the submission as the request"}),(b.data?.questions??[]).map(s=>e.jsx(d,{value:s.key,children:s.label},s.key))]})]}),e.jsx("div",{className:"flex gap-4 pt-1",children:["sms","email","call"].map(s=>e.jsxs("label",{className:"flex items-center gap-2 text-xs",children:[e.jsx(X,{checked:a.consentChannels.includes(s),onCheckedChange:h=>t({...a,consentChannels:h?[...a.consentChannels,s]:a.consentChannels.filter(B=>B!==s)})}),s==="sms"?"Text":s==="call"?"Call":"Email"]},s))})]}),e.jsxs("div",{className:"space-y-1",children:[e.jsx(c,{children:"Email notifications for this form"}),e.jsx(M,{value:a.notifyEmails,placeholder:"[email protected], [email protected]",onChange:s=>t({...a,notifyEmails:s.target.value})}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Every submission from this form is emailed to these addresses with all the answers the lead filled in. Separate several addresses with commas. The workspace list below always gets a copy too."})]}),e.jsxs("div",{className:"space-y-2 rounded-md border p-3",children:[e.jsx(c,{children:"Form questions â Brain fields"}),b.isLoading?e.jsx("p",{className:"text-xs text-muted-foreground",children:"Reading the formâ¦"}):b.data?.error?e.jsx("p",{className:"text-xs text-destructive",children:b.data.error}):(b.data?.questions??[]).length===0?e.jsx("p",{className:"text-xs text-muted-foreground",children:"Pick a form to see its questions."}):e.jsx("div",{className:"space-y-2",children:(b.data?.questions??[]).map(s=>e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("span",{className:"flex-1 truncate text-xs",children:s.label}),e.jsxs(j,{value:a.fieldMap[s.key]??s.suggested,onValueChange:h=>t({...a,fieldMap:{...a.fieldMap,[s.key]:h}}),children:[e.jsx(y,{className:"w-56",children:e.jsx(v,{})}),e.jsx(_,{children:Ie.map(h=>e.jsx(d,{value:h.value,children:h.label},h.value))})]})]},s.key))})]})]}):null,e.jsxs(je,{children:[e.jsx(x,{variant:"ghost",onClick:()=>t(null),children:"Cancel"}),e.jsx(x,{disabled:!a?.formId||K.isPending,onClick:()=>a&&K.mutate(a),children:"Save connection"})]})]})})]})}function U(n){return e.jsxs("div",{className:"space-y-1",children:[e.jsx(c,{children:n.label}),e.jsx("div",{className:"max-h-40 space-y-1 overflow-y-auto rounded-md border p-2",children:n.people.length===0?e.jsx("p",{className:"text-xs text-muted-foreground",children:"Nobody active yet."}):n.people.map(i=>e.jsxs("label",{className:"flex items-center gap-2 text-xs",children:[e.jsx(X,{checked:n.selected.includes(i.id),onCheckedChange:L=>n.onChange(L?[...n.selected,i.id]:n.selected.filter(E=>E!==i.id))}),i.label]},i.id))}),e.jsx("p",{className:"text-xs text-muted-foreground",children:n.hint})]})}function qe(){return e.jsxs("section",{className:"space-y-4",children:[e.jsxs("header",{children:[e.jsx("h2",{className:"text-xl font-semibold tracking-tight",children:"Lead forms"}),e.jsx("p",{className:"text-sm text-muted-foreground",children:"Facebook Instant Form leads land straight in Brain â routed to an offer and pipeline, assigned to an SDR and Head of Strategy, and called by the AI to book the appointment."})]}),e.jsx(Le,{})]})}
4export{qe as component};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.