Replace legacy category/static data with derived logic

cgen-7b67955bc415433c8f31ef71241267a4
This commit is contained in:
Builder.io 2025-10-04 10:58:48 +00:00
parent 782f4e7fd2
commit 999e723ced

View file

@ -212,97 +212,82 @@ export default function Blog() {
}; };
}, []); }, []);
const categories = [ useEffect(() => {
{ id: "all", name: "All Posts", count: 45 }, if (selectedCategory === "all") {
{ id: "technology", name: "Technology", count: 18 }, return;
{ id: "tutorials", name: "Tutorials", count: 12 }, }
{ id: "research", name: "Research", count: 8 }, const dataset = posts.length ? posts : staticPosts;
{ id: "company", name: "Company News", count: 7 }, const hasCategory = dataset.some(
]; (post) => normalizeCategory(post.category) === selectedCategory,
);
if (!hasCategory) {
setSelectedCategory("all");
}
}, [posts, staticPosts, selectedCategory]);
const postsStatic = [ const categories = useMemo(() => {
{ const dataset = posts.length ? posts : staticPosts;
title: "Building Scalable Game Architecture with Microservices", const counts = new Map<string, { name: string; count: number }>();
excerpt: dataset.forEach((post) => {
"Learn how to design game backends that can handle millions of concurrent players using modern microservices patterns.", const name = post.category || "General";
author: "Marcus Rodriguez", const id = normalizeCategory(name);
date: "December 12, 2024", const existing = counts.get(id);
readTime: "6 min read", counts.set(id, {
category: "Technology", name,
likes: 89, count: (existing?.count ?? 0) + 1,
comments: 15, });
trending: true, });
}, const preferredOrder = [
{ "technology",
title: "Advanced Unity Optimization Techniques", "tutorials",
excerpt: "research",
"Performance optimization strategies that can boost your Unity game's frame rate by up to 300%.", "company-news",
author: "Alex Thompson", "general",
date: "December 10, 2024", ];
readTime: "12 min read", const ordered: { id: string; name: string; count: number }[] = [];
category: "Tutorials", preferredOrder.forEach((id) => {
likes: 156, const entry = counts.get(id);
comments: 34, if (entry) {
trending: false, ordered.push({ id, name: entry.name, count: entry.count });
}, counts.delete(id);
{ }
title: "AeThex Labs: Breakthrough in Neural Network Compression", });
excerpt: counts.forEach((value, id) => {
"Our research team achieves 90% model size reduction while maintaining accuracy for mobile game AI.", ordered.push({ id, name: value.name, count: value.count });
author: "Dr. Aisha Patel", });
date: "December 8, 2024", return [
readTime: "5 min read", { id: "all", name: "All Posts", count: dataset.length },
category: "Research", ...ordered,
likes: 203, ];
comments: 41, }, [posts, staticPosts]);
trending: true,
},
{
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,
},
{
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,
},
{
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,
},
];
const filteredPosts = const filteredPosts = useMemo(() => {
selectedCategory === "all" const dataset = posts.length ? posts : staticPosts;
? posts.length if (selectedCategory === "all") {
? posts return dataset;
: postsStatic }
: (posts.length ? posts : postsStatic).filter( return dataset.filter(
(post) => (post.category || "").toLowerCase() === selectedCategory, (post) => normalizeCategory(post.category) === selectedCategory,
); );
}, [posts, staticPosts, selectedCategory]);
const activeFeaturedPost = useMemo(() => {
const dataset = posts.length ? posts : staticPosts;
if (featuredPost) {
return featuredPost;
}
return dataset.find((post) => post.trending) ?? dataset[0] ?? null;
}, [featuredPost, posts, staticPosts]);
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) { if (isLoading) {
return ( return (