Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | 46x 46x 46x 46x 92x 46x 92x 46x 46x 46x 92x 138x 92x 138x 138x 138x 46x 46x 46x 46x 644x 184x 46x 184x 368x 46x 33x 13x 26x 13x 26x 39x 15046x 21x 21x 42x 4x 17x 34x 41x 51x | import type { ReactNode } from "react";
import { Badge } from "@/ahuora-design-system/ui/badge";
import { Button } from "@/ahuora-design-system/ui/button";
import { cn } from "@/lib/utils";
import { formatAmount } from "../../shared/model/economicsFormatters";
import type {
DrillbackHandler,
ParsedChartDataset,
SourceRow,
WarningRef,
} from "../model/types";
import {
chartSourcePoints,
chartTitle,
displayedChartDataLabels,
visibleChartWarnings,
} from "./chartData";
import { cashFlowRenderingMetadata } from "./chartFormatters";
import { chartCardDefinition } from "./chartRenderers";
import { CHART_CASH_FLOW_NPV } from "./constants";
export function ChartCard({
dataset,
onDrillback,
controls,
ariaLabelPrefix = "Chart",
}: {
dataset: ParsedChartDataset;
onDrillback: DrillbackHandler;
controls?: ReactNode;
ariaLabelPrefix?: string;
}) {
const isCashFlowChart = dataset.chart_key === CHART_CASH_FLOW_NPV;
const chartDefinition = chartCardDefinition(dataset);
const warningRefs = visibleChartWarnings(dataset);
const paybackYears =
cashFlowRenderingMetadata(dataset.rendering_metadata)?.payback_years ??
null;
const title = chartTitle(dataset);
const displayedDataLabels = displayedChartDataLabels(dataset);
return (
<section
className={cn(
"rounded-md border bg-background p-3",
chartDefinition.wide && "xl:col-span-2",
)}
aria-label={`${ariaLabelPrefix} ${title}`}
>
<div className="mb-3 flex items-start justify-between gap-3">
<div className="min-w-0">
<h5 className="text-sm font-semibold">{title}</h5>
{isCashFlowChart && paybackYears != null && (
<div
className="mt-1 text-xs text-muted-foreground"
aria-label="Cash-flow payback marker"
>
Payback {formatAmount(paybackYears)} years
</div>
)}
</div>
<div className="flex shrink-0 flex-wrap items-center justify-end gap-2">
{controls}
{warningRefs.length > 0 && (
<Badge variant="secondary" size="xs" borderRadius="round">
{warningRefs.length} warning
{warningRefs.length === 1 ? "" : "s"}
</Badge>
)}
</div>
</div>
<ChartWarningSummary dataset={dataset} warningRefs={warningRefs} />
{displayedDataLabels.length > 0 && (
<div className="sr-only" aria-label={`${title} displayed data`}>
{displayedDataLabels.join(", ")}
</div>
)}
<div
className={chartDefinition.heightClassName}
style={chartDefinition.heightStyle?.(dataset)}
>
{chartDefinition.render({ dataset, onDrillback })}
</div>
{chartDefinition.showSourceRows && (
<SourceRowButtons dataset={dataset} onDrillback={onDrillback} />
)}
</section>
);
}
function ChartWarningSummary({
dataset,
warningRefs,
}: {
dataset: ParsedChartDataset;
warningRefs: WarningRef[];
}) {
Iif (warningRefs.length === 0) return null;
return (
<div
className="mb-3 space-y-1 rounded-md border border-amber-500/25 bg-amber-500/10 px-2 py-1.5 text-[11px] leading-snug text-amber-700 dark:text-amber-300"
aria-label={`Chart warnings for ${chartTitle(dataset)}`}
>
{warningRefs.map((warning) => (
<div key={`${warning.code}-${warning.message}`} className="flex gap-1">
<span>{warning.message}</span>
</div>
))}
</div>
);
}
function SourceRowButtons({
dataset,
onDrillback,
}: {
dataset: ParsedChartDataset;
onDrillback: DrillbackHandler;
}) {
const sourceRows = chartSourcePoints(dataset)
.map((point) => point.source_row)
.filter((sourceRow): sourceRow is SourceRow => Boolean(sourceRow));
const uniqueRows = Array.from(
new Map(sourceRows.map((row) => [row.id, row])).values(),
);
Iif (uniqueRows.length === 0) return null;
return (
<div className="mt-3 flex flex-wrap gap-2 border-t pt-3">
{uniqueRows.slice(0, 6).map((sourceRow) => (
<Button
key={sourceRow.id}
type="button"
size="sm"
variant="outline"
aria-label={`Show calculation trace for ${sourceRow.label}`}
title={sourceRow.label}
onClick={() => onDrillback(sourceRow.id)}
>
{sourceRow.label}
</Button>
))}
</div>
);
}
|