Files
sencho/frontend/src/hooks/useTopologyPreferences.ts
T
Anso 2e82eb44fe feat(fleet): multi-mode topology with label grouping and persisted positions (#1054)
The Topology view now offers three layouts so the canvas adapts to how the
fleet is organised, not the other way around:

- Hub: the gateway anchored on the left with remotes radiating right
- Grouped (Skipper+): remotes cluster by their primary node label, with the
  local node in its own cluster and unlabeled remotes in an Unlabeled cluster
- Free (Skipper+): drag any node; positions persist per browser via local
  storage

Node cards gain label pills (Skipper+), a cordon banner with reason tooltip,
a latency chip for online remotes, and a stale pilot-heartbeat glyph. All
fields are sourced from /api/fleet/overview, which already returns cordon,
latency, and pilot timestamps; no backend changes required.

Community continues to see the single Hub layout (without the toolbar, no
gating cues), matching the visibility principle that paid affordances are
hidden from lower tiers rather than displayed as locked teasers. The
backend /api/node-labels route is already requirePaid, so the data gate is
honoured end to end.

Includes unit tests for the layout module (hub/grouped/free coverage) and
the preferences hook (round-trip plus corrupt-JSON fallback).
2026-05-15 08:59:52 -04:00

76 lines
2.3 KiB
TypeScript

import { useCallback, useEffect, useState } from 'react';
import type { LayoutMode, SavedPositions } from '@/lib/fleet-topology-layout';
const PREFS_KEY = 'sencho-topology-preferences';
export interface TopologyPreferences {
mode: LayoutMode;
positions: SavedPositions;
}
const DEFAULT_PREFS: TopologyPreferences = {
mode: 'hub',
positions: {},
};
function isLayoutMode(value: unknown): value is LayoutMode {
return value === 'hub' || value === 'grouped' || value === 'free';
}
function isSavedPositions(value: unknown): value is SavedPositions {
if (!value || typeof value !== 'object') return false;
for (const v of Object.values(value as Record<string, unknown>)) {
if (!v || typeof v !== 'object') return false;
const p = v as { x?: unknown; y?: unknown };
if (typeof p.x !== 'number' || typeof p.y !== 'number') return false;
}
return true;
}
function loadPreferences(): TopologyPreferences {
try {
const stored = localStorage.getItem(PREFS_KEY);
if (!stored) return { ...DEFAULT_PREFS };
const parsed = JSON.parse(stored) as Partial<TopologyPreferences>;
return {
mode: isLayoutMode(parsed.mode) ? parsed.mode : DEFAULT_PREFS.mode,
positions: isSavedPositions(parsed.positions) ? parsed.positions : {},
};
} catch {
return { ...DEFAULT_PREFS };
}
}
function savePreferences(prefs: TopologyPreferences) {
try {
localStorage.setItem(PREFS_KEY, JSON.stringify(prefs));
} catch {
/* non-fatal: localStorage unavailable or quota exceeded */
}
}
export function useTopologyPreferences() {
const [prefs, setPrefs] = useState<TopologyPreferences>(loadPreferences);
useEffect(() => {
savePreferences(prefs);
}, [prefs]);
const setMode = useCallback((mode: LayoutMode) => {
setPrefs(prev => (prev.mode === mode ? prev : { ...prev, mode }));
}, []);
const setPositions = useCallback((positions: SavedPositions) => {
setPrefs(prev => ({ ...prev, positions }));
}, []);
const updatePositions = useCallback(
(updater: (current: SavedPositions) => SavedPositions) => {
setPrefs(prev => ({ ...prev, positions: updater(prev.positions) }));
},
[],
);
return { prefs, setMode, setPositions, updatePositions };
}