tahamajs/IE / Projects /front /src /components /home /BlogSection.tsx
tahamajs's picture
download
raw
18.1 kB
// src/components/home/BlogSection.tsx
import { Link } from "react-router-dom";
import { useState, useRef } from "react";
import { motion, useInView, AnimatePresence } from "framer-motion";
import {
BookOpen, Eye, Heart, MessageCircle, User,
Calendar, Clock, ChevronRight, ArrowRight,
TrendingUp, Award, Star, Sparkles, Quote,
Share2, Bookmark, ExternalLink, Tag,
Filter, Search, Grid3x3, List
} from "lucide-react";
import { Article } from "@/types";
import { formatDistanceToNow } from "@/lib/date";
interface BlogSectionProps {
articles: Article[];
loading: boolean;
title?: string;
subtitle?: string;
variant?: "default" | "grid" | "list" | "cards" | "compact" | "featured";
showStats?: boolean;
showAuthor?: boolean;
showImage?: boolean;
showTags?: boolean;
showExcerpt?: boolean;
columns?: 2 | 3 | 4;
limit?: number;
animate?: boolean;
showSearch?: boolean;
showFilter?: boolean;
}
export function BlogSection({
articles,
loading,
title = "Latest Blog Posts",
subtitle = "Discover the newest research and insights",
variant = "default",
showStats = true,
showAuthor = true,
showImage = true,
showTags = true,
showExcerpt = true,
columns = 3,
limit = 6,
animate = true,
showSearch = false,
showFilter = false
}: BlogSectionProps) {
const ref = useRef(null);
const isInView = useInView(ref, { once: true, margin: "-100px" });
const [hoveredId, setHoveredId] = useState<number | null>(null);
const [searchQuery, setSearchQuery] = useState("");
const [filterCategory, setFilterCategory] = useState<string>("all");
const [viewMode, setViewMode] = useState<"grid" | "list">("grid");
const categories = ["all", "Research", "Tutorials", "News", "Opinion", "Case Study"];
const filteredArticles = articles.filter(article => {
const matchesSearch = article.title.toLowerCase().includes(searchQuery.toLowerCase()) ||
(article.abstract || "").toLowerCase().includes(searchQuery.toLowerCase());
const matchesCategory = filterCategory === "all" || (article.tags || []).includes(filterCategory.toLowerCase());
return matchesSearch && matchesCategory;
}).slice(0, limit);
const getColumnClass = () => {
if (viewMode === "list") return "grid-cols-1";
switch (columns) {
case 2: return "grid-cols-1 md:grid-cols-2";
case 4: return "grid-cols-1 sm:grid-cols-2 lg:grid-cols-4";
default: return "grid-cols-1 md:grid-cols-2 lg:grid-cols-3";
}
};
// Loading Skeletons
if (loading) {
return (
<div className="space-y-6">
<div className="flex justify-between items-center">
<div className="h-8 w-48 bg-gray-200 dark:bg-gray-700 rounded animate-pulse" />
<div className="h-4 w-20 bg-gray-200 dark:bg-gray-700 rounded animate-pulse" />
</div>
<div className="space-y-4">
{[...Array(4)].map((_, i) => (
<div key={i} className="h-28 bg-gray-100 dark:bg-gray-800 rounded-xl animate-pulse" />
))}
</div>
</div>
);
}
if (articles.length === 0) return null;
// Variant: Compact
if (variant === "compact") {
return (
<div className="space-y-4">
<div className="flex justify-between items-center">
<h3 className="text-lg font-semibold">Recent Posts</h3>
<Link to="/articles" className="text-xs text-primary hover:underline">View all →</Link>
</div>
<div className="space-y-2">
{filteredArticles.slice(0, 5).map((article) => (
<Link
key={article.id}
to={`/articles/${article.id}`}
className="block p-3 rounded-lg hover:bg-gray-50 dark:hover:bg-gray-800 transition group"
>
<div className="flex items-start gap-3">
{showImage && (
<div className="w-12 h-12 rounded-lg overflow-hidden flex-shrink-0 bg-gray-100">
<img
src={article.coverImage || `https://picsum.photos/seed/${article.id}/50/50`}
alt=""
className="w-full h-full object-cover"
onError={(e) => { (e.target as HTMLImageElement).style.display = 'none'; }}
/>
</div>
)}
<div className="flex-1 min-w-0">
<h4 className="text-sm font-medium group-hover:text-primary transition line-clamp-1">
{article.title}
</h4>
<p className="text-xs text-gray-500 mt-0.5">
{formatDistanceToNow(new Date(article.createdAt), { addSuffix: true })}
</p>
</div>
<ChevronRight className="w-4 h-4 text-gray-400 opacity-0 group-hover:opacity-100 transition" />
</div>
</Link>
))}
</div>
</div>
);
}
// Variant: Cards (grid with images)
if (variant === "cards") {
return (
<div ref={ref} className="space-y-6">
<div className="text-center">
<h2 className="text-2xl font-bold">{title}</h2>
<p className="text-gray-500 text-sm">{subtitle}</p>
</div>
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
{filteredArticles.map((article, idx) => (
<motion.div
key={article.id}
initial={animate ? { opacity: 0, y: 20 } : false}
animate={isInView ? { opacity: 1, y: 0 } : {}}
transition={{ delay: idx * 0.05 }}
whileHover={{ y: -4 }}
className="bg-white dark:bg-gray-800 rounded-xl overflow-hidden shadow-sm border hover:shadow-lg transition"
>
{showImage && (
<Link to={`/articles/${article.id}`} className="block h-44 overflow-hidden">
<img
src={article.coverImage || `https://picsum.photos/seed/${article.id}/400/200`}
alt={article.title}
className="w-full h-full object-cover hover:scale-105 transition duration-500"
loading="lazy"
onError={(e) => { (e.target as HTMLImageElement).src = `https://picsum.photos/seed/${article.id}/400/200`; }}
/>
</Link>
)}
<div className="p-4">
<div className="flex items-center gap-2 text-xs text-gray-500 mb-2">
<span>{formatDistanceToNow(new Date(article.createdAt), { addSuffix: true })}</span>
<span>•</span>
<span>{article.readingTime || 5} min read</span>
</div>
<Link to={`/articles/${article.id}`}>
<h3 className="font-semibold text-lg mb-2 line-clamp-2 hover:text-primary transition">
{article.title}
</h3>
</Link>
{showExcerpt && (
<p className="text-gray-600 text-sm line-clamp-2 mb-3">
{article.abstract || article.description}
</p>
)}
{showAuthor && (
<div className="flex items-center gap-2 pt-2 border-t">
<div className="w-6 h-6 rounded-full bg-gradient-to-br from-primary to-secondary flex items-center justify-center text-white text-[8px] font-bold">
{article.authorUsername?.[0]?.toUpperCase()}
</div>
<span className="text-xs text-gray-500">{article.authorUsername}</span>
</div>
)}
</div>
</motion.div>
))}
</div>
</div>
);
}
// Variant: Featured (hero post + list)
if (variant === "featured" && filteredArticles.length > 0) {
const featured = filteredArticles[0];
const rest = filteredArticles.slice(1, 4);
return (
<div ref={ref} className="space-y-6">
<div className="flex justify-between items-center">
<h2 className="text-2xl font-bold">{title}</h2>
<Link to="/articles" className="text-primary hover:underline flex items-center gap-1">
View all <ArrowRight className="w-4 h-4" />
</Link>
</div>
{/* Featured Article */}
<div className="bg-gradient-to-r from-primary/10 to-secondary/10 rounded-xl overflow-hidden">
<div className="grid md:grid-cols-2 gap-6 p-6">
{showImage && (
<div className="h-48 md:h-auto rounded-lg overflow-hidden">
<img
src={featured.coverImage || `https://picsum.photos/seed/${featured.id}/400/300`}
alt=""
className="w-full h-full object-cover"
onError={(e) => { (e.target as HTMLImageElement).style.display = 'none'; }}
/>
</div>
)}
<div>
<div className="inline-flex items-center gap-1 px-2 py-0.5 bg-primary/20 text-primary text-xs rounded-full mb-2">
<Award className="w-3 h-3" />
Featured
</div>
<Link to={`/articles/${featured.id}`}>
<h3 className="text-xl font-bold hover:text-primary transition line-clamp-2">
{featured.title}
</h3>
</Link>
<p className="text-gray-600 text-sm mt-2 line-clamp-3">
{featured.abstract || featured.description}
</p>
<div className="flex items-center gap-3 mt-3 text-xs text-gray-500">
<span className="flex items-center gap-1"><User className="w-3 h-3" /> {featured.authorUsername}</span>
<span className="flex items-center gap-1"><Calendar className="w-3 h-3" /> {formatDistanceToNow(new Date(featured.createdAt), { addSuffix: true })}</span>
</div>
</div>
</div>
</div>
{/* Rest of articles */}
<div className="space-y-3">
{rest.map((article) => (
<Link
key={article.id}
to={`/articles/${article.id}`}
className="flex items-start gap-3 p-3 rounded-lg hover:bg-gray-50 dark:hover:bg-gray-800 transition group"
>
{showImage && (
<div className="w-16 h-16 rounded-lg overflow-hidden flex-shrink-0 bg-gray-100">
<img
src={article.coverImage || `https://picsum.photos/seed/${article.id}/80/80`}
alt=""
className="w-full h-full object-cover"
onError={(e) => { (e.target as HTMLImageElement).style.display = 'none'; }}
/>
</div>
)}
<div className="flex-1">
<h4 className="font-medium group-hover:text-primary transition line-clamp-1">{article.title}</h4>
<p className="text-xs text-gray-500 mt-0.5">{article.authorUsername} • {formatDistanceToNow(new Date(article.createdAt), { addSuffix: true })}</p>
</div>
<ChevronRight className="w-4 h-4 text-gray-400 opacity-0 group-hover:opacity-100 transition" />
</Link>
))}
</div>
</div>
);
}
// Default variant (original design - list view)
return (
<div ref={ref} className="space-y-6">
<div className="flex flex-wrap justify-between items-center gap-4">
<div>
<h2 className="text-2xl font-bold">{title}</h2>
<p className="text-gray-500 text-sm">{subtitle}</p>
</div>
<div className="flex items-center gap-3">
{showSearch && (
<div className="relative">
<Search className="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-gray-400" />
<input
type="text"
placeholder="Search posts..."
value={searchQuery}
onChange={(e) => setSearchQuery(e.target.value)}
className="pl-9 pr-3 py-1.5 text-sm border rounded-lg focus:outline-none focus:ring-2 focus:ring-primary"
/>
</div>
)}
<div className="flex gap-1">
<button
onClick={() => setViewMode("grid")}
className={`p-1.5 rounded ${viewMode === "grid" ? "bg-primary text-white" : "hover:bg-gray-100"}`}
>
<Grid3x3 className="w-4 h-4" />
</button>
<button
onClick={() => setViewMode("list")}
className={`p-1.5 rounded ${viewMode === "list" ? "bg-primary text-white" : "hover:bg-gray-100"}`}
>
<List className="w-4 h-4" />
</button>
</div>
</div>
</div>
{showFilter && (
<div className="flex flex-wrap gap-2">
{categories.map((cat) => (
<button
key={cat}
onClick={() => setFilterCategory(cat)}
className={`px-3 py-1 text-xs rounded-full transition ${
filterCategory === cat
? "bg-primary text-white"
: "bg-gray-100 dark:bg-gray-800 hover:bg-gray-200"
}`}
>
{cat.charAt(0).toUpperCase() + cat.slice(1)}
</button>
))}
</div>
)}
<div className={`grid ${getColumnClass()} gap-4`}>
{filteredArticles.map((article, idx) => (
<motion.div
key={article.id}
initial={animate ? { opacity: 0, y: 20 } : false}
animate={isInView ? { opacity: 1, y: 0 } : {}}
transition={{ delay: idx * 0.03 }}
whileHover={{ y: -2 }}
onMouseEnter={() => setHoveredId(article.id)}
onMouseLeave={() => setHoveredId(null)}
>
<Link
to={`/articles/${article.id}`}
className="block bg-white dark:bg-gray-800 rounded-xl shadow-sm border p-5 hover:shadow-md transition-all group h-full"
>
{showImage && (
<div className="w-full h-40 mb-3 rounded-lg overflow-hidden bg-gray-100">
<img
src={article.coverImage || `https://picsum.photos/seed/${article.id}/400/200`}
alt={article.title}
className="w-full h-full object-cover group-hover:scale-105 transition duration-500"
loading="lazy"
onError={(e) => { (e.target as HTMLImageElement).style.display = 'none'; }}
/>
</div>
)}
<h3 className="text-lg font-semibold group-hover:text-primary transition line-clamp-2">
{article.title}
</h3>
{showExcerpt && (
<p className="text-gray-600 dark:text-gray-400 text-sm mt-2 line-clamp-2">
{article.abstract || article.description}
</p>
)}
<div className="flex flex-wrap items-center gap-3 mt-3 text-xs text-gray-500">
{showAuthor && (
<span className="flex items-center gap-1">
<User className="w-3 h-3" />
{article.authorUsername}
</span>
)}
<span className="flex items-center gap-1">
<Calendar className="w-3 h-3" />
{formatDistanceToNow(new Date(article.createdAt), { addSuffix: true})}
</span>
{showStats && (
<>
<span className="flex items-center gap-1">
<Eye className="w-3 h-3" />
{article.views || 0}
</span>
<span className="flex items-center gap-1">
<Heart className="w-3 h-3" />
{article.likesCount || 0}
</span>
<span className="flex items-center gap-1">
<MessageCircle className="w-3 h-3" />
{article.commentsCount || 0}
</span>
</>
)}
</div>
{showTags && article.tags && article.tags.length > 0 && (
<div className="flex flex-wrap gap-1 mt-2">
{article.tags.slice(0, 2).map((tag) => (
<span key={tag} className="text-xs px-1.5 py-0.5 bg-gray-100 dark:bg-gray-700 rounded-full">
#{tag}
</span>
))}
</div>
)}
</Link>
</motion.div>
))}
</div>
{filteredArticles.length === 0 && (
<div className="text-center py-8 text-gray-500">
No articles found matching your criteria.
</div>
)}
</div>
);
}
// Simple version for sidebar
export function SimpleBlogSection({ articles = [] }: { articles?: Article[] }) {
const demoArticles = articles.length > 0 ? articles : [
{ id: 1, title: "Advances in Machine Learning", authorUsername: "Dr. Sarah Chen", createdAt: new Date().toISOString() },
{ id: 2, title: "Climate Change Research", authorUsername: "Prof. David Kim", createdAt: new Date().toISOString() },
];
return (
<div className="space-y-3">
{demoArticles.slice(0, 4).map((article) => (
<Link key={article.id} to={`/articles/${article.id}`} className="flex gap-3 p-2 rounded-lg hover:bg-gray-50 transition group">
<BookOpen className="w-4 h-4 text-primary mt-0.5" />
<div className="flex-1">
<div className="text-sm font-medium group-hover:text-primary transition line-clamp-1">{article.title}</div>
<div className="text-xs text-gray-500">{article.authorUsername}</div>
</div>
</Link>
))}
</div>
);
}

Xet Storage Details

Size:
18.1 kB
·
Xet hash:
2a8afcbeed0d6077101396f2f814decb1b937ac5b1931dfcb3b2f56f184bc298

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.