mirror of
https://github.com/kilyabin/psysonic.git
synced 2026-07-22 14:35:41 +00:00
fc34a0ec59
* feat(offline): local-bytes browse for artists and albums Make Artists, All Albums, and artist/album detail pages work offline from the library index limited to on-disk library and favorite-auto tracks. Add a DEV header toggle to simulate offline browse for testing. * feat(offline): reactive DEV offline toggle with full disconnect simulation Subscribe nav and browse/detail hooks to useOfflineBrowseActive so UI refreshes on toggle. DEV force-offline now blocks server probes, reports disconnected status, and gates Subsonic like real offline for player parity. * feat(offline): bytes-first favorites when offline browse is active Load Favorites from local playback bytes, filter starred tracks client-side, and restrict album-level star queries to local album ids. Drop interim perf attempts (lean SQL, progressive load, connection singleton, prefetch UX). * feat(offline): tracks, help, player stats; suspend library picker offline - Offline browse for Tracks hub from local bytes; sidebar nav for tracks/help/statistics - Statistics redirects to player-stats offline; server/Last.fm tabs skip network fetches - Hide music-library picker offline; save filter and restore on reconnect (all libraries while disconnected) - Unified isOfflineSidebarNavAllowed for library + system entries * feat(offline): fork disconnect navigation by offline browse capability When the server drops: stay on the page if nothing is browsable offline; reload in place on offline-capable routes; otherwise redirect to All Albums instead of the old /offline or /favorites bounce. * feat(offline): browse cached playlists when the server is down List and open manually pinned regular playlists from local library-tier bytes offline, with sidebar/nav routing and read-only playlist UI. * feat(offline): read-only artist detail and local play-all paths Hide favorites and discography offline actions when browse is offline; load Play All, Shuffle, and top-track continuation from local album bytes. * feat(offline): read-only album detail and enqueue from local bytes Hide favorites, download, and cache-offline actions on album pages when offline browse is active. Favorites album cards enqueue via the same resolveAlbumForServer path as play, including local playback bytes. * chore: remove unused import in AlbumCard after enqueue refactor * feat(offline): unify browse integration contract across the app Add useOfflineBrowseContext, offlineMediaResolve, and offlineActionPolicy; wire shell nav to a single capability source; migrate play/enqueue and context-menu paths off raw getAlbum; replace readOnly with action policy on detail surfaces. Tests updated for the media-resolve facade. * feat(offline): close browse contract gaps and fix offline Home feed Split offline browse modules, align favorites capability across servers, wire action policy on context menus, migrate hooks to useOfflineBrowseContext, and preserve stale Home feed cache when offline so the UI does not empty. * fix(offline): block playbar stars, close audit gaps, trim dead exports Hide star rating and favorite in PlayerBar when offline browse is active via offlineActionPolicy playerBar surface. Wire stay-reload token into browse hooks, migrate hooks to context.active, guard rating prefetch network calls, and route playlist load through resolvePlaylist. * docs: add CHANGELOG and credits for offline browse PR #1017 * fix(offline): stop DEV connection probe regression in tests React to devForceOffline transitions only in useConnectionStatus so mount does not double-fire check() or ignore disableBackgroundPolling. Add pingWithCredentials to PlayerBar test mock and DEV-toggle unit tests.
121 lines
4.2 KiB
TypeScript
121 lines
4.2 KiB
TypeScript
import { getArtist } from './subsonicArtists';
|
|
import { getAlbum } from './subsonicLibrary';
|
|
import { shouldAttemptSubsonicForActiveServer } from '../utils/network/subsonicNetworkGuard';
|
|
|
|
const MIX_RATING_PREFETCH_CONCURRENCY = 8;
|
|
const RATING_CACHE_TTL = 7 * 60 * 1000; // 7 minutes
|
|
const ratingCache = new Map<string, { value: number; expiresAt: number }>();
|
|
|
|
function getCachedRating(key: string): number | null {
|
|
const entry = ratingCache.get(key);
|
|
if (!entry) return null; // cache miss
|
|
if (Date.now() > entry.expiresAt) { ratingCache.delete(key); return null; }
|
|
return entry.value;
|
|
}
|
|
|
|
function setCachedRating(key: string, value: number): void {
|
|
ratingCache.set(key, { value, expiresAt: Date.now() + RATING_CACHE_TTL });
|
|
}
|
|
|
|
/** Drop cached entity ratings after `setRating` so mixes see fresh stars. */
|
|
export function invalidateEntityUserRatingCaches(id: string): void {
|
|
ratingCache.delete(`artist:${ENTITY_RATING_CACHE_KEY_VER}:${id}`);
|
|
ratingCache.delete(`album:${ENTITY_RATING_CACHE_KEY_VER}:${id}`);
|
|
}
|
|
|
|
function parseEntityUserRating(v: unknown): number | undefined {
|
|
if (v === null || v === undefined) return undefined;
|
|
const n = typeof v === 'number' ? v : Number(v);
|
|
if (!Number.isFinite(n)) return undefined;
|
|
return n;
|
|
}
|
|
|
|
/** Navidrome and some JSON shapes use `rating` where Subsonic docs say `userRating`. */
|
|
export function parseSubsonicEntityStarRating(entity: {
|
|
userRating?: unknown;
|
|
rating?: unknown;
|
|
}): number | undefined {
|
|
return parseEntityUserRating(entity.userRating ?? entity.rating);
|
|
}
|
|
|
|
/** Bump when rating parse keys change so stale cache entries are not reused. */
|
|
const ENTITY_RATING_CACHE_KEY_VER = 'v2';
|
|
|
|
/** Parallel `getArtist` calls to fill mix/album filters when list endpoints omit ratings. */
|
|
export async function prefetchArtistUserRatings(
|
|
ids: string[],
|
|
concurrency = MIX_RATING_PREFETCH_CONCURRENCY,
|
|
): Promise<Map<string, number>> {
|
|
const unique = [...new Set(ids.filter(Boolean))];
|
|
const out = new Map<string, number>();
|
|
if (!unique.length) return out;
|
|
const uncached: string[] = [];
|
|
for (const id of unique) {
|
|
const cached = getCachedRating(`artist:${ENTITY_RATING_CACHE_KEY_VER}:${id}`);
|
|
if (cached !== null) out.set(id, cached);
|
|
else uncached.push(id);
|
|
}
|
|
if (!uncached.length) return out;
|
|
if (!shouldAttemptSubsonicForActiveServer()) return out;
|
|
let next = 0;
|
|
async function worker() {
|
|
for (;;) {
|
|
const i = next++;
|
|
if (i >= uncached.length) return;
|
|
const id = uncached[i];
|
|
try {
|
|
const { artist } = await getArtist(id);
|
|
const r = parseSubsonicEntityStarRating(artist);
|
|
if (r !== undefined && r > 0) {
|
|
setCachedRating(`artist:${ENTITY_RATING_CACHE_KEY_VER}:${id}`, r);
|
|
out.set(id, r);
|
|
}
|
|
} catch {
|
|
/* ignore */
|
|
}
|
|
}
|
|
}
|
|
const nWorkers = Math.min(concurrency, uncached.length);
|
|
await Promise.all(Array.from({ length: nWorkers }, () => worker()));
|
|
return out;
|
|
}
|
|
|
|
/** Parallel `getAlbum` calls when `albumList2` entries lack `userRating`. */
|
|
export async function prefetchAlbumUserRatings(
|
|
ids: string[],
|
|
concurrency = MIX_RATING_PREFETCH_CONCURRENCY,
|
|
): Promise<Map<string, number>> {
|
|
const unique = [...new Set(ids.filter(Boolean))];
|
|
const out = new Map<string, number>();
|
|
if (!unique.length) return out;
|
|
const uncached: string[] = [];
|
|
for (const id of unique) {
|
|
const cached = getCachedRating(`album:${ENTITY_RATING_CACHE_KEY_VER}:${id}`);
|
|
if (cached !== null) out.set(id, cached);
|
|
else uncached.push(id);
|
|
}
|
|
if (!uncached.length) return out;
|
|
if (!shouldAttemptSubsonicForActiveServer()) return out;
|
|
let next = 0;
|
|
async function worker() {
|
|
for (;;) {
|
|
const i = next++;
|
|
if (i >= uncached.length) return;
|
|
const id = uncached[i];
|
|
try {
|
|
const { album } = await getAlbum(id);
|
|
const r = parseSubsonicEntityStarRating(album);
|
|
if (r !== undefined && r > 0) {
|
|
setCachedRating(`album:${ENTITY_RATING_CACHE_KEY_VER}:${id}`, r);
|
|
out.set(id, r);
|
|
}
|
|
} catch {
|
|
/* ignore */
|
|
}
|
|
}
|
|
}
|
|
const nWorkers = Math.min(concurrency, uncached.length);
|
|
await Promise.all(Array.from({ length: nWorkers }, () => worker()));
|
|
return out;
|
|
}
|