import { useEffect, useState } from 'react'; import { useAppSelector } from './useAppDispatch'; import { isStreamCached } from '../lib/sw'; import { getStreamUrl } from '../api/endpoints/streaming'; /** * Whether the given track is available from the offline audio cache (Tier 3). * Drives the player-bar source indicator (local vs streaming). Returns false * until the service worker is controlling and confirms a hit. */ export function useStreamCached(trackId: string | undefined): boolean { const token = useAppSelector((s) => s.auth.accessToken); const [cached, setCached] = useState(false); useEffect(() => { if (!trackId || !token) { setCached(false); return; } let active = true; void isStreamCached(getStreamUrl(trackId, token)).then((hit) => { if (active) setCached(hit); }); return () => { active = false; }; }, [trackId, token]); return cached; }