2026-10-27 16:28:54 +05:30

33 lines
1.4 KiB
TypeScript

import { useMemo } from 'react';
import { cva } from 'class-variance-authority';
import { getArUrl, qrLogoFill, qrSvgMarkup } from '~/lib/assets/qr';
import { cn } from '~/lib/utils';
import { isBrandedQr, toBrand } from '~/services/asset-media';
import type { ArAsset } from '~/types/asset';
const frameVariants = cva('relative aspect-square w-full overflow-hidden bg-qr-surface [&>svg]:block', {
variants: {
size: {
card: 'rounded-item',
panel: 'max-w-[300px] rounded-card-sm',
},
},
});
// The asset's QR code; a branded code has the client logo inside it, in a cleared square at the centre
export function AssetQr({ asset, size, className }: { asset: ArAsset; size: 'card' | 'panel'; className?: string }) {
const project = asset.expand?.project;
const brand = isBrandedQr(asset) && project ? toBrand(project) : undefined;
const markup = useMemo(
() =>
qrSvgMarkup(getArUrl(asset.id), {
logo: brand?.logoUrl ? { href: brand.logoUrl, fill: qrLogoFill(brand) } : undefined,
label: `QR code for ${asset.name}`,
}),
[asset.id, asset.name, brand?.logoUrl, brand?.logoBg, brand?.logoBgColor],
);
// Markup built by qrSvgMarkup from our own data, with attribute values escaped
return <div className={cn(frameVariants({ size }), className)} dangerouslySetInnerHTML={{ __html: markup }} />;
}