import { useState, useEffect, useRef, useMemo } from "react"; import Layout from "@/components/Layout"; import { Button } from "@/components/ui/button"; import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from "@/components/ui/card"; import { Badge } from "@/components/ui/badge"; import LoadingScreen from "@/components/LoadingScreen"; import { aethexToast } from "@/lib/aethex-toast"; import { Link } from "react-router-dom"; import { PenTool, Calendar, User, ArrowRight, Search, Filter, Bookmark, Share, ThumbsUp, MessageCircle, TrendingUp, } from "lucide-react"; type BlogPost = { id?: string | number; slug?: string; title: string; excerpt: string; author: string; date: string; readTime?: string; category?: string; image?: string | null; likes?: number; comments?: number; trending?: boolean; }; const normalizeCategory = (value?: string) => value ? value .toString() .toLowerCase() .trim() .replace(/[^a-z0-9]+/g, "-") .replace(/^-+|-+$/g, "") : "general"; const buildSlug = (post: BlogPost): string => { const base = (post.slug || post.id || post.title || "").toString(); const sanitized = base .trim() .toLowerCase() .replace(/[^a-z0-9]+/g, "-") .replace(/^-+|-+$/g, ""); return sanitized || "article"; }; export default function Blog() { const [isLoading, setIsLoading] = useState(true); const [selectedCategory, setSelectedCategory] = useState("all"); const toastShownRef = useRef(false); const [posts, setPosts] = useState([]); const [featuredPost, setFeaturedPost] = useState(null); const staticPosts = useMemo( () => [ { id: "static-1", slug: "building-scalable-game-architecture", title: "Building Scalable Game Architecture with Microservices", excerpt: "Learn how to design game backends that can handle millions of concurrent players using modern microservices patterns.", author: "Marcus Rodriguez", date: "December 12, 2024", readTime: "6 min read", category: "Technology", likes: 89, comments: 15, trending: true, image: "https://images.unsplash.com/photo-1518770660439-4636190af475?auto=format&fit=crop&w=1200&q=80", }, { id: "static-2", slug: "advanced-unity-optimization-techniques", title: "Advanced Unity Optimization Techniques", excerpt: "Performance optimization strategies that can boost your Unity game's frame rate by up to 300%.", author: "Alex Thompson", date: "December 10, 2024", readTime: "12 min read", category: "Tutorials", likes: 156, comments: 34, trending: false, image: "https://images.unsplash.com/photo-1527443224154-dcc0707b462b?auto=format&fit=crop&w=1200&q=80", }, { id: "static-3", slug: "aethex-labs-neural-network-compression", title: "AeThex Labs: Breakthrough in Neural Network Compression", excerpt: "Our research team achieves 90% model size reduction while maintaining accuracy for mobile game AI.", author: "Dr. Aisha Patel", date: "December 8, 2024", readTime: "5 min read", category: "Research", likes: 203, comments: 41, trending: true, image: "https://images.unsplash.com/photo-1504384308090-c894fdcc538d?auto=format&fit=crop&w=1200&q=80", }, { id: "static-4", slug: "introducing-aethex-cloud-gaming-platform", title: "Introducing AeThex Cloud Gaming Platform", excerpt: "Launch games instantly across any device with our new cloud gaming infrastructure and global CDN.", author: "AeThex Team", date: "December 5, 2024", readTime: "4 min read", category: "Company News", likes: 278, comments: 52, trending: false, image: "https://images.unsplash.com/photo-1511512578047-dfb367046420?auto=format&fit=crop&w=1200&q=80", }, { id: "static-5", slug: "real-time-ray-tracing-in-web-games", title: "Real-time Ray Tracing in Web Games", excerpt: "Tutorial: Implementing hardware-accelerated ray tracing in browser-based games using WebGPU.", author: "Jordan Kim", date: "December 3, 2024", readTime: "15 min read", category: "Tutorials", likes: 94, comments: 18, trending: false, image: "https://images.unsplash.com/photo-1489515217757-5fd1be406fef?auto=format&fit=crop&w=1200&q=80", }, { id: "static-6", slug: "the-evolution-of-game-ai", title: "The Evolution of Game AI: From Scripts to Neural Networks", excerpt: "A comprehensive look at how artificial intelligence in games has evolved and where it's heading next.", author: "Dr. Michael Chen", date: "December 1, 2024", readTime: "10 min read", category: "Technology", likes: 167, comments: 29, trending: false, image: "https://images.unsplash.com/photo-1523580846011-d3a5bc25702b?auto=format&fit=crop&w=1200&q=80", }, ], [], ); useEffect(() => { let cancelled = false; (async () => { try { const res = await fetch("/api/blog?limit=50"); const data = res.ok ? await res.json() : []; if (!cancelled && Array.isArray(data)) { const mapped: BlogPost[] = data.map((r: any) => ({ id: r.id, slug: r.slug ?? r.id ?? undefined, title: r.title, excerpt: r.excerpt, author: r.author, date: r.date, readTime: r.read_time ?? r.readTime, category: r.category ?? "General", image: r.image ?? null, likes: typeof r.likes === "number" ? r.likes : 0, comments: typeof r.comments === "number" ? r.comments : 0, trending: Boolean(r.trending), })); setPosts(mapped); const highlighted = mapped.find((post) => post.trending) ?? mapped[0] ?? null; setFeaturedPost(highlighted); } } catch (e) { console.warn("Blog fetch failed:", e); } finally { if (!cancelled) { setIsLoading(false); if (!toastShownRef.current) { aethexToast.system("AeThex Blog loaded successfully"); toastShownRef.current = true; } } } })(); return () => { cancelled = true; }; }, []); useEffect(() => { if (selectedCategory === "all") { return; } const dataset = posts.length ? posts : staticPosts; const hasCategory = dataset.some( (post) => normalizeCategory(post.category) === selectedCategory, ); if (!hasCategory) { setSelectedCategory("all"); } }, [posts, staticPosts, selectedCategory]); const categories = useMemo(() => { const dataset = posts.length ? posts : staticPosts; const counts = new Map(); dataset.forEach((post) => { const name = post.category || "General"; const id = normalizeCategory(name); const existing = counts.get(id); counts.set(id, { name, count: (existing?.count ?? 0) + 1, }); }); const preferredOrder = [ "technology", "tutorials", "research", "company-news", "general", ]; const ordered: { id: string; name: string; count: number }[] = []; preferredOrder.forEach((id) => { const entry = counts.get(id); if (entry) { ordered.push({ id, name: entry.name, count: entry.count }); counts.delete(id); } }); counts.forEach((value, id) => { ordered.push({ id, name: value.name, count: value.count }); }); return [ { id: "all", name: "All Posts", count: dataset.length }, ...ordered, ]; }, [posts, staticPosts]); const filteredPosts = useMemo(() => { const dataset = posts.length ? posts : staticPosts; if (selectedCategory === "all") { return dataset; } return dataset.filter( (post) => normalizeCategory(post.category) === selectedCategory, ); }, [posts, staticPosts, selectedCategory]); const activeFeaturedPost = useMemo(() => { const dataset = posts.length ? posts : staticPosts; const scoped = selectedCategory === "all" ? dataset : filteredPosts; const scopedPosts = scoped.length ? scoped : dataset; const featuredSlug = featuredPost ? buildSlug(featuredPost) : null; if (featuredSlug) { const matchingFeatured = scopedPosts.find( (post) => buildSlug(post) === featuredSlug, ); if (matchingFeatured) { return matchingFeatured; } } return ( scopedPosts.find((post) => post.trending) ?? (featuredSlug ? dataset.find((post) => buildSlug(post) === featuredSlug) : null) ?? dataset.find((post) => post.trending) ?? scopedPosts[0] ?? dataset[0] ?? null ); }, [featuredPost, filteredPosts, posts, staticPosts, selectedCategory]); const displayedPosts = useMemo(() => { if (!activeFeaturedPost) { return filteredPosts; } const featuredSlug = buildSlug(activeFeaturedPost); return filteredPosts.filter((post) => buildSlug(post) !== featuredSlug); }, [filteredPosts, activeFeaturedPost]); const placeholderImage = "/placeholder.svg"; if (isLoading) { return ( ); } return (
{/* Hero Section */}
AeThex Blog

Insights & Innovation

Stay updated with the latest developments in game technology, AI research, and industry insights from the AeThex team.

{/* Search and Filter */}
{/* Categories */}
{categories.map((category, index) => ( ))}
{/* Featured Post */} {activeFeaturedPost && (
{activeFeaturedPost.title}
Featured {activeFeaturedPost.title} {activeFeaturedPost.excerpt}
{activeFeaturedPost.author || "AeThex Team"}
{activeFeaturedPost.date}
{activeFeaturedPost.readTime && ( {activeFeaturedPost.readTime} )}
{activeFeaturedPost.likes ?? 0}
{activeFeaturedPost.comments ?? 0}
)} {/* Recent Posts */}

Recent Articles

Latest insights and tutorials from our team

{displayedPosts.length > 0 ? (
{displayedPosts.map((post, index) => (
{post.category || "General"} {post.trending && ( Trending )}
{post.title} {post.excerpt}
{post.author || "AeThex Team"}
{post.date || "Coming soon"}
{post.readTime ? ( {post.readTime} ) : ( Quick read )}
{post.likes ?? 0}
{post.comments ?? 0}
))}
) : (
No articles available in this category yet. Please check back soon.
)}
{/* Newsletter CTA */}

Stay in the Loop

Subscribe to our newsletter for the latest articles, tutorials, and technology insights delivered directly to your inbox.

Join 10,000+ developers getting weekly insights. Unsubscribe anytime.

); }