"use server";
import Blogs from "@/components/blog";
import request from "@/lib/request";
import { Blog } from "@/types/types";
import Categories from "./categories";
import NoBlogs from "./no-blogs";

export default async function History({
	searchParams,
}: {
	searchParams: Promise<{ category?: string }>;
}) {
	const { category } = await searchParams;
	let url = "/blogs";
	if (category) url += `?category=${category}`;
	const blogs = await request<Blog[]>(url, {
		requiresAuth: false,
	});

	if (!blogs.data || blogs.data.length < 1) {
		return <NoBlogs />;
	}

	if (blogs?.status === "Error") {
		return <p>Un problème est survenue.</p>;
	}

	return (
		<div className="flex flex-col md:flex-row">
			<Categories selectedCategory={category} />
			<Blogs blogs={blogs.data} />
		</div>
	);
}
