"use client"; import React, { useState, useEffect, useMemo } from "react"; import Link from "next/link"; import { BlogData } from "@/utils/constant.utils"; interface BlogSidebarProps { activeCategory?: string; onCategoryClick?: (category: string) => void; initialSearchTerm?: string; onSearch?: (term: string) => void; excludeSlug?: string; } const BlogSidebar = ({ activeCategory, onCategoryClick, initialSearchTerm = "", onSearch, excludeSlug }: BlogSidebarProps) => { const [searchTerm, setSearchTerm] = useState(initialSearchTerm); // Debounced real-time search useEffect(() => { const timer = setTimeout(() => { if (searchTerm !== initialSearchTerm && onSearch) { onSearch(searchTerm); } }, 500); return () => clearTimeout(timer); }, [searchTerm, onSearch, initialSearchTerm]); // Update search term when initial changes useEffect(() => { setSearchTerm(initialSearchTerm); }, [initialSearchTerm]); // Recent News (Latest 3 blogs, excluding current one, filtered by category if active) const recentPosts = useMemo(() => { let list = [...BlogData]; if (excludeSlug) { list = list.filter(p => p.slug !== excludeSlug); } if (activeCategory) { // Priority to same category, but if not enough, show others const sameCat = list.filter(p => p.category === activeCategory); const others = list.filter(p => p.category !== activeCategory); list = [...sameCat, ...others]; } return list .sort((a, b) => new Date(b.date).getTime() - new Date(a.date).getTime()) .slice(0, 3); }, [activeCategory, excludeSlug]); // Categories and Counts (Sorted to show active category at the top, then by most recent post date) const categories = useMemo(() => { const catMap = BlogData.reduce((acc: any, item: any) => { if (!acc[item.category]) { acc[item.category] = { count: 0, latestDate: new Date(0) }; } acc[item.category].count += 1; const itemDate = new Date(item.date); if (itemDate > acc[item.category].latestDate) { acc[item.category].latestDate = itemDate; } return acc; }, {}); const allBlogsCat = { name: "All Blogs", count: BlogData.length, latestDate: new Date() // Doesn't matter for sorting as we'll put it first }; let result = Object.keys(catMap) .map(cat => ({ name: cat, count: catMap[cat].count, latestDate: catMap[cat].latestDate })) .sort((a, b) => { const normalizedActive = (activeCategory || "").trim().toLowerCase(); const normalizedA = (a.name || "").trim().toLowerCase(); const normalizedB = (b.name || "").trim().toLowerCase(); // Active category always first (if not All Blogs) if (normalizedActive && normalizedA === normalizedActive) return -1; if (normalizedActive && normalizedB === normalizedActive) return 1; // Then sort by most recent post date return b.latestDate.getTime() - a.latestDate.getTime(); }); // Optimize for Detail Page (excludeSlug exists) if (excludeSlug && result.length > 6) { return [allBlogsCat, ...result.slice(0, 5)]; } return [allBlogsCat, ...result]; }, [activeCategory, excludeSlug]); const handleSearchSubmit = (e: React.FormEvent) => { e.preventDefault(); if (onSearch) { onSearch(searchTerm); } }; const handleCategoryClick = (e: React.MouseEvent, category: string) => { if (onCategoryClick) { e.preventDefault(); onCategoryClick(category); } else { // Default behavior if no handler is provided // Redirect to results page } }; return (
{/* Search Widget */}

Search

setSearchTerm(e.target.value)} />
{/* Recent News Widget */}

Recent Blogs

{recentPosts.map((post: any) => (
{post.title}
{post.title}
{post.date}
))} {recentPosts.length === 0 &&

No recent posts found.

}
{/* Categories Widget */}

{excludeSlug ? "Blog Related Category" : "Categories"}

    {categories.map((cat) => { const isAllBlogs = cat.name === "All Blogs"; // Normalize for comparison to handle case-sensitivity or leading/trailing spaces const normalizedActive = (activeCategory || "").trim().toLowerCase(); const normalizedCat = (cat.name || "").trim().toLowerCase(); const isActive = isAllBlogs ? (normalizedActive === "") : (normalizedActive === normalizedCat); return (
  • handleCategoryClick(e, isAllBlogs ? "" : cat.name)} className="category-item" > {cat.name} {cat.count < 10 ? `0${cat.count}` : cat.count}
  • ); })}
); }; export default BlogSidebar;