import { useQuery } from '@tanstack/react-query'; import { Link } from '@tanstack/react-router'; import { fetchPinnedLiveBlogs } from '@/lib/api'; import { Button } from '@/components/ui/button'; import { Calendar, Eye, MessageSquare, Pin, ChevronDown, ChevronUp, Clock } from 'lucide-react'; import { useState } from 'react'; export function PinnedLiveBlogsSidebar() { const [showUpdates, setShowUpdates] = useState(true); const { data: liveBlogs, isLoading, error } = useQuery({ queryKey: ['pinned-live-blogs'], queryFn: fetchPinnedLiveBlogs, }); const getStatusBadge = (status: string) => { switch (status) { case 'live': return ( LIVE ); case 'ended': return ( ENDED ); case 'archived': return ( ARCHIVED ); default: return ( DRAFT ); } }; const formatDate = (dateString: string) => { const date = new Date(dateString); return date.toLocaleDateString('mk-MK', { day: 'numeric', month: 'short', }); }; const formatRelativeTime = (dateString: string) => { const date = new Date(dateString); const now = new Date(); const diffMs = now.getTime() - date.getTime(); const diffMins = Math.floor(diffMs / 60000); const diffHours = Math.floor(diffMs / 3600000); const diffDays = Math.floor(diffMs / 86400000); if (diffMins < 1) return 'сега'; if (diffMins < 60) return `${diffMins}м`; if (diffHours < 24) return `${diffHours}ч`; return `${diffDays}д`; }; // Collect last 5 updates from all pinned live blogs const getLastFiveUpdates = () => { if (!liveBlogs) return []; const allUpdates: Array<{ id: string; content: string; createdAt: string; liveBlogTitle: string; liveBlogSlug: string; }> = []; liveBlogs.forEach((liveBlog) => { if (liveBlog.updates && liveBlog.updates.length > 0) { liveBlog.updates.forEach((update) => { allUpdates.push({ ...update, liveBlogTitle: liveBlog.title, liveBlogSlug: liveBlog.slug, }); }); } }); // Sort by date descending and take first 5 return allUpdates .sort((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime()) .slice(0, 5); }; const lastFiveUpdates = getLastFiveUpdates(); if (isLoading) { return (

Pinned Live

{[1, 2, 3].map((i) => (
))}
); } if (error) { return (

Pinned Live

ERROR
); } if (!liveBlogs || liveBlogs.length === 0) { return (

Pinned Live

No pinned live blogs

Pin live blogs from the admin panel.

); } return (
{/* Latest Updates Section - Collapsible */} {lastFiveUpdates.length > 0 && (
{showUpdates && (
{lastFiveUpdates.map((update) => (
{update.liveBlogTitle} {formatRelativeTime(update.createdAt)}

{update.content.replace(/<[^>]*>/g, '').substring(0, 120)} {update.content.length > 120 ? '...' : ''}

))}
)}
)}

Pinned Live

{liveBlogs.length}
{liveBlogs.map((liveBlog) => (

{liveBlog.title}

{getStatusBadge(liveBlog.status)}
{liveBlog.description && (

{liveBlog.description}

)}
{formatDate(liveBlog.createdAt)}
{liveBlog.viewCount}
{liveBlog.updates && liveBlog.updates.length > 0 && (
{liveBlog.updates.length}
)} {liveBlog.author && (
{liveBlog.author.name}
)}
{liveBlog.featuredImage && (
{liveBlog.title}
)}
))}
); }