1import{a as L,r as d,g as f,j as e,B as N,aa as M,i as q,z as v}from"./index-CdeDfzB2.js";import{C}from"./Card-BeosiVXA.js";import{u as A}from"./useMutation-CPU-rogR.js";const p=[{value:"visibility_score_drop",label:"Visibility Score Drop",unit:"points",default:10},{value:"visibility_score_increase",label:"Visibility Score Increase",unit:"points",default:5},{value:"traffic_drop",label:"Traffic Drop",unit:"%",default:20},{value:"traffic_spike",label:"Traffic Spike",unit:"%",default:20},{value:"keyword_position_drop",label:"Keyword Position Drop",unit:"positions",default:5},{value:"keyword_position_gain",label:"Keyword Position Gain",unit:"positions",default:3},{value:"site_health_critical",label:"Critical Health Issues",unit:"issues",default:0}],I=({onClose:a,onSuccess:c})=>{const{activeSite:n}=L(),[u,o]=d.useState(p[0].value),[g,b]=d.useState(p[0].default),[m,s]=d.useState(["in_app","email"]),[l,x]=d.useState(null),[j,h]=d.useState(!1),[y,k]=d.useState(null),i=p.find(t=>t.value===u);d.useEffect(()=>{n&&f.from("slack_connections").select("channel_name").eq("site_id",n.id).maybeSingle().then(({data:t})=>x(t))},[n==null?void 0:n.id]);const _=t=>s(r=>r.includes(t)?r.filter(w=>w!==t):[...r,t]),E=async t=>{t.preventDefault(),k(null),h(!0);try{if(!n)throw new Error("No active site");const{error:r}=await f.from("alert_rules").insert({site_id:n.id,alert_type:u,alert_name:((i==null?void 0:i.label)??"")+" Alert",threshold_value:g,comparison_operator:"gt",channels:m,is_active:!0,slack_channel:m.includes("slack")?(l==null?void 0:l.channel_name)??null:null});if(r)throw r;c(),a()}catch(r){k(r.message)}finally{h(!1)}};return e.jsx("div",{className:"fixed inset-0 bg-black bg-opacity-60 backdrop-blur-sm flex items-center justify-center z-[60] p-4 animate-fade-in-up",style:{animationDuration:"0.3s"},onClick:a,children:e.jsxs("div",{className:"bg-white rounded-2xl shadow-xl w-full max-w-md flex flex-col",onClick:t=>t.stopPropagation(),children:[e.jsx("div",{className:"p-6 border-b border-slate-200",children:e.jsx("h2",{className:"text-xl font-bold text-slate-900",children:"Create New Alert"})}),e.jsxs("form",{onSubmit:E,className:"p-6 space-y-6",children:[y&&e.jsx("div",{className:"p-3 bg-red-50 text-red-700 rounded-md text-sm",children:y}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium text-gray-700 mb-1",children:"Alert Type"}),e.jsx("select",{value:u,onChange:t=>{o(t.target.value);const r=p.find(w=>w.value===t.target.value);r&&b(r.default)},className:"w-full rounded-lg border-gray-300 border px-3 py-2 text-gray-900 focus:border-blue-500 focus:ring-blue-500",children:p.map(t=>e.jsx("option",{value:t.value,children:t.label},t.value))})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium text-gray-700 mb-1",children:"Condition"}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsxs("span",{className:"text-gray-600 text-sm",children:["Trigger when ",i==null?void 0:i.label.toLowerCase()," changes by"]}),e.jsx("input",{type:"number",value:g,onChange:t=>b(Number(t.target.value)),className:"w-20 rounded-lg border-gray-300 border px-3 py-2 text-gray-900 focus:border-blue-500 focus:ring-blue-500",min:"0"}),e.jsx("span",{className:"text-gray-600 text-sm",children:i==null?void 0:i.unit})]})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium text-gray-700 mb-2",children:"Notification Channels"}),e.jsxs("div",{className:"flex flex-wrap gap-4",children:[[{id:"in_app",label:"In-App"},{id:"email",label:"Email"},{id:"webhook",label:"Webhook"}].map(t=>e.jsxs("label",{className:"flex items-center gap-2 cursor-pointer",children:[e.jsx("input",{type:"checkbox",checked:m.includes(t.id),onChange:()=>_(t.id),className:"rounded border-gray-300 text-blue-600 focus:ring-blue-500"}),e.jsx("span",{className:"text-sm text-gray-700",children:t.label})]},t.id)),l&&e.jsxs("label",{className:"flex items-center gap-2 cursor-pointer",children:[e.jsx("input",{type:"checkbox",checked:m.includes("slack"),onChange:()=>_("slack"),className:"rounded border-gray-300 text-blue-600 focus:ring-blue-500"}),e.jsxs("span",{className:"text-sm text-gray-700",children:["Slack",l.channel_name?` (${l.channel_name})`:""]})]})]})]}),e.jsxs("div",{className:"flex justify-end gap-3 pt-4 border-t border-slate-100",children:[e.jsx(N,{type:"button",variant:"outline",onClick:a,children:"Cancel"}),e.jsx(N,{type:"submit",isLoading:j,children:"Create Alert"})]})]})]})})},V=()=>{const{activeSite:a}=L(),c=M(),[n,u]=d.useState(!1),{data:o,isLoading:g}=q({queryKey:["alerts",a==null?void 0:a.id],queryFn:async()=>
1{if(!a)return[];const{data:s,error:l}=await f.from("alert_rules").select("*").eq("site_id",a.id).order("created_at",{ascending:!1});if(l)throw l;return s},enabled:!!a}),b=A({mutationFn:async s=>{const{error:l}=await f.from("alert_rules").delete().eq("id",s);if(l)throw l},onSuccess:()=>{c.invalidateQueries({queryKey:["alerts"]}),v.success("Alert deleted successfully")},onError:s=>{v.error(s.message)}}),m=A({mutationFn:async({id:s,isActive:l})=>{const{error:x}=await f.from("alert_rules").update({is_active:l}).eq("id",s);if(x)throw x},onSuccess:()=>{c.invalidateQueries({queryKey:["alerts"]}),v.success("Alert updated")}});return g?e.jsx("div",{className:"p-8 text-center text-gray-500",children:"Loading alerts..."}):e.jsxs("div",{className:"p-6 max-w-5xl mx-auto",children:[e.jsxs("div",{className:"flex justify-between items-center mb-8",children:[e.jsxs("div",{children:[e.jsx("h1",{className:"text-3xl font-bold text-gray-900",children:"Alerts & Notifications"}),e.jsx("p",{className:"mt-1 text-gray-600",children:"Get notified when important metric changes occur"})]}),e.jsx(N,{onClick:()=>u(!0),children:"+ Create Alert"})]}),e.jsx("div",{className:"grid gap-6",children:(o==null?void 0:o.length)===0?e.jsx(C,{children:e.jsxs("div",{className:"text-center py-12",children:[e.jsx("h3",{className:"text-lg font-medium text-gray-900",children:"No alerts configured"}),e.jsx("p",{className:"mt-2 text-gray-500",children:"Set up your first alert to stay on top of your site's performance."}),e.jsx("div",{className:"mt-6",children:e.jsx(N,{onClick:()=>u(!0),children:"Create Your First Alert"})})]})}):o==null?void 0:o.map(s=>{var l,x,j,h;return e.jsx(C,{children:e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{className:"flex items-start gap-4",children:[e.jsx("div",{className:`p-3 rounded-full ${s.is_active?"bg-blue-100 text-blue-600":"bg-gray-100 text-gray-400"}`,children:e.jsx(S,{className:"h-6 w-6"})}),e.jsxs("div",{children:[e.jsx("h3",{className:"text-lg font-semibold text-gray-900",children:s.alert_name}),e.jsxs("p",{className:"text-sm text-gray-500 mt-1",children:["Triggers when ",e.jsx("strong",{children:D(s.alert_type)})," is",e.jsxs("strong",{children:[" ",s.comparison_operator==="gt"?">":"<"," ",s.threshold_value]})]}),e.jsxs("div",{className:"flex items-center gap-3 mt-2 flex-wrap",children:[((l=s.channels)==null?void 0:l.includes("in_app"))&&e.jsxs("span",{className:"flex items-center gap-1 text-xs text-gray-500",children:[e.jsx(S,{className:"h-3 w-3"})," In-App"]}),((x=s.channels)==null?void 0:x.includes("email"))&&e.jsxs("span",{className:"flex items-center gap-1 text-xs text-gray-500",children:[e.jsx(T,{className:"h-3 w-3"})," Email"]}),((j=s.channels)==null?void 0:j.includes("webhook"))&&e.jsx("span",{className:"text-xs text-blue-600 font-medium",children:"â¡ Webhook"}),((h=s.channels)==null?void 0:h.includes("slack"))&&e.jsxs("span",{className:"text-xs text-purple-600 font-medium",children:["# ",s.slack_channel??"Slack"]}),e.jsxs("span",{className:"text-xs text-gray-400 ml-auto",children:["Last fired: ",s.last_triggered_at?new Date(s.last_triggered_at).toLocaleDateString():"Never"]})]})]})]}),e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsxs("label",{className:"relative inline-flex items-center cursor-pointer",children:[e.jsx("input",{type:"checkbox",className:"sr-only peer",checked:s.is_active,onChange:y=>m.mutate({id:s.id,isActive:y.target.checked})}),e.jsx("div",{className:"w-11 h-6 bg-gray-200 peer-focus:outline-none peer-focus:ring-4 peer-focus:ring-blue-300 rounded-full peer peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:border-gray-300 after:border after:rounded-full after:h-5 after:w-5 after:transition-all peer-checked:bg-blue-600"})]}),e.jsx("button",{onClick:()=>b.mutate(s.id),className:"text-gray-400 hover:text-red-600 p-2",title:"Delete Alert",children:e.jsx(B,{className:"h-5 w-5"})})]})]})},s.id)})}),n&&e.jsx(I,{onClose:()=>u(!1),onSuccess:()=>{c.invalidateQueries({queryKey:["alerts"]}),v.success("Alert created successfully!")}})]})},D=a=>a.split("_").map(c=>c.charAt(0).toUpperCase()+c.slice(1)).join(" "),S=({className:a})=>e.jsx("svg",{xmlns:"http://www.w3.org/2000/svg",fill:"none",viewBox:"0 0 24 24",strokeWidth:1.5,stroke:"currentColor",className:a,children:e.jsx("path",{strokeLinecap:"round",strokeLinejoin:"round",d:"M14.857 17.082a23.848 23.848 0 005.454-1.31A8.967 8.967 0 0118 9.75v-.7V9A6 6 0 006 9v.75a8.967 8.967 0 01-2.312 6.022c1.733.64 3.56 1.085 5.455 1.31m5.714 0a24.255 24.255 0 01-5.714 0m5.714 0a3 3 0 11-5.714 0"})}),T=({className:a})=>e.jsx("svg",{xmlns:"http://www.w3.org/2000/svg",fill:"none",viewBox:"0 0 24 24",strokeWidth:1.5,stroke:"currentColor",className:a,children:e.jsx("path",{strokeLinecap:"round",strokeLinejoin:"round",d:"M21.75 6.75v10.5a2.25 2.25 0 01-2.25 2.25h-15a2.25 2.25 0 01-2.25-2.25V6.75m19.5 0A2.25 2.25 0 0019.5 4.5h-15a2.25 2.25 0 00-2.25 2.25m19.5 0v.243a2.25 2.25 0 01-1.07 1.916l-7.5 4.615a2.25 2.25 0 01-2.36 0L3.32 8.91a2.25 2.25 0 01-1.07-1.916V6.75"})}),B=({className:a})=>e.jsx("svg",{xmlns:"http://www.w3.org/2000/svg",fill:"none",viewBox:"0 0 24 24",strokeWidth:1.5,stroke:"currentColor",className:a,children:e.jsx("path",{strokeLinecap:"round",strokeLinejoin:"round",d:"M14.74 9l-.346 9m-4.788 0L9.26 9m9.968-3.21c.342.052.682.107 1.022.166m-1.022-.165L18.16 19.673a2.25 2.25 0 01-2.244 2.077H8.084a2.25 2.25 0 01-2.244-2.077L4.772 5.79m14.456 0a48.108 48.108 0 00-3.478-.397m-12 .562c.34-.059.68-.114 1.022-.165m0 0a48.11 48.11 0 013.478-.397m7.5 0v-.916c0-1.18-.91-2.164-2.09-2.201a51.964 51.964 0 00-3.32 0c-1.18.037-2.09 1.022-2.09 2.201v.916m7.5 0a48.667 48.667 0 00-7.5 0"})});
1export{V 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.