` for devices whose last update is older than `STALE_THRESHOLD_MS` (default 60s). Map layers reading this Set apply a faded-icon variant. Or: a separate symbol layer renders a "warning" badge over stale devices' markers.
## Specification
### ``
```tsx
export function ConnectionChip() {
const status = useConnectionStore((s) => s.status);
const lastConnectedAt = useConnectionStore((s) => s.lastConnectedAt);
if (status === 'connected') {
return (
Live
);
}
if (status === 'connecting' || status === 'reconnecting') {
return (
{status === 'connecting' ? 'Connecting…' : 'Reconnecting…'}
);
}
return (
Offline {lastConnectedAt ? `· last live ${formatLastSeen(lastConnectedAt)}` : ''}
);
}
```
Position: top-right of the monitor route, in a small horizontal bar with the basemap switcher and other controls. Inline-flex.
### Per-device last-seen — two strategies
**Strategy A: Faded marker.** A separate symbol layer with a `'icon-opacity'` expression based on a `staleSec` property on each feature:
```ts
'icon-opacity': [
'interpolate', ['linear'], ['get', 'staleSec'],
0, 1.0, // fresh
60, 1.0, // 60s — full opacity
300, 0.4, // 5min — faded
1800, 0.2, // 30min — very faded
],
```
The position store includes `staleSec` in each feature's properties at coalescer-flush time.
**Strategy B: Badge / overlay.** A second symbol layer drawing a small "no-signal" icon over stale markers, filtered to `['>=', ['get', 'staleSec'], 60]`.
**Pick A for v1.** Simpler, fewer layers, cleaner visually. B can layer on top later if A isn't legible enough.
### Updating `staleSec`
Recomputed every second by a setInterval inside the position store (not on every position update — that would invalidate `latestByDevice` constantly):
```ts
// In src/live/position-store.ts
let staleTickerId: ReturnType | null = null;
function startStaleTicker() {
if (staleTickerId) return;
staleTickerId = setInterval(() => {
const now = Date.now();
set((state) => {
// Only bump versionTick if any device's stale-bucket changed.
// ... or just trigger subscribers wholesale every second.
return { stalenessTick: now };
});
}, 1000);
}
```
Map layers re-derive `staleSec` from `latestByDevice` + `stalenessTick` on every flush. The store doesn't mutate position data; it just bumps a version tick that triggers selectors that compute `staleSec` themselves.
### Logout / unmount cleanup
The stale ticker runs as long as the SPA is mounted. Stop on logout (status flips to anonymous) or when navigating away from `/monitor`. Use a `useEffect` in ``:
```tsx
useEffect(() => {
startStaleTicker();
return () => stopStaleTicker();
}, []);
```
### What this task does NOT include
- **Loud "device offline!" notifications.** No toast, no modal. The faded icon is the signal. If operators need louder, Phase 3.4's per-device detail panel can add an alert badge.
- **Per-event "X devices offline" summary.** Phase 3 polish.
- **Auto-recovery testing for the WS reconnect.** That's covered by 2.4's acceptance.
- **Notification API integration.** Browser push on disconnect — Phase 4.
## Acceptance criteria
- [ ] `pnpm typecheck`, `pnpm lint`, `pnpm format:check`, `pnpm build` clean.
- [ ] On `/monitor`, the connection chip top-right shows "Live" with a green dot during normal operation.
- [ ] Disconnecting the network: the chip flips to "Reconnecting…" within a few seconds; on reconnect, back to "Live"; on permanent disconnect (close + no reconnect for 30s+), shows "Offline · last live HH:MM:SS".
- [ ] A device that hasn't reported in 5+ minutes appears noticeably faded on the map (not invisible — operators still see _where it was last seen_).
- [ ] On a fresh page load with no positions yet, the chip says "Connecting…" briefly, then "Live" once the snapshot arrives.
- [ ] No banner / toast spam during normal operation.
## Risks / open questions
- **Threshold tuning.** 60s = "fresh", 5min = "fading", 30min = "stale". For Teltonika devices reporting at 1Hz this is generous; for 0.2Hz devices the 60s window catches a device that just paused at a checkpoint. Watch dogfood data and adjust.
- **Setinterval ticker accuracy.** `setInterval(1000ms)` is approximate; on a slow tab it can drift. The visual effect doesn't need wall-clock precision — "this marker is faded because it's been a while" is still correct even if the timer drifted by a few seconds.
- **Race with reconnect on offline.** If the SPA is offline and reconnects, the snapshot replays; some markers' last-seen will jump from "stale" to "fresh" in one frame. Verify the visual transition is clean (no flicker).
- **Connection chip during boot.** Right after login, the WS hasn't connected yet — chip says "Connecting…". Should this be its own state, or just folded into "reconnecting"? Spec above keeps `connecting` distinct for clarity; either is defensible.
## Done
(Filled in when the task lands.)