1import{j as e,ah as A,r as o,ax as _,i as T,s as k,U as Q,ay as F}from"./index-B5Aoxc7f.js";import{u as L}from"./auth-DCQNNPBD.js";import{A as M,a as $,u as B}from"./AdminShell-ubpJOWqi.js";import{L as P}from"./loader-circle-BavV0jUT.js";import{P as K}from"./plus-xDagU1me.js";import{T as D}from"./trash-2-HCSm3v03.js";import"./dialog-CzbOn1_E.js";import"./input-CaPPmnTN.js";import"./package-Ib0LAcmi.js";import"./flower-2-hJUklDEA.js";import"./info-CDiq_qOh.js";import"./search-C4_LEUwQ.js";function re(){const{user:t,isAdmin:a,loading:i}=L();return e.jsx(M,{hasUser:!!t,isAdmin:a,loading:i,children:e.jsx($,{children:e.jsx(I,{})})})}function I(){const{activeTenant:t,activeTenantId:a,isLoading:i}=B(),d=A(),[l,u]=o.useState({kind:"legacy",slug:_[0].slug}),{data:m}=T({queryKey:["admin-blog-posts",a],enabled:!!a,queryFn:async()=>{const{data:s,error:n}=await k.from("blog_posts").select("id,tenant_id,slug,title,excerpt,body,layout,is_published,sort_order").eq("tenant_id",a).order("sort_order",{ascending:!0}).order("created_at",{ascending:!0});if(n)throw n;return s??[]}}),b=o.useMemo(()=>{const s=new Map;for(const n of m??[])n.layout==="legacy"&&s.set(n.slug,n);return s},[m]),x=o.useMemo(()=>(m??[]).filter(s=>s.layout==="markdown"),[m]);if(i||!t)return e.jsxs("div",{className:"text-muted-foreground text-sm",children:[e.jsx(P,{className:"inline h-4 w-4 animate-spin"})," Loading tenantâ¦"]});const y=async()=>{const s=window.prompt("New post title?")?.trim();if(!s)return;const n=s.toLowerCase().normalize("NFKD").replace(/[\u0300-\u036f]/g,"").replace(/[^a-z0-9]+/g,"-").replace(/^-+|-+$/g,"").slice(0,80);if(!n)return;const{data:g,error:f}=await k.from("blog_posts").insert({tenant_id:a,slug:n,title:s,excerpt:null,body:{content:""},layout:"markdown",is_published:!1}).select("id").single();if(f){alert(f.message);return}d.invalidateQueries({queryKey:["admin-blog-posts",a]}),g?.id&&u({kind:"custom",id:g.id})};return e.jsxs("div",{className:"space-y-6",children:[e.jsxs("div",{children:[e.jsx("h1",{className:"font-display text-3xl font-semibold",children:"Blogs / Articles"}),e.jsxs("p",{className:"text-sm text-muted-foreground mt-1",children:["Editing content for ",e.jsx("b",{children:t.city_name}),". Changes only affect this tenant. Use ",e.jsx("code",{children:"**bold**"})," and ",e.jsx("code",{children:"[label](/path)"})," ","for links."]})]}),e.jsxs("div",{className:"flex flex-wrap gap-2 border-b border-border items-center",children:[_.map(s=>{const n=l?.kind==="legacy"&&l.slug===s.slug;return e.jsx("button",{onClick:()=>u({kind:"legacy",slug:s.slug}),className:"px-4 py-2 text-sm font-medium border-b-2 -mb-px transition-colors "+(n?"border-foreground text-foreground":"border-transparent text-muted-foreground hover:text-foreground"),children:s.label},s.slug)}),x.map(s=>{const n=l?.kind==="custom"&&l.id===s.id;return e.jsx("button",{onClick:()=>u({kind:"custom",id:s.id}),className:"px-4 py-2 text-sm font-medium border-b-2 -mb-px transition-colors "+(n?"border-foreground text-foreground":"border-transparent text-muted-foreground hover:text-foreground"),children:s.title||s.slug},s.id)}),e.jsxs("button",{onClick:y,className:"ml-auto flex items-center gap-1 px-3 py-1.5 rounded-lg border border-border text-xs font-medium hover:bg-muted",children:[e.jsx(K,{className:"h-3.5 w-3.5"})," New post"]})]}),l?.kind==="legacy"&&a&&e.jsx(R,{slug:l.slug,tenantId:a,existing:b.get(l.slug)??null},`${a}:${l.slug}`),l?.kind==="custom"&&e.jsx(O,{post:x.find(s=>s.id===l.id)??null,onDeleted:()=>{u({kind:"legacy",slug:_[0].slug}),d.invalidateQueries({queryKey:["admin-blog-posts",a]})},onSaved:()=>d.invalidateQueries({queryKey:["admin-blog-posts",a]})},l.id)]})}function R({slug:t,tenantId:a,existing:i}){const d=A(),l=o.useMemo(()=>Q(t),[t]),u=o.useMemo(()=>F(t),[t]),m=`blog.${t}.`,b=o.useMemo(()=>{const y=i?.body??{},s={};for(const n of u){const g=n.slice(m.length),f=y[g];s[n]=typeof f=="string"&&f.length>0?f:l[n]??""}return s},[i,u,l,m]),x=()=>{d.invalidateQueries({queryKey:["admin-blog-posts",a]}),d.invalidateQueries({queryKey:["blog-public"]})};return e.jsx(H,{slug:t,keys:u,defaults:l,initial:b,existing:i,tenantId:a,onSaved:x})}const w="w-full rounded-lg border border-border bg-background px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-ring";function N({label:t,hint:a,children:i}){return e.jsxs("label",{className:"block",children:[e.jsx("span",{className:"block text-xs font-semibold text-muted-foreground mb-1",children:t}),i,a&&e.jsx("span",{className:"block text-[11px] text-muted-foreground mt-1 whitespace-pre-line",children:a})]})}const U={"meta.title":{label:"Meta title (browser tab)"},"meta.description":{label:"Meta description (search snippet)",type:"textarea",rows:3},eyebrow:{label:"Eyebrow label (above headline)"},heading:{label:"Page heading (H1)"},intro:{label:"Intro paragraph",type:"textarea",rows:4},"cta.heading":{label:"Bottom CTA â heading"},"cta.body":{label:"Bottom CTA â body",type:"textarea",rows:3},"cta.btn_quote":{label:"Quote button label"},"cta.phone_label":{label:"Phone button label"},"cta.phone_href":{label:"Phone button link",hint:"tel: format, e.g. tel:8777894377"},"quick.bullets":{label:"Quick-answer bullets (one per line)",type:"textarea",rows:5,hint:"Use **text** for bold and [label](/path) for links."},"checklist.bullets":{label:"Checklist bullets (one per line)",type:"textarea",rows:5},"checklist.intro":{label:"Checklist intro"},faqs:{label:"FAQs",type:"textarea",rows:14,hint:`Each FAQ is a block separated by a blank line. 2First line = question, following line(s) = answer.`},"quick.intro":{label:"Quick-answer intro"},tiers:{label:"Pricing tiers",type:"textarea",rows:16,hint:`Each tier is a block separated by a blank line. 3First line: Name | Price | Per | /href 4Then one bullet per line starting with - .`},"included.bullets":{label:"Included bullets (one per line)",type:"textarea",rows:6},"lower.steps":{label:"Cost-saving tips (one per line)",type:"textarea",rows:6},"lower.footer":{label:"Footer paragraph (supports links)",type:"textarea",rows:2},"before.bullets":{label:"Pre-checklist bullets (one per line)",type:"textarea",rows:5},steps:{label:"Steps",type:"textarea",rows:22,hint:"Each step is a block separated by a blank line.\nFirst line = step name.\nNext lines = body text.\nOptional `Detail: ...` line for the secondary paragraph.\nOptional `Link: label | /href` line for a CTA."}};function G(t){return t.split(".").map(a=>a.replace(/_/g," ")).join(" · ")}function H({slug:t,keys:a,defaults:i,initial:d,existing:l,tenantId:u,onSaved:m}
4){const[b,x]=o.useState(d),[y,s]=o.useState(!1),[n,g]=o.useState(!1);o.useEffect(()=>{x(d)},[d]);const f=o.useMemo(()=>a.some(c=>(b[c]??"")!==(d[c]??"")),[b,d,a]),S=(c,r)=>x(h=>({...h,[c]:r})),q=async()=>{s(!0),g(!1);const c={};for(const p of a){const v=b[p]??"";v!==(i[p]??"")&&(c[p.slice(`blog.${t}.`.length)]=v)}const r=_.find(p=>p.slug===t)??{label:t},h={tenant_id:u,slug:t,layout:"legacy",title:b[`blog.${t}.meta.title`]||i[`blog.${t}.meta.title`]||r.label,excerpt:b[`blog.${t}.meta.description`]||i[`blog.${t}.meta.description`]||null,body:c,is_published:!0};let j=null;if(l){const{error:p}=await k.from("blog_posts").update(h).eq("id",l.id);j=p}else{const{error:p}=await k.from("blog_posts").insert(h);j=p}if(s(!1),j){alert(j.message);return}g(!0),m(),setTimeout(()=>g(!1),2e3)},C=()=>{confirm("Reset every field on this page to the original default text?")&&x({...i})},E=`blog.${t}.`;return e.jsxs("section",{className:"rounded-2xl border border-border bg-card p-5 space-y-5",children:[e.jsxs("div",{className:"flex items-center justify-between gap-3 flex-wrap",children:[e.jsx("h2",{className:"font-display text-lg font-semibold",children:"Page text"}),e.jsxs("div",{className:"flex items-center gap-2",children:[n&&e.jsx("span",{className:"text-xs text-primary",children:"Saved"}),e.jsx("button",{type:"button",onClick:C,className:"text-xs text-muted-foreground hover:text-foreground px-2 py-1",children:"Reset to defaults"}),e.jsx("button",{onClick:q,disabled:y||!f,className:"px-4 py-2 rounded-lg bg-foreground text-background text-sm font-semibold disabled:opacity-50",children:y?e.jsx(P,{className:"h-4 w-4 animate-spin inline"}):"Save changes"})]})]}),e.jsx("div",{className:"grid sm:grid-cols-2 gap-3",children:a.map(c=>{const r=c.slice(E.length),h=U[r]??{},j=h.label??G(r),p=b[c]??"";return h.type==="textarea"?e.jsx("div",{className:"sm:col-span-2",children:e.jsx(N,{label:j,hint:h.hint,children:e.jsx("textarea",{value:p,onChange:v=>S(c,v.target.value),rows:h.rows??4,className:w+" font-mono text-[13px]"})})},c):e.jsx(N,{label:j,hint:h.hint,children:e.jsx("input",{value:p,onChange:v=>S(c,v.target.value),className:w})},c)})})]})}function O({post:t,onSaved:a,onDeleted:i}){const[d,l]=o.useState(t?.title??""),[u,m]=o.useState(t?.slug??""),[b,x]=o.useState(t?.excerpt??""),[y,s]=o.useState(typeof t?.body?.content=="string"?t.body.content:""),[n,g]=o.useState(t?.is_published??!0),[f,S]=o.useState(!1),[q,C]=o.useState(!1);if(o.useEffect(()=>{l(t?.title??""),m(t?.slug??""),x(t?.excerpt??""),s(typeof t?.body?.content=="string"?t.body.content:""),g(t?.is_published??!0)},[t]),!t)return e.jsx("div",{className:"text-muted-foreground text-sm",children:"Select or create a post."});const E=async()=>{S(!0),C(!1);const{error:r}=await k.from("blog_posts").update({title:d,slug:u,excerpt:b||null,body:{content:y},is_published:n}).eq("id",t.id);if(S(!1),r){alert(r.message);return}C(!0),a(),setTimeout(()=>C(!1),2e3)},c=async()=>{if(!confirm(`Delete "${t.title}"? This can't be undone.`))return;const{error:r}=await k.from("blog_posts").delete().eq("id",t.id);if(r){alert(r.message);return}i()};return e.jsxs("section",{className:"rounded-2xl border border-border bg-card p-5 space-y-4",children:[e.jsxs("div",{className:"flex items-center justify-between gap-3 flex-wrap",children:[e.jsx("h2",{className:"font-display text-lg font-semibold",children:"Custom post"}),e.jsxs("div",{className:"flex items-center gap-2",children:[q&&e.jsx("span",{className:"text-xs text-primary",children:"Saved"}),e.jsxs("button",{onClick:c,className:"flex items-center gap-1 px-3 py-1.5 rounded-lg border border-border text-xs text-destructive hover:bg-destructive/10",children:[e.jsx(D,{className:"h-3.5 w-3.5"})," Delete"]}),e.jsx("button",{onClick:E,disabled:f,className:"px-4 py-2 rounded-lg bg-foreground text-background text-sm font-semibold disabled:opacity-50",children:f?e.jsx(P,{className:"h-4 w-4 animate-spin inline"}):"Save"})]})]}),e.jsxs("div",{className:"grid sm:grid-cols-2 gap-3",children:[e.jsx(N,{label:"Title",children:e.jsx("input",{value:d,onChange:r=>l(r.target.value),className:w})}),e.jsx(N,{label:"Slug (URL segment)",children:e.jsx("input",{value:u,onChange:r=>m(r.target.value),className:w})}),e.jsx("div",{className:"sm:col-span-2",children:e.jsx(N,{label:"Excerpt",hint:"Shown on the /blog index card.",children:e.jsx("textarea",{value:b,onChange:r=>x(r.target.value),rows:3,className:w})})}),e.jsx("div",{className:"sm:col-span-2",children:e.jsx(N,{label:"Body (Markdown)",hint:"Full Markdown supp
4orted â headings, links, lists, images.",children:e.jsx("textarea",{value:y,onChange:r=>s(r.target.value),rows:20,className:w+" font-mono text-[13px]"})})}),e.jsxs("label",{className:"flex items-center gap-2 text-sm",children:[e.jsx("input",{type:"checkbox",checked:n,onChange:r=>g(r.target.checked)}),"Published"]})]})]})}export{re 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.