Files
gods-eye-view/src/stories/boot.js
T
Sameh Khamis b1b2ac91c0 Voice: an app can supply what open_stories does
createApplicationTools took the Stories page as the open_stories action
even when the app's voice options gave its own. voice.openStories now
takes precedence, spoken or typed, with the Stories page as the default.
2026-11-16 16:33:46 -08:00

148 lines
4.6 KiB
JavaScript

const STORIES_PARAMS = ['stories', 'story'];
const wantsStories = () => {
const params = new URLSearchParams(location.search);
return STORIES_PARAMS.some((p) => params.has(p));
};
/** Stop microphone use on entry while letting an active typed turn finish. */
export function stopVoiceForStories(session) {
if (!session || session.adapter.getStartOptions?.()?.textOnly) return false;
const wasActive = session.isActive();
session.stop();
return wasActive;
}
/**
* What the `open_stories` action does, spoken or typed: the app's own
* `openStories` when it supplies one, else open the Stories page (none
* where Stories is not offered). It is called with `{ isCurrent }` and
* answers true when it opened, 'already-open', or false.
* @param {{openStories?: Function|null,
* store: {available: boolean, mode: string},
* stories: {open: (options: {isCurrent: () => boolean}) => unknown}}} options
* @returns {Function|null}
*/
export function storiesOpener({ openStories, store, stories }) {
if (typeof openStories === 'function') return openStories;
if (!store.available) return null;
return ({ isCurrent }) =>
store.mode === 'closed' ? stories.open({ isCurrent }) : 'already-open';
}
/**
* Stories, loaded on first use. The controller, the story catalog, the
* screens and their styles are separate chunks, fetched when the person
* opens Stories, follows a Stories link or
* steps into Stories with Back or Forward. Until then the store holds this
* shell, whose calls load the controller and then run.
*
* @param {{viewer: object,
* director: import('../scenes/director.js').SceneDirector,
* store: ReturnType<typeof import('../ui/stores/stories.js').useStoriesStore>,
* onEnter?: () => void,
* onLoadFailed?: () => void,
* loadModules?: () => Promise<[object, object]>}} options `onLoadFailed`
* tells the person a click did nothing because Stories could not load;
* `available` is false where Stories is not offered (an inline embed:
* its URL and history are the host page's);
* `loadModules` fetches the controller, catalog and Stories' styles
* (tests replace it).
*/
export function bootStories({
viewer,
director,
store,
onEnter,
onLoadFailed,
available = true,
loadModules = () =>
Promise.all([
import('./controller.js'),
import('./catalog.js'),
// Stories' screen styles, in place before the screens show, and the
// screens themselves, so opening does not wait on a second fetch.
import('../ui/styles/stories.css'),
import('../ui/views/stories/ExploreScreen.vue'),
import('../ui/views/stories/StoryScreen.vue'),
]),
}) {
store.available = available;
if (!available) {
const off = {
open: async () => {},
openStory: async () => {},
backToExplore: async () => {},
close: () => {},
reframe: () => {},
kind: null,
load: async () => null,
destroy: () => {},
};
store.connect(off, []);
return off;
}
let real = null;
let loading = null;
let destroyed = false;
function load() {
loading ??= loadModules().then(
([{ createStoriesController }, { listStories }]) => {
if (destroyed) return null;
removeEventListener('popstate', onPopState);
real = createStoriesController({
viewer,
director,
store,
onEnter,
});
store.connect(real, listStories());
return real;
},
);
// A failed fetch (offline, or a deploy replaced the chunk) is reported
// once, whatever asked (a click, a link, Back or Forward), and not kept:
// the next attempt tries again.
loading.catch((error) => {
loading = null;
console.warn('Stories could not load', error);
onLoadFailed?.();
});
return loading;
}
// The controller reads a Stories link on creation and opens it.
const onPopState = () => {
if (wantsStories()) load();
};
addEventListener('popstate', onPopState);
if (wantsStories()) load();
const later =
(name) =>
(...args) =>
load().then(
(c) => c?.[name](...args),
() => {}, // reported by load()
);
const shell = {
open: later('open'),
openStory: later('openStory'),
backToExplore: later('backToExplore'),
close: () => real?.close(),
reframe: () => real?.reframe(),
get kind() {
return real?.kind ?? null;
},
/** Resolves to the controller once it has loaded. */
load,
destroy() {
if (destroyed) return;
destroyed = true;
removeEventListener('popstate', onPopState);
real?.destroy();
},
};
store.connect(shell, []);
return shell;
}