mirror of
https://github.com/BigBodyCobain/Shadowbroker.git
synced 2026-12-06 09:34:09 +01:00
Merge branch 'feat/public-viewer' into feat/viewer-skill
# Conflicts: # viewer/README.md
This commit is contained in:
commit
ed61456eb1
4 files changed
+129
-8
No files matched your search
+1
-1
@@ -133,7 +133,7 @@ Each failure names the file. Fix the cut, stand-in or allowlist, and rebuild.
|
||||
```bash
|
||||
python -m pytest viewer/bridge -q
|
||||
python -m pytest openclaw-skills/shadowbroker-public -q
|
||||
node --test viewer/web/tests/guardRules.test.ts
|
||||
node --test viewer/web/tests/guardRules.test.ts viewer/web/tests/layerPrefs.test.ts
|
||||
node viewer/web/build.mjs # ends with the built-site inspection
|
||||
```
|
||||
|
||||
|
||||
@@ -38,6 +38,7 @@ import { useWatchlist } from '@/hooks/useWatchlist';
|
||||
import { useTranslation } from '@/i18n';
|
||||
import { LocateBar } from './LocateBar';
|
||||
import { VIEWER_HIDDEN_LAYERS } from '@/viewer/config';
|
||||
import { layersOnLoad, layersToStore, readLayerRev, withHiddenOff, writeLayerRev } from '@/viewer/layerPrefs';
|
||||
import { AboutModal, StaleBanner, ViewerHeaderCard, ViewerToolsPanel } from '@/viewer/ViewerChrome';
|
||||
import SkillModal from '@/viewer/SkillModal';
|
||||
|
||||
@@ -47,11 +48,7 @@ const MaplibreViewer = dynamic(() => import('@/components/MaplibreViewer'), { ss
|
||||
const VIEWER_STYLES: MapStyle[] = ['DEFAULT', 'SATELLITE'];
|
||||
|
||||
function viewerLayers(layers: ActiveLayers): ActiveLayers {
|
||||
const next = { ...layers };
|
||||
for (const key of VIEWER_HIDDEN_LAYERS) {
|
||||
if (key in next) (next as Record<string, boolean>)[key] = false;
|
||||
}
|
||||
return next;
|
||||
return withHiddenOff(layers, VIEWER_HIDDEN_LAYERS);
|
||||
}
|
||||
|
||||
function viewerStyle(style: MapStyle): MapStyle {
|
||||
@@ -130,13 +127,15 @@ export default function ViewerDashboard() {
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
setActiveLayersRaw(viewerLayers(loadActiveLayers()));
|
||||
setActiveLayersRaw(layersOnLoad(getDefaultActiveLayers(), loadActiveLayers(), VIEWER_HIDDEN_LAYERS, readLayerRev()));
|
||||
setActiveStyle(viewerStyle(loadMapStyle()));
|
||||
setActiveFilters(loadActiveFilters());
|
||||
setPrefsHydrated(true);
|
||||
}, []);
|
||||
useEffect(() => {
|
||||
if (prefsHydrated) saveActiveLayers(activeLayers);
|
||||
if (!prefsHydrated) return;
|
||||
saveActiveLayers(layersToStore(activeLayers, VIEWER_HIDDEN_LAYERS));
|
||||
writeLayerRev();
|
||||
}, [activeLayers, prefsHydrated]);
|
||||
useEffect(() => {
|
||||
if (prefsHydrated) saveMapStyle(activeStyle);
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
/**
|
||||
* Which layer switches the viewer keeps in the visitor's browser.
|
||||
*
|
||||
* Switches the viewer never shows are forced off on screen. They must not be
|
||||
* saved that way: if such a layer is published later, a saved "off" that the
|
||||
* visitor never chose would keep it dark for everyone who had visited before.
|
||||
*
|
||||
* No imports, so the rules can be tested without a browser.
|
||||
*/
|
||||
|
||||
/** Raise when a layer moves from hidden to shown; the list below says which. */
|
||||
export const VIEWER_LAYER_REV = 2;
|
||||
export const VIEWER_LAYER_REV_KEY = 'sb_viewer_layer_rev';
|
||||
|
||||
/**
|
||||
* Layers that were hidden in an earlier viewer build and are shown now.
|
||||
* Browsers that saved switches under an older build get the default back for
|
||||
* these, once.
|
||||
*/
|
||||
export const VIEWER_RESTORED_LAYERS: readonly string[] = ['cctv', 'kiwisdr', 'scanners', 'sigint_aprs'];
|
||||
|
||||
type Switches = Record<string, boolean>;
|
||||
|
||||
/** The switches as shown: anything the viewer hides is off. */
|
||||
export function withHiddenOff<T extends object>(layers: T, hidden: ReadonlySet<string>): T {
|
||||
const next = { ...layers } as Switches;
|
||||
for (const key of hidden) {
|
||||
if (key in next) next[key] = false;
|
||||
}
|
||||
return next as T;
|
||||
}
|
||||
|
||||
/** The switches as saved: hidden ones are left out, so they fall back to the default. */
|
||||
export function layersToStore<T extends object>(layers: T, hidden: ReadonlySet<string>): T {
|
||||
const next = { ...layers } as Switches;
|
||||
for (const key of hidden) delete next[key];
|
||||
return next as T;
|
||||
}
|
||||
|
||||
/** The switches on page load, from the defaults and whatever this browser saved. */
|
||||
export function layersOnLoad<T extends object>(
|
||||
defaults: T,
|
||||
saved: T,
|
||||
hidden: ReadonlySet<string>,
|
||||
savedRev: number,
|
||||
restored: readonly string[] = VIEWER_RESTORED_LAYERS,
|
||||
): T {
|
||||
const next = { ...saved } as Switches;
|
||||
if (!(savedRev >= VIEWER_LAYER_REV)) {
|
||||
const base = defaults as Switches;
|
||||
for (const key of restored) {
|
||||
if (key in base) next[key] = base[key];
|
||||
}
|
||||
}
|
||||
return withHiddenOff(next as T, hidden);
|
||||
}
|
||||
|
||||
export function readLayerRev(): number {
|
||||
try {
|
||||
return Number(localStorage.getItem(VIEWER_LAYER_REV_KEY)) || 0;
|
||||
} catch {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
export function writeLayerRev(): void {
|
||||
try {
|
||||
localStorage.setItem(VIEWER_LAYER_REV_KEY, String(VIEWER_LAYER_REV));
|
||||
} catch {
|
||||
/* private mode */
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
/**
|
||||
* Tests for which layer switches the viewer saves and restores.
|
||||
* Run: node --test viewer/web/tests/layerPrefs.test.ts
|
||||
*/
|
||||
|
||||
import assert from 'node:assert/strict';
|
||||
import { test } from 'node:test';
|
||||
import {
|
||||
VIEWER_LAYER_REV,
|
||||
layersOnLoad,
|
||||
layersToStore,
|
||||
withHiddenOff,
|
||||
} from '../overlay/src/viewer/layerPrefs.ts';
|
||||
|
||||
const HIDDEN = new Set(['sar', 'ai_intel']);
|
||||
const DEFAULTS = { flights: true, cctv: false, sigint_aprs: true, sar: true, ai_intel: true };
|
||||
|
||||
test('hidden layers are shown as off', () => {
|
||||
const shown = withHiddenOff(DEFAULTS, HIDDEN);
|
||||
assert.equal(shown.sar, false);
|
||||
assert.equal(shown.ai_intel, false);
|
||||
assert.equal(shown.flights, true);
|
||||
});
|
||||
|
||||
test('hidden layers are never saved', () => {
|
||||
const stored = layersToStore(withHiddenOff(DEFAULTS, HIDDEN), HIDDEN) as Record<string, boolean>;
|
||||
assert.equal('sar' in stored, false);
|
||||
assert.equal('ai_intel' in stored, false);
|
||||
assert.equal(stored.flights, true);
|
||||
assert.equal(stored.sigint_aprs, true);
|
||||
});
|
||||
|
||||
test('a forced "off" saved by an older build goes back to the default', () => {
|
||||
const saved = { ...DEFAULTS, sigint_aprs: false, flights: false };
|
||||
const loaded = layersOnLoad(DEFAULTS, saved, HIDDEN, 0, ['sigint_aprs']);
|
||||
assert.equal(loaded.sigint_aprs, true, 'restored to default');
|
||||
assert.equal(loaded.flights, false, "the visitor's own choice is kept");
|
||||
assert.equal(loaded.sar, false, 'still hidden');
|
||||
});
|
||||
|
||||
test('after the one-time restore, a visitor who switches the layer off keeps it off', () => {
|
||||
const saved = { ...DEFAULTS, sigint_aprs: false };
|
||||
const loaded = layersOnLoad(DEFAULTS, saved, HIDDEN, VIEWER_LAYER_REV, ['sigint_aprs']);
|
||||
assert.equal(loaded.sigint_aprs, false);
|
||||
});
|
||||
|
||||
test('a layer that is hidden stays off even if it is on the restore list', () => {
|
||||
const loaded = layersOnLoad(DEFAULTS, { ...DEFAULTS }, HIDDEN, 0, ['sar']);
|
||||
assert.equal(loaded.sar, false);
|
||||
});
|
||||
Reference in new issue
Block a user