Files
cozsweet-frontend-nextjs/src/app/tip/tip-screen.helpers.ts
T

48 lines
1.3 KiB
TypeScript

import type {
GiftCategory,
GiftProduct,
} from "@/data/schemas/payment";
export const TIP_GIFT_PLACEHOLDER_IMAGE = "/images/tip/medium.png";
const TIP_GIFT_PLACEHOLDER_BY_TIP_TYPE: Readonly<Record<string, string>> = {
coffee_small: "/images/tip/small.jpg",
coffee_medium: "/images/tip/medium.png",
coffee_large: "/images/tip/large.png",
};
export function formatGiftPrice(
amountCents: number,
currency: string,
): string {
const normalizedCurrency = currency.trim().toUpperCase();
try {
return new Intl.NumberFormat("en-US", {
style: "currency",
currency: normalizedCurrency,
}).format(amountCents / 100);
} catch {
const amount = (amountCents / 100).toFixed(2);
return normalizedCurrency ? `${normalizedCurrency} ${amount}` : amount;
}
}
export function getGiftImageSources(
product: GiftProduct | null,
category: GiftCategory | null,
): readonly string[] {
const placeholderImage = product
? (TIP_GIFT_PLACEHOLDER_BY_TIP_TYPE[product.tipType] ??
TIP_GIFT_PLACEHOLDER_IMAGE)
: TIP_GIFT_PLACEHOLDER_IMAGE;
return [
product?.imageUrl,
category?.imageUrl,
placeholderImage,
].filter(
(source, index, sources): source is string =>
Boolean(source) && sources.indexOf(source) === index,
);
}