1import{aw as R,av as $,r as d,ac as G,ax as y,j as e,B as r,aE as J,_ as D,$ as T,a2 as B,a0 as E,a1 as L,d as n,ay as V,bb as k,s as m,a3 as c}from"./index-ATB_ByK-.js";import{C as W,b as X,c as Y,d as Z,a as ee}from"./card-DsP0fy73.js";import{B as se}from"./badge-DstT-dnt.js";import{I as u}from"./input-CEay7ZDk.js";import{T as F}from"./textarea-BxRCKWEN.js";import{T as te,a as ae,b as O,c as g,d as ie,e as j}from"./table-Ba5z49CO.js";import{B as le}from"./play-xfi2cvpf.js";import{P as re}from"./plus-BWJ1HGR6.js";import{S as ne}from"./search-BzY8nBH_.js";import{E as oe}from"./eye-off-BmjfnkfL.js";import{E as de}from"./eye-CPSF_y3T.js";import{P as ce}from"./pencil-BA-KPD0u.js";const N={title:"",slug:"",excerpt:"",content:"",featured_image:"",author:"",is_published:!1,published_at:""};function ue(h){return h.toLowerCase().replace(/[^a-z0-9\s-]/g,"").replace(/\s+/g,"-").replace(/-+/g,"-").trim()}function ye(){const h=R();$();const[f,q]=d.useState(""),[I,b]=d.useState(!1),[M,p]=d.useState(!1),[o,v]=d.useState(null),[x,S]=d.useState(null),[a,i]=d.useState(N),{data:z=[],isLoading:A}=G({queryKey:["admin-blog-posts"],queryFn:async()=>{const{data:s,error:t}=await m.from("blog_posts").select("*").order("created_at",{ascending:!1});if(t)throw t;return s}}),w=y({mutationFn:async s=>{const t={...s,published_at:s.published_at?new Date(s.published_at).toISOString():s.is_published?new Date().toISOString():null};if(o){const{error:l}=await m.from("blog_posts").update(t).eq("id",o.id);if(l)throw l}else{const{error:l}=await m.from("blog_posts").insert(t);if(l)throw l}},onSuccess:()=>{h.invalidateQueries({queryKey:["admin-blog-posts"]}),c.success(o?"Blog post updated":"Blog post created"),_()},onError:s=>c.error(s.message)}),C=y({mutationFn:async s=>{const{error:t}=await m.from("blog_posts").delete().eq("id",s);if(t)throw t},onSuccess:()=>{h.invalidateQueries({queryKey:["admin-blog-posts"]}),c.success("Blog post deleted"),p(!1),S(null)},onError:s=>c.error(s.message)}),H=y({mutationFn:async({id:s,is_published:t})=>{const{error:l}=await m.from("blog_posts").update({is_published:t,published_at:t?new Date().toISOString():null}).eq("id",s);if(l)throw l},onSuccess:()=>{h.invalidateQueries({queryKey:["admin-blog-posts"]}),c.success("Publish status updated")},onError:s=>c.error(s.message)});function Q(){v(null),i(N),b(!0)}function K(s){v(s),i({title:s.title||"",slug:s.slug||"",excerpt:s.excerpt||"",content:s.content||"",featured_image:s.featured_image||"",author:s.author||"",is_published:s.is_published||!1,published_at:s.published_at?s.published_at.slice(0,10):""}),b(!0)}function _(){b(!1),v(null),i(N)}function U(s){i(t=>({...t,title:s,slug:o?t.slug:ue(s)}))}const P=z.filter(s=>{var t,l;return((t=s.title)==null?void 0:t.toLowerCase().includes(f.toLowerCase()))||((l=s.author)==null?void 0:l.toLowerCase().includes(f.toLowerCase()))});return e.jsxs(W,{children:[e.jsxs(X,{children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsxs(Y,{className:"flex items-center gap-2",children:[e.jsx(le,{className:"h-5 w-5"}),"Blog Posts"]}),e.jsx(Z,{children:"Manage blog posts for the Media page"})]}),e.jsxs(r,{onClick:Q,size:"sm",children:[e.jsx(re,{className:"h-4 w-4 mr-2"}),"New Post"]})]}),e.jsxs("div",{className:"relative mt-4",children:[e.jsx(ne,{className:"absolute left-3 top-1/2 -translate-y-1/2 h-4 w-4 text-muted-foreground"}),e.jsx(u,{placeholder:"Search posts...",value:f,onChange:s=>q(s.target.value),className:"pl-10 max-w-sm"})]})]}),e.jsx(ee,{children:A?e.jsx("p",{className:"text-muted-foreground text-sm",children:"Loading..."}):P.length===0?e.jsx("p",{className:"text-muted-foreground text-sm text-center py-8",children:"No blog posts found"}):e.jsx("div",{className:"overflow-x-auto",children:e.jsxs(te,{children:[e.jsx(ae,{children:e.jsxs(O,{children:[e.jsx(g,{children:"Title"}),e.jsx(g,{children:"Author"}),e.jsx(g,{children:"Status"}),e.jsx(g,{children:"Date"}),e.jsx(g,{className:"text-right",children:"Actions"})]})}),e.jsx(ie,{children:P.map(s=>e.jsxs(O,{children:[e.jsx(j,{className:"font-medium max-w-[250px] truncate",children:s.title}),e.jsx(j,{children:s.author||"â"}),e.jsx(j,{children:e.jsx(se,{variant:s.is_published?"default":"secondary",children:s.is_published?"Published":"Draft"})}),e.jsx(j,{className:"text-sm text-muted-foreground",children:s.published_at?new Date(s.published_at).toLocaleDateString():new Date(s.created_at).toLocaleDateString()}),e.jsx(j,{className:"text-right",children:e.jsxs("div",{className:"flex items-center justify-end gap-1",children:[e.jsx(r,{variant:"ghost",size:"icon",onClick:()=>H.mutate({id:s.id,is_published:!s.is_published}),title:s.is_published?"Unpublish":"Publish",children:s.is_published?e.jsx(oe,{className:"h-4 w-4"}):e.jsx(de,{className:"h-4 w-4"})}),e.jsx(r,{variant:"ghost",size:"icon",onClick:()=>K(s),children:e.jsx(ce,{className:"h-4 w-4"})}),e.jsx(r,{variant:"ghost",size:"icon",className:"text-destructive",onClick:()=>{S(s),p(!0)},children:e.jsx(J,{className:"h-4 w-4"})})]})})]},s.id))})]})})}),e.jsx(D,{open:I,onOpenChange:s=>!s&&_(),children:e.jsxs(T,{className:"max-w-2xl max-h-[85vh] overflow-y-auto",children:[e.jsxs(B,{children:[e.jsx(E,{children:o?"Edit Blog Post":"Create Blog Post"}),e.jsx(L,{children:o?"Update the blog post details.":"Fill in the details to create a new blog post."})]}),e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{children:[e.jsx(n,{children:"Title *"}),e.jsx(u,{value:a.title,onChange:s=>U(s.target.value)})]}),e.jsxs("div",{children:[e.jsx(n,{children:"Slug *"}),e.jsx(u,{value:a.slug,onChange:s=>i(t=>({...t,slug:s.target.value}))})]}),e.jsxs("div",{children:[e.jsx(n,{children:"Author"}),e.jsx(u,{value:a.author,onChange:s=>i(t=>({...t,author:s.target.value}))})]}),e.jsxs("div",{children:[e.jsx(n,{children:"Featured Image URL"}),e.jsx(u,{value:a.featured_image,onChange:s=>i(t=>({...t,featured_image:s.target.value}))})]}),e.jsxs("div",{children:[e.jsx(n,{children:"Excerpt"}),e.jsx(F,{rows:2,value:a.excerpt,onChange:s=>i(t=>({...t,excerpt:s.target.value}))})]}),e.jsxs("div",{children:[e.jsx(n,{children:"Content (HTML / Markdown)"}),e.jsx(F,{rows:10,value:a.content,onChange:s=>i(t=>({...t,content:s.target.value}))})]}),e.jsxs("div",{children:[e.jsx(n,{children:"Publish Date"}),e.jsx(u,{type:"date",value:a.published_at,onChange:s=>i(t=>({...t,published_at:s.target.value}))}),e.jsx("p",{className:"text-xs text-muted-foreground mt-1",children:"Leave empty to use current date when publishing"})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(V,{checked:a.is_published,onCheckedChange:s=>i(t=>({...t,is_published:s}))}),e.jsx(n,{children:"Publish immediately"})]})]}),e.jsxs(k,{children:[e.jsx(r,{variant:"outline",onClick:_,children:"Cancel"}),e.jsx(r,{onClick:()=>w.mutate(a),disabled:!a.title.trim()||!a.slug.trim()||w.isPending,children:w.isPending?"Saving...":o?"Update":"Create"})]})]})}),e.jsx(D,{open:M,onOpenChange:s=>!s&&p(!1),children:e.jsxs(T,{children:[e.jsxs(B,{children:[e.jsx(E,{children:"Delete Blog Post"}),e.jsxs(L,{children:['Are you sure you want to delete "',x==null?void 0:x.title,'"? This action cannot be undone.']})]}),e.jsxs(k,{children:[e.jsx(r,{variant:"outline",onClick:()=>p(!1),children:"Cancel"}),e.jsx(r,{variant:"destructive",onClick:()=>x&&C.mutate(x.id),disabled:C.isPending,children:C.isPending?"Deleting...":"Delete"})]})]})})]})}export{ye as BlogPostsTab};
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.