memorium/routes/recipes/[name].tsx

127 lines
3.6 KiB
TypeScript
Raw Normal View History

2023-07-26 13:47:01 +02:00
import { Handlers, PageProps } from "$fresh/server.ts";
2023-07-30 19:40:39 +02:00
import { IngredientsList } from "@islands/IngredientsList.tsx";
2023-07-27 15:28:50 +02:00
import { MainLayout } from "@components/layouts/main.tsx";
2023-07-30 19:40:39 +02:00
import Counter from "@islands/Counter.tsx";
2025-01-18 00:46:05 +01:00
import { Signal, useSignal } from "@preact/signals";
2023-08-01 17:50:00 +02:00
import { getRecipe, Recipe } from "@lib/resource/recipes.ts";
2023-08-07 14:44:04 +02:00
import { RedirectSearchHandler } from "@islands/Search.tsx";
import { KMenu } from "@islands/KMenu.tsx";
2023-10-16 01:40:10 +02:00
import PageHero from "@components/PageHero.tsx";
import { Star } from "@components/Stars.tsx";
2025-01-18 00:46:05 +01:00
import { renderMarkdown } from "@lib/documents.ts";
2025-01-19 19:49:24 +01:00
import { isValidRecipe } from "@lib/recipeSchema.ts";
2025-01-23 18:42:29 +01:00
import { MetaTags } from "@components/MetaTags.tsx";
2023-07-26 13:47:01 +02:00
2023-10-16 01:40:10 +02:00
export const handler: Handlers<{ recipe: Recipe; session: unknown } | null> = {
2023-07-26 13:47:01 +02:00
async GET(_, ctx) {
2025-01-18 00:46:05 +01:00
try {
const recipe = await getRecipe(ctx.params.name);
if (!recipe) {
return ctx.renderNotFound();
}
return ctx.render({ recipe, session: ctx.state.session });
} catch (_e) {
return ctx.renderNotFound();
}
2023-07-26 13:47:01 +02:00
},
};
2025-01-18 00:46:05 +01:00
function ValidRecipe({
recipe,
amount,
portion,
}: { recipe: Recipe; amount: Signal<number>; portion: number }) {
return (
<>
<div class="flex items-center gap-8">
<h3 class="text-3xl my-5">Ingredients</h3>
{portion && <Counter count={amount} />}
</div>
<IngredientsList
ingredients={recipe.ingredients}
amount={amount}
portion={portion}
/>
<h3 class="text-3xl my-5">Preparation</h3>
<div class="pl-2">
<ol class="list-decimal grid gap-4">
{recipe.instructions && (recipe.instructions.map((instruction) => {
return (
<li
dangerouslySetInnerHTML={{
__html: renderMarkdown(instruction),
}}
/>
);
}))}
</ol>
</div>
2025-01-18 00:46:05 +01:00
</>
);
}
export default function Page(
props: PageProps<{ recipe: Recipe; session: Record<string, string> }>,
) {
const { recipe, session } = props.data;
2023-07-30 19:40:39 +02:00
const portion = recipe.meta?.portion;
const amount = useSignal(portion || 1);
2023-08-04 23:36:35 +02:00
const subline = [
recipe?.meta?.time && `Duration ${recipe.meta.time}`,
2023-10-16 01:40:10 +02:00
].filter(Boolean) as string[];
2023-08-04 23:36:35 +02:00
2023-07-26 13:47:01 +02:00
return (
2023-08-06 18:06:09 +02:00
<MainLayout
url={props.url}
title={`Recipes > ${recipe.name}`}
context={recipe}
>
2023-08-07 14:44:04 +02:00
<RedirectSearchHandler />
<KMenu type="main" context={recipe} />
2025-01-23 18:42:29 +01:00
<MetaTags resource={recipe} />
2023-10-16 01:40:10 +02:00
<PageHero image={recipe.meta?.image} thumbnail={recipe.meta?.thumbnail}>
<PageHero.Header>
<PageHero.BackLink href="/recipes" />
2023-10-16 01:40:10 +02:00
{session && (
<PageHero.EditLink
href={`https://notes.max-richter.dev/Recipes/${recipe.id}`}
/>
)}
</PageHero.Header>
<PageHero.Footer>
2025-01-18 00:46:05 +01:00
<PageHero.Title link={recipe.meta?.link}>
{recipe.name}
</PageHero.Title>
2023-10-16 01:40:10 +02:00
<PageHero.Subline
entries={subline}
>
{recipe.meta?.rating && <Star rating={recipe.meta?.rating} />}
</PageHero.Subline>
</PageHero.Footer>
</PageHero>
2025-01-18 00:46:05 +01:00
2023-07-26 15:48:03 +02:00
<div class="px-8 text-white mt-10">
2025-01-18 00:46:05 +01:00
{isValidRecipe(recipe)
? (
<ValidRecipe
recipe={recipe}
amount={amount}
portion={portion || 1}
/>
)
: (
<div
2025-01-25 18:51:10 +01:00
class="whitespace-break-spaces markdown-body"
2025-01-18 00:46:05 +01:00
dangerouslySetInnerHTML={{
__html: renderMarkdown(recipe?.markdown || ""),
}}
/>
)}
2023-07-26 13:47:01 +02:00
</div>
</MainLayout>
);
}