195 lines
8.7 KiB
TypeScript
195 lines
8.7 KiB
TypeScript
"use client";
|
|
|
|
import { ChevronLeft, ChevronRight, Minus, Plus, Sun, X } from "lucide-react";
|
|
import Link from "next/link";
|
|
import { useTranslations } from "next-intl";
|
|
import { useEffect, useRef, useState } from "react";
|
|
import { DishPhoto } from "@/components/dishes/dish-tile";
|
|
import { Button } from "@/components/ui/button";
|
|
import { cn } from "@/lib/utils";
|
|
import { AlarmNotice, TimerButton, TimerChips, useAmount, useStepTimers, useWakeLock, withTextTimer, type Ingredient, type Step } from "./kit";
|
|
|
|
// Präsentationsmodus (Kai 7.11.2026, 26.11.28): ein Rezept zum Ablesen aus
|
|
// Abstand — am Handy hochkant, auf Tablet oder Echo Show quer. Erst die Zutaten
|
|
// in der Portionszahl, dann Schritt für Schritt in sehr großer Schrift. Blättern
|
|
// durch Tippen (linkes Drittel zurück, sonst weiter), Wischen, Pfeiltasten oder
|
|
// die großen Knöpfe unten. Ändert nichts: keine API, nur Ansehen und Timer.
|
|
export function Presentation({
|
|
dishId,
|
|
title,
|
|
photo,
|
|
portions: initialPortions,
|
|
ingredients,
|
|
steps,
|
|
closeHref,
|
|
}: {
|
|
dishId: string;
|
|
title: string;
|
|
photo: string | null;
|
|
portions: number;
|
|
ingredients: Ingredient[];
|
|
steps: Step[];
|
|
closeHref: string;
|
|
}) {
|
|
const t = useTranslations("present");
|
|
const tc = useTranslations("cooking");
|
|
const ts = useTranslations("dishes.sections");
|
|
const [index, setIndex] = useState(-1); // -1 = Zutaten
|
|
const [portions, setPortions] = useState(initialPortions);
|
|
const awake = useWakeLock();
|
|
const { timers, alarm, toggle, running } = useStepTimers();
|
|
const { amount, label } = useAmount(portions);
|
|
const touch = useRef<{ x: number; y: number } | null>(null);
|
|
const swiped = useRef(false);
|
|
|
|
const last = steps.length - 1;
|
|
const go = (by: number) => setIndex((i) => Math.min(last, Math.max(-1, i + by)));
|
|
|
|
useEffect(() => {
|
|
const key = (e: KeyboardEvent) => {
|
|
if (e.key === "ArrowRight" || e.key === "PageDown") go(1);
|
|
if (e.key === "ArrowLeft" || e.key === "PageUp") go(-1);
|
|
};
|
|
window.addEventListener("keydown", key);
|
|
return () => window.removeEventListener("keydown", key);
|
|
});
|
|
|
|
const step = index >= 0 && steps[index] ? withTextTimer(steps[index]) : null;
|
|
const forStep = step
|
|
? step.ingredientIds.map((id) => ingredients.find((i) => i.ingredientId === id)).filter((i): i is Ingredient => !!i)
|
|
: [];
|
|
|
|
return (
|
|
<main className="flex min-h-dvh flex-col bg-background">
|
|
<header className="flex items-center gap-3 border-b border-border px-4 py-2 pt-[max(0.5rem,env(safe-area-inset-top))]">
|
|
<Link
|
|
href={closeHref}
|
|
aria-label={t("close")}
|
|
className="flex size-12 shrink-0 items-center justify-center rounded-full bg-muted focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring"
|
|
>
|
|
<X aria-hidden className="size-6" />
|
|
</Link>
|
|
<h1 className="min-w-0 flex-1 truncate text-2xl leading-tight">{title}</h1>
|
|
{awake ? (
|
|
<span className="hidden items-center gap-1 text-sm text-muted-foreground sm:flex">
|
|
<Sun aria-hidden className="size-4" />
|
|
{tc("awake")}
|
|
</span>
|
|
) : null}
|
|
</header>
|
|
|
|
<AlarmNotice alarm={alarm} />
|
|
<TimerChips running={running} onPick={setIndex} className="px-4 pt-3" />
|
|
|
|
{/* Die ganze Fläche blättert: linkes Drittel zurück, der Rest weiter. Knöpfe darin bleiben Knöpfe. */}
|
|
<div
|
|
data-testid="present-stage"
|
|
className="flex flex-1 cursor-pointer select-none flex-col px-4 py-4 landscape:px-8"
|
|
onTouchStart={(e) => {
|
|
const p = e.touches[0];
|
|
touch.current = p ? { x: p.clientX, y: p.clientY } : null;
|
|
swiped.current = false;
|
|
}}
|
|
onTouchEnd={(e) => {
|
|
const start = touch.current;
|
|
const end = e.changedTouches[0];
|
|
touch.current = null;
|
|
if (!start || !end) return;
|
|
const dx = end.clientX - start.x;
|
|
if (Math.abs(dx) < 60 || Math.abs(dx) < Math.abs(end.clientY - start.y)) return;
|
|
swiped.current = true;
|
|
go(dx < 0 ? 1 : -1);
|
|
}}
|
|
onClick={(e) => {
|
|
if (swiped.current) return void (swiped.current = false);
|
|
if ((e.target as HTMLElement).closest("button, a, input")) return;
|
|
const box = e.currentTarget.getBoundingClientRect();
|
|
go(e.clientX - box.left < box.width / 3 ? -1 : 1);
|
|
}}
|
|
>
|
|
{step ? (
|
|
<section aria-labelledby="present-step" className="grid flex-1 content-start gap-6 landscape:grid-cols-[3fr_2fr] landscape:gap-10">
|
|
<div className="flex flex-col gap-5">
|
|
<h2 id="present-step" className="eyebrow text-base">
|
|
{ts(step.section)} · {tc("step", { n: index + 1, of: steps.length })}
|
|
</h2>
|
|
<p className="text-3xl leading-snug sm:text-4xl landscape:text-4xl">{step.text}</p>
|
|
{step.timerSeconds ? (
|
|
<TimerButton
|
|
running={timers.find((r) => r.step === index)}
|
|
seconds={step.timerSeconds}
|
|
onToggle={() => toggle(index, step.timerSeconds!)}
|
|
className="h-20 px-8 text-4xl"
|
|
/>
|
|
) : null}
|
|
{step.timerSeconds && step.timerFrom ? <p className="text-muted-foreground">{tc("timerFromProduct", { product: step.timerFrom })}</p> : null}
|
|
</div>
|
|
{forStep.length ? (
|
|
<ul aria-label={tc("forStep")} className="flex flex-col gap-2 self-start rounded-2xl bg-muted p-5 text-2xl">
|
|
{forStep.map((i) => (
|
|
<li key={i.ingredientId}>
|
|
<span className="font-semibold tabular-nums">{amount(i)}</span> {label(i)}
|
|
</li>
|
|
))}
|
|
</ul>
|
|
) : null}
|
|
</section>
|
|
) : (
|
|
<section aria-labelledby="present-ingredients" className="grid flex-1 content-start gap-6 landscape:grid-cols-[2fr_3fr] landscape:gap-10">
|
|
<div className="flex flex-col gap-4">
|
|
{/* Ohne Foto kein Platzhalter: hochkant gehört der Platz den Zutaten. */}
|
|
{photo ? (
|
|
<div className="aspect-[16/9] overflow-hidden rounded-2xl landscape:aspect-[4/3] landscape:max-h-[50dvh]">
|
|
<DishPhoto dishId={dishId} photo={photo} />
|
|
</div>
|
|
) : null}
|
|
<div className="flex items-center gap-3" role="group" aria-label={t("portions")}>
|
|
<Button type="button" variant="outline" size="lg" className="size-14" aria-label={t("fewer")} disabled={portions <= 1} onClick={() => setPortions((p) => Math.max(1, p - 1))}>
|
|
<Minus aria-hidden />
|
|
</Button>
|
|
<p className="min-w-0 flex-1 text-center text-xl tabular-nums" aria-live="polite">
|
|
{t("portionsN", { n: portions })}
|
|
</p>
|
|
<Button type="button" variant="outline" size="lg" className="size-14" aria-label={t("more")} disabled={portions >= 30} onClick={() => setPortions((p) => Math.min(30, p + 1))}>
|
|
<Plus aria-hidden />
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
<div className="flex flex-col gap-3">
|
|
<h2 id="present-ingredients" className="eyebrow text-base">
|
|
{t("ingredients")}
|
|
</h2>
|
|
<ul className="flex flex-col text-2xl">
|
|
{ingredients.map((i) => (
|
|
<li key={i.ingredientId} className="border-b border-border py-2">
|
|
<span className="font-semibold tabular-nums">{amount(i)}</span> {label(i)}
|
|
{i.note ? <span className="text-muted-foreground">, {i.note}</span> : null}
|
|
</li>
|
|
))}
|
|
</ul>
|
|
</div>
|
|
</section>
|
|
)}
|
|
</div>
|
|
|
|
<nav aria-label={t("steps")} className="sticky bottom-0 flex flex-col gap-2 border-t border-border bg-background px-4 pb-[max(0.5rem,env(safe-area-inset-bottom))] pt-2">
|
|
<ol className="flex justify-center gap-1.5" aria-hidden>
|
|
{[-1, ...steps.map((_, i) => i)].map((i) => (
|
|
<li key={i} className={cn("h-1.5 rounded-full", i === index ? "w-6 bg-accent" : "w-1.5 bg-border")} />
|
|
))}
|
|
</ol>
|
|
<div className="flex gap-2">
|
|
<Button type="button" variant="outline" className="h-16 flex-1 text-lg" disabled={index < 0} onClick={() => go(-1)}>
|
|
<ChevronLeft aria-hidden />
|
|
{tc("back")}
|
|
</Button>
|
|
<Button type="button" className="h-16 flex-[2] text-lg" disabled={index >= last} onClick={() => go(1)}>
|
|
{index < 0 ? tc("start") : tc("next")}
|
|
<ChevronRight aria-hidden />
|
|
</Button>
|
|
</div>
|
|
</nav>
|
|
</main>
|
|
);
|
|
}
|