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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 | 103x 103x | import { skipToken } from "@reduxjs/toolkit/query";
import { Bar, BarChart, CartesianGrid, Label, XAxis, YAxis } from "recharts";
import { useLocalStorage } from "usehooks-ts";
import {
ChartConfig,
ChartContainer,
ChartTooltip,
ChartTooltipContent,
} from "@/ahuora-design-system/ui/chart";
import { Label as FormLabel } from "@/ahuora-design-system/ui/label";
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@/ahuora-design-system/ui/select";
import {
ResultSummary,
ScenarioRead,
useCoreDataRowOutputSummaryRetrieveQuery,
} from "@/api/apiStore.gen";
import { ResultMetricStrip } from "../components/ResultMetricStrip";
import {
ResultEmptyMessage,
ResultLoadingMessage,
ResultSummaryStatusMessage,
} from "../components/ResultStatusMessage";
import { ResultCard, ResultViewGrid } from "../components/ResultViewLayout";
import type { ResultMetricNavigationTarget } from "../utils/resultMetricNavigation";
import {
getResultSummaryStatus,
isResultSummaryPending,
isResultSummaryUnavailable,
} from "../utils/resultSummaryStatus";
const chartConfig = {
count: {
label: "Count",
color: "hsl(var(--chart-1))",
},
} satisfies ChartConfig;
const HISTOGRAM_BIN_OPTIONS = ["auto", "10", "20", "30", "50"] as const;
type HistogramBinCount = (typeof HISTOGRAM_BIN_OPTIONS)[number];
function histogramBinCountLabel(value: HistogramBinCount): string {
return value === "auto" ? "Auto" : value;
}
function buildHistogramBinCountsQuery(
histogramBinCountsByKey: Record<string, HistogramBinCount>,
): string | undefined {
const overrides = Object.entries(histogramBinCountsByKey)
.filter(([, histogramBins]) => histogramBins !== "auto")
.map(([key, histogramBins]) => ({
key,
histogram_bins: histogramBins,
}));
return overrides.length > 0 ? JSON.stringify(overrides) : undefined;
}
function histogramBinSelectId(key: string): string {
return `histogram-bins-${key.replace(/[^a-zA-Z0-9_-]+/g, "-")}`;
}
function updateHistogramBinCount(
currentCounts: Record<string, HistogramBinCount>,
key: string,
value: HistogramBinCount,
): Record<string, HistogramBinCount> {
const nextCounts = { ...currentCounts };
if (value === "auto") {
delete nextCounts[key];
} else {
nextCounts[key] = value;
}
return nextCounts;
}
export default function HistogramResultView({
onMetricTargetSelect,
scenario,
selectedObjectId,
}: {
onMetricTargetSelect?: (target: ResultMetricNavigationTarget) => void;
scenario: ScenarioRead;
selectedObjectId: number | undefined;
}) {
const [histogramBinCountsByKey, setHistogramBinCountsByKey] = useLocalStorage<
Record<string, HistogramBinCount>
>("scenario-histogram-bin-counts", {});
const histogramBinCountsQuery = buildHistogramBinCountsQuery(
histogramBinCountsByKey,
);
const {
data: summaryData,
error: summaryError,
...summaryQuery
} = useCoreDataRowOutputSummaryRetrieveQuery(
selectedObjectId
? {
scenario: scenario.id,
simulationObject: selectedObjectId,
histogramBinCounts: histogramBinCountsQuery,
}
: skipToken,
);
const summaryStatus =
getResultSummaryStatus(summaryData) ?? getResultSummaryStatus(summaryError);
const summaries: ResultSummary[] = Array.isArray(summaryData)
? summaryData
: [];
if (selectedObjectId && isQueryLoading(summaryQuery)) {
return (
<ResultViewGrid selectedObjectId={selectedObjectId}>
<ResultLoadingMessage />
</ResultViewGrid>
);
}
if (
summaryStatus &&
(isResultSummaryPending(summaryStatus) ||
isResultSummaryUnavailable(summaryStatus))
) {
return (
<ResultViewGrid selectedObjectId={selectedObjectId}>
<ResultSummaryStatusMessage status={summaryStatus} />
</ResultViewGrid>
);
}
if (selectedObjectId && summaries.length === 0) {
return (
<ResultViewGrid selectedObjectId={selectedObjectId}>
<ResultEmptyMessage />
</ResultViewGrid>
);
}
return (
<ResultViewGrid selectedObjectId={selectedObjectId}>
{summaries.map((summary) => {
const resultLabel = summary.label ?? summary.key;
const selectedBinCount = histogramBinCountsByKey[summary.key] ?? "auto";
const selectId = histogramBinSelectId(summary.key);
return (
<ResultCard
key={summary.key}
title={resultLabel}
actions={
<div className="flex items-center gap-2">
<FormLabel
htmlFor={selectId}
className="text-xs text-muted-foreground"
>
Bins
</FormLabel>
<Select
value={selectedBinCount}
onValueChange={(value: HistogramBinCount) =>
setHistogramBinCountsByKey((currentCounts) =>
updateHistogramBinCount(
currentCounts,
summary.key,
value,
),
)
}
>
<SelectTrigger
id={selectId}
aria-label={`Bins for ${resultLabel}`}
className="h-8 w-20 rounded-md border px-2 py-1 text-xs"
>
<SelectValue />
</SelectTrigger>
<SelectContent>
{HISTOGRAM_BIN_OPTIONS.map((option) => (
<SelectItem key={option} value={option}>
{histogramBinCountLabel(option)}
</SelectItem>
))}
</SelectContent>
</Select>
</div>
}
>
<ResultMetricStrip
metricTargets={summary.metric_targets}
onMetricTargetSelect={onMetricTargetSelect}
resultKey={summary.key}
resultLabel={resultLabel}
stats={summary.stats}
unit={summary.unit}
/>
{summary.histogram_bins.length > 0 ? (
<ChartContainer config={chartConfig} className="w-full h-[220px]">
<BarChart
data={summary.histogram_bins}
margin={{ bottom: 8, left: 6, right: 8, top: 4 }}
>
<CartesianGrid vertical={false} />
<XAxis
dataKey="label"
height={56}
interval="preserveStartEnd"
minTickGap={8}
tick={{ fontSize: 11 }}
tickMargin={8}
>
<Label
value={
summary.unit
? `Value range (${summary.unit})`
: "Value range"
}
offset={-2}
position="insideBottom"
/>
</XAxis>
<YAxis
allowDecimals={false}
tick={{ fontSize: 11 }}
tickMargin={8}
width={78}
>
<Label
value="Count"
angle={-90}
offset={8}
position="insideLeft"
/>
</YAxis>
<Bar dataKey="count" fill="var(--color-count)" />
<ChartTooltip
cursor={false}
content={<ChartTooltipContent hideLabel />}
/>
</BarChart>
</ChartContainer>
) : (
<p className="text-sm text-muted-foreground">
No numeric values are available for this result.
</p>
)}
</ResultCard>
);
})}
</ResultViewGrid>
);
}
function isQueryLoading(query: {
currentData?: unknown;
isFetching?: boolean;
isLoading?: boolean;
}): boolean {
return Boolean(
query.isLoading || (query.isFetching && query.currentData == null),
);
}
|