61 lines
2.0 KiB
TypeScript
Raw Normal View History

2023-08-01 21:35:21 +02:00
import { Handlers, PageProps } from "$fresh/server.ts";
import { MainLayout } from "@components/layouts/main.tsx";
import { Article, getAllArticles } from "@lib/resource/articles.ts";
import { Card } from "@components/Card.tsx";
import { KMenu } from "@islands/KMenu.tsx";
2023-08-02 01:58:03 +02:00
import { Grid } from "@components/Grid.tsx";
2023-08-02 13:11:17 +02:00
import { IconArrowLeft } from "@components/icons.tsx";
2023-08-07 14:44:04 +02:00
import { RedirectSearchHandler } from "@islands/Search.tsx";
2023-08-10 16:59:18 +02:00
import { parseResourceUrl, searchResource } from "@lib/search.ts";
import { SearchResult } from "@lib/types.ts";
2023-08-01 21:35:21 +02:00
export const handler: Handlers<Article[] | null> = {
2023-08-10 16:59:18 +02:00
async GET(req, ctx) {
const articles = await getAllArticles();
const searchParams = parseResourceUrl(req.url);
const searchResults = searchParams &&
await searchResource({ ...searchParams, type: "article" });
return ctx.render({ articles, searchResults });
2023-08-01 21:35:21 +02:00
},
};
2023-08-10 16:59:18 +02:00
export default function Greet(
props: PageProps<{ articles: Article[] | null; searchResults: SearchResult }>,
) {
const { articles, searchResults } = props.data;
2023-08-01 21:35:21 +02:00
return (
2023-08-10 16:59:18 +02:00
<MainLayout
url={props.url}
title="Articles"
context={{ type: "article" }}
searchResults={searchResults}
>
2023-08-01 21:35:21 +02:00
<header class="flex gap-4 items-center mb-5 md:hidden">
<a
class="px-4 ml-4 py-2 bg-gray-300 text-gray-800 rounded-lg flex items-center gap-1"
href="/"
>
<IconArrowLeft class="w-5 h-5" />
Back
</a>
<h3 class="text-2xl text-white font-light">📝 Articles</h3>
</header>
2023-08-07 14:44:04 +02:00
<RedirectSearchHandler />
<KMenu type="main" context={{ type: "article" }} />
2023-08-02 01:58:03 +02:00
<Grid>
2023-08-10 16:59:18 +02:00
{articles?.map((doc) => {
return (
<Card
2023-08-04 22:38:09 +02:00
image={doc?.meta?.image || "/placeholder.svg"}
thumbnail={doc?.meta?.thumbnail}
link={`/articles/${doc.id}`}
title={doc.name}
/>
);
2023-08-01 21:35:21 +02:00
})}
2023-08-02 01:58:03 +02:00
</Grid>
2023-08-01 21:35:21 +02:00
</MainLayout>
);
}