// The title a page gives itself, for the browser tab and the page's heading: for // a route the shell cannot name from its nav items or open tabs, such as the // flow editor, where several tabs would otherwise all read as the bare brand. // A page calls `usePageTitle("flow fan-out")`; the shell reads it with // `usePageTitleValue()` and falls back to its own label when none is set. import { useEffect, useState } from "preact/hooks"; let current: string | undefined; const listeners = new Set<() => void>(); /** Set the page's title (or clear it with `undefined`). */ export function setPageTitle(title: string | undefined): void { if (current === title) return; current = title; listeners.forEach((fn) => fn()); } export function getPageTitle(): string | undefined { return current; } /** Subscribe to changes; returns the way to stop. */ export function subscribePageTitle(fn: () => void): () => void { listeners.add(fn); return () => listeners.delete(fn); } /** Give the page this title while it is mounted. */ export function usePageTitle(title: string | undefined): void { useEffect(() => { setPageTitle(title); return () => setPageTitle(undefined); }, [title]); } /** The title a page has set, if any, following changes. */ export function usePageTitleValue(): string | undefined { const [title, setTitle] = useState(getPageTitle()); useEffect(() => { setTitle(getPageTitle()); return subscribePageTitle(() => setTitle(getPageTitle())); }, []); return title; }