| // src/components/home/RecentPosts.tsx | |
| import { Link } from "react-router-dom"; | |
| import { useState, useRef } from "react"; | |
| import { motion, useInView, AnimatePresence } from "framer-motion"; | |
| import { | |
| Clock, Eye, Heart, MessageCircle, User, Calendar, | |
| Tag, Bookmark, Share2, ExternalLink, ChevronRight, | |
| TrendingUp, Sparkles, Award, Star, Zap, ArrowRight | |
| } from "lucide-react"; | |
| import { Article } from "@/types"; | |
| import { formatDistanceToNow } from "@/lib/date"; | |
| interface RecentPostsProps { | |
| articles: Article[]; | |
| title?: string; | |
| subtitle?: string; | |
| variant?: "default" | "grid" | "list" | "compact" | "cards"; | |
| showStats?: boolean; | |
| showAuthor?: boolean; | |
| showImage?: boolean; | |
| showTags?: boolean; | |
| limit?: number; | |
| animate?: boolean; | |
| } | |
| export function RecentPosts({ | |
| articles, | |
| title = "Recent Posts", | |
| subtitle = "Latest research and insights", | |
| variant = "default", | |
| showStats = true, | |
| showAuthor = true, | |
| showImage = true, | |
| showTags = true, | |
| limit = 6, | |
| animate = true | |
| }: RecentPostsProps) { | |
| const ref = useRef(null); | |
| const isInView = useInView(ref, { once: true, margin: "-100px" }); | |
| const [hoveredId, setHoveredId] = useState<number | null>(null); | |
| const displayArticles = articles.slice(0, limit); | |
| if (articles.length === 0) return null; | |
| // Variant: Compact | |
| if (variant === "compact") { | |
| return ( | |
| <div className="space-y-3"> | |
| <div className="flex items-center justify-between"> | |
| <h3 className="font-semibold flex items-center gap-2"> | |
| <Clock className="w-4 h-4 text-primary" /> | |
| Recent | |
| </h3> | |
| <Link to="/articles" className="text-xs text-primary hover:underline">View all</Link> | |
| </div> | |
| <div className="space-y-2"> | |
| {displayArticles.slice(0, 5).map((article) => ( | |
| <Link | |
| key={article.id} | |
| to={`/articles/${article.id}`} | |
| className="block p-2 rounded-lg hover:bg-gray-50 dark:hover:bg-gray-800 transition group" | |
| > | |
| <div className="flex items-start gap-2"> | |
| <div className="flex-1 min-w-0"> | |
| <p className="text-sm font-medium line-clamp-1 group-hover:text-primary transition"> | |
| {article.title} | |
| </p> | |
| <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: List | |
| if (variant === "list") { | |
| return ( | |
| <div ref={ref} className="space-y-6"> | |
| <div className="flex items-center justify-between"> | |
| <h2 className="text-xl font-bold">{title}</h2> | |
| <Link to="/articles" className="text-sm text-primary hover:underline flex items-center gap-1"> | |
| View all <ArrowRight className="w-4 h-4" /> | |
| </Link> | |
| </div> | |
| <div className="space-y-4"> | |
| {displayArticles.map((article, idx) => ( | |
| <motion.div | |
| key={article.id} | |
| initial={animate ? { opacity: 0, x: -20 } : false} | |
| animate={isInView ? { opacity: 1, x: 0 } : {}} | |
| transition={{ delay: idx * 0.05 }} | |
| onMouseEnter={() => setHoveredId(article.id)} | |
| onMouseLeave={() => setHoveredId(null)} | |
| > | |
| <Link | |
| to={`/articles/${article.id}`} | |
| className="block bg-white dark:bg-gray-800 rounded-xl p-4 shadow-sm border hover:shadow-md transition group" | |
| > | |
| <div className="flex gap-4"> | |
| {showImage && ( | |
| <div className="flex-shrink-0 w-24 h-24 rounded-lg overflow-hidden bg-gray-100 dark:bg-gray-700"> | |
| <img | |
| src={article.coverImage || `https://picsum.photos/seed/${article.id}/100/100`} | |
| alt="" | |
| className="w-full h-full object-cover group-hover:scale-105 transition" | |
| onError={(e) => { (e.target as HTMLImageElement).style.display = 'none'; }} | |
| /> | |
| </div> | |
| )} | |
| <div className="flex-1 min-w-0"> | |
| <h3 className="font-semibold text-lg mb-1 line-clamp-1 group-hover:text-primary transition"> | |
| {article.title} | |
| </h3> | |
| <p className="text-gray-600 dark:text-gray-400 text-sm line-clamp-2 mb-2"> | |
| {article.abstract || article.description} | |
| </p> | |
| <div className="flex flex-wrap items-center gap-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> | |
| <span className="flex items-center gap-1"> | |
| <Clock className="w-3 h-3" /> | |
| {article.readingTime || 5} min read | |
| </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> | |
| </> | |
| )} | |
| </div> | |
| </div> | |
| <ChevronRight className="w-5 h-5 text-gray-400 opacity-0 group-hover:opacity-100 transition group-hover:translate-x-1" /> | |
| </div> | |
| </Link> | |
| </motion.div> | |
| ))} | |
| </div> | |
| </div> | |
| ); | |
| } | |
| // Variant: Cards | |
| 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-5"> | |
| {displayArticles.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-40 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" | |
| 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> | |
| <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> | |
| ); | |
| } | |
| // Default variant (grid with images) | |
| return ( | |
| <div ref={ref} className="space-y-6"> | |
| <div className="flex items-center justify-between"> | |
| <div> | |
| <h2 className="text-2xl font-bold">{title}</h2> | |
| <p className="text-gray-500 text-sm">{subtitle}</p> | |
| </div> | |
| <Link to="/articles" className="text-sm text-primary hover:underline flex items-center gap-1"> | |
| View all <ArrowRight className="w-4 h-4" /> | |
| </Link> | |
| </div> | |
| <div className="grid grid-cols-1 sm:grid-cols-2 gap-4"> | |
| {displayArticles.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: -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-4 hover:shadow-md transition group" | |
| > | |
| <div className="flex gap-3"> | |
| {showImage && ( | |
| <div className="flex-shrink-0 w-20 h-20 rounded-lg overflow-hidden bg-gray-100 dark:bg-gray-700"> | |
| <img | |
| src={article.coverImage || `https://picsum.photos/seed/${article.id}/80/80`} | |
| alt="" | |
| className="w-full h-full object-cover group-hover:scale-105 transition" | |
| onError={(e) => { (e.target as HTMLImageElement).style.display = 'none'; }} | |
| /> | |
| </div> | |
| )} | |
| <div className="flex-1 min-w-0"> | |
| <h3 className="font-semibold text-base line-clamp-2 group-hover:text-primary transition"> | |
| {article.title} | |
| </h3> | |
| <div className="flex flex-wrap items-center gap-2 mt-1 text-xs text-gray-500"> | |
| <span className="flex items-center gap-1"> | |
| <Calendar className="w-3 h-3" /> | |
| {formatDistanceToNow(new Date(article.createdAt), { addSuffix: true })} | |
| </span> | |
| <span>•</span> | |
| <span className="flex items-center gap-1"> | |
| <Clock className="w-3 h-3" /> | |
| {article.readingTime || 5} min | |
| </span> | |
| {showStats && ( | |
| <span className="flex items-center gap-1"> | |
| <Eye className="w-3 h-3" /> {article.views || 0} | |
| </span> | |
| )} | |
| </div> | |
| {showAuthor && ( | |
| <p className="text-xs text-gray-500 mt-1 truncate"> | |
| by {article.authorUsername} | |
| </p> | |
| )} | |
| </div> | |
| </div> | |
| </Link> | |
| </motion.div> | |
| ))} | |
| </div> | |
| {/* Load More Button */} | |
| {articles.length > limit && ( | |
| <div className="text-center pt-2"> | |
| <Link to="/articles" className="inline-flex items-center gap-1 text-primary hover:gap-2 transition"> | |
| Load more articles <ArrowRight className="w-4 h-4" /> | |
| </Link> | |
| </div> | |
| )} | |
| </div> | |
| ); | |
| } | |
| // Simple version for sidebar | |
| export function SimpleRecentPosts({ articles = [] }: { articles?: Article[] }) { | |
| const demoArticles = articles.length > 0 ? articles : [ | |
| { id: 1, title: "Advances in Machine Learning", createdAt: new Date().toISOString(), authorUsername: "Dr. Sarah Chen" }, | |
| { id: 2, title: "Climate Change Research Methods", createdAt: new Date().toISOString(), authorUsername: "Prof. David Kim" }, | |
| { id: 3, title: "Open Access Publishing Trends", createdAt: new Date().toISOString(), authorUsername: "Dr. Amira Hassan" }, | |
| ]; | |
| return ( | |
| <div className="bg-white dark:bg-gray-800 rounded-xl p-4 shadow-sm border"> | |
| <h4 className="font-semibold text-sm mb-3 flex items-center gap-2"> | |
| <Clock className="w-4 h-4 text-primary" /> | |
| Recent Posts | |
| </h4> | |
| <div className="space-y-2"> | |
| {demoArticles.slice(0, 5).map((article) => ( | |
| <Link key={article.id} to={`/articles/${article.id}`} className="block p-2 rounded-lg hover:bg-gray-50 transition"> | |
| <p className="text-sm font-medium line-clamp-1">{article.title}</p> | |
| <p className="text-xs text-gray-500 mt-0.5">by {article.authorUsername}</p> | |
| </Link> | |
| ))} | |
| </div> | |
| </div> | |
| ); | |
| } |
Xet Storage Details
- Size:
- 13.9 kB
- Xet hash:
- f58478b6f5bd547b8e42ab87bc5a7085b92fb23902f54c18133c48a955b46f53
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.