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 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 | 103x 103x 103x 103x 103x 103x 28x 28x 32x 28x 28x 28x 5x 32x 28x 9x 55x 28x 39x 39x 39x 50x 28x 4x 68x 28x 11x 50x 28x 870x 31x 28x 46x 46x 20x 28x 3x 435x 3x 435x 59x 208x 3x 8x 140x 1220x 140x 3x 8x 8x 8x 77x 8x 77x 8x 25x 25x 16x 22x 53x 77x 4605x 266x 25x 241x 4605x | import { skipToken } from "@reduxjs/toolkit/query";
import type { ColumnDef } from "@tanstack/react-table";
import { X } from "lucide-react";
import type { ReactNode } from "react";
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import { Button } from "@/ahuora-design-system/ui/button";
import { PaginatedTable } from "@/ahuora-design-system/ui/paginated-table";
import {
ResultObjectTableColumn,
ResultObjectTableColumnKind,
ResultObjectTableResponse,
ScenarioRead,
useCoreDataRowObjectTableRetrieveQuery,
} from "@/api/apiStore.gen";
import { cn } from "@/lib/utils";
import type { ResultMetricNavigationTarget } from "../utils/resultMetricNavigation";
const DEFAULT_TABLE_PAGE_SIZE = 20;
const ROW_COLUMN_KEY = "row_index";
const ROW_SEPARATOR_CLASS = "border-r border-border";
const ROW_STICKY_HEADER_CLASS = cn(
"sticky left-0 z-30 bg-muted",
ROW_SEPARATOR_CLASS,
);
const ROW_STICKY_CELL_CLASS = cn(
"sticky left-0 z-10 bg-background",
ROW_SEPARATOR_CLASS,
);
const OUTPUT_SEPARATOR_CLASS = "border-l border-border";
type ResultObjectTableRowData = Record<string, number | string | null>;
type TablePageState = {
selectedObjectId: number | undefined;
page: number;
};
export function ObjectResultsTable({
handleDownload,
highlightedMetricTarget,
objectPicker,
onClearMetricTarget,
scenario,
selectedObjectId,
}: {
handleDownload: () => void;
highlightedMetricTarget: ResultMetricNavigationTarget | null;
objectPicker: ReactNode;
onClearMetricTarget: () => void;
scenario: ScenarioRead;
selectedObjectId: number | undefined;
}) {
const [pageState, setPageState] = useState<TablePageState>({
selectedObjectId,
page: 1,
});
const highlightedRowRef = useRef<HTMLTableRowElement | null>(null);
const page =
pageState.selectedObjectId === selectedObjectId ? pageState.page : 1;
const setPage = useCallback(
(nextPage: number) => {
setPageState({ selectedObjectId, page: nextPage });
},
[selectedObjectId],
);
const { data: tableData } = useCoreDataRowObjectTableRetrieveQuery(
selectedObjectId
? {
page,
scenario: scenario.id,
simulationObject: selectedObjectId,
}
: skipToken,
);
const tablePageSize = tableData?.page_size ?? DEFAULT_TABLE_PAGE_SIZE;
const tableRows = useMemo(() => buildTableRows(tableData), [tableData]);
const tableColumns = useMemo(() => buildTableColumns(tableData), [tableData]);
const paginatedTableData = tableData
? { ...tableData, results: tableRows }
: undefined;
useEffect(() => {
if (!highlightedMetricTarget) {
return;
}
// Metric selections originate in sibling graph/histogram views. The table
// keeps normal pagination after this one-time jump, so deriving `page`
// directly from the target would make it impossible to page away.
setPage(
Math.floor(highlightedMetricTarget.target.row_index / tablePageSize) + 1,
);
}, [highlightedMetricTarget, setPage, tablePageSize]);
useEffect(() => {
if (!highlightedMetricTarget) {
return;
}
// The highlighted row is produced by TanStack Table after data arrives; the
// scroll must wait until React has attached the row ref to the DOM.
const animationFrame = requestAnimationFrame(() => {
highlightedRowRef.current?.scrollIntoView({
behavior: "smooth",
block: "center",
inline: "nearest",
});
});
return () => cancelAnimationFrame(animationFrame);
}, [highlightedMetricTarget, tableRows]);
function isHighlightedTableRow(row: ResultObjectTableRowData) {
if (!highlightedMetricTarget) {
return false;
}
return row[ROW_COLUMN_KEY] === highlightedMetricTarget.target.row_index;
}
const tableToolbarStart = (
<div className="flex min-w-0 flex-wrap items-center gap-2">
{objectPicker}
{highlightedMetricTarget && (
<div className="inline-flex max-w-full items-center gap-2 rounded-md border bg-muted/40 px-3 py-1.5 text-sm text-muted-foreground">
<p className="min-w-0 truncate">
<span className="font-medium">
{highlightedMetricTarget.resultLabel}{" "}
{highlightedMetricTarget.metricLabel}
</span>
{": "}
{tableData?.row_label.toLowerCase() ?? "row"}{" "}
{highlightedMetricTarget.target.row_index}
</p>
<Button
variant="ghost"
size="icon"
className="h-6 w-6 shrink-0 text-muted-foreground hover:text-foreground"
aria-label="Clear highlighted result row"
onClick={onClearMetricTarget}
>
<X className="h-3.5 w-3.5" />
</Button>
</div>
)}
</div>
);
return (
<div className="flex h-full min-h-0 min-w-0 flex-col overflow-hidden">
<PaginatedTable
columns={tableColumns}
data={paginatedTableData}
page={page}
setPage={setPage}
className="pt-0"
title={
scenario.enable_dynamics
? "Dynamic results"
: "Multi-steady-state results"
}
hideTitle
ariaLabel="mss-results-table"
handleDownload={handleDownload}
toolbarStart={tableToolbarStart}
columnSelection
getRowClassName={(row: ResultObjectTableRowData) =>
isHighlightedTableRow(row)
? cn(
"bg-primary/15 shadow-[inset_3px_0_0_hsl(var(--primary))]",
"hover:bg-primary/20 [&>td:first-child]:bg-primary/15 hover:[&>td:first-child]:bg-primary/20",
)
: undefined
}
getRowRef={(row: ResultObjectTableRowData) =>
isHighlightedTableRow(row)
? (node) => {
highlightedRowRef.current = node;
}
: undefined
}
/>
</div>
);
}
function buildTableRows(
tableData: ResultObjectTableResponse | undefined,
): ResultObjectTableRowData[] {
if (!tableData) {
return [];
}
return tableData.rows.map((row) => {
const rowData: ResultObjectTableRowData = {
[ROW_COLUMN_KEY]: row.row_index,
};
for (const cell of row.cells) {
rowData[cell.column_key] = cell.value;
}
return rowData;
});
}
function buildTableColumns(
tableData: ResultObjectTableResponse | undefined,
): ColumnDef<ResultObjectTableRowData, unknown>[] {
if (!tableData) {
return [];
}
const rowColumn = tableData.columns.find(
(column) => column.kind === ResultObjectTableColumnKind.Row,
);
const inputColumns = tableData.columns.filter((column) =>
[
ResultObjectTableColumnKind.Input,
ResultObjectTableColumnKind.InputOutput,
].includes(column.kind),
);
const outputColumns = tableData.columns.filter(
(column) => column.kind === ResultObjectTableColumnKind.Output,
);
return [
{
id: "row",
header: () => null,
meta: {
headerClassName: ROW_STICKY_HEADER_CLASS,
},
columns: [
resultTableColumn(rowColumn ?? tableData.columns[0], {
cellClassName: ROW_STICKY_CELL_CLASS,
headerClassName: ROW_STICKY_HEADER_CLASS,
}),
],
},
...(inputColumns.length > 0
? [
{
id: "inputs",
header: () => "Inputs",
columns: inputColumns.map((column) => resultTableColumn(column)),
},
]
: []),
...(outputColumns.length > 0
? [
{
id: "outputs",
header: () => "Outputs",
meta: {
headerClassName: OUTPUT_SEPARATOR_CLASS,
},
columns: outputColumns.map((column, index) =>
resultTableColumn(column, {
cellClassName: index === 0 ? OUTPUT_SEPARATOR_CLASS : undefined,
headerClassName:
index === 0 ? OUTPUT_SEPARATOR_CLASS : undefined,
}),
),
},
]
: []),
];
}
function resultTableColumn(
column: ResultObjectTableColumn,
meta?: {
cellClassName?: string;
headerClassName?: string;
},
): ColumnDef<ResultObjectTableRowData, unknown> {
return {
accessorKey: column.key,
cell: ({ getValue }) => formatResultTableValue(getValue()),
header: () => columnHeaderLabel(column),
meta,
};
}
function columnHeaderLabel(column: ResultObjectTableColumn): string {
if (!column.unit) {
return column.label;
}
return `${column.label} (${column.unit})`;
}
function formatResultTableValue(value: unknown): string {
if (value === null || value === undefined || value === "") {
return "";
}
if (typeof value !== "number" || !Number.isFinite(value)) {
return String(value);
}
return Number.isInteger(value)
? value.toLocaleString()
: value.toLocaleString(undefined, {
maximumFractionDigits: 4,
});
}
|