1import{a as fe,ad as je,r as c,s as l,j as e,L as k,B as _,aq as we,ak as ve,E as Ne}from"./index-KSWbwNeY.js";import{A as F}from"./AdminNav-DsaLvjFx.js";import{D as be,a as ye,b as Se,c as ke,f as _e}from"./dialog-Bpfij23O.js";import{P as H,T as De}from"./trending-down-DkMB12LV.js";import{C as Ce}from"./circle-alert-B57G6NgG.js";import{S as Ae}from"./settings-2-DosyAUqX.js";import{D as Pe}from"./download-BtORIOsS.js";import"./index-DsYLezIw.js";import"./user-cog-DvqSS7bY.js";import"./trash-2-CwcwS_bf.js";import"./book-open-DS7Nmuzn.js";import"./receipt-DgWJf9Ou.js";import"./chevron-down-D7IdZFzc.js";import"./index-BpbyRUKB.js";function Me(){const{toast:d}=fe(),[D,K]=je(),[N,Y]=c.useState(null),[J,T]=c.useState(!0),[C,O]=c.useState(!1),[A,R]=c.useState(!1),[Q,P]=c.useState(!1),[$,V]=c.useState([]),[X,q]=c.useState(!1),[m,Z]=c.useState(null),[j,ee]=c.useState(null),[w,se]=c.useState({}),[B,te]=c.useState([]),[x,ie]=c.useState(null),[o,ae]=c.useState(null);c.useEffect(()=>{let s=!1;const t=async()=>{const h=new Date(Date.now()-36e5).toISOString(),{data:u}=await l.from("gsc_sync_log").select("status,rows_synced").gte("started_at",h);if(s||!u)return;const n={running:0,success:0,partial:0,error:0,rows:0};for(const p of u)p.status==="running"?n.running++:p.status==="success"?n.success++:p.status==="partial"?n.partial++:p.status==="error"&&n.error++,n.rows+=p.rows_synced||0;ae(n),!s&&n.running>0&&setTimeout(t,8e3)};return t(),()=>{s=!0}},[C,A]),c.useEffect(()=>{const s=D.get("gsc");s==="connected"?d({title:"Search Console connected"}):s==="choose"?(d({title:"Multiple properties found",description:"Pick the one to track."}),setTimeout(()=>I(),300)):s!=null&&s.startsWith("error:")&&d({title:"Connection failed",description:s.slice(6),variant:"destructive"}),s&&(D.delete("gsc"),K(D,{replace:!0}))},[]);const I=async()=>{P(!0),q(!0);const{data:s,error:t}=await l.functions.invoke("gsc-set-site",{body:{action:"list"}});if(q(!1),t){d({title:"Failed to load properties",description:t.message,variant:"destructive"});return}V((s==null?void 0:s.sites)||[])},re=async s=>{const{error:t}=await l.functions.invoke("gsc-set-site",{body:{action:"set",siteUrl:s}});if(t){d({title:"Failed to switch",description:t.message,variant:"destructive"});return}P(!1),d({title:"Property switched",description:s}),b()},b=async()=>{T(!0);const{data:s}=await l.rpc("get_gsc_connection_status"),t=Array.isArray(s)?s[0]??null:s??null;if(Y(t),t){let h=null;try{const a=new URL(t.site_url);h=`${a.protocol}//${a.host}/`}catch{h=null}const u=a=>h?a.like("page_url",`${h}%`):a,n=new Date(Date.now()-2*864e5).toISOString().slice(0,10),p=new Date(Date.now()-30*864e5).toISOString().slice(0,10),y=new Date(Date.now()-58*864e5).toISOString().slice(0,10),{data:S}=await u(l.from("gsc_daily_page_stats").select("clicks,impressions,ctr,position").eq("search_type","web").gte("date",p).lte("date",n)),v=a=>a.reduce((r,i)=>({clicks:r.clicks+i.clicks,impressions:r.impressions+i.impressions,ctrAcc:r.ctrAcc+i.ctr*i.impressions,posAcc:r.posAcc+i.position*i.impressions}),{clicks:0,impressions:0,ctrAcc:0,posAcc:0}),f=v(S||[]);Z({clicks:f.clicks,impressions:f.impressions,ctr:f.impressions?f.ctrAcc/f.impressions:0,position:f.impressions?f.posAcc/f.impressions:0});const{data:de}=await u(l.from("gsc_daily_page_stats").select("clicks,impressions").eq("search_type","web").gte("date",y).lt("date",p)),E=v(de||[]);ee({clicks:E.clicks,impressions:E.impressions});const U={};for(const a of["web","discover","news"]){const{data:r}=await u(l.from("gsc_daily_page_stats").select("clicks,impressions").eq("search_type",a).gte("date",p).lte("date",n));U[a]=(r||[]).reduce((i,g)=>({clicks:i.clicks+g.clicks,impressions:i.impressions+g.impressions}),{clicks:0,impressions:0})}se(U);const W=new Date(Date.now()-16*864e5).toISOString().slice(0,10),me=new Date(Date.now()-30*864e5).toISOString().slice(0,10),{data:pe}=await u(l.from("gsc_daily_page_stats").select("page_url,clicks,impressions,position").eq("search_type","web").gte("date",W).lte("date",n)),{data:ue}=await u(l.from("gsc_daily_page_stats").select("page_url,impressions,position").eq("search_type","web").gte("date",me).lt("date",W)),z=a=>{const r=new Map;for(const i of a||[]){const g=r.get(i.page_url)||{imp:0,posAcc:0};g.imp+=i.impressions,g.posAcc+=i.position*i.impressions,r.set(i.page_url,g)}return r},xe=z(pe||[]),he=z(ue||[]),L=[];for(const[a,r]of xe){if(r.imp<50)continue;const i=he.get(a);if(!i||i.imp<50)continue;const g=r.posAcc/r.imp,G=i.posAcc/i.imp,M=g-G;M>1&&L.push({page_url:a,recent_pos:g,prior_pos:G,impressions:r.imp,delta:M})}L.sort((a,r)=>r.delta-a.delta),te(L.slice(0,50));const{data:ge}=await l.from("gsc_sync_log").select("*").order("started_at",{ascending:!1}).limit(1).maybeSingle();ie(ge)}T(!1)};c.useEffect(()=>{b()},[]);const ce=async()=>{const{data:s,error:t}=await l.functions.invoke("gsc-oauth-start",{body:{}});if(t||!(s!=null&&s.url)){d({title:"Failed to start OAuth",description:t==null?void 0:t.message,variant:"destructive"});return}window.location.href=s.url},ne=async()=>{O(!0);const{data:s,error:t}=await l.functions.invoke("gsc-sync-daily",{body:{}});O(!1),t?d({title:"Sync failed",description:t.message,variant:"destructive"}):(d({title:"Sync complete",description:`${(s==null?void 0:s.rows)??0} rows synced`}),b())},oe=async()=>{R(!0);const{data:s,error:t}=await l.functions.invoke("gsc-sync-backfill",{body:{days:90}});R(!1),t?d({title:"Backfill failed",description:t.message,variant:"destructive"}
1):s!=null&&s.skipped?d({title:"Backfill skipped",description:s.reason||"Another sync is already running.",variant:"destructive"}):(d({title:"Backfill queued",description:`${(s==null?void 0:s.queued)??0} chunks queued. Will populate over the next several minutes.`}),b())},le=(s,t)=>t===0?0:(s-t)/t*100;return J?e.jsxs("div",{className:"min-h-screen bg-background",children:[e.jsx(F,{}),e.jsx("main",{className:"md:ml-52 p-6",children:e.jsx(k,{className:"h-5 w-5 animate-spin"})})]}):N?e.jsxs("div",{className:"min-h-screen bg-background",children:[e.jsx(F,{}),e.jsx("main",{className:"md:ml-52 p-4 md:p-6",children:e.jsxs("div",{className:"max-w-7xl mx-auto space-y-4",children:[e.jsxs("div",{className:"flex items-center justify-between gap-4",children:[e.jsxs("div",{className:"min-w-0",children:[e.jsx("h1",{className:"text-xl font-semibold",children:"Search Console"}),e.jsxs("p",{className:"text-xs text-muted-foreground truncate",children:[N.site_url," · last sync ",x?new Date(x.started_at).toLocaleString():"â"," (",x==null?void 0:x.status,")"]}),o&&o.running+o.success+o.partial+o.error>0&&e.jsxs("p",{className:"text-xs text-muted-foreground mt-1",children:["Backfill (last hour): ",o.success," done",o.partial>0&&e.jsxs(e.Fragment,{children:[" · ",o.partial," partial"]}),o.error>0&&e.jsxs(e.Fragment,{children:[" · ",e.jsxs("span",{className:"text-red-600",children:[o.error," failed"]})]}),o.running>0&&e.jsxs(e.Fragment,{children:[" · ",e.jsxs("span",{className:"text-amber-600",children:[o.running," in progress"]})]})," · ",o.rows.toLocaleString()," rows imported"]}),(x==null?void 0:x.error)&&e.jsxs("p",{className:"text-xs text-red-600 mt-1 flex items-start gap-1.5",children:[e.jsx(Ce,{className:"h-3.5 w-3.5 mt-0.5 flex-shrink-0"}),e.jsx("span",{className:"break-all",children:x.error})]})]}),e.jsxs("div",{className:"flex gap-2 flex-shrink-0",children:[e.jsxs(_,{onClick:I,variant:"outline",size:"sm",className:"gap-1.5 h-8",children:[e.jsx(Ae,{className:"h-3.5 w-3.5"})," Change property"]}),e.jsxs(_,{onClick:ne,variant:"outline",size:"sm",className:"gap-1.5 h-8",disabled:C,children:[C?e.jsx(k,{className:"h-3.5 w-3.5 animate-spin"}):e.jsx(we,{className:"h-3.5 w-3.5"})," Sync now"]}),e.jsxs(_,{onClick:oe,variant:"outline",size:"sm",className:"gap-1.5 h-8",disabled:A,children:[A?e.jsx(k,{className:"h-3.5 w-3.5 animate-spin"}):e.jsx(Pe,{className:"h-3.5 w-3.5"})," Backfill 90d"]})]})]}),e.jsx(be,{open:Q,onOpenChange:P,children:e.jsxs(ye,{children:[e.jsxs(Se,{children:[e.jsx(ke,{children:"Choose Search Console property"}),e.jsx(_e,{children:"Pick which property to pull data for. You can change this anytime."})]}),X?e.jsx("div",{className:"py-8 flex justify-center",children:e.jsx(k,{className:"h-5 w-5 animate-spin text-muted-foreground"})}):e.jsxs("div",{className:"space-y-1 max-h-96 overflow-y-auto",children:[$.length===0&&e.jsx("p",{className:"text-sm text-muted-foreground",children:"No properties available."}),$.map(s=>e.jsxs("button",{onClick:()=>re(s.siteUrl),className:`w-full text-left p-3 border hover:bg-muted/50 transition-colors ${s.siteUrl===N.site_url?"border-primary bg-primary/5":""}`,children:[e.jsx("div",{className:"font-mono text-sm",children:s.siteUrl}),e.jsxs("div",{className:"text-xs text-muted-foreground",children:[s.permissionLevel,s.siteUrl===N.site_url?" · current":""]})]},s.siteUrl))]})]})}),e.jsx("div",{className:"grid grid-cols-4 gap-3",children:[{label:"Clicks (28d)",value:(m==null?void 0:m.clicks)??0,prev:(j==null?void 0:j.clicks)??0,fmt:s=>s.toLocaleString()},{label:"Impressions (28d)",value:(m==null?void 0:m.impressions)??0,prev:(j==null?void 0:j.impressions)??0,fmt:s=>s.toLocaleString()},{label:"CTR (28d)",value:((m==null?void 0:m.ctr)??0)*100,prev:0,fmt:s=>`${s.toFixed(2)}%`,hideDelta:!0},{label:"Avg Position",value:(m==null?void 0:m.position)??0,prev:0,fmt:s=>s.toFixed(1),hideDelta:!0}].map(s=>{const t=s.hideDelta?0:le(s.value,s.prev);return e.jsxs("div",{className:"bg-muted/30 border p-3 rounded-none",children:[e.jsx("div",{className:"text-xs text-muted-foreground mb-1",children:s.label}),e.jsx("div",{className:"text-2xl font-bold",children:s.fmt(s.value)}),!s.hideDelta&&e.jsxs("div",{className:`text-xs flex items-center gap-1 ${t>=0?"text-emerald-600":"text-red-600"}`,children:[t>=0?e.jsx(ve,{className:"h-3 w-3"}):e.jsx(De,{className:"h-3 w-3"}),t.toFixed(1),"% vs prior"]})]},s.label)})}),e.jsxs("div",{className:"border bg-card p-4",children:[e.jsx("h2",{className:"text-sm font-semibold mb-3",children:"Traffic source split (28d, by impressions)"}),e.jsx("div",{className:"grid grid-cols-3 gap-4",children:["web","discover","news"].map(s=>{var u,n,p,y,S,v;const t=(((u=w.web)==null?void 0:u.impressions)||0)+(((n=w.discover)==null?void 0:n.impressions)||0)+(((p=w.news)==null?void 0:p.impressions)||0),h=t?(((y=w[s])==null?void 0:y.impressions)||0)/t*100:0;return e.jsxs("div",{children:[e.jsxs("div",{className:"flex justify-between text-xs mb-1",children:[e.jsx("span",{className:"capitalize font-medium",children:s==="web"?"Web Search":s}),e.jsxs("span",{className:"text-muted-foreground",children:[h.toFixed(0),"%"]})]}),e.jsx("div",{className:"h-2 bg-muted",children:e.jsx("div",{className:"h-full bg-primary",style:{width:`${h}%`}})}),e.jsxs("div",{className:"text-xs text-muted-foreground mt-1",children:[(((S=w[s])==null?void 0:S.clicks)||0).toLocaleStr
1ing()," clicks · ",(((v=w[s])==null?void 0:v.impressions)||0).toLocaleString()," impressions"]})]},s)})}),e.jsx("p",{className:"text-xs text-muted-foreground mt-3",children:"Heavy lean on Discover/News confirms Google sees us primarily as a news publisher. Goal: grow the Web Search share via evergreen content."})]}),e.jsxs("div",{className:"border bg-card",children:[e.jsxs("div",{className:"p-4 border-b",children:[e.jsx("h2",{className:"text-sm font-semibold",children:"Top declining pages â last 14d vs prior 14d (Web Search)"}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Sorted by largest position drop. Refresh these first."})]}),e.jsx("div",{className:"overflow-x-auto",children:e.jsxs("table",{className:"w-full text-xs",children:[e.jsx("thead",{className:"bg-muted/40 text-muted-foreground",children:e.jsxs("tr",{children:[e.jsx("th",{className:"text-left p-2",children:"Page"}),e.jsx("th",{className:"text-right p-2 w-24",children:"Prior pos"}),e.jsx("th",{className:"text-right p-2 w-24",children:"Recent pos"}),e.jsx("th",{className:"text-right p-2 w-24",children:"Î"}),e.jsx("th",{className:"text-right p-2 w-28",children:"Impressions"}),e.jsx("th",{className:"p-2 w-12"})]})}),e.jsxs("tbody",{children:[B.length===0&&e.jsx("tr",{children:e.jsx("td",{colSpan:6,className:"p-6 text-center text-muted-foreground",children:"No data yet â run a sync or backfill."})}),B.map(s=>e.jsxs("tr",{className:"border-t hover:bg-muted/20",children:[e.jsx("td",{className:"p-2 truncate max-w-md",children:e.jsx("span",{className:"truncate block",children:s.page_url})}),e.jsx("td",{className:"p-2 text-right",children:s.prior_pos.toFixed(1)}),e.jsx("td",{className:"p-2 text-right",children:s.recent_pos.toFixed(1)}),e.jsxs("td",{className:"p-2 text-right text-red-600",children:["+",s.delta.toFixed(1)]}),e.jsx("td",{className:"p-2 text-right",children:s.impressions.toLocaleString()}),e.jsx("td",{className:"p-2",children:e.jsx("a",{href:s.page_url,target:"_blank",rel:"noreferrer",className:"text-muted-foreground hover:text-foreground",children:e.jsx(Ne,{className:"h-3.5 w-3.5"})})})]},s.page_url))]})]})})]})]})})]}):e.jsxs("div",{className:"min-h-screen bg-background",children:[e.jsx(F,{}),e.jsx("main",{className:"md:ml-52 p-4 md:p-6",children:e.jsxs("div",{className:"max-w-2xl mx-auto mt-12 border bg-card p-8 text-center space-y-4",children:[e.jsx(H,{className:"h-10 w-10 mx-auto text-muted-foreground"}),e.jsx("h1",{className:"text-xl font-semibold",children:"Connect Google Search Console"}),e.jsxs("p",{className:"text-sm text-muted-foreground",children:["Sign in with the Google account that has access to ",e.jsx("strong",{children:"analyticsindiamag.com"})," in Search Console. We'll cache daily clicks, impressions, CTR and position so editors can spot ranking decay and prioritize content refreshes."]}),e.jsxs(_,{onClick:ce,className:"gap-2",children:[e.jsx(H,{className:"h-4 w-4"})," Connect Search Console"]})]})})]})}export{Me 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.