import { useSyncExternalStore } from 'react'; import { useAviationPlayer } from '../AviationContext'; import type { DomainEvent } from '../specs/types.nitro'; import type { AviationPlayer } from '../Aviation'; /** * React hook that surfaces the player's last audio interruption — a phone * call, focus loss, Siri. Pair with `player.setInterruptionMode` when the * UI needs to explain what happened ("Resuming after call…") or offer * manual controls under the 'pause' policy. * * Returns `undefined` until an interruption occurs; re-renders only when a * new interruption arrives. The event's `began` flag distinguishes start * from end; `resumePolicy` reports the policy in effect at that moment. * * Interruptions only react — they never request focus. A player that * deactivated itself (autoDeactivateOnQueueEnd) stays quiet. * * @example * ```tsx * function CallBanner() { * const interruption = useInterruption(); * if (!interruption?.began) return null; * return Paused for phone call…; * } * ``` */ export function useInterruption( playerArg?: AviationPlayer ): DomainEvent | undefined { const player = useAviationPlayer(playerArg); return useSyncExternalStore( player.store.subscribeInterruption, () => player.store.lastInterruption ); }