Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

4.0 KB
Raw Blame
import { useTranslation } from '../../i18n';
import { currencyDecimals, currencyLocale, formatMoney } from '../../utils/formatters';
import { fs } from '../shared/DialogShell';

/** What a card shows for a balance that is still loading or could not be read. */
export const UNKNOWN_AMOUNT = '…';

/** A tone's colour, from the status tokens the booking cards use. */
const TONE_COLOR = {
  owe: 'var(--danger)',
  owed: 'var(--success)',
  unfinished: 'var(--warning)',
  total: 'var(--accent)',
} as const;

/**
 * One of the four figures on top, drawn like a booking card: a head band tinted
 * by what the figure means, with its icon, name and a line on what it counts,
 * then the amount and a foot with who or what is behind it.
 */
export default function SummaryCard({
  label,
  sub,
  amount,
  currency,
  locale,
  icon,
  foot,
  tone,
  failed = false,
}: {
  label: string;
  sub: string;
  amount: number | null;
  currency: string;
  locale: string;
  icon: React.ReactNode;
  foot: React.ReactNode;
  tone: 'owe' | 'owed' | 'total' | 'unfinished';
  /** With no amount: the read failed rather than still running. */
  failed?: boolean;
}) {
  const { t } = useTranslation();
  const color = TONE_COLOR[tone];
  // formatToParts keeps the design's "big integer + muted symbol/decimals" styling
  // while letting Intl place the symbol and pick separators per locale + currency.
  let parts: Intl.NumberFormatPart[] | null = null;
  try {
    const d = currencyDecimals(currency);
    parts =
      amount == null
        ? null
        : new Intl.NumberFormat(currencyLocale(currency), {
            style: 'currency',
            currency: (currency || 'EUR').toUpperCase(),
            minimumFractionDigits: d,
            maximumFractionDigits: d,
          }).formatToParts(amount || 0);
  } catch {
    parts = null;
  }
  const big = (p: Intl.NumberFormatPart) => p.type === 'integer' || p.type === 'group' || p.type === 'minusSign';
  return (
    <section
      aria-label={label}
      className="flex flex-col overflow-hidden rounded-2xl border border-edge-faint bg-surface-secondary"
    >
      <div
        className="flex items-center gap-2.5 border-b border-edge-faint px-4 py-3"
        style={{ background: `color-mix(in srgb, ${color} ${tone === 'total' ? 7 : 11}%, transparent)` }}
      >
        <span
          className="grid h-8 w-8 flex-none place-items-center rounded-[10px] bg-surface-card shadow-sm"
          style={{ color: tone === 'total' ? 'var(--text-primary)' : color }}
        >
          {icon}
        </span>
        <div className="min-w-0">
          <div className="truncate font-bold text-content" style={fs(13.5, 'body')}>
            {label}
          </div>
          <div className="truncate text-content-faint" style={fs(11.5)}>
            {sub}
          </div>
        </div>
      </div>
      <div className="flex flex-1 flex-col gap-3 px-4 pb-4 pt-3.5">
        <div
          className="flex items-baseline font-semibold leading-none tracking-[-0.035em]"
          style={{
            fontSize: 'calc(40px * var(--fs-scale-title, 1))',
            color: tone === 'total' ? 'var(--text-primary)' : color,
          }}
        >
          {parts ? (
            parts.map((p, i) => (
              <span
                key={i}
                className={big(p) ? undefined : 'font-medium text-content-faint'}
                style={big(p) ? undefined : { fontSize: 'calc(22px * var(--fs-scale-title, 1))' }}
              >
                {p.value}
              </span>
            ))
          ) : (
            <span>{amount == null ? UNKNOWN_AMOUNT : formatMoney(amount, currency, locale)}</span>
          )}
        </div>
        <div className="mt-auto flex flex-wrap items-center gap-1.5 text-content-muted" style={fs(12.5, 'body')}>
          {amount == null ? (
            <span className="text-content-faint">{t(failed ? 'common.unknownError' : 'common.loading')}</span>
          ) : (
            foot
          )}
        </div>
      </div>
    </section>
  );
}