1import{j as e}from"./ui-pZ967-NS.js";import{r as p,aw as V,s as K,ce as M,X as S,C as $,aa as H,ab as W}from"./utils-B5_VV7Cb.js";import{p as G,u as X,d as u,B as k,s as g,l as d,D as J,y as Y,z as Z,E as ee,G as se,I as o,T as F,F as ae}from"./blog-BCH5Rkw1.js";import{l as w}from"./activityLog-Df4hgbNQ.js";import{f as te,g as ne,h as L,i as ie,C as A,d as E,a as le,b as re,c as ce,L as r,n as q,o as B,p as O,q as R,r as h,s as C}from"./tools-Dnd2ifXP.js";import"./vendor-COMpnQoB.js";const de=n=>({id:"",kind:n,title:"",body:"",image_url:"",cta_label:"",cta_url:"",cta_new_tab:!0,variant:"info",dismissible:!0,show_once_per_session:!0,start_at:null,end_at:null,show_on_paths:[],enabled:!1,priority:0});function z(n){if(!n)return"";const l=new Date(n);if(Number.isNaN(l.getTime()))return"";const j=l.getTime()-l.getTimezoneOffset()*6e4;return new Date(j).toISOString().slice(0,16)}function U(n){return n?new Date(n).toISOString():null}function ve(){const n=G(),[l,j]=p.useState("announcement"),[m,a]=p.useState(null),{data:b=[],isLoading:v}=X({queryKey:["admin-site-notices"],queryFn:async()=>{const{data:t,error:s}=await g.from("site_notices").select("*").order("priority",{ascending:!1}).order("created_at",{ascending:!1});if(s)throw s;return t??[]}}),f=p.useMemo(()=>b.filter(t=>t.kind===l),[b,l]),x=()=>{n.invalidateQueries({queryKey:["admin-site-notices"]}),n.invalidateQueries({queryKey:["site-notices"]})},_=async t=>{const{error:s}=await g.from("site_notices").update({enabled:!t.enabled}).eq("id",t.id);if(s){d.error(s.message);return}w({action:"update",entityType:"site_notices",entityId:t.id,diff:{enabled:!t.enabled}}),d.success(t.enabled?"Notice disabled":"Notice enabled"),x()},i=async t=>{if(!confirm(`Delete "${t.title||t.kind}"? This cannot be undone.`))return;const{error:s}=await g.from("site_notices").delete().eq("id",t.id);if(s){d.error(s.message);return}w({action:"delete",entityType:"site_notices",entityId:t.id,diff:{title:t.title}}),d.success("Deleted"),x()};return e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"flex flex-col sm:flex-row sm:items-center sm:justify-between gap-3",children:[e.jsxs("div",{children:[e.jsx("h1",{className:"text-2xl font-semibold",children:"Announcements"}),e.jsx("p",{className:"text-sm text-muted-foreground",children:"Create and schedule the top-of-page announcement bar and the site popup. Only one enabled item per type is shown at a time (highest priority wins)."})]}),e.jsxs(u,{onClick:()=>a(de(l)),children:[e.jsx(V,{className:"h-4 w-4 mr-1"})," New ",l]})]}),e.jsxs(te,{value:l,onValueChange:t=>j(t),children:[e.jsxs(ne,{children:[e.jsxs(L,{value:"announcement",children:[e.jsx(K,{className:"h-4 w-4 mr-1"})," Announcement bar"]}),e.jsxs(L,{value:"popup",children:[e.jsx(M,{className:"h-4 w-4 mr-1"})," Popup"]})]}),e.jsx(ie,{value:l,className:"space-y-3 pt-3",children:v?e.jsx("p",{className:"text-sm text-muted-foreground",children:"Loadingâ¦"}):f.length===0?e.jsx(A,{children:e.jsxs(E,{className:"py-10 text-center text-sm text-muted-foreground",children:["No ",l,'s yet. Click "New ',l,'" to create one.']})}):e.jsx("div",{className:"grid gap-3",children:f.map(t=>e.jsxs(A,{children:[e.jsx(le,{className:"pb-3",children:e.jsxs("div",{className:"flex items-start justify-between gap-3",children:[e.jsxs("div",{className:"min-w-0",children:[e.jsx(re,{className:"text-base truncate",children:t.title||e.jsx("span",{className:"text-muted-foreground italic",children:"Untitled"})}),e.jsx(ce,{className:"line-clamp-2",children:t.body||"â"})]}),e.jsxs("div",{className:"flex items-center gap-2 shrink-0",children:[e.jsx(k,{variant:t.enabled?"default":"secondary",children:t.enabled?"Live":"Disabled"}),e.jsx(k,{variant:"outline",children:t.variant}),e.jsxs(k,{variant:"outline",children:["P",t.priority]})]})]})}),e.jsxs(E,{className:"flex flex-wrap items-center justify-between gap-3 pt-0",children:[e.jsxs("div",{className:"text-xs text-muted-foreground flex flex-wrap gap-3",children:[t.start_at&&e.jsxs("span",{children:["From: ",new Date(t.start_at).toLocaleString()]}),t.end_at&&e.jsxs("span",{children:["Until: ",new Date(t.end_at).toLocaleStr
1ing()]}),t.show_on_paths.length>0&&e.jsxs("span",{children:["Paths: ",t.show_on_paths.join(", ")]}),t.cta_url&&e.jsxs("span",{children:["CTA â ",t.cta_label||t.cta_url]})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(u,{variant:"outline",size:"sm",onClick:()=>_(t),children:t.enabled?e.jsxs(e.Fragment,{children:[e.jsx(S,{className:"h-4 w-4 mr-1"})," Disable"]}):e.jsxs(e.Fragment,{children:[e.jsx($,{className:"h-4 w-4 mr-1"})," Enable"]})}),e.jsxs(u,{variant:"outline",size:"sm",onClick:()=>a(t),children:[e.jsx(H,{className:"h-4 w-4 mr-1"})," Edit"]}),e.jsx(u,{variant:"ghost",size:"sm",className:"text-destructive",onClick:()=>i(t),children:e.jsx(W,{className:"h-4 w-4"})})]})]})]},t.id))})})]}),m&&e.jsx(oe,{row:m,onClose:()=>a(null),onSaved:()=>{a(null),x()}})]})}function oe({row:n,onClose:l,onSaved:j}){const m=!n.id,[a,b]=p.useState(n),[v,f]=p.useState((n.show_on_paths||[]).join(` 2`)),[x,_]=p.useState(!1),i=(s,y)=>b(N=>({...N,[s]:y})),t=async()=>{var y,N,T,D,I;const s={kind:a.kind,title:((y=a.title)==null?void 0:y.trim())||null,body:((N=a.body)==null?void 0:N.trim())||null,image_url:((T=a.image_url)==null?void 0:T.trim())||null,cta_label:((D=a.cta_label)==null?void 0:D.trim())||null,cta_url:((I=a.cta_url)==null?void 0:I.trim())||null,cta_new_tab:a.cta_new_tab,variant:a.variant,dismissible:a.dismissible,show_once_per_session:a.show_once_per_session,start_at:a.start_at,end_at:a.end_at,show_on_paths:v.split(` 3`).map(c=>c.trim()).filter(Boolean),enabled:a.enabled,priority:Number(a.priority)||0};if(!s.title&&!s.body&&!s.image_url){d.error("Add at least a title, body, or image.");return}_(!0);try{if(m){const{data:c,error:P}=await g.from("site_notices").insert(s).select("id").maybeSingle();if(P)throw P;w({action:"create",entityType:"site_notices",entityId:c==null?void 0:c.id,diff:s}),d.success("Notice created")}else{const{error:c}=await g.from("site_notices").update(s).eq("id",n.id);if(c)throw c;w({action:"update",entityType:"site_notices",entityId:n.id,diff:s}),d.success("Notice saved")}j()}catch(c){d.error(c.message)}finally{_(!1)}};return e.jsx(J,{open:!0,onOpenChange:s=>!s&&l(),children:e.jsxs(Y,{className:"max-w-3xl max-h-[92vh] overflow-y-auto",children:[e.jsxs(Z,{children:[e.jsxs(ee,{children:[m?"New":"Edit"," ",a.kind]}),e.jsx(se,{children:a.kind==="announcement"?"Rendered as a slim bar above the header on every page it targets.":"Rendered as a centered dialog with a close button."})]}),e.jsxs("div",{className:"grid gap-4 md:grid-cols-2",children:[e.jsxs("div",{className:"space-y-3",children:[e.jsxs("div",{children:[e.jsx(r,{children:"Type"}),e.jsxs(q,{value:a.kind,onValueChange:s=>i("kind",s),children:[e.jsx(B,{children:e.jsx(O,{})}),e.jsxs(R,{children:[e.jsx(h,{value:"announcement",children:"Announcement bar"}),e.jsx(h,{value:"popup",children:"Popup"})]})]})]}),e.jsxs("div",{children:[e.jsx(r,{children:"Title"}),e.jsx(o,{value:a.title??"",onChange:s=>i("title",s.target.value),placeholder:"Black Friday sale"})]}),e.jsxs("div",{children:[e.jsx(r,{children:"Body / message"}),e.jsx(F,{rows:a.kind==="popup"?4:2,value:a.body??"",onChange:s=>i("body",s.target.value),placeholder:"Short details shown to visitors."})]}),a.kind==="popup"&&e.jsxs("div",{children:[e.jsx(r,{children:"Image URL (optional)"}),e.jsx(o,{value:a.image_url??"",onChange:s=>i("image_url",s.target.value),placeholder:"https://â¦/banner.jpg"})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-2",children:[e.jsxs("div",{children:[e.jsx(r,{children:"CTA label"}),e.jsx(o,{value:a.cta_label??"",onChange:s=>i("cta_label",s.target.value),placeholder:"Shop now"})]}),e.jsxs("div",{children:[e.jsx(r,{children:"CTA URL"}),e.jsx(o,{value:a.cta_url??"",onChange:s=>i("cta_url",s.target.value),placeholder:"https://â¦"})]})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(C,{id:"new-tab",checked:a.cta_new_tab,onCheckedChange:s=>i("cta_new_tab",s)}),e.jsx(r,{htmlFor:"new-tab",children:"Open CTA in new tab"})]})]}),e.jsxs("div",{className:"space-y-3",children:[e.jsxs("div",{children:[e.jsx(r,{children:"Style variant"}),e.jsxs(q,{value:a.variant,onValueChange:s=>i("variant",s),children:[e.jsx(B,{children:e.jsx(O,{})}),e.jsxs(R,{children:[e.jsx(h,{value:"info",children:"Info (brand)"}),e.jsx(h,{value:"warning",children:"Warning"}),e.jsx(h,{value:"success",children:"Success"}),e.jsx(h,{value:"promo",children:"Promo (gradient)"}),e.jsx(h,{value:"dark",children:"Dark"})]})]})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-2",children:[e.jsxs("div",{children:[e.jsx(r,{children:"Start"}),e.jsx(o,{type:"datetime-local",value:z(a.start_at),onChange:s=>i("start_at",U(s.target.value))})]}),e.jsxs("div",{children:[e.jsx(r,{children:"End"}),e.jsx(o,{type:"datetime-local",value:z(a.end_at),onChange:s=>i("end_at",U(s.target.value))})]})]}),e.jsxs("div",{children:[e.jsx(r,{children:"Show on paths (one per line, empty = all pages)"}
3),e.jsx(F,{rows:3,value:v,onChange:s=>f(s.target.value),placeholder:`/ 4/blog/* 5/services`,className:"font-mono text-xs"}),e.jsxs("p",{className:"text-[11px] text-muted-foreground mt-1",children:["Use ",e.jsx("code",{children:"/*"})," at the end for prefixes, or ",e.jsx("code",{children:"*"})," for everywhere."]})]}),e.jsxs("div",{children:[e.jsx(r,{children:"Priority"}),e.jsx(o,{type:"number",value:a.priority,onChange:s=>i("priority",Number(s.target.value))})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(C,{id:"dismissible",checked:a.dismissible,onCheckedChange:s=>i("dismissible",s)}),e.jsx(r,{htmlFor:"dismissible",children:"Show close button"})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(C,{id:"once",checked:a.show_once_per_session,onCheckedChange:s=>i("show_once_per_session",s)}),e.jsxs(r,{htmlFor:"once",children:["Show once per ",(a.kind==="popup","session")," (else remember forever)"]})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(C,{id:"enabled",checked:a.enabled,onCheckedChange:s=>i("enabled",s)}),e.jsx(r,{htmlFor:"enabled",children:"Enabled (live on site)"})]})]})]}),e.jsxs("div",{className:"rounded-md border p-3 bg-muted/30",children:[e.jsx("p",{className:"text-xs font-medium mb-2 text-muted-foreground",children:"Preview"}),a.kind==="announcement"?e.jsx(he,{row:a}):e.jsx(me,{row:a})]}),e.jsxs(ae,{children:[e.jsx(u,{variant:"outline",onClick:l,children:"Cancel"}),e.jsx(u,{onClick:t,disabled:x,children:x?"Savingâ¦":m?"Create":"Save changes"})]})]})})}const Q={info:"bg-primary text-primary-foreground",warning:"bg-amber-500 text-black",success:"bg-emerald-500 text-white",promo:"bg-gradient-to-r from-fuchsia-600 to-indigo-600 text-white",dark:"bg-foreground text-background"};function he({row:n}){return e.jsx("div",{className:`w-full rounded ${Q[n.variant]} text-sm`,children:e.jsxs("div",{className:"px-3 py-2 flex items-center gap-3",children:[e.jsxs("div",{className:"flex-1 flex flex-wrap gap-x-2",children:[n.title&&e.jsx("strong",{children:n.title}),n.body&&e.jsx("span",{children:n.body}),n.cta_label&&e.jsx("span",{className:"underline",children:n.cta_label})]}),n.dismissible&&e.jsx(S,{className:"h-4 w-4 opacity-70"})]})})}function me({row:n}){return e.jsxs("div",{className:"relative w-full max-w-sm mx-auto rounded-2xl border bg-background shadow overflow-hidden",children:[n.dismissible&&e.jsx("div",{className:"absolute top-2 right-2 rounded-full p-1 bg-background/80",children:e.jsx(S,{className:"h-3.5 w-3.5"})}),n.image_url&&e.jsx("img",{src:n.image_url,alt:"",className:"w-full aspect-video object-cover"}),e.jsxs("div",{className:"p-4 space-y-2",children:[n.title&&e.jsx("p",{className:"font-semibold",children:n.title}),n.body&&e.jsx("p",{className:"text-xs text-muted-foreground whitespace-pre-line",children:n.body}),n.cta_label&&e.jsx("div",{className:`mt-2 rounded text-center py-1.5 text-xs font-medium ${Q[n.variant]}`,children:n.cta_label})]})]})}export{ve 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.