53 lines
1.8 KiB
TypeScript
53 lines
1.8 KiB
TypeScript
import DraftsDialog from "@/components/drafts-dialog";
|
|
import { NewsCard } from "@/components/news-card";
|
|
import { Button } from "@/components/ui/button";
|
|
import { getPages } from "@/lib/pages/repository";
|
|
import { getCurrentUser } from "@/lib/user";
|
|
import { Metadata } from "next";
|
|
import Link from "next/link";
|
|
import SearchBar from "../../components/searchBar";
|
|
|
|
export const metadata: Metadata = { title: "News" };
|
|
|
|
export default async function News({ searchParams }: { searchParams: Promise<{ q?: string }> }) {
|
|
const { q } = await searchParams;
|
|
const user = await getCurrentUser();
|
|
const news = await getPages("NEWS", "PUBLISHED");
|
|
|
|
const query = q?.trim().toLowerCase() ?? "";
|
|
|
|
const filteredNews = query
|
|
? news.filter((n) =>
|
|
`${n.title.toLowerCase()} ${n.meta.tags?.join(" ").toLowerCase() ?? ""}`.includes(query)
|
|
)
|
|
: news;
|
|
|
|
const newsDrafts = await getPages("NEWS", "DRAFT");
|
|
const pageDrafts = await getPages("PAGE", "DRAFT");
|
|
|
|
const drafts = [...newsDrafts, ...pageDrafts];
|
|
|
|
return (
|
|
<div className="flex flex-col p-4 md:p-16 gap-8 md:gap-16 mb-4 md:mb-0">
|
|
<div className="flex flex-col gap-4 md:flex-row md:items-center justify-between">
|
|
<h1>News</h1>
|
|
<SearchBar />
|
|
<div className="flex flex-row items-center gap-4">
|
|
{user?.isAdmin && (
|
|
<div className="flex flex-row items-center gap-4">
|
|
<DraftsDialog drafts={drafts} />
|
|
<Link href="/create/"><Button variant="secondary">Create a new page</Button></Link>
|
|
</div>
|
|
)}
|
|
</div>
|
|
</div>
|
|
<div className="flex flex-col gap-6">
|
|
{
|
|
filteredNews.length > 0 ? filteredNews.map((n) => (
|
|
<NewsCard news={n} key={n.id} />
|
|
)) : <h2 className="text-muted-foreground">There are no news yet...</h2>
|
|
}
|
|
</div>
|
|
</div>
|
|
);
|
|
} |