Files
gods-eye-view/mobile/ui/globeHost.js
T
Claude 52de186005 feat(mobile): add the mobile app shell, capture and the /mobile/ build page
Five tabs (Now, Globe, Capture, Threads, You) over one persistent globe:
a live situation feed whose cards open into the world with a detented
brief, the lens dock and time ribbon, AR Look and signed Witness
capture (ECDSA P-256 in IndexedDB, SHA-256 hash chain, IMU and GPS
trace, eight-check verify), the Bhote Koshi reconstruction thread with
a live ledger, and hinge-aware two-pane layouts for foldables.

The production build now emits mobile/index.html beside the app; the
panel build keeps the app alone.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01VVYRAqiQj9Y1V9ZfG1RcDH
2026-10-18 05:40:33 +00:00

184 lines
6.0 KiB
JavaScript

/**
* Mounts the one persistent globe behind the shell.
*
* Prefers the real Cesium engine (mobile/globe/index.js, DESIGN.md §10) and
* falls back to the offline stub so the shell is always demoable. Every call
* goes through a guard so a missing method or a throwing engine never takes
* the shell down; mismatches are collected in `contractReport` for the lead.
*/
const REQUIRED = [
'flyTo',
'home',
'layers',
'setLayer',
'setFamily',
'setTime',
'timeWindow',
'setLens',
'focus',
'select',
'on',
'off',
'pause',
'resume',
'contactsNear',
'situations',
];
export const contractReport = {
engine: 'none',
missing: [],
errors: [],
notes: [],
};
if (typeof window !== 'undefined') window.__gevContract = contractReport;
/** Families are lower-case ids in the shell; the engine may capitalise them. */
const fam = (f) => String(f || 'infra').toLowerCase();
const normLayer = (l) => ({ ...l, family: fam(l.family) });
function normSituation(s) {
const section = String(s.section || '').toLowerCase();
return {
...s,
family: fam(s.family),
section:
section === 'reconstruction' ? 'reconstructions' : section || undefined,
alwaysOn: s.alwaysOn ?? section === 'always',
};
}
function guard(engine, kind) {
const note = (m) => {
if (!contractReport.errors.includes(m)) contractReport.errors.push(m);
};
const call = (name, fallback, ...args) => {
const fn = engine[name];
if (typeof fn !== 'function') return fallback;
try {
return fn.apply(engine, args);
} catch (err) {
note(`${name}(): ${err?.message || err}`);
console.warn(`[globe:${kind}] ${name} failed`, err);
return fallback;
}
};
const asyncCall = async (name, fallback, ...args) => {
try {
const v = await call(name, fallback, ...args);
return v ?? fallback;
} catch (err) {
note(`${name}(): ${err?.message || err}`);
return fallback;
}
};
return {
kind,
raw: engine,
isStub: kind === 'stub',
flyTo: (o) => asyncCall('flyTo', undefined, o),
home: () => call('home', undefined),
layers: () => {
const v = call('layers', []);
return Array.isArray(v) ? v.map(normLayer) : [];
},
setLayer: (id, on) => call('setLayer', undefined, id, on),
setFamily: (f, on) => call('setFamily', undefined, f, on),
setTime: (d) => call('setTime', undefined, d),
timeWindow: () => {
const v = call('timeWindow', null);
const n = Date.now();
if (!v || !v.start || !v.end)
return {
start: new Date(n - 72 * 3600e3),
end: new Date(n + 120 * 3600e3),
now: new Date(n),
};
return {
start: new Date(v.start),
end: new Date(v.end),
now: new Date(v.now ?? n),
};
},
setLens: (id) => call('setLens', undefined, id),
focus: (s) => asyncCall('focus', undefined, s),
select: (id) => call('select', undefined, id),
on: (ev, fn) => call('on', undefined, ev, fn),
off: (ev, fn) => call('off', undefined, ev, fn),
pause: () => call('pause', undefined),
resume: () => call('resume', undefined),
contactsNear: (lon, lat, km) => {
const v = call('contactsNear', [], lon, lat, km);
return Array.isArray(v) ? v : [];
},
situations: async () => {
const v = await asyncCall('situations', []);
return Array.isArray(v) ? v.map(normSituation) : [];
},
credits: () => {
const v = call('credits', null);
return Array.isArray(v) ? v : null;
},
/** Screen-space label candidates (engine draws none; the shell renders them). */
labels: (max = 40) => {
const v = call('labels', [], max);
return Array.isArray(v) ? v : [];
},
project: (lon, lat, alt) => call('project', null, lon, lat, alt),
time: () => call('time', null),
};
}
/**
* @param {HTMLElement} container
* @param {{ quality?: string, preferStub?: boolean }} opts
*/
export async function mountGlobe(container, opts = {}) {
const params = new URLSearchParams(location.search);
const forceStub = opts.preferStub || params.get('engine') === 'stub';
if (!forceStub) {
// vite-plugin-cesium serves Workers/Assets at /cesium/ and passes the base
// as a build-time define, which the pre-bundled dev dependency never sees;
// Cesium then resolves them next to node_modules/.vite/deps and gets HTML.
if (typeof window !== 'undefined' && !window.CESIUM_BASE_URL)
window.CESIUM_BASE_URL = '/cesium/';
try {
// A glob (not a bare dynamic import) so a checkout without the engine
// still builds; Vite re-evaluates it when mobile/globe/index.js appears.
const loaders = import.meta.glob('../globe/index.js');
const load = loaders['../globe/index.js'];
if (!load) throw new Error('mobile/globe/index.js not present');
const mod = await load();
const create = mod.createGlobe || mod.default?.createGlobe || mod.default;
if (typeof create !== 'function')
throw new Error('mobile/globe/index.js exports no createGlobe');
const quality =
opts.quality === 'battery' ? 'low' : opts.quality || 'auto';
const engine = await create(container, { quality });
contractReport.engine = 'cesium';
contractReport.missing = REQUIRED.filter(
(m) => typeof engine?.[m] !== 'function',
);
if (typeof engine?.credits !== 'function')
contractReport.notes.push(
'credits() not provided (optional, used for attribution sheet)',
);
if (contractReport.missing.length)
console.warn('[globe] contract gaps', contractReport.missing);
return guard(engine, 'cesium');
} catch (err) {
contractReport.notes.push(
`real engine unavailable: ${err?.message || err}`,
);
console.info('[globe] using offline stub:', err?.message || err);
container.replaceChildren();
}
}
const { createGlobe } = await import('./globeStub.js');
const engine = await createGlobe(container, {
quality: opts.quality || 'auto',
});
contractReport.engine = 'stub';
return guard(engine, 'stub');
}