wired-tools: hoist monitorSnapshot + polling reset to the Zustand store

Move the monitor snapshot off WiredCreatorToolsView's useState into
useWiredCreatorToolsUiStore. The WiredMonitorDataEvent listener still
lives in the component (it can't move alongside without dragging
useMessageEvent into the store), but it now writes to setMonitorSnapshot
and the room-change reset calls resetMonitorSnapshot() instead of
re-instantiating the default in the component.

Direct benefit: the snapshot now survives closing and reopening the
panel between two server pushes. Before this commit, the parent
remounted on every visibility flip (parent renders null while
`!isVisible`) which dropped the snapshot back to the empty default;
the user would briefly see zeroed stats until the next `monitor:fetch`
roundtrip landed. Holding the snapshot in zustand decouples the data
from the component's mount lifecycle.

Tests: three new cases on the store cover setMonitorSnapshot,
resetMonitorSnapshot returning a fresh empty instance, and the
"close/reopen panel preserves snapshot" lifecycle. Total 181/181.
This commit is contained in:
simoleo89
2026-05-16 12:20:35 +02:00
parent 803de20dfe
commit 82bccd4040
3 changed files with 83 additions and 7 deletions
@@ -1,4 +1,5 @@
import { beforeEach, describe, expect, it } from 'vitest';
import { createEmptyMonitorSnapshot } from './WiredCreatorTools.helpers';
import { useWiredCreatorToolsUiStore } from './wiredCreatorToolsUiStore';
const INITIAL = {
@@ -15,7 +16,8 @@ const INITIAL = {
monitorHistoryTypeFilter: 'ALL',
variableManageTypeFilter: 'ALL',
variableManageSort: 'highest_value',
variableManagePage: 1
variableManagePage: 1,
monitorSnapshot: createEmptyMonitorSnapshot()
};
describe('useWiredCreatorToolsUiStore', () =>
@@ -43,6 +45,7 @@ describe('useWiredCreatorToolsUiStore', () =>
expect(state.variableManageTypeFilter).toBe('ALL');
expect(state.variableManageSort).toBe('highest_value');
expect(state.variableManagePage).toBe(1);
expect(state.monitorSnapshot).toEqual(createEmptyMonitorSnapshot());
});
describe('setIsVisible', () =>
@@ -177,4 +180,57 @@ describe('useWiredCreatorToolsUiStore', () =>
expect(useWiredCreatorToolsUiStore.getState().variableManagePage).toBe(2);
});
});
describe('monitorSnapshot', () =>
{
it('setMonitorSnapshot replaces the snapshot with the server payload shape', () =>
{
const next = {
...createEmptyMonitorSnapshot(),
usageCurrentWindow: 7,
usageLimitPerWindow: 10,
isHeavy: true,
averageExecutionMs: 42
};
useWiredCreatorToolsUiStore.getState().setMonitorSnapshot(next);
expect(useWiredCreatorToolsUiStore.getState().monitorSnapshot).toEqual(next);
expect(useWiredCreatorToolsUiStore.getState().monitorSnapshot.isHeavy).toBe(true);
});
it('resetMonitorSnapshot returns a fresh empty snapshot (new reference)', () =>
{
const populated = {
...createEmptyMonitorSnapshot(),
usageCurrentWindow: 5,
logs: [ { amount: 1, latestOccurrenceSeconds: 0, latestReason: '', latestSourceId: 0, latestSourceLabel: '', severity: 'ERROR', type: 'foo' } ],
history: [ { occurredAtSeconds: 0, reason: '', sourceId: 0, sourceLabel: '', severity: 'ERROR', type: 'foo' } ]
};
useWiredCreatorToolsUiStore.getState().setMonitorSnapshot(populated);
useWiredCreatorToolsUiStore.getState().resetMonitorSnapshot();
const cleared = useWiredCreatorToolsUiStore.getState().monitorSnapshot;
expect(cleared).toEqual(createEmptyMonitorSnapshot());
expect(cleared).not.toBe(populated);
expect(cleared.logs).toEqual([]);
expect(cleared.history).toEqual([]);
});
it('the snapshot persists across the panel close/reopen lifecycle (UI flag flip)', () =>
{
// Server pushed a non-empty snapshot while the panel was open.
const payload = { ...createEmptyMonitorSnapshot(), usageCurrentWindow: 3 };
useWiredCreatorToolsUiStore.getState().setMonitorSnapshot(payload);
// User closes the panel — UI flag flips, snapshot should NOT reset.
useWiredCreatorToolsUiStore.getState().setIsVisible(false);
// User reopens — the last-known stats are still there.
useWiredCreatorToolsUiStore.getState().setIsVisible(true);
expect(useWiredCreatorToolsUiStore.getState().monitorSnapshot.usageCurrentWindow).toBe(3);
});
});
});