Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

Preserve keyed rows across snapshot reordering

pulse-triage[bot]40f2c12bf45a
diff --git a/frontend-modern/src/features/platformPage/PlatformWindowedRows.tsx b/frontend-modern/src/features/platformPage/PlatformWindowedRows.tsx
index 7ebaf3aecc..f230e80b6c 100644
--- a/frontend-modern/src/features/platformPage/PlatformWindowedRows.tsx
+++ b/frontend-modern/src/features/platformPage/PlatformWindowedRows.tsx
@@ -27,8 +27,10 @@ export interface PlatformWindowedRowsProps<Row> {
}
type StablePlatformRow<Row> = {
+ __platformWindowStableRow: true;
__platformWindowKey: string | number;
- value: Row;
+ value: Accessor<Row>;
+ update: (value: Row) => void;
};
const defaultRowKey = <Row,>(item: Row): string | number | undefined => {
@@ -37,18 +39,42 @@ const defaultRowKey = <Row,>(item: Row): string | number | undefined => {
return typeof id === 'string' || typeof id === 'number' ? id : undefined;
};
-const buildStableRows = <Row,>(
+const stableRowKeys = <Row,>(
items: readonly Row[],
keyExtractor?: (item: Row) => string | number,
-): StablePlatformRow<Row>[] | undefined => {
+): (string | number)[] | undefined => {
const keys = items.map((item) => keyExtractor?.(item) ?? defaultRowKey(item));
if (keys.some((key) => key === undefined) || new Set(keys).size !== keys.length) return undefined;
- return items.map((value, index) => ({
- __platformWindowKey: keys[index]!,
+ return keys as (string | number)[];
+};
+
+const createStableRow = <Row,>(key: string | number, initialValue: Row): StablePlatformRow<Row> => {
+ if (typeof initialValue === 'object' && initialValue !== null) {
+ const [value, setValue] = createStore(initialValue);
+ return {
+ __platformWindowStableRow: true,
+ __platformWindowKey: key,
+ value: () => value,
+ update: (nextValue) => {
+ if (typeof nextValue === 'object' && nextValue !== null) {
+ setValue(reconcile(nextValue));
+ }
+ },
+ };
+ }
+
+ const [value, setValue] = createSignal(initialValue);
+ return {
+ __platformWindowStableRow: true,
+ __platformWindowKey: key,
value,
- }));
+ update: (nextValue) => setValue(() => nextValue),
+ };
};
+const isStableRow = <Row,>(item: Row | StablePlatformRow<Row>): item is StablePlatformRow<Row> =>
+ typeof item === 'object' && item !== null && '__platformWindowStableRow' in item;
+
/**
* Canonical bounded renderer for ordinary platform table rows.
*
@@ -57,24 +83,47 @@ const buildStableRows = <Row,>(
* prewarms the runway while touch input remains compositor-native.
*/
export function PlatformWindowedRows<Row>(props: PlatformWindowedRowsProps<Row>) {
- const initialStableRows = buildStableRows(props.items(), props.keyExtractor);
- const [stableRows, setStableRows] = createStore<StablePlatformRow<Row>[]>(
- initialStableRows ?? [],
- );
- const [usesStableRows, setUsesStableRows] = createSignal(initialStableRows !== undefined);
+ const rowsByKey = new Map<string | number, StablePlatformRow<Row>>();
+ const initialItems = props.items();
+ const initialKeys = stableRowKeys(initialItems, props.keyExtractor);
+ const initialRows = initialKeys?.map((key, index) => {
+ const row = createStableRow<Row>(key, initialItems[index] as Row);
+ rowsByKey.set(key, row);
+ return row;
+ });
+ const [stableRows, setStableRows] = createStore<StablePlatformRow<Row>[]>(initialRows ?? []);
+ const [usesStableRows, setUsesStableRows] = createSignal(initialRows !== undefined);
createEffect(() => {
- const next = buildStableRows(props.items(), props.keyExtractor);
- if (!next) {
+ const items = props.items();
+ const keys = stableRowKeys(items, props.keyExtractor);
+ if (!keys) {
+ rowsByKey.clear();
setUsesStableRows(false);
return;
}
- setStableRows(reconcile(next, { key: '__platformWindowKey' }));
+
+ const activeKeys = new Set(keys);
+ const nextRows = items.map((item, index) => {
+ const key = keys[index]!;
+ const existing = rowsByKey.get(key);
+ if (existing) {
+ existing.update(item);
+ return existing;
+ }
+ const created = createStableRow<Row>(key, item);
+ rowsByKey.set(key, created);
+ return created;
+ });
+ for (const key of rowsByKey.keys()) {
+ if (!activeKeys.has(key)) rowsByKey.delete(key);
+ }
+ setStableRows(reconcile(nextRows, { key: '__platformWindowKey' }));
setUsesStableRows(true);
});
- const renderItems = createMemo<readonly Row[]>(() =>
- usesStableRows() ? stableRows.map((stableRow) => stableRow.value) : props.items(),
+ const renderItems = createMemo<readonly (Row | StablePlatformRow<Row>)[]>(() =>
+ usesStableRows() ? stableRows : props.items(),
);
const windowing = usePlatformWindowedItems({
items: renderItems,
@@ -83,8 +132,12 @@ export function PlatformWindowedRows<Row>(props: PlatformWindowedRowsProps<Row>)
windowSize: props.windowSize,
});
- const renderRows = (items: readonly Row[], globalOffset: number) => (
- <For each={items}>{(item, index) => props.children(item, () => globalOffset + index())}</For>
+ const renderRows = (items: readonly (Row | StablePlatformRow<Row>)[], globalOffset: number) => (
+ <For each={items}>
+ {(item, index) =>
+ props.children(isStableRow(item) ? item.value() : item, () => globalOffset + index())
+ }
+ </For>
);
return (
diff --git a/frontend-modern/src/features/platformPage/__tests__/PlatformWindowedRows.test.tsx b/frontend-modern/src/features/platformPage/__tests__/PlatformWindowedRows.test.tsx
index ccdc8dde4f..7c466a95ae 100644
--- a/frontend-modern/src/features/platformPage/__tests__/PlatformWindowedRows.test.tsx
+++ b/frontend-modern/src/features/platformPage/__tests__/PlatformWindowedRows.test.tsx
@@ -83,10 +83,38 @@ describe('PlatformWindowedRows', () => {
setItems([{ id: 'node-a', label: 'Refreshed snapshot' }]);
expect(await screen.findByText('Refreshed snapshot')).toBeInTheDocument();
- expect(screen.getByRole('textbox', { name: 'Row-local state' })).toBe(input);
- expect(input).toHaveValue('still editing');
expect(mounts).toBe(1);
expect(disposals).toBe(0);
+ expect(screen.getByRole('textbox', { name: 'Row-local state' })).toBe(input);
+ expect(input).toHaveValue('still editing');
+ });
+
+ it('renders keyed rows once in their latest order', async () => {
+ const [items, setItems] = createSignal([
+ { key: 'alpha', label: 'Alpha' },
+ { key: 'mike', label: 'Mike' },
+ { key: 'zulu', label: 'Zulu' },
+ ]);
+ const { container } = render(() => (
+ <table>
+ <tbody>
+ <PlatformWindowedRows items={items} keyExtractor={(item) => item.key}>
+ {(item) => <tr data-row-key={item.key}>{item.label}</tr>}
+ </PlatformWindowedRows>
+ </tbody>
+ </table>
+ ));
+
+ setItems([
+ { key: 'zulu', label: 'Zulu refreshed' },
+ { key: 'mike', label: 'Mike refreshed' },
+ { key: 'alpha', label: 'Alpha refreshed' },
+ ]);
+
+ expect(await screen.findByText('Zulu refreshed')).toBeInTheDocument();
+ expect([...container.querySelectorAll('[data-row-key]')].map((row) => row.textContent)).toEqual(
+ ['Zulu refreshed', 'Mike refreshed', 'Alpha refreshed'],
+ );
});
it('keeps estate-sized card lists within their configured mounted-item budget', () => {