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 | 1x 1x 212x 1x 212x 1x 39x 39x 39x 39x 14x 39x 10x 39x 1x 1x 1x 1x 1x 39x 2x 2x 1x 1x 2x 2x 2x 1x 1x 39x 622x | import { memo, useCallback, useEffect, useMemo, useState } from "react";
import type { KeyboardEvent } from "react";
import type { PortfolioRow } from "../../../benchmark/src/data";
import { formatCurrency, formatPercent } from "../../../benchmark/src/data";
import { calculateVirtualWindow } from "../../../benchmark/src/virtualization";
interface VirtualizedDataTableProps {
rows: readonly PortfolioRow[];
onVisibleRowsChange: (count: number) => void;
}
interface VirtualRowProps {
row: PortfolioRow;
selected: boolean;
rowIndex: number;
onToggle: (id: string) => void;
}
const ROW_HEIGHT = 58;
const VIEWPORT_HEIGHT = 580;
function HealthBadge({ health }: { health: PortfolioRow["health"] }) {
return <span className={`health-badge health-${health.toLowerCase()}`}>{health}</span>;
}
const VirtualRow = memo(function VirtualRow({ row, selected, rowIndex, onToggle }: VirtualRowProps) {
return (
<div
className="virtual-row"
role="row"
aria-rowindex={rowIndex}
aria-selected={selected}
data-testid="optimized-row"
onClick={() => onToggle(row.id)}
>
<div className="virtual-cell account-cell" role="cell">
<img src={row.logoUrl} alt="" loading="lazy" decoding="async" />
<span>
<strong>{row.accountName}</strong>
<small>{row.id}</small>
</span>
</div>
<div className="virtual-cell" role="cell">{row.owner}</div>
<div className="virtual-cell" role="cell">{row.region}</div>
<div className="virtual-cell" role="cell">{row.tier}</div>
<div className="virtual-cell" role="cell">
<HealthBadge health={row.health} />
</div>
<div className="virtual-cell" role="cell">{formatCurrency(row.annualContractValue)}</div>
<div className="virtual-cell" role="cell">{formatPercent(row.conversionRate)}</div>
<div className="virtual-cell" role="cell">{row.latencyMs} ms</div>
<div className="virtual-cell" role="cell">{row.riskScore}</div>
</div>
);
});
export function VirtualizedDataTable({ rows, onVisibleRowsChange }: VirtualizedDataTableProps) {
const [scrollTop, setScrollTop] = useState(0);
const [selectedRowIds, setSelectedRowIds] = useState<Set<string>>(() => new Set());
const virtualWindow = useMemo(
() =>
calculateVirtualWindow({
itemCount: rows.length,
rowHeight: ROW_HEIGHT,
viewportHeight: VIEWPORT_HEIGHT,
scrollTop,
overscan: 8
}),
[rows.length, scrollTop]
);
const visibleRows = useMemo(
() => rows.slice(virtualWindow.startIndex, virtualWindow.endIndex),
[rows, virtualWindow.endIndex, virtualWindow.startIndex]
);
useEffect(() => {
onVisibleRowsChange(virtualWindow.visibleCount);
}, [onVisibleRowsChange, virtualWindow.visibleCount]);
const toggleRow = useCallback((id: string) => {
setSelectedRowIds((current) => {
const next = new Set(current);
Iif (next.has(id)) {
next.delete(id);
} else {
next.add(id);
}
return next;
});
}, []);
const handleKeyDown = useCallback((event: KeyboardEvent<HTMLDivElement>) => {
const target = event.currentTarget;
if (event.key === "PageDown") {
target.scrollTop += VIEWPORT_HEIGHT;
event.preventDefault();
}
Iif (event.key === "PageUp") {
target.scrollTop -= VIEWPORT_HEIGHT;
event.preventDefault();
}
Iif (event.key === "Home") {
target.scrollTop = 0;
event.preventDefault();
}
if (event.key === "End") {
target.scrollTop = rows.length * ROW_HEIGHT;
event.preventDefault();
}
}, [rows.length]);
return (
<section className="panel table-panel" aria-labelledby="optimized-table-title">
<div className="panel-header">
<div>
<p className="eyebrow">Virtualized table</p>
<h2 id="optimized-table-title">Account portfolio</h2>
</div>
<span className="table-count">{rows.length.toLocaleString()} rows</span>
</div>
<div className="virtual-table" role="table" aria-label="Account portfolio" aria-rowcount={rows.length + 1}>
<div className="virtual-header" role="rowgroup">
<div className="virtual-row virtual-heading-row" role="row" aria-rowindex={1}>
<div className="virtual-cell" role="columnheader">Account</div>
<div className="virtual-cell" role="columnheader">Owner</div>
<div className="virtual-cell" role="columnheader">Region</div>
<div className="virtual-cell" role="columnheader">Tier</div>
<div className="virtual-cell" role="columnheader">Health</div>
<div className="virtual-cell" role="columnheader">ARR</div>
<div className="virtual-cell" role="columnheader">Conversion</div>
<div className="virtual-cell" role="columnheader">Latency</div>
<div className="virtual-cell" role="columnheader">Risk</div>
</div>
</div>
<div
className="virtual-scroll"
data-table-scroll="optimized"
style={{ height: VIEWPORT_HEIGHT }}
tabIndex={0}
role="rowgroup"
onScroll={(event) => setScrollTop(event.currentTarget.scrollTop)}
onKeyDown={handleKeyDown}
>
<div className="virtual-spacer" style={{ height: virtualWindow.totalHeight }}>
<div className="virtual-window" style={{ transform: `translateY(${virtualWindow.offsetTop}px)` }}>
{visibleRows.map((row, index) => (
<VirtualRow
key={row.id}
row={row}
rowIndex={virtualWindow.startIndex + index + 2}
selected={selectedRowIds.has(row.id)}
onToggle={toggleRow}
/>
))}
</div>
</div>
</div>
</div>
</section>
);
}
|