1import{u as q,a as D,r,j as e,B as d,C as M,d as P,f as Q,$ as _,a5 as R,e as X,X as H,s as u}from"./index-CEISlULX.js";import{I as J}from"./input-CaLoYHgx.js";import{B as S}from"./badge-CmNrSZhw.js";import{d as A,S as p}from"./BrandIcons-C3jZg5F7.js";import{A as K}from"./arrow-left-Dt5WA4Nk.js";import{B as b}from"./book-user-CU26YudE.js";import{U as O}from"./upload-C39KYR9i.js";import{S as k}from"./search-Ctpt4-cV.js";import{L as I}from"./loader-circle-C0qiwHXp.js";import{C as V}from"./check-BFjtj0TV.js";import{P as W}from"./plus-CMUH1C5J.js";const de=()=>{const B=q(),{toast:f}=D(),[t,L]=r.useState(null),[l,G]=r.useState([]),[n,g]=r.useState(""),[j,N]=r.useState(null),[U,w]=r.useState(new Set),[$,v]=r.useState(!0),[c,h]=r.useState("all"),m=r.useMemo(()=>{const s=new Set(l.map(a=>a.source));return{hasGoogle:s.has("google"),hasApple:s.has("apple")}},[l]),x=r.useMemo(()=>{let s=l;if(c!=="all"&&(s=s.filter(a=>a.source===c)),n.trim()){const a=n.toLowerCase();s=s.filter(o=>{var i,y,C;return((i=o.display_name)==null?void 0:i.toLowerCase().includes(a))||((y=o.email)==null?void 0:y.toLowerCase().includes(a))||((C=o.phone_e164)==null?void 0:C.includes(a))})}return s},[l,n,c]);r.useEffect(()=>{(async()=>{const{data:{session:a}}=await u.auth.getSession();a!=null&&a.user?L(a.user):console.warn("[AddContacts] No session â AuthGuard should have caught this")})()},[]);const z=async()=>{if(t!=null&&t.id){v(!0);try{const{data:s,error:a}=await u.from("imported_contacts").select("id, display_name, first_name, last_name, email, phone_e164, avatar_url, source, created_at").eq("user_id",t.id).order("display_name");if(a)throw a;G(s||[])}catch(s){console.error("Error fetching address book:",s)}finally{v(!1)}}},E=async()=>{if(t!=null&&t.id)try{const{data:s,error:a}=await u.from("user_contacts").select("source, external_id").eq("user_id",t.id);if(a)throw a;const o=new Set((s||[]).map(i=>`${i.source}-${i.external_id}`));w(o)}catch(s){console.error("Error fetching user contact IDs:",s)}};r.useEffect(()=>{t&&(z(),E())},[t]);const F=async s=>{if(t!=null&&t.id){N(s.id);try{const a={user_id:t.id,display_name:s.display_name,first_name:s.first_name||null,last_name:s.last_name||null,email:s.email||null,phone_e164:s.phone_e164||null,avatar_url:s.avatar_url||null,source:s.source,external_id:s.id,emails:[],phones:[]},{error:o}=await u.from("user_contacts").upsert(a,{onConflict:"user_id,source,external_id",ignoreDuplicates:!1});if(o)throw o;w(i=>new Set([...i,`${s.source}-${s.id}`])),f({title:"Contact added!",description:`${s.display_name} added to your Fyra contacts`})}catch(a){console.error("Error adding contact:",a),f({title:"Failed to add contact",description:a.message||"Please try again",variant:"destructive"})}finally{N(null)}}},T=s=>U.has(`${s.source}-${s.id}`);return e.jsx("div",{className:"min-h-screen bg-background",children:e.jsxs("div",{className:"container max-w-2xl mx-auto px-4 py-6",children:[e.jsxs("div",{className:"flex items-center gap-3 mb-6",children:[e.jsx(d,{variant:"ghost",size:"icon",onClick:()=>B("/network"),children:e.jsx(K,{className:"h-5 w-5"})}),e.jsxs("div",{children:[e.jsx("h1",{className:"text-xl font-semibold",children:"Add Contacts"}),e.jsx("p",{className:"text-sm text-muted-foreground",children:"Add contacts from your address book to Fyra"})]})]}),e.jsxs(M,{children:[e.jsxs(P,{className:"pb-3",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(b,{className:"h-5 w-5 text-muted-foreground"}),e.jsx(Q,{className:"text-lg",children:"Address Book"})]}),e.jsx(_,{to:"/app-connectors",children:e.jsxs(d,{variant:"outline",size:"sm",children:[e.jsx(O,{className:"h-4 w-4 mr-2"}),"Import More"]})})]}),e.jsxs(R,{children:[l.length," contacts imported from Google & Apple"]})]}),e.jsxs(X,{className:"space-y-4",children:[(m.hasGoogle||m.hasApple)&&e.jsxs("div",{className:"flex gap-2",children:[e.jsx(d,{variant:c==="all"?"default":"outline",size:"sm",onClick:()=>h("all"),className:"h-8",children:"All"}),m.hasGoogle&&e.jsxs(d,{variant:c==="google"?"default":"outline",size:"sm",onClick:()=>h("google"),className:"h-8 gap-1.5",children:[e.jsx(A,{className:"h-3.5 w-3.5"}),"Google"]}),m.hasApple&&e.jsxs(d,{variant:c==="apple"?"default":"outline",size:"sm",onClick:()=>h("apple"),className:"h-8 gap-1.5",children:[e.jsx(p,{className:"h-3.5 w-3.5"}),"Apple"]})]}),e.jsxs("div",{className:"relative",children:[e.jsx(k,{className:"absolute left-3 top-1/2 -translate-y-1/2 h-4 w-4 text-muted-foreground"}),e.jsx(J,{placeholder:"Search by name, email, or phone...",value:n,onChange:s=>g(s.target.value),className:"pl-9 pr-9"}),n&&e.jsx("button",{onClick:()=>g(""),className:"absolute right-3 top-1/2 -translate-y-1/2 text-muted-foreground hover:text-foreground",children:e.jsx(H,{className:"h-4 w-4"})})]}),n&&e.jsxs("p",{className:"text-xs text-muted-foreground",children:["Showing ",x.length," of ",l.length," contacts"]}),$?e.jsx("div",{className:"flex items-center justify-center py-12",children:e.jsx(I,{className:"h-6 w-6 animate-spin text-muted-foreground"})}):x.length===0?e.jsx("div",{className:"text-center py-12 text-muted-foreground",children:l.length===0?e.jsxs(e.Fragment,{children:[e.jsx(b,{className:"h-12 w-12 mx-auto mb-4 opacity-50"}),e.jsx("p",{className:"font-medium",children:"No contacts imported yet"}),e.jsx("p",{className:"text-sm mt-1",children:"Import contacts from Google or Apple to get started"}),e.jsx(_,{to:"/app-connectors",className:"mt-4 inline-block",children:e.jsx(d,{children:"Import Contacts"})})]}):e.jsxs(e.Fragment,{children:[e.jsx(k,{className:"h-12 w-12 mx-auto mb-4 opacity-50"}),e.jsxs("p",{children:['No contacts match "',n,'"']})]})}):e.jsx("div",{className:"space-y-2 max-h-[60vh] overflow-y-auto",children:x.map(s=>e.jsxs("div",{className:"flex items-center justify-between p-3 rounded-lg hover:bg-accent transition-colors gap-2",children:[e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsx("p",{className:"font-medium text-foreground",children:s.display_name}),s.email&&e.jsx("p",{className:"text-sm text-muted-foreground truncate",children:s.email}),!s.email&&s.phone_e164&&e.jsx("p",{className:"text-sm text-muted-foreground truncate",children:s.phone_e164})]}),e.jsxs("div",{className:"flex items-center gap-2 flex-shrink-0",children:[T(s)?e.jsx(S,{variant:"secondary",className:"text-xs px-2",children:e.jsx(V,{className:"h-3 w-3"})}):e.jsx(d,{variant:"outline",size:"icon",onClick:()=>F(s),disabled:j===s.id,className:"h-8 w-8",children:j===s.id?e.jsx(I,{className:"h-4 w-4 animate-spin"}):e.jsx(W,{className:"h-4 w-4"})}),s.source==="apple"?e.jsx(p,{className:"h-4 w-4 text-muted-foreground"}):s.source==="google"?e.jsx(A,{className:"h-4 w-4 text-muted-foreground"}):s.source==="device_ios"?e.jsxs("span",{className:"flex items-center gap-0.5",children:[e.jsx(p,{className:"h-3.5 w-3.5 text-muted-foreground"}),e.jsx("span",{className:"text-[10px] text-muted-foreground",children:"ð±"})]}):s.source==="device_android"?e.jsx("span",{className:"h-4 w-4 text-green-500 text-xs",children:"ð¤"}):s.source==="device"?e.jsx("span",{className:"h-4 w-4 text-purple-500 text-xs",children:"ð±"}):e.jsx(S,{variant:"outline",className:"text-xs capitalize",children:s.source})]})]},s.id))})]})]})]})})};export{de as default};
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.