1import{j as e}from"./ui-3gk4mjtH.js";import{r as h}from"./vendor-BmnQ2z9d.js";import{s as p,J as d,N as M,H as ae,B as f}from"./index-B8vKJ0nv.js";import{I as x}from"./input-Cz2gG-xl.js";import{T as le}from"./textarea-DMrD7eA4.js";import{B as E}from"./badge-gauGGL5O.js";import{C as q,a as ie,b as ne,c as oe}from"./card-CABPJcqX.js";import{D as O,a as ce,b as de,c as ue,d as me,E as he}from"./dialog-DZ0LNxCF.js";import{S as xe,a as ge,b as pe,c as fe,d as je}from"./select-Cr8taK69.js";import{R as ve,S as T,a as be}from"./RichTextEditor-juwssKKa.js";import{L as i}from"./label-DS0oEM1Q.js";import z from"./Footer-C8KMx-dV.js";import{u as _e}from"./useAdminAuth-cjXpDSz8.js";import{b as ye}from"./router-DFtDxSAI.js";import{A as Ce}from"./arrow-left-DCcIa99T.js";import{R as we}from"./refresh-cw-CDvsDf-f.js";import{P as ke}from"./plus-Dg8PMBCj.js";import{T as Ne}from"./trash-2-CqwnzcHD.js";import{E as Fe}from"./external-link-BuQ4YlHt.js";import"./i18n--vPob74L.js";import"./index-BlGrdXc0.js";import"./quote-CzGMq6rl.js";function P(u){return/<p[>\s]|<div[>\s]/i.test(u)}function L(u){const v=/(https?:\/\/[^\s<]+)/g;return u.replace(v,'<a href="$1" target="_blank" rel="noopener noreferrer">$1</a>')}function Se(u){return`<ol> 2${u.split(` 3`).filter(n=>n.trim()).map(n=>{const o=n.replace(/^\d+\.\s*/,"").trim();return` <li>${L(o)}</li>`}).join(` 4`)} 5</ol>`}function Ee(u){return`<ul> 6${u.split(` 7`).filter(n=>n.trim()).map(n=>{const o=n.replace(/^[â¢\-\*]\s*/,"").trim();return` <li>${L(o)}</li>`}).join(` 8`)} 9</ul>`}function V(u){return P(u)?u:u.split(/\n\s*\n/).map(n=>{const o=n.trim();if(!o)return"";if(/^\d+\.\s/m.test(o)&&o.split(` 10`).length>1)return Se(o);if(/^[â¢\-\*]\s/m.test(o)&&o.split(` 11`).length>1)return Ee(o);const a=o.replace(/\n/g,"<br>");return`<p>${L(a)}</p>`}).filter(n=>n).join(` 12`)}const Ye=()=>{const{isAdmin:u,isLoading:v}=_e(),_=ye(),[n,o]=h.useState([]),[$,D]=h.useState(!0),[a,w]=h.useState(null),[A,y]=h.useState(!1),[j,J]=h.useState(null),[U,k]=h.useState(null),[B,N]=h.useState(null),[R,C]=h.useState(!1),[F,I]=h.useState(!1),[r,l]=h.useState({title:"",excerpt:"",content:"",category:"latest-posts",author:"",read_time:"5 min read",slug:"",is_external:!1,external_link:"",external_source:"",published:!1,featured:!1,tags:"",meta_title:"",meta_description:"",meta_keywords:""}),K=[{value:"latest-posts",label:"Latest Posts"},{value:"case-studies",label:"Case Studies"},{value:"user-testimonials",label:"User Testimonials"},{value:"industry-news",label:"Industry News"},{value:"videos",label:"Videos"}];h.useEffect(()=>{G()},[]),h.useEffect(()=>{j&&b()},[j]);const G=async()=>{const{data:{session:t}}=await p.auth.getSession();if(!t){_("/auth");return}J(t.user)},b=async()=>{try{const{data:t,error:s}=await p.from("blog_posts").select("*").order("created_at",{ascending:!1});if(s)throw s;o(t||[])}catch(t){console.error("Error fetching posts:",t),d.error("Failed to fetch posts")}finally{D(!1)}},Q=t=>t.toLowerCase().replace(/[^a-z0-9 -]/g,"").replace(/\s+/g,"-").replace(/-+/g,"-").trim(),W=async t=>{if(!t)return null;C(!0);try{const s=t.name.split(".").pop(),m=`${`${Date.now()}-${Math.random()}.${s}`}`,{error:c,data:re}=await p.storage.from("blog-images").upload(m,t);if(c)throw c;const{data:{publicUrl:S}}=p.storage.from("blog-images").getPublicUrl(m);return S}catch(s){return console.error("Error uploading image:",s),d.error("Failed to upload image"),null}finally{C(!1)}},X=async t=>{if(!t)return null;C(!0);try{const s=t.name.split(".").pop(),m=`${`${Date.now()}-${Math.random()}.${s}`}`,{error:c,data:re}=await p.storage.from("blog-videos").upload(m,t);if(c)throw c;const{data:{publicUrl:S}}=p.storage.from("blog-videos").getPublicUrl(m);return S}catch(s){return console.error("Error uploading video:",s),d.error("Failed to upload video"),null}finally{C(!1)}},Y=async t=>{t.preventDefault();try{let s=(a==null?void 0:a.image_url)||null,g=(a==null?void 0:a.video_url)||null;U&&(s=await W(U)),B&&(g=await X(B));const m={...r,slug:r.slug||Q(r.title),tags:r.tags?r.tags.split(",").map(c=>c.trim()):[],image_url:s,video_url:g,created_by:j==null?void 0:j.id,updated_by:j==null?void 0:j.id};if(a){const{error:c}=await p.from("blog_posts").update(m).eq("id",a.id);if(c)throw c;d.success("Post updated successfully")}else{const{error:c}=await p.from("blog_posts").insert([m]);if(c)throw c;d.success("Post created successfully")}b(),H(),y(!1),w(null)}catch(s){console.error("Error saving post:",s),d.error("Failed to save post")}},Z=async t=>{if(confirm("Are you sure you want to delete this post?"))try{o(g=>g.filter(m=>m.id!==t));const{error:s}=await p.from("blog_posts").delete().eq("id",t);if(s)throw s;d.success("Post deleted successfully"),setTimeout(()=>{b()},500)}catch(s){console.error("Error deleting post:",s),d.error("Failed to delete post"),b()}},ee=async t=>{try{const{error:s}=await p.from("blog_posts").update({published:!t.published}).eq("id",t.id);if(s)throw s;d.success(`Post ${t.published?"unpublished":"published"}`),b()}catch(s){console.error("Error toggling publish status:",s),d.error("Failed to update publish status")}},H=()=>{l({title:"",excerpt:"",content:"",category:"latest-posts",author:"",read_time:"5 min read",slug:"",is_external:!1,external_link:"",external_source:"",published:!1,featured:!1,tags:"",meta_title:"",meta_description:"",meta_keywords:""}),k(null),N(null)},te=t=>{w(t);let s=t.content;P(s)||(s=V(s),d.success("⨠Content automatically formatted for editing")),l({title:t.title,excerpt:t.excerpt,content:s,category:t.category,author:t.author,read_time:t.read_time,slug:t.slug,is_external:t.is_external,external_link:t.external_link||"",external_source:t.external_source||"",published:t.published,featured:t.featured,tags:t.tags.join(", "),meta_title:t.meta_title||"",meta_description:t.meta_description||"",meta_keywords:t.meta_keywords||""}),k(null),N(null)},se=async()=>{I(!0);try{const t=n.filter(g=>!P(g.content));if(t.length===0){d.info("All posts are already in HTML format");return}
12d.info(`Converting ${t.length} plain text posts to HTML...`);let s=0;for(const g of t){const m=V(g.content),{error:c}=await p.from("blog_posts").update({content:m}).eq("id",g.id);c||s++}d.success(`â
Successfully converted ${s} posts to HTML format`),b()}catch(t){console.error("Error converting posts:",t),d.error("Failed to convert posts")}finally{I(!1)}};return v||$?e.jsxs("div",{className:"min-h-screen bg-background",children:[e.jsx(M,{}),e.jsx("div",{className:"container mx-auto px-4 py-8",children:e.jsx("div",{className:"text-center",children:"Loading..."})}),e.jsx(z,{})]}):u?e.jsxs(e.Fragment,{children:[e.jsxs(ae,{children:[e.jsx("title",{children:"Blog Posts Admin - Timereaction"}),e.jsx("meta",{name:"description",content:"Manage blog posts and content"}),e.jsx("meta",{name:"robots",content:"noindex, nofollow"})]}),e.jsxs("div",{className:"min-h-screen bg-background",children:[e.jsx(M,{}),e.jsxs("div",{className:"container mx-auto px-4 py-8",children:[e.jsxs("div",{className:"flex justify-between items-center mb-8",children:[e.jsxs("div",{className:"flex items-center gap-4",children:[e.jsxs(f,{variant:"ghost",onClick:()=>_("/admin"),className:"flex items-center gap-2",children:[e.jsx(Ce,{className:"w-4 h-4"}),"Back to Admin"]}),e.jsx("h1",{className:"text-3xl font-bold",children:"Blog Posts"})]}),e.jsxs("div",{className:"flex gap-2",children:[e.jsxs(f,{variant:"outline",onClick:se,disabled:F,children:[e.jsx(we,{className:`w-4 h-4 mr-2 ${F?"animate-spin":""}`}),F?"Converting...":"Convert Plain Text Posts"]}),e.jsx(O,{open:A,onOpenChange:y,children:e.jsx(ce,{asChild:!0,children:e.jsxs(f,{onClick:H,children:[e.jsx(ke,{className:"w-4 h-4 mr-2"}),"Create Post"]})})})]})]}),e.jsx(O,{open:A,onOpenChange:y,children:e.jsxs(de,{className:"max-w-4xl max-h-[90vh] overflow-y-auto",children:[e.jsx(ue,{children:e.jsx(me,{children:a?"Edit Post":"Create New Post"})}),e.jsxs("form",{onSubmit:Y,className:"space-y-4",children:[e.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[e.jsxs("div",{children:[e.jsx(i,{htmlFor:"title",children:"Title"}),e.jsx(x,{id:"title",value:r.title,onChange:t=>l({...r,title:t.target.value}),required:!0})]}),e.jsxs("div",{children:[e.jsx(i,{htmlFor:"slug",children:"Slug"}),e.jsx(x,{id:"slug",value:r.slug,onChange:t=>l({...r,slug:t.target.value}),placeholder:"Auto-generated from title"})]})]}),e.jsxs("div",{children:[e.jsx(i,{htmlFor:"excerpt",children:"Excerpt"}),e.jsx(le,{id:"excerpt",value:r.excerpt,onChange:t=>l({...r,excerpt:t.target.value}),required:!0})]}),e.jsxs("div",{children:[e.jsx(i,{htmlFor:"content",children:"Content"}),e.jsx(ve,{content:r.content,onChange:t=>l({...r,content:t}),placeholder:"Start writing your blog post... Use the toolbar to format text and add links."})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[e.jsxs("div",{children:[e.jsx(i,{htmlFor:"category",children:"Category"}),e.jsxs(xe,{value:r.category,onValueChange:t=>l({...r,category:t}),children:[e.jsx(ge,{children:e.jsx(pe,{})}),e.jsx(fe,{children:K.map(t=>e.jsx(je,{value:t.value,children:t.label},t.value))})]})]}),e.jsxs("div",{children:[e.jsx(i,{htmlFor:"author",children:"Author"}),e.jsx(x,{id:"author",value:r.author,onChange:t=>l({...r,author:t.target.value}),required:!0})]})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[e.jsxs("div",{children:[e.jsx(i,{htmlFor:"read_time",children:"Read Time"}),e.jsx(x,{id:"read_time",value:r.read_time,onChange:t=>l({...r,read_time:t.target.value})})]}),e.jsxs("div",{children:[e.jsx(i,{htmlFor:"tags",children:"Tags (comma separated)"}),e.jsx(x,{id:"tags",value:r.tags,onChange:t=>l({...r,tags:t.target.value}),placeholder:"tag1, tag2, tag3"})]})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[e.jsxs("div",{children:[e.jsx(i,{htmlFor:"image",children:"Featured Image"}),e.jsx(x,{id:"image",type:"file",accept:"image/*",onChange:t=>{var s;return k(((s=t.target.files)==null?void 0:s[0])||null)}}),(a==null?void 0:a.image_url)&&e.jsx("img",{src:a.image_url,alt:"Current image",className:"mt-2 w-32 h-20 object-cover rounded"})]}),e.jsxs("div",{children:[e.jsx(i,{htmlFor:"video",children:"Video File"}),e.jsx(x,{id:"video",type:"file",accept:"video/*",onChange:t=>{var s;return N(((s=t.target.files)==null?void 0:s[0])||null)}}),(a==null?void 0:a.video_url)&&e.jsx("video",{src:a.video_url,className:"mt-2 w-32 h-20 object-cover rounded",controls:!0})]})]}),e.jsxs("div",{className:"flex items-center space-x-2",children:[e.jsx(T,{id:"is_external",checked:r.is_external,onCheckedChange:t=>l({...r,is_external:t})}
12),e.jsx(i,{htmlFor:"is_external",children:"External Article"})]}),r.is_external&&e.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[e.jsxs("div",{children:[e.jsx(i,{htmlFor:"external_link",children:"External Link"}),e.jsx(x,{id:"external_link",value:r.external_link,onChange:t=>l({...r,external_link:t.target.value}),placeholder:"https://..."})]}),e.jsxs("div",{children:[e.jsx(i,{htmlFor:"external_source",children:"External Source"}),e.jsx(x,{id:"external_source",value:r.external_source,onChange:t=>l({...r,external_source:t.target.value}),placeholder:"Source name"})]})]}),e.jsxs("div",{className:"grid grid-cols-3 gap-4",children:[e.jsxs("div",{children:[e.jsx(i,{htmlFor:"meta_title",children:"Meta Title"}),e.jsx(x,{id:"meta_title",value:r.meta_title,onChange:t=>l({...r,meta_title:t.target.value}),placeholder:"SEO title"})]}),e.jsxs("div",{children:[e.jsx(i,{htmlFor:"meta_description",children:"Meta Description"}),e.jsx(x,{id:"meta_description",value:r.meta_description,onChange:t=>l({...r,meta_description:t.target.value}),placeholder:"SEO description"})]}),e.jsxs("div",{children:[e.jsx(i,{htmlFor:"meta_keywords",children:"Meta Keywords"}),e.jsx(x,{id:"meta_keywords",value:r.meta_keywords,onChange:t=>l({...r,meta_keywords:t.target.value}),placeholder:"keyword1, keyword2"})]})]}),e.jsxs("div",{className:"flex items-center space-x-4",children:[e.jsxs("div",{className:"flex items-center space-x-2",children:[e.jsx(T,{id:"published",checked:r.published,onCheckedChange:t=>l({...r,published:t})}),e.jsx(i,{htmlFor:"published",children:"Published"})]}),e.jsxs("div",{className:"flex items-center space-x-2",children:[e.jsx(T,{id:"featured",checked:r.featured,onCheckedChange:t=>l({...r,featured:t})}),e.jsx(i,{htmlFor:"featured",children:"Featured"})]})]}),e.jsxs("div",{className:"flex justify-end gap-2",children:[e.jsx(f,{type:"button",variant:"outline",onClick:()=>y(!1),children:"Cancel"}),e.jsx(f,{type:"submit",disabled:R,children:R?"Uploading...":a?"Update":"Create"})]})]})]})}),e.jsx("div",{className:"space-y-4",children:n.length===0?e.jsx(q,{children:e.jsx(ie,{className:"text-center py-8",children:e.jsx("p",{className:"text-muted-foreground",children:"No blog posts found. Create your first post!"})})}):n.map(t=>e.jsx(q,{className:"hover:shadow-md transition-shadow",children:e.jsx(ne,{children:e.jsxs("div",{className:"flex justify-between items-start",children:[e.jsxs("div",{className:"flex-1",children:[e.jsx(oe,{className:"text-lg",children:t.title}),e.jsx("p",{className:"text-sm text-muted-foreground mt-1",children:t.excerpt}),e.jsxs("div",{className:"flex gap-2 mt-2",children:[e.jsx(E,{variant:t.published?"default":"secondary",children:t.published?"Published":"Draft"}),t.featured&&e.jsx(E,{variant:"outline",children:"Featured"}),e.jsx(E,{variant:"outline",children:t.category})]})]}),e.jsxs("div",{className:"flex gap-2 ml-4",children:[e.jsx(f,{size:"sm",variant:"outline",onClick:()=>ee(t),children:t.published?"Unpublish":"Publish"}),e.jsx(f,{size:"sm",variant:"outline",onClick:()=>{te(t),y(!0)},children:e.jsx(be,{className:"w-4 h-4"})}),e.jsx(f,{size:"sm",variant:"outline",onClick:()=>Z(t.id),children:e.jsx(Ne,{className:"w-4 h-4"})}),t.is_external&&t.external_link?e.jsx(f,{size:"sm",variant:"outline",asChild:!0,children:e.jsx("a",{href:t.external_link,target:"_blank",rel:"noopener noreferrer",children:e.jsx(Fe,{className:"w-4 h-4"})})}):e.jsx(f,{size:"sm",variant:"outline",asChild:!0,children:e.jsx("a",{href:`/blog/${t.slug}`,target:"_blank",rel:"noopener noreferrer",children:e.jsx(he,{className:"w-4 h-4"})})})]})]})})},t.id))})]}),e.jsx(z,{})]})]}):null};export{Ye 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.