1import{ad as Z,r as t,K as b,ap as K,j as e,H as M,q as D,I as O,B as X,X as x,N,$ as G}from"./index-KSWbwNeY.js";import{S as v,a as S,b as w,c as C,d as i}from"./select-DzTIkJMJ.js";import{P as J}from"./PostCard-DYfhVM8A.js";import W from"./Footer-DPSMpmUX.js";import{S as Y}from"./seoDefaults-DHsbBPCr.js";import{R as ee}from"./responsive-heading-DW57rgMc.js";import{F as se}from"./filter-BFFcBMjy.js";import{o as re,s as q,_ as te,Z as ae}from"./schemas-eq1hNim8.js";import"./index-BdQq_4o_.js";import"./chevron-down-D7IdZFzc.js";import"./chevron-up-B5q4f77c.js";import"./card-CwL_ed79.js";import"./clock-CzRotAbS.js";import"./twitter-yEUqfNJX.js";import"./linkedin-B1bA0DJP.js";import"./youtube-CwivUiPT.js";import"./facebook-DahhNxAJ.js";const le=re({query:q().trim().max(200,{message:"Search query must be less than 200 characters"}).optional(),category:q().optional(),author:q().optional(),sortBy:te(["newest","oldest","title"]).optional()}),Se=()=>{var T,I;const[m,A]=Z(),[a,g]=t.useState(m.get("q")||""),[h,j]=t.useState([]),[_,L]=t.useState([]),[k,R]=t.useState([]),[U,F]=t.useState(!1),[l,p]=t.useState(m.get("category")||""),[c,f]=t.useState(m.get("author")||""),[u,B]=t.useState(m.get("sort")||"newest"),[E,y]=t.useState("");t.useEffect(()=>{V(),z()},[]),t.useEffect(()=>{const s=setTimeout(()=>{a.length>=2||l||c?H():a.length===0&&j([])},300);return()=>clearTimeout(s)},[a,l,c,u]);const V=async()=>{try{const{data:s,error:r}=await b.from("categories").select("id, name").order("name");if(r)throw r;L(s||[])}catch(s){console.error("Error fetching categories:",s)}},z=async()=>{try{const{data:s,error:r}=await b.from("authors_public").select("id, name").order("name");if(r)throw r;R(s||[])}catch(s){console.error("Error fetching authors:",s)}},H=async()=>{F(!0),y("");try{const s=le.parse({query:a,category:l,author:c,sortBy:u});let r=b.from("posts").select(` 2 id, 3 title, 4 excerpt, 5 image_url, 6 read_time, 7 created_at, 8 slug, 9 categories (id, name), 10 authors!posts_author_id_fkey (id, name) 11 `).eq("published",!0);if(s.query&&s.query.length>=2){const $=s.query.replace(/[%_]/g,"\\$&");r=r.or(`title.ilike.%${$}%,excerpt.ilike.%${$}%`)}switch(s.category&&(r=r.eq("category_id",s.category)),s.author&&(r=r.eq("author_id",s.author)),s.sortBy){case"oldest":r=r.order("created_at",{ascending:!0});break;case"title":r=r.order("title",{ascending:!0});break;case"newest":default:r=r.order("created_at",{ascending:!1});break}const{data:o,error:n}=await r.limit(50);if(n)throw n;j(o||[]),s.query&&s.query.length>=2&&K({query:s.query,resultsCount:(o==null?void 0:o.length)||0});const d=new URLSearchParams;s.query&&d.set("q",s.query),s.category&&d.set("category",s.category),s.author&&d.set("author",s.author),s.sortBy&&d.set("sort",s.sortBy),A(d)}catch(s){s instanceof ae?y(s.issues[0].message):(y("An error occurred while searching. Please try again."),console.error("Search error:",s))}finally{F(!1)}},Q=()=>{g(""),p(""),f(""),B("newest"),j([]),A({})},P=a||l||c||u!=="newest";return e.jsxs("div",{className:"min-h-screen bg-background",children:[e.jsxs(M,{children:[e.jsxs("title",{children:["Search Articles - ",Y]}),e.jsx("meta",{name:"description",content:"Search through our collection of AI, technology, and business articles."}),e.jsx("meta",{name:"robots",content:"noindex, follow"})]}),e.jsx("main",{className:"container mx-auto px-6 py-12",children:e.jsxs("div",{className:"max-w-6xl mx-auto",children:[e.jsx(ee,{size:"md",className:"mb-8",children:"Search Articles"}),e.jsxs("div",{className:"mb-6",children:[e.jsxs("div",{className:"relative",children:[e.jsx(D,{className:"absolute left-3 top-1/2 transform -translate-y-1/2 h-5 w-5 text-muted-foreground"}),e.jsx(O,{type:"text",placeholder:"Search articles by title, content, or keywords...",value:a,onChange:s=>g(s.target.value),className:"pl-10 pr-4 py-6 text-base",maxLength:200})]}),E&&e.jsx("p",{className:"text-destructive text-sm mt-2",children:E})]}),e.jsxs("div",{className:"bg-muted/50 border border-border rounded-lg p-6 mb-8",children:[e.jsxs("div",{className:"flex items-center gap-2 mb-4",children:[e.jsx(se,{className:"h-4 w-4"}),e.jsx("h2",{className:"font-semibold text-sm uppercase",children:"Filters & Sort"})]}),e.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-4 gap-4",children:[e.jsxs("div",{children:[e.jsx("label",{className:"text-xs font-semibold text-muted-foreground uppercase mb-2 block",children:"Category"}),e.jsxs(v,{value:l||"all",onValueChange:s=>p(s==="all"?"":s),children:[e.jsx(S,{children:e.jsx(w,{placeholder:"All Categories"})}),e.jsxs(C,{children:[e.jsx(i,{value:"all",children:"All Categories"}),_.map(s=>e.jsx(i,{value:s.id,children:s.name},s.id))]})]})]}),e.jsxs("div",{children:[e.jsx("label",{className:"text-xs font-semibold text-muted-foreground uppercase mb-2 block",children:"Author"}),e.jsxs(v,{value:c||"all",onValueChange:s=>
11f(s==="all"?"":s),children:[e.jsx(S,{children:e.jsx(w,{placeholder:"All Authors"})}),e.jsxs(C,{children:[e.jsx(i,{value:"all",children:"All Authors"}),k.map(s=>e.jsx(i,{value:s.id,children:s.name},s.id))]})]})]}),e.jsxs("div",{children:[e.jsx("label",{className:"text-xs font-semibold text-muted-foreground uppercase mb-2 block",children:"Sort By"}),e.jsxs(v,{value:u,onValueChange:B,children:[e.jsx(S,{children:e.jsx(w,{})}),e.jsxs(C,{children:[e.jsx(i,{value:"newest",children:"Newest First"}),e.jsx(i,{value:"oldest",children:"Oldest First"}),e.jsx(i,{value:"title",children:"Title (A-Z)"})]})]})]}),e.jsx("div",{className:"flex items-end",children:P&&e.jsxs(X,{variant:"outline",onClick:Q,className:"w-full",children:[e.jsx(x,{className:"h-4 w-4 mr-2"}),"Clear All"]})})]}),P&&e.jsxs("div",{className:"flex flex-wrap gap-2 mt-4 pt-4 border-t border-border",children:[a&&e.jsxs(N,{variant:"secondary",className:"gap-1",children:['Search: "',a,'"',e.jsx("button",{onClick:()=>g(""),className:"ml-1 hover:text-destructive",children:e.jsx(x,{className:"h-3 w-3"})})]}),l&&e.jsxs(N,{variant:"secondary",className:"gap-1",children:["Category:"," ",(T=_.find(s=>s.id===l))==null?void 0:T.name,e.jsx("button",{onClick:()=>p(""),className:"ml-1 hover:text-destructive",children:e.jsx(x,{className:"h-3 w-3"})})]}),c&&e.jsxs(N,{variant:"secondary",className:"gap-1",children:["Author: ",(I=k.find(s=>s.id===c))==null?void 0:I.name,e.jsx("button",{onClick:()=>f(""),className:"ml-1 hover:text-destructive",children:e.jsx(x,{className:"h-3 w-3"})})]})]})]}),e.jsx("div",{children:U?e.jsx("div",{className:"text-center py-16",children:e.jsx("div",{className:"text-lg text-muted-foreground",children:"Searching..."})}):h.length>0?e.jsxs(e.Fragment,{children:[e.jsx("div",{className:"mb-6",children:e.jsxs("p",{className:"text-sm text-muted-foreground",children:["Found ",h.length," ",h.length===1?"article":"articles"]})}),e.jsx("div",{className:"grid grid-cols-1 md:grid-cols-2 gap-6",children:h.map((s,r)=>{var o,n;return e.jsxs(t.Fragment,{children:[e.jsx(J,{id:s.id,slug:s.slug,title:s.title,excerpt:s.excerpt,image:s.image_url||"",category:((o=s.categories)==null?void 0:o.name)||"Uncategorized",author:((n=s.authors)==null?void 0:n.name)||"Unknown",date:new Date(s.created_at).toLocaleDateString("en-US",{month:"short",day:"numeric",year:"numeric"}),readTime:s.read_time||"5 min read"}),r>0&&(r+1)%6===0&&e.jsx("div",{className:"col-span-1 md:col-span-2",children:e.jsx(G,{slot:"listing_inline",className:"my-4"})})]},s.id)})})]}):a.length>=2||l||c?e.jsxs("div",{className:"text-center py-16",children:[e.jsx("p",{className:"text-muted-foreground mb-4",children:"No articles found."}),e.jsx("p",{className:"text-sm text-muted-foreground",children:"Try adjusting your search query or filters."})]}):e.jsxs("div",{className:"text-center py-16",children:[e.jsx(D,{className:"h-16 w-16 text-muted-foreground/50 mx-auto mb-4"}),e.jsx("p",{className:"text-muted-foreground mb-2",children:"Start searching for articles"}),e.jsx("p",{className:"text-sm text-muted-foreground",children:"Enter a search term or select filters above"})]})})]})}),e.jsx(W,{})]})};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.