1import{bG as V,a9 as b,cc as X,cd as J,ce as W,cf as Z,cg as ee,r as u,bK as se,aI as te,ch as ae,ci as _,bY as ie,bX as le,j as s,y as E,I as re,w as ne,t as oe}from"./index-ChBq3-4g.js";import{u as de,a as k,A as ce}from"./useAdminAuth-DBjlL-5q.js";import{u as me}from"./uploadImage-DMVOQuHR.js";import{B as ue}from"./BlockEditor-BEo_b1Hu.js";import{E as xe}from"./external-link-U371LrfO.js";import{C as he}from"./circle-alert-9iGbgk__.js";import{L as D}from"./loader-circle-BCB7xZCK.js";import{P as ge}from"./plus-_NCZ8t9P.js";import{T as K}from"./trash-2-B_z4QeIM.js";import"./RichTextEditor-B_kJmsLN.js";import"./sanitize-DBNh9d1r.js";import"./minus-t1WN-uMi.js";import"./link-2-tbLkCZL_.js";import"./mouse-pointer-click-CqIvFqwD.js";import"./copy-tOl4nxoF.js";import"./upload-dFz_y0jl.js";const r="w-full rounded-xl border border-input bg-background px-3 py-2 text-sm text-ink outline-none focus:ring-2 focus:ring-ring",n="mb-1 block text-xs font-semibold uppercase tracking-wide text-ink-soft",h="rounded-2xl border border-border bg-background p-5 shadow-soft",Q=()=>({title:"",slug:"",slugTouched:!1,category_id:"",short_description:"",blocks:[],cover_image_url:"",cover_image_alt:"",author:"",keywords:"",is_published:!1,is_featured:!1,sort_order:0,publish_date:new Date().toISOString().slice(0,10),meta_title:"",meta_description:"",canonical_url:"",og_image:"",og_description:"",robots:"index, follow",faqs:[]}),pe=a=>({id:a.id,title:a.title,slug:a.slug,slugTouched:!0,category_id:a.category_id??"",short_description:a.short_description??"",blocks:oe(a.content),cover_image_url:a.cover_image_url??"",cover_image_alt:a.cover_image_alt??"",author:a.author??"",keywords:(a.keywords??[]).join(", "),is_published:a.is_published,is_featured:a.is_featured,sort_order:a.sort_order,publish_date:a.publish_date.slice(0,10),meta_title:a.meta_title??"",meta_description:a.meta_description??"",canonical_url:a.canonical_url??"",og_image:a.og_image??"",og_description:a.og_description??"",robots:a.robots||"index, follow",faqs:ne(a.faqs)}),T=()=>({name:"",slug:"",description:"",parent_id:"",sort_order:0});function Ke(){const a=V(),j=b(X),w=b(J),B=b(W),R=b(Z),U=b(ee),[P,O]=u.useState("articles"),[l,i]=u.useState(Q()),[d,x]=u.useState(T()),[f,C]=u.useState(null),[v,g]=u.useState(null),[S,z]=u.useState(""),$=de(),L=$.isAdmin,F=se({queryKey:["admin","knowledge"],queryFn:()=>j({}),enabled:L}),p=F.data?.categories??[],N=F.data?.articles??[],I=u.useMemo(()=>{const e=S.trim().toLowerCase();return e?N.filter(t=>[t.title,t.slug,M(p,t.category_id),t.short_description].filter(Boolean).some(c=>String(c).toLowerCase().includes(e))):N},[N,p,S]),y=u.useMemo(()=>{const e=te(p,[]);return ae(e)},[p]);u.useEffect(()=>{if(!f&&!v)return;const e=window.setTimeout(()=>{C(null),g(null)},4500);return()=>window.clearTimeout(e)},[f,v]);const G=async e=>{try{return await me(e,"knowledge-images")}catch(t){return g(t instanceof Error?t.message:"Image upload failed."),null}},q=k({mutationFn:e=>{const t=e.blocks,c=(e.slug||_(e.title)).trim(),m=e.short_description.trim()||ie(t).slice(0,220);return w({data:{...e.id?{id:e.id}:{},title:e.title.trim(),slug:c,category_id:e.category_id||null,short_description:m,content:t,body_html:le(t),cover_image_url:e.cover_image_url.trim()||null,cover_image_alt:e.cover_image_alt.trim()||null,author:e.author.trim()||null,keywords:e.keywords.split(",").map(o=>o.trim()).filter(Boolean),is_published:e.is_published,is_featured:e.is_featured,sort_order:e.sort_order,publish_date:new Date(e.publish_date).toISOString(),meta_title:e.meta_title.trim()||e.title.slice(0,60),meta_description:e.meta_description.trim()||m.slice(0,155)||null,canonical_url:e.canonical_url.trim()||null,og_image:e.og_image.trim()||null,og_description:e.og_description.trim()||null,robots:e.robots.trim()||"index, follow",faqs:e.faqs.map(o=>({question:o.question.trim(),answer:o.answer.trim()})).filter(o=>o.question&&o.answer)}})},onSuccess:e=>{if(!e.ok){g(e.error);return}C("Article saved."),i(Q()),a.invalidateQueries({queryKey:["admin","knowledge"]}),a.invalidateQueries({queryKey:["knowledge"]})},onError:e=>g(e instanceof Error?e.message:"Save failed.")}),Y=k({mutationFn:e=>B({data:{id:e}}),onSuccess:()=>{a.invalidateQueries({queryKey:["admin","knowledge"]}),a.invalidateQueries({queryKey:["knowledge"]})}}),A=k({mutationFn:e=>R({data:{...e.id?{id:e.id}:{},name:e.name.trim(),slug:(e.slug||_(e.name)).trim(),description:e.description.trim(),parent_id:e.parent_id||null,sort_order:e.sort_order}}),onSuccess:e=>{if(!e.ok){g(e.error);return}C("Category saved."),x(T()),a.invalidateQueries({queryKey:["admin","knowledge"]}),a.invalidateQueries({queryKey:["knowledge"]})},onError:e=>g(e instanceof Error?e.message:"Save failed.")}),H=k({mutationFn:e=>U({data:{id:e}}),onSuccess:()=>{a.invalidateQueries({queryKey:["admin","knowledge"]}),a.invalidateQueries({queryKey:["knowledge"]})}});return L?s.jsx("div",{className:"min-h-dvh bg-secondary/40 py-10",children:s.jsxs("div",{className:"mx-auto w-full max-w-6xl px-4 md:px-8",children:[s.jsxs("header",{className:"mb-6 flex flex-wrap items-center justify-between gap-4",children:[s.jsxs("div",{children:[s.jsx("h1",{className:"font-display text-2xl font-semibold text-ink",children:"Knowledge Center CMS"}),s.jsx("p",{className:"text-sm text-ink-soft",children:"Build documentation, guides and help articles with blocks, categories and SEO."})]}),s.jsxs("div",{className:"flex items-center gap-3",children:[s.jsxs(E,{to:"/knowledge-center",className:"inline-flex items-center gap-1 text-sm font-semibold text-primary",children:["View site ",s.jsx(xe,{className:"h-3.5 w-3.5","aria-hidden":!0})]}),s.jsx(E,{to:"/admin/blog",className:"text-sm font-semibold text-ink-soft",children:"Blog CMS"}),s.jsx(E,{to:"/admin/seo",className:"text-sm font-semibold text-ink-soft",children:"SEO Panel"})]})]}),f?s.jsxs("p",{className:"mb-4 flex items-center gap-2 rounded-xl border border-primary/30 bg-primary-soft px-4 py-2 text-sm text-primary",children:[s.jsx(re,{className:"h-4 w-4","aria-hidden":!0})," ",f]}):null,v?s.jsxs("p",{className:"mb-4 flex items-center gap-2 rounded-xl border border-destructive/30 bg-destructive/5 px-4 py-2 text-sm text-destructive",role:"alert",children:[s.jsx(he,{className:"h-4 w-4","aria-hidden":!0})," ",v]}
1):null,s.jsx("div",{className:"mb-6 inline-flex rounded-full border border-border bg-background p-1",children:["articles","categories"].map(e=>s.jsx("button",{type:"button",onClick:()=>O(e),className:`rounded-full px-4 py-1.5 text-sm font-semibold capitalize transition-colors ${P===e?"bg-primary text-primary-foreground":"text-ink-soft"}`,children:e},e))}),P==="categories"?s.jsxs("div",{className:"grid gap-6 lg:grid-cols-[1fr_1.2fr]",children:[s.jsxs("section",{className:h,children:[s.jsx("h2",{className:"mb-4 font-display text-lg font-semibold text-ink",children:d.id?"Edit category":"New category"}),s.jsxs("div",{className:"space-y-3",children:[s.jsxs("div",{children:[s.jsx("label",{className:n,htmlFor:"cat-name",children:"Name"}),s.jsx("input",{id:"cat-name",className:r,value:d.name,onChange:e=>x(t=>({...t,name:e.target.value,slug:t.id?t.slug:_(e.target.value)}))})]}),s.jsxs("div",{children:[s.jsx("label",{className:n,htmlFor:"cat-slug",children:"Slug"}),s.jsx("input",{id:"cat-slug",className:r,value:d.slug,onChange:e=>x(t=>({...t,slug:e.target.value}))})]}),s.jsxs("div",{children:[s.jsx("label",{className:n,htmlFor:"cat-desc",children:"Description"}),s.jsx("textarea",{id:"cat-desc",className:`${r} min-h-20`,value:d.description,onChange:e=>x(t=>({...t,description:e.target.value}))})]}),s.jsxs("div",{className:"grid gap-3 md:grid-cols-2",children:[s.jsxs("div",{children:[s.jsx("label",{className:n,htmlFor:"cat-parent",children:"Parent category"}),s.jsxs("select",{id:"cat-parent",className:r,value:d.parent_id,onChange:e=>x(t=>({...t,parent_id:e.target.value})),children:[s.jsx("option",{value:"",children:"None (top level)"}),y.filter(e=>e.category.id!==d.id).map(({category:e,depth:t})=>s.jsxs("option",{value:e.id,children:["â ".repeat(t),e.name]},e.id))]})]}),s.jsxs("div",{children:[s.jsx("label",{className:n,htmlFor:"cat-order",children:"Sort order"}),s.jsx("input",{id:"cat-order",type:"number",className:r,value:d.sort_order,onChange:e=>x(t=>({...t,sort_order:Number(e.target.value)||0}))})]})]}),s.jsxs("div",{className:"flex gap-3",children:[s.jsxs("button",{type:"button",disabled:!d.name.trim()||A.isPending,onClick:()=>A.mutate(d),className:"inline-flex items-center gap-2 rounded-full bg-primary px-5 py-2 text-sm font-semibold text-primary-foreground disabled:opacity-50",children:[A.isPending?s.jsx(D,{className:"h-4 w-4 animate-spin","aria-hidden":!0}):s.jsx(ge,{className:"h-4 w-4","aria-hidden":!0}),"Save category"]}),d.id?s.jsx("button",{type:"button",onClick:()=>x(T()),className:"rounded-full border border-border px-5 py-2 text-sm font-semibold text-ink",children:"Cancel"}):null]})]})]}),s.jsxs("section",{className:h,children:[s.jsx("h2",{className:"mb-4 font-display text-lg font-semibold text-ink",children:"Category tree"}),y.length===0?s.jsx("p",{className:"text-sm text-ink-soft",children:"No categories yet."}):s.jsx("ul",{className:"space-y-1",children:y.map(({category:e,depth:t})=>s.jsxs("li",{className:"flex items-center justify-between gap-3 rounded-xl px-2 py-1.5 hover:bg-secondary",style:{paddingLeft:`${8+t*16}px`},children:[s.jsxs("span",{className:"min-w-0 truncate text-sm text-ink",children:[e.name,s.jsxs("span",{className:"ml-2 text-xs text-ink-soft",children:["/",e.slug]})]}),s.jsxs("span",{className:"flex shrink-0 items-center gap-2",children:[s.jsx("button",{type:"button",className:"text-xs font-semibold text-primary",onClick:()=>x({id:e.id,name:e.name,slug:e.slug,description:e.description,parent_id:e.parent_id??"",sort_order:e.sort_order}),children:"Edit"}),s.jsx("button",{type:"button",className:"text-destructive","aria-label":`Delete ${e.name}`,onClick:()=>{window.confirm(`Delete category â${e.name}â?`)&&H.mutate(e.id)},children:s.jsx(K,{className:"h-4 w-4"})})]})]},e.id))})]})]}):s.jsxs("div",{className:"grid gap-6 lg:grid-cols-[minmax(0,1fr)_320px]",children:[s.jsxs("section",{className:"space-y-6",children:[s.jsxs("div",{className:h,children:[s.jsx("h2",{className:"mb-4 font-display text-lg font-semibold text-ink",children:l.id?"Edit article":"New article"}),s.jsxs("div",{className:"space-y-3",children:[s.jsxs("div",{children:[s.jsx("label",{className:n,htmlFor:"a-title",children:"Title"}),s.jsx("input",{id:"a-title",className:r,value:l.title,onChange:e=>i(t=>({...t,title:e.target.value,slug:t.slugTouched?t.slug:_(e.target.value)}))})]}),s.jsxs("div",{className:"grid gap-3 md:grid-cols-2",children:[s.jsxs("div",{children:[s.jsx("label",{className:n,htmlFor:"a-slug",children:"Slug (auto-generated)"}),s.jsx("input",{id:"a-slug",className:r,value:l.slug,onChange:e=>i(t=>({...t,slug:e.target.value,slugTouched:!0}))})]}),s.jsxs("div",{children:[s.jsx("label",{className:n,htmlFor:"a-cat",children:"Category"}),s.jsxs("select",{id:"a-cat",className:r,value:l.category_id,onChange:e=>i(t=>({...t,category_id:e.target.value})),children:[s.jsx("option",{value:"",children:"Uncategorized"}),y.map(({category:e,depth:t})=>s.jsxs("option",{value:e.id,children:["â ".repeat(t),e.name]},e.id))]})]})]}),s.jsxs("div",{children:[s.jsx("label",{className:n,htmlFor:"a-desc",children:"Short description"}),s.jsx("textarea",{id:"a-desc",className:`${r} min-h-20`,value:l.short_description,onChange:e=>i(t=>({...t,short_description:e.target.value}))})]})]})]}),s.jsxs("div",{className:h,children:[s.jsx("h2",{className:"mb-4 font-display text-lg font-semibold text-ink",children:"Content blocks"}),s.jsx(ue,{blocks:l.blocks,onChange:e=>i(t=>({...t,blocks:e})),onUploadImage:G})]}),s.jsxs("div",{className:h,children:[s.jsx("h2",{className:"mb-4 font-display text-lg font-semibold text-ink",children:"SEO & metadata"}),s.jsxs("div",{className:"grid gap-3 md:grid-cols-2",children:[s.jsxs("div",{children:[s.jsx("label",{className:n,htmlFor:"a-meta-title",children:"Meta title"}),s.jsx("input",{id:"a-meta-title",className:r,value:l.meta_title,onChange:e=>i(t=>({...t,meta_title:e.target.value})),placeholder:"Defaults to the article title"})]}),s.jsxs("div",{children:[s.jsx("label",{className:n,htmlFor:"a-keywords",children:"Keywords (comma separated)"}),s.jsx("input",{id:"a-keywords",className:r,value:l.keywords,onChange:e=>i(t=>({...t,keywords:e.target.value}))})]}),s.jsxs("div",{className:"md:col-span-2",children:[s.jsx("label",{className:n,htmlFor:"a-meta-desc",children:"Meta description"}),s.jsx("textarea",{id:"a-meta-desc",className:`${r} min-h-16`,value:l.meta_description,onChange:e=>i(t=>({...t,meta_description:e.target.value})),placeholder:"Defaults to the short description"})]}),s.jsxs("div",{children:[s.jsx("label",{className:n,htmlFor:"a-canonical",children:"Canonical URL"}),s.jsx("input",{id:"a-canonical",className:r,value:l.canonical_url,onChange:e=>i(t=>({...t,canonical_url:e.target.value}))})]}),s.jsxs("div",{children:[s.jsx("label",{className:n,htmlFor:"a-robots",children:"Robots"}),s.jsx("input",{id:"a-robots",className:r,value:l.robots,onChange:e=>i(t=>({...t,robots:e.target.value}))})]}),s.jsxs("div",{children:[s.jsx("label",{className:n,htmlFor:"a-og-image",children:"Social image URL"}),s.jsx("input",{id:"a-og-image",className:r,value:l.og_image,onChange:e=>i(t=>({...t,og_image:e.target.value}))})]}),s.jsxs("div",{children:[s.jsx("label",{className:n,htmlFor:"a-og-desc",children:"Social description"}),s.jsx("input",{id:"a-og-desc",className:r,value:l.og_description,onChange:e=>i(t=>({...t,og_description:e.target.value}))})]}),s.jsxs("div",{children:[s.jsx("label",{className:n,htmlFor:"a-cover",children:"Cover image URL"}),s.jsx("input",{id:"a-cover",className:r,value:l.cover_image_url,onChange:e=>i(t=>({...t,cover_image_url:e.target.value}))})]}),s.jsxs("div",{children:[s.jsx("label",{className:n,htmlFor:"a-cover-alt",children:"Cover image alt text"}),s.jsx("input",{id:"a-cover-alt",className:r,value:l.cover_image_alt,onChange:e=>i(t=>({...t,cover_image_alt:e.target.value}))})]})]}),s.jsx("h3",{className:"mb-3 mt-6 text-sm font-semibold text-ink",children:"FAQ schema entries"}),s.jsxs("div",{className:"space-y-3",children:[l.faqs.map((e,t)=>s.jsxs("div",{className:"space-y-2 rounded-xl border border-border p-3",children:[s.jsxs("div",{className:"flex gap-2",children:[s.jsx("input",{className:`${r} flex-1`,value:e.question,onChange:c=>i(m=>{const o=[...m.faqs];return o[t]={...e,question:c.target.value},{...m,faqs:o}}),placeholder:"Question"}),s.jsx("button",{type:"button",className:"text-destructive","aria-label":"Remove FAQ",onClick:()=>i(c=>({...c,faqs:c.faqs.filter((m,o)=>
1o!==t)})),children:s.jsx(K,{className:"h-4 w-4"})})]}),s.jsx("textarea",{className:`${r} min-h-16`,value:e.answer,onChange:c=>i(m=>{const o=[...m.faqs];return o[t]={...e,answer:c.target.value},{...m,faqs:o}}),placeholder:"Answer"})]},t)),s.jsx("button",{type:"button",className:"rounded-full border border-border px-3 py-1 text-xs font-semibold",onClick:()=>i(e=>({...e,faqs:[...e.faqs,{question:"",answer:""}]})),children:"Add FAQ"})]})]})]}),s.jsxs("aside",{className:"space-y-6",children:[s.jsxs("div",{className:`${h} lg:sticky lg:top-6`,children:[s.jsx("h2",{className:"mb-4 font-display text-lg font-semibold text-ink",children:"Publish"}),s.jsxs("div",{className:"space-y-3",children:[s.jsxs("label",{className:"flex items-center gap-2 text-sm text-ink",children:[s.jsx("input",{type:"checkbox",checked:l.is_published,onChange:e=>i(t=>({...t,is_published:e.target.checked}))}),"Published"]}),s.jsxs("label",{className:"flex items-center gap-2 text-sm text-ink",children:[s.jsx("input",{type:"checkbox",checked:l.is_featured,onChange:e=>i(t=>({...t,is_featured:e.target.checked}))}),"Featured on the hub"]}),s.jsxs("div",{children:[s.jsx("label",{className:n,htmlFor:"a-date",children:"Publish date"}),s.jsx("input",{id:"a-date",type:"date",className:r,value:l.publish_date,onChange:e=>i(t=>({...t,publish_date:e.target.value}))})]}),s.jsxs("div",{children:[s.jsx("label",{className:n,htmlFor:"a-author",children:"Author"}),s.jsx("input",{id:"a-author",className:r,value:l.author,onChange:e=>i(t=>({...t,author:e.target.value}))})]}),s.jsxs("div",{children:[s.jsx("label",{className:n,htmlFor:"a-order",children:"Sort order"}),s.jsx("input",{id:"a-order",type:"number",className:r,value:l.sort_order,onChange:e=>i(t=>({...t,sort_order:Number(e.target.value)||0}))})]}),s.jsxs("div",{className:"flex flex-wrap gap-2 pt-2",children:[s.jsxs("button",{type:"button",disabled:!l.title.trim()||q.isPending,onClick:()=>q.mutate(l),className:"inline-flex items-center gap-2 rounded-full bg-primary px-5 py-2 text-sm font-semibold text-primary-foreground disabled:opacity-50",children:[q.isPending?s.jsx(D,{className:"h-4 w-4 animate-spin","aria-hidden":!0}):null,"Save article"]}),l.id?s.jsx("button",{type:"button",onClick:()=>i(Q()),className:"rounded-full border border-border px-5 py-2 text-sm font-semibold text-ink",children:"New"}):null]})]})]}),s.jsxs("div",{className:h,children:[s.jsx("h2",{className:"mb-4 font-display text-lg font-semibold text-ink",children:"Articles"}),s.jsx("input",{type:"search",value:S,onChange:e=>z(e.target.value),placeholder:"Search by title, slug or category","aria-label":"Search knowledge articles",className:`${r} mb-4`}),F.isLoading?s.jsx(D,{className:"h-4 w-4 animate-spin text-primary","aria-hidden":!0}):I.length===0?s.jsx("p",{className:"text-sm text-ink-soft",children:N.length===0?"No articles yet.":"No matching articles."}):s.jsx("ul",{className:"space-y-2",children:I.map(e=>s.jsxs("li",{className:"flex items-start justify-between gap-2 rounded-xl border border-border p-3",children:[s.jsxs("div",{className:"min-w-0",children:[s.jsx("p",{className:"truncate text-sm font-semibold text-ink",children:e.title}),s.jsxs("p",{className:"text-xs text-ink-soft",children:[e.is_published?"Published":"Draft"," ·"," ",M(p,e.category_id)]})]}),s.jsxs("span",{className:"flex shrink-0 items-center gap-2",children:[s.jsx("button",{type:"button",className:"text-xs font-semibold text-primary",onClick:()=>{i(pe(e)),window.scrollTo({top:0,behavior:"smooth"})},children:"Edit"}),s.jsx("button",{type:"button",className:"text-destructive","aria-label":`Delete ${e.title}`,onClick:()=>{window.confirm(`Delete â${e.title}â?`)&&Y.mutate(e.id)},children:s.jsx(K,{className:"h-4 w-4"})})]})]},e.id))})]})]})]})]})}):s.jsx(ce,{guard:$,message:"You need an admin account to manage the Knowledge Center."})}function M(a,j){return j?a.find(w=>w.id===j)?.name??"Uncategorized":"Uncategorized"}export{Ke 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.