1import{r as i,a as re,e as ie,s as p,j as e,am as le,o as ne,B as o,q as oe,I as m,N as R,V as de,W as ce,Y as me,bQ as y,bR as C,bS as k,E as ue,y as he,a9 as xe}from"./index-KSWbwNeY.js";import{A as W}from"./AdminNav-DsaLvjFx.js";import{D as ge,a as pe,b as fe,c as je,d as Ne}from"./dialog-Bpfij23O.js";import{L as u}from"./label-ndOKmXJw.js";import{T as ve}from"./textarea-BXv-lbww.js";import{T as be,a as we,b as G,c as A,d as _e,e as T}from"./table-BWUN_Dp9.js";import{U as ye}from"./upload-CstIFHrJ.js";import{P as Ce}from"./plus-oseq6Y0_.js";import{U as ke}from"./user-cog-DvqSS7bY.js";import{U as Ae}from"./user-x-Dnw7rX3O.js";import{S as Te}from"./square-pen-CRmh_xc0.js";import{T as De}from"./trash-2-CwcwS_bf.js";import{C as Se}from"./chevron-left-C0OR5oyH.js";import"./index-DsYLezIw.js";import"./book-open-DS7Nmuzn.js";import"./receipt-DgWJf9Ou.js";import"./chevron-down-D7IdZFzc.js";import"./index-BpbyRUKB.js";const Xe=()=>{const[U,Q]=i.useState([]),[V,M]=i.useState(!0),[P,X]=i.useState(""),[f,Y]=i.useState(""),[d,D]=i.useState(1),[J,b]=i.useState(!1),[S,z]=i.useState(null),[F,$]=i.useState(!1),[t,l]=i.useState({name:"",first_name:"",last_name:"",email:"",bio:"",role:"Contributor",image_url:"",twitter_url:"",linkedin_url:""}),{toast:c}=re(),K=ie(),q=i.useRef(),w=20;i.useEffect(()=>(q.current=setTimeout(()=>{Y(P),D(1)},300),()=>clearTimeout(q.current)),[P]),i.useEffect(()=>{L()},[]);const L=async()=>{const{data:s,error:a}=await p.from("authors").select("*");if(a){c({variant:"destructive",title:"Error fetching authors",description:a.message}),M(!1);return}const{data:r,error:j}=await p.rpc("get_author_post_stats").limit(1e4);j&&console.error("Failed to load author post stats:",j);const E=new Map,N=new Map;for(const n of r??[])E.set(n.author_id,Number(n.post_count)||0),n.latest_post_date&&N.set(n.author_id,n.latest_post_date);const I=(s||[]).map(n=>({...n,article_count:E.get(n.id)||0})).sort((n,_)=>{const g=N.get(n.id),v=N.get(_.id);return g&&v?new Date(v).getTime()-new Date(g).getTime():g&&!v?-1:!g&&v?1:new Date(_.created_at).getTime()-new Date(n.created_at).getTime()});Q(I),M(!1)},Z=async s=>{var r;const a=(r=s.target.files)==null?void 0:r[0];if(a){$(!0);try{const{optimizeImage:j,isGif:E}=await xe(async()=>{const{optimizeImage:te,isGif:ae}=await import("./imageOptimizer-DU1AwbgF.js");return{optimizeImage:te,isGif:ae}},[]),N=E(a),O=await j(a,{maxWidth:800,maxHeight:800,quality:.85,format:"webp"}),I=N?"gif":"webp",n=N?"image/gif":"image/webp",_=`authors/${Date.now()}-${Math.random().toString(36).substring(7)}.${I}`,{error:g}=await p.storage.from("imported-images").upload(_,O.blob,{contentType:n,cacheControl:"public, max-age=31536000, immutable"});if(g)throw g;const{data:{publicUrl:v}}=p.storage.from("imported-images").getPublicUrl(_);l({...t,image_url:v}),c({title:"Image uploaded",description:"Author image uploaded successfully."})}catch(j){c({variant:"destructive",title:"Upload failed",description:j.message})}finally{$(!1)}}},ee=async s=>{if(s.preventDefault(),S){const{error:a}=await p.from("authors").update(t).eq("id",S.id);a?c({variant:"destructive",title:"Error updating author",description:a.message}):(c({title:"Author updated",description:"The author has been successfully updated."}),b(!1),z(null),L())}else{const{error:a}=await p.from("authors").insert([t]);a?c({variant:"destructive",title:"Error creating author",description:a.message}):(c({title:"Author created",description:"The author has been successfully created."}),b(!1),L())}},se=async s=>{if(!confirm("Are you sure you want to delete this author?"))return;const{error:a}=await p.from("authors").delete().eq("id",s);a?c({variant:"destructive",title:"Error deleting author",description:a.message}):(c({title:"Author deleted",description:"The author has been successfully deleted."}),L())},B=s=>{s?(z(s),l({name:s.name,first_name:s.first_name||"",last_name:s.last_name||"",email:s.email||"",bio:s.bio||"",role:s.role,image_url:s.image_url||"",twitter_url:s.twitter_url||"",linkedin_url:s.linkedin_url||""})):(z(null),l({name:"",first_name:"",last_name:"",email:"",bio:"",role:"Contributor",image_url:"",twitter_url:"",linkedin_url:""})),b(!0)},h=i.useMemo(()=>U.filter(s=>s.name.toLowerCase().includes(f.toLowerCase())||s.email&&s.email.toLowerCase().includes(f.toLowerCase())),[U,f]),x=Math.ceil(h.length/w),H=i.useMemo(()=>{const s=(d-1)*w;return h.slice(s,s+w)},[h,d]);return V?e.jsxs("div",{className:"min-h-screen bg-background",children:[e.jsx(W,{}),e.jsx("main",{className:"md:ml-52 p-6",children:e.jsx("div",{className:"flex items-center justify-center h-64",children:e.jsxs("div",{className:"flex items-center gap-3 text-muted-foreground",children:[e.jsx("div",{className:"h-5 w-5 animate-spin rounded-none border-2 border-primary border-t-transparent"}),e.jsx("span",{children:"Loading authors..."})]})})})]}):e.jsxs("div",{className:"min-h-screen bg-background",children:[e.jsx(W,{}),e.jsxs("main",{className:"md:ml-52 p-6 overflow-auto",children:[e.jsxs("div",{className:"max-w-[1400px] mx-auto
1space-y-5",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx("div",{className:"p-2 rounded-none bg-primary/10",children:e.jsx(le,{className:"h-5 w-5 text-primary"})}),e.jsxs("div",{children:[e.jsx("h1",{className:"text-xl font-semibold tracking-tight",children:"Authors"}),e.jsxs("p",{className:"text-xs text-muted-foreground",children:[U.length.toLocaleString()," total"]})]})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(ne,{to:"/admin/import-authors",children:e.jsxs(o,{variant:"ghost",size:"sm",className:"text-xs h-8 px-3 hover:bg-muted/80 transition-colors",children:[e.jsx(ye,{className:"h-3.5 w-3.5 mr-1.5"}),"Import"]})}),e.jsxs(o,{onClick:()=>B(),size:"sm",className:"h-8 px-3 text-xs bg-primary hover:bg-primary/90 transition-colors rounded-none",children:[e.jsx(Ce,{className:"h-3.5 w-3.5 mr-1.5"}),"New Author"]})]})]}),e.jsxs("div",{className:"flex items-center gap-3 p-4 rounded-none bg-muted/30 border border-border/50",children:[e.jsxs("div",{className:"relative flex-1 max-w-md",children:[e.jsx(oe,{className:"absolute left-2.5 top-1/2 -translate-y-1/2 h-3.5 w-3.5 text-muted-foreground"}),e.jsx(m,{type:"text",placeholder:"Search by name or email...",value:P,onChange:s=>X(s.target.value),className:"h-8 pl-8 text-xs bg-background/80 border-border/50 focus-visible:ring-primary/30 rounded-none"})]}),f&&e.jsxs(R,{variant:"secondary",className:"text-xs rounded-none",children:[h.length," result",h.length!==1?"s":""]})]}),e.jsxs("div",{className:"rounded-none border border-border/50 bg-card/50 overflow-hidden",children:[e.jsxs(be,{children:[e.jsx(we,{children:e.jsxs(G,{className:"bg-muted/40 hover:bg-muted/40 border-border/50",children:[e.jsx(A,{className:"text-xs font-medium text-muted-foreground h-10 w-[280px]",children:"Author"}),e.jsx(A,{className:"text-xs font-medium text-muted-foreground h-10 w-[140px]",children:"Role"}),e.jsx(A,{className:"text-xs font-medium text-muted-foreground h-10 w-[80px] text-center",children:"Posts"}),e.jsx(A,{className:"text-xs font-medium text-muted-foreground h-10 w-[100px] text-center",children:"Status"}),e.jsx(A,{className:"text-xs font-medium text-muted-foreground h-10 w-[140px] text-right",children:"Actions"})]})}),e.jsx(_e,{children:H.map((s,a)=>e.jsxs(G,{className:"group border-border/30 hover:bg-muted/30 transition-colors animate-fade-in",style:{animationDelay:`${a*20}ms`},children:[e.jsx(T,{className:"py-2.5",children:e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsxs(de,{className:"h-9 w-9 rounded-none",children:[e.jsx(ce,{src:s.image_url||void 0,alt:s.name}),e.jsx(me,{className:"text-xs font-medium bg-muted rounded-none",children:s.name.split(" ").map(r=>r[0]).join("").slice(0,2).toUpperCase()})]}),e.jsxs("div",{className:"min-w-0",children:[e.jsx("div",{className:"font-medium text-sm text-foreground truncate max-w-[200px]",children:s.name}),s.email&&e.jsx("div",{className:"text-[11px] text-muted-foreground truncate max-w-[200px]",children:s.email})]})]})}),e.jsx(T,{className:"py-2.5",children:e.jsx("span",{className:"text-xs text-muted-foreground",children:s.role})}),e.jsx(T,{className:"py-2.5 text-center",children:e.jsx(o,{variant:"ghost",size:"sm",onClick:()=>K(`/admin/posts?author=${s.id}`),className:"h-6 px-2 text-xs text-primary hover:bg-primary/10 rounded-none",children:s.article_count})}),e.jsx(T,{className:"py-2.5 text-center",children:s.user_id?e.jsxs(y,{children:[e.jsx(C,{children:e.jsxs(R,{variant:"outline",className:"text-[10px] px-1.5 py-0.5 text-emerald-600 border-emerald-300 bg-emerald-50 rounded-none",children:[e.jsx(ke,{className:"h-3 w-3 mr-1"}),"Linked"]})}),e.jsx(k,{children:"Has login account"})]}):e.jsxs(y,{children:[e.jsx(C,{children:e.jsxs(R,{variant:"outline",className:"text-[10px] px-1.5 py-0.5 text-muted-foreground border-border rounded-none",children:[e.jsx(Ae,{className:"h-3 w-3 mr-1"}),"No login"]})}),e.jsx(k,{children:"No linked login account"})]})}),e.jsx(T,{className:"py-2.5 text-right",children:e.jsxs("div",{className:"flex items-center justify-end gap-1 opacity-60 group-hover:opacity-100 transition-opacity",children:[e.jsxs(y,{children:[e.jsx(C,{asChild:!0,children:e.jsx(o,{variant:"ghost",size:"sm",onClick:()=>window.open(`/author/${s.slug||s.id}`,"_blank"),className:"h-7 w-7 p-0 rounded-none",children:e.jsx(ue,{className:"h-3.5 w-3.5"})})}),e.jsx(k,{children:"View Profile"})]}),e.jsxs(y,{children:[e.jsx(C,{asChild:!0,children:e.jsx(o,{variant:"ghost",size:"sm",onClick:()=>B(s),className:"h-7 w-7 p-0 rounded-none",children:e.jsx(Te,{className:"h-3.5 w-3.5"})})}),e.jsx(k,{children:"Edit"})]}),e.jsxs(y,{children:[e.jsx(C,{asChild:!0,children:e.jsx(o,{variant:"ghost",size:"sm",onClick:()=>se(s.id),className:"h-7 w-7 p-0 rounded-none text-destructive hover:text-destructive hover:bg-destructive/10",children:e.jsx(De,{className:"h-3.5 w-3.5"})})}),e.jsx(k,{children:"Delete"})]})]})})]},s.id))})]}),H.length===0&&e.jsx("div",{className:"text-center py-12 text-muted-foreground text-sm",children:f?`No authors found matching "${f}"`:"No authors yet. Create your first author!"})]}),x>1&&e.jsxs("div",{className:"flex items-center justify-between px-2",children:[e.jsxs("p",{className:"text-xs text-muted-foreground",children:["Showing ",(d-1)*w+1,"-",Math.min(d*w,h.length)," of ",h.length]}),e.jsxs("div",{className:"flex items-center gap-1",children:[e.jsx(o,{variant:"outline",size:"sm",onClick:()=>D(s=>Math.max(1,s-1)),disabled:d===1,className:"h-7 w-7 p-0 rounded-none",children:e.jsx(Se,{className:"h-3.5 w-3.5"})}),Array.from({length:Math.min(5,x)},(s,a)=>{let r;return x<=5||d<=3?r=a+1:d>=x-2?r=x-4+a:r=d-2+a,e.jsx(o,{variant:d===r?"default":"outline",size:"sm",onClick:()=>D(r),className:`h-7 w-7 p-0 text-xs rounded-none ${d===r?"bg-primary text-primary-foreground":""}`,children:r},r)}),e.jsx(o,{variant:"outline",size:"sm",onClick:()=>D(s=>Math.min(x,s+1)),disabled:d===x,className:"h-7 w-7 p-0 rounded-none",children:e.jsx(he,{className:"h-3.5 w-3.5"})})]})]})]}),e.jsx(ge,{open:J,onOpenChange:b,children:e.jsxs(pe,{className:"max-w-lg max-h-[90vh] overflow-y-auto",children:[e.jsx(fe,{children:e.jsx(je,{children:S?"Edit Author":"Add New Author"})}),e.jsxs("form",{onSubmit:ee,children:[e.jsxs("div",{className:"grid gap-3 py-3",children:[e.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[e.jsxs("div",{className:"grid gap-1.5",children:[e.jsx(u,{htmlFor:"name",className:"text-xs",children:"Full Name*"}),e.jsx(m,{id:"name",value:t.name,onChange:s=>l({...t,name:s.target.value}),required:!0,className:"h-9"})]}),e.jsxs("div",{className:"grid gap-1.5",children:[e.jsx(u,{htmlFor:"role",className:"text-xs",children:"Role*"}),e.jsx(m,{id:"role",value:t.role,onChange:s=>l({...t,role:s.target.value}),required:!0,className:"h-9"})]})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[e.jsxs("div",{className:"grid gap-1.5",children:[e.jsx(u,{htmlFor:"first_name",className:"text-xs",children:"First Name"}),e.jsx(m,{id:"first_name",value:t.first_name,onChange:s=>l({...t,first_name:s.target.value}),className:"h-9"})]}),e.jsxs("div",{className:"grid gap-1.5",children:[e.jsx(u,{htmlFor:"last_name",className:"text-xs",children:"Last Name"}),e.jsx(m,{id:"last_name",value:t.last_name,onChange:s=>l({...t,last_name:s.target.value}),className:"h-9"})]})]}),e.jsxs("div",{className:"grid gap-1.5",children:[e.jsx(u,{htmlFor:"email",className:"text-xs",children:"Email"}),e.jsx(m,{id:"email",type:"email",value:t.email,onChange:s=>l({...t,email:s.target.value}),className:"h-9"})]}),e.jsxs("div",{className:"grid gap-1.5",children:[e.jsx(u,{className:"text-xs",children:"Author Image"}),e.jsxs("div",{className:"flex items-center gap-3",children:[t.image_url&&e.jsx("div",{className:"relative w-16 h-16 rounded-lg overflow-hidden border flex-shrink-0",children:e.jsx("img",{src:t.image_url,alt:"Author preview",className:"w-full h-full object-cover"})}),e.jsxs("div",{className:"flex-1 space-y-2",children:[e.jsxs("div",{className:"flex gap-2",children:[e.jsx("input",{type:"file",id:"author-image-upload",className:"hidden",accept:"image/*",onChange:Z,disabled:F}),e.jsx(o,{type:"button",variant:"outline",size:"sm",onClick:()=>{var s;return(s=document.getElementById("author-image-upload"))==null?void 0:s.click()},disabled:F,className:"h-8 text-xs",children:F?"Uploading...":"Upload"}),t.image_url&&e.jsx(o,{type:"button",variant:"outline",size:"sm",onClick:()=>l({...t,image_url:""}),className:"h-8 text-xs",children:"Remove"})]}),e.jsx(m,{id:"image_url",type:"url",placeholder:"Or paste image URL",value:t.image_url,onChange:s=>l({...t,image_url:s.target.value}),className:"h-8 text-xs"})]})]})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[e.jsxs("div",{className:"grid gap-1.5",children:[e.jsx(u,{htmlFor:"twitter_url",className:"text-xs",children:"Twitter URL"}),e.jsx(m,{id:"twitter_url",type:"url",placeholder:"https://twitter.com/username",value:t.twitter_url,onChange:s=>l({...t,twitter_url:s.target.value}),className:"h-9"})]}),e.jsxs("div",{className:"grid gap-1.5",children:[e.jsx(u,{htmlFor:"linkedin_url",className:"text-xs",children:"LinkedIn URL"}),e.jsx(m,{id:"linkedin_url",type:"url",placeholder:"https://linkedin.com/in/username",value:t.linkedin_url,onChange:s=>l({...t,linkedin_url:s.target.value}),className:"h-9"})]})]}),e.jsxs("div",{className:"grid gap-1.5",children:[e.jsx(u,{htmlFor:"bio",className:"text-xs",children:"Bio"}),e.jsx(ve,{id:"bio",value:t.bio,onChange:s=>l({...t,bio:s.target.value}),rows:3,className:"text-sm"})]})]}),e.jsxs(Ne,{className:"gap-2 pt-2",children:[e.jsx(o,{type:"button",variant:"outline",size:"sm",onClick:()=>b(!1),children:"Cancel"}),e.jsx(o,{type:"submit",size:"sm",children:S?"Update":"Create"})]})]})]})})]})]})};export{Xe 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.