import type { SessionStatus } from './session.svelte.js' import { withSpace } from './space.js' interface SessionTarget { status: SessionStatus space: { uri: string } | undefined } /** * URLs choose a space when navigation changes. Session publications are deliberately not inputs: * polling and picker opens rebuild the current space without turning the old URL into a command. */ export function createSpaceNavigationObserver(open: (url: URL) => void): (url: URL, openUri?: string) => void { let observed = '' return (url, openUri) => { const href = url.href if (href === observed) return observed = href const target = url.searchParams.get('space') if (target && target !== openUri) open(url) } } /** A picker choice owns both halves of a switch: opening the fold, then naming it in the URL. */ export async function selectSpace( uri: string, open: (uri: string) => Promise, navigate: (href: string, options: { replaceState: true }) => Promise, state: SessionTarget, ): Promise { await open(uri) if (state.status !== 'ready' || state.space?.uri !== uri) return false await navigate(withSpace('/', uri), { replaceState: true }) return true }