30 lines
1.1 KiB
TypeScript
30 lines
1.1 KiB
TypeScript
"use client";
|
|
|
|
import { useLocale, useTranslations } from "next-intl";
|
|
import { isOne } from "./ingredient-name";
|
|
import { formatKitchen, type BaseUnit, type DisplayUnit, type KitchenUnit } from "./units";
|
|
|
|
/** Was eine Zutat für die Mengenanzeige mitbringt (lib/dishes.ts, getDish). */
|
|
export type Measured = {
|
|
unit: BaseUnit;
|
|
displayUnit?: DisplayUnit | null;
|
|
packSize?: number | null;
|
|
};
|
|
|
|
/**
|
|
* Menge mit Küchenmaß in der Sprache der Person (26.11.39): „2 EL (30 ml)“,
|
|
* „400 g (1 Dose)“. Liefert Hauptteil und Klammer getrennt, damit Listen sie
|
|
* verschieden setzen können; text() fügt beides zusammen.
|
|
*/
|
|
export function useKitchenAmount() {
|
|
const locale = useLocale();
|
|
const tm = useTranslations("measure");
|
|
const word = (u: KitchenUnit, n: number) => tm(u, { n: isOne(n) ? 1 : 2 });
|
|
const parts = (i: Measured, qty: number) => formatKitchen(qty, i.unit, i.displayUnit, i.packSize, locale, word);
|
|
const text = (i: Measured, qty: number) => {
|
|
const p = parts(i, qty);
|
|
return p.extra ? `${p.main} (${p.extra})` : p.main;
|
|
};
|
|
return { parts, text };
|
|
}
|