1import{i as M,k as P,l as S,bS as D,r as m,s as p,j as e,a as z,B as r,L as l,A as B,bd as $,P as F,N,a0 as H,a2 as q}from"./index-D6S94l3r.js";import{T as I,a as R,b as y,c as n,d as V,e as d}from"./table-DNwFNVSx.js";import{u as O}from"./usePageViews-B7U4TuAq.js";import{P as U}from"./plus-BOxbBXe6.js";import{C as G}from"./chart-column-dcCPsaOS.js";import{P as J}from"./pencil-C_L5e1Pl.js";const K=M("Tags",[["path",{d:"m15 5 6.3 6.3a2.4 2.4 0 0 1 0 3.4L17 19",key:"1cbfv1"}],["path",{d:"M9.586 5.586A2 2 0 0 0 8.172 5H3a1 1 0 0 0-1 1v5.172a2 2 0 0 0 .586 1.414L8.29 18.29a2.426 2.426 0 0 0 3.42 0l3.58-3.58a2.426 2.426 0 0 0 0-3.42z",key:"135mg7"}],["circle",{cx:"6.5",cy:"9.5",r:".5",fill:"currentColor",key:"5pm5xn"}]]);function se(){const{user:c,isAdmin:g,isMarketing:v,isTrustee:_,loading:j}=P(),o=g||v||_,b=S(),{toast:h}=D(),[u,f]=m.useState([]),[C,A]=m.useState(!0),{viewCounts:w}=O("news");m.useEffect(()=>{!j&&(!c||!o)&&b("/auth?redirect=/admin/news")},[c,o,j,b]),m.useEffect(()=>{c&&o&&T()},[c,o]);const T=async()=>{try{const{data:s,error:i}=await p.from("news_articles").select("id, title, slug, is_published, published_at, created_at, updated_at").order("created_at",{ascending:!1});if(i)throw i;f(s||[])}catch(s){console.error("Error fetching articles:",s),h({title:"Error",description:"Failed to load articles",variant:"destructive"})}finally{A(!1)}},k=async(s,i)=>{try{const t={is_published:!i};if(!i){const a=u.find(L=>L.id===s);a&&!a.published_at&&(t.published_at=new Date().toISOString())}const{error:x}=await p.from("news_articles").update(t).eq("id",s);if(x)throw x;f(u.map(a=>a.id===s?{...a,is_published:!i,published_at:t.published_at||a.published_at}:a)),h({title:"Success",description:`Article ${i?"unpublished":"published"}`})}catch(t){console.error("Error toggling publish status:",t),h({title:"Error",description:"Failed to update article",variant:"destructive"})}},E=async(s,i)=>{if(confirm(`Are you sure you want to delete "${i}"?`))try{const{error:t}=await p.from("news_articles").delete().eq("id",s);if(t)throw t;f(u.filter(x=>x.id!==s)),h({title:"Success",description:"Article deleted"})}catch(t){console.error("Error deleting article:",t),h({title:"Error",description:"Failed to delete article",variant:"destructive"})}};return j||C?e.jsx("div",{className:"min-h-screen flex items-center justify-center",children:e.jsx(z,{className:"h-8 w-8 animate-spin text-primary"})}):!c||!o?null:e.jsx("div",{className:"min-h-screen bg-background",children:e.jsxs("div",{className:"container mx-auto px-4 py-8",children:[e.jsxs("div",{className:"flex items-center justify-between mb-8",children:[e.jsxs("div",{className:"flex items-center gap-4",children:[e.jsx(r,{variant:"ghost",size:"icon",asChild:!0,children:e.jsx(l,{to:"/admin",children:e.jsx(B,{className:"h-5 w-5"})})}),e.jsx("h1",{className:"text-3xl font-bold text-foreground",children:"News Articles"})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(r,{variant:"outline",asChild:!0,children:e.jsxs(l,{to:"/admin/news/tags",children:[e.jsx(K,{className:"h-4 w-4 mr-2"}),"Manage Tags"]})}),e.jsx(r,{asChild:!0,children:e.jsxs(l,{to:"/admin/news/new",children:[e.jsx(U,{className:"h-4 w-4 mr-2"}),"New Article"]})})]})]}),u.length===0?e.jsxs("div",{className:"text-center py-12 bg-card rounded-lg border border-border",children:[e.jsx("p",{className:"text-muted-foreground mb-4",children:"No articles yet"}),e.jsx(r,{asChild:!0,children:e.jsx(l,{to:"/admin/news/new",children:"Create your first article"})})]}):e.jsx("div",{className:"bg-card rounded-lg border border-border overflow-hidden",children:e.jsxs(I,{children:[e.jsx(R,{children:e.jsxs(y,{children:[e.jsx(n,{children:"Title"}),e.jsx(n,{children:"Slug"}),e.jsx(n,{children:"Status"}),e.jsx(n,{children:"Views"}),e.jsx(n,{children:"Published"}),e.jsx(n,{children:"Updated"}),e.jsx(n,{className:"text-right",children:"Actions"})]})}),e.jsx(V,{children:u.map(s=>e.jsxs(y,{children:[e.jsx(d,{className:"font-medium",children:s.title}),e.jsxs(d,{className:"text-muted-foreground",children:["/news/",s.slug]}),e.jsx(d,{children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx($,{checked:s.is_published,onCheckedChange:()=>k(s.id,s.is_published)}),e.jsx(F,{variant:s.is_published?"default":"secondary",children:s.is_published?"Published":"Draft"})]})}),e.jsx(d,{children:w[s.id]!=null?e.jsxs("span",{className:"flex items-center gap-1 text-muted-foreground",children:[e.jsx(G,{className:"h-3 w-3"}
1),w[s.id].toLocaleString()]}):e.jsx("span",{className:"text-muted-foreground",children:"-"})}),e.jsx(d,{children:s.published_at?N(new Date(s.published_at),"dd MMM yyyy"):"-"}),e.jsx(d,{children:N(new Date(s.updated_at),"dd MMM yyyy")}),e.jsx(d,{className:"text-right",children:e.jsxs("div",{className:"flex items-center justify-end gap-2",children:[s.is_published&&e.jsx(r,{variant:"ghost",size:"icon",asChild:!0,children:e.jsx(l,{to:`/news/${s.slug}`,target:"_blank",children:e.jsx(H,{className:"h-4 w-4"})})}),e.jsx(r,{variant:"ghost",size:"icon",asChild:!0,children:e.jsx(l,{to:`/admin/news/${s.id}`,children:e.jsx(J,{className:"h-4 w-4"})})}),g&&e.jsx(r,{variant:"ghost",size:"icon",onClick:()=>E(s.id,s.title),children:e.jsx(q,{className:"h-4 w-4 text-destructive"})})]})})]},s.id))})]})})]})})}export{se 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.