Files
psysonic/src/components/tracks/TracksPageChrome.tsx
T
cucadmuh 4ac373a65b feat(search): scoped live search on browse pages (#938)
* feat(artists): scoped live search badge replaces page filter

Move Artists browse text search into the header Live Search with a page
scope badge (Users icon), field-local undo, and double-click/backspace
to clear scope. Block the live-search dropdown while scoped so results
only filter the Artists grid; mobile overlay follows the same rules.

* fix(artists): plain grid for scoped search fixes broken card layout

Route the browse grid through VirtualCardGrid, switch to non-virtual CSS
grid when live search filters the catalog, reset scroll on filter changes,
and skip content-visibility on plain tiles to avoid blank/black cards.

* fix(search): scope badge double-Backspace and single clear control

Require two Backspaces on an empty scoped field after prior text input;
one Backspace still clears the badge when the field was never filled.
Move live-search clear/advanced controls inside the field pill, drop the
extra outer clear button, and use type=text to avoid native search clears.

* fix(search): drop duplicate outer live-search clear button

Keep the native in-field clear on type=search and the original pill layout;
remove only the extra × control outside the search border. Reset dropdown
state when the query is cleared via the native control.

* refactor(search): generic scoped browse query helper, drop dead code

Rename artistsBrowseSearchQuery to scopedBrowseSearchQuery with an
expectedScope argument; wire Artists via useScopedBrowseSearchQuery.
Remove unused liveSearchScoped dropdown helper (scoped mode blocks it).

* feat(search): ghost scope badge and single-click badge remove

After clearing the artists scope on /artists, show a faded ghost chip to
restore page-only search while keeping the global search placeholder.
Active badge removes on one click; tooltips and styles updated.

* feat(search): scoped live search for All Albums and New Releases

Wire albums and newReleases scope badges with debounced album title search
(local index title-only FTS + filtered search3). Plain grid, scroll reset,
and session query restore on album grid browse pages.

* fix(browse): preserve scroll restore after album/artist detail back

Only reset in-page scroll when filter resetKey changes, not when
isScrollRestorePending clears after session restore.

* feat(search): scoped live search for Tracks browse

Wire /tracks to header live search with wide title/artist/album FTS,
hide hero and discovery rails while search is active, and remove the
inline search field from the browse list.

* fix(search): clear header query when leaving scoped browse pages

Prevent global live search from firing on album/detail routes after a
scoped browse query; browse session stashes still restore on back.

* fix(tracks): restore scroll after back from detail during scoped search

Hold stashed song results across fetchSongPage churn, defer leave-stash
teardown past AppShell scroll reset, restore tracks scroll after the list
is ready, and save scroll snapshot when opening artist from song context menu.

* fix(tracks): hide discovery headings during scoped search

Hide the page subtitle and "Browse all tracks" section title when
tracks search is active, matching hero/rails chrome behavior.

* feat(search): scoped live search for Composers browse

Wire /composers to header live search with composers scope badge,
session stash, scroll restore, and plain grid/list during text filter.
Remove the in-page filter input; add i18n and navigation helpers.

* docs: CHANGELOG and credits for scoped browse live search (PR #938)
2026-06-01 13:04:36 +03:00

205 lines
7.6 KiB
TypeScript

import { AlbumCoverArtImage } from '../../cover/AlbumCoverArtImage';
import { getRandomSongs } from '../../api/subsonicLibrary';
import type { SubsonicSong } from '../../api/subsonicTypes';
import { songToTrack } from '../../utils/playback/songToTrack';
import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { Play, ListPlus, RefreshCw, Sparkles } from 'lucide-react';
import { useTranslation } from 'react-i18next';
import { useAuthStore } from '../../store/authStore';
import { usePlayerStore } from '../../store/playerStore';
import SongRail from '../SongRail';
import { playSongNow } from '../../utils/playback/playSong';
import { ndListSongs, ndInvalidateSongsCache } from '../../api/navidromeBrowse';
import { usePerfProbeFlags } from '../../utils/perf/perfFlags';
import { useNavigateToAlbum } from '../../hooks/useNavigateToAlbum';
import { useNavigateToArtist } from '../../hooks/useNavigateToArtist';
const RANDOM_RAIL_SIZE = 18;
const RATED_RAIL_FETCH = 60;
const RATED_RAIL_DISPLAY = 30;
const RATED_RAIL_CACHE_MS = 60_000;
const TRACKS_SONG_RAIL_WINDOWING = true;
const TRACKS_SONG_RAIL_INITIAL_ARTWORK_BUDGET = 14;
/** Tracks hub hero + song rails (above the browse-all list). */
export default function TracksPageChrome({
onLayoutReady,
hideDiscoveryChrome = false,
}: {
/** Fires once when hero + rails finish their initial load (or fail). */
onLayoutReady?: () => void;
/** When true, skip hero and song rails (active scoped search). */
hideDiscoveryChrome?: boolean;
}) {
const perfFlags = usePerfProbeFlags();
const { t } = useTranslation();
const navigateToArtist = useNavigateToArtist();
const navigateToAlbum = useNavigateToAlbum();
const activeServerId = useAuthStore(s => s.activeServerId);
const enqueue = usePlayerStore(s => s.enqueue);
const [hero, setHero] = useState<SubsonicSong | null>(null);
const [heroLoading, setHeroLoading] = useState(false);
const [random, setRandom] = useState<SubsonicSong[]>([]);
const [randomLoading, setRandomLoading] = useState(true);
const [rated, setRated] = useState<SubsonicSong[]>([]);
const [ratedLoading, setRatedLoading] = useState(true);
const [ratedSupported, setRatedSupported] = useState(true);
const layoutReadyNotifiedRef = useRef(false);
const rerollHero = useCallback(async () => {
setHeroLoading(true);
try {
const picks = await getRandomSongs(1);
if (picks[0]) setHero(picks[0]);
} finally {
setHeroLoading(false);
}
}, []);
const rerollRandom = useCallback(async () => {
setRandomLoading(true);
try {
setRandom(await getRandomSongs(RANDOM_RAIL_SIZE));
} finally {
setRandomLoading(false);
}
}, []);
const reloadRated = useCallback(async () => {
setRatedLoading(true);
try {
const songs = await ndListSongs(0, RATED_RAIL_FETCH, 'rating', 'DESC', RATED_RAIL_CACHE_MS);
const filtered = songs.filter(s => (s.userRating ?? 0) > 0).slice(0, RATED_RAIL_DISPLAY);
setRated(filtered);
setRatedSupported(true);
} catch {
setRated([]);
setRatedSupported(false);
} finally {
setRatedLoading(false);
}
}, []);
useEffect(() => {
if (!activeServerId || hideDiscoveryChrome) return;
rerollHero();
rerollRandom();
reloadRated();
}, [activeServerId, hideDiscoveryChrome, rerollHero, rerollRandom, reloadRated]);
useEffect(() => {
if (!onLayoutReady || layoutReadyNotifiedRef.current) return;
if (hideDiscoveryChrome || !activeServerId) {
layoutReadyNotifiedRef.current = true;
onLayoutReady();
return;
}
if (heroLoading || randomLoading || ratedLoading) return;
layoutReadyNotifiedRef.current = true;
onLayoutReady();
}, [activeServerId, hideDiscoveryChrome, onLayoutReady, heroLoading, randomLoading, ratedLoading]);
const railSongs = useMemo(
() => (hero ? random.filter(s => s.id !== hero.id) : random),
[random, hero],
);
return (
<>
{!perfFlags.disableMainstageStickyHeader && (
<header className="tracks-header">
<div className="tracks-header-text">
<h1 className="page-title">{t('tracks.title')}</h1>
{!hideDiscoveryChrome && (
<p className="tracks-subtitle">{t('tracks.subtitle')}</p>
)}
</div>
</header>
)}
{!perfFlags.disableMainstageHero && !hideDiscoveryChrome && hero && (
<section className="tracks-hero">
<div className="tracks-hero-cover">
{hero.albumId && hero.coverArt ? (
<AlbumCoverArtImage
albumId={hero.albumId}
coverArt={hero.coverArt}
displayCssPx={600}
surface="sparse"
alt=""
/>
) : (
<div className="tracks-hero-cover-placeholder" />
)}
</div>
<div className="tracks-hero-content">
<span className="tracks-hero-eyebrow">
<Sparkles size={14} />
{t('tracks.heroEyebrow')}
</span>
<h2 className="tracks-hero-title" title={hero.title}>{hero.title}</h2>
<p className="tracks-hero-meta">
<span
className={hero.artistId ? 'track-artist-link' : ''}
style={{ cursor: hero.artistId ? 'pointer' : 'default' }}
onClick={() => hero.artistId && navigateToArtist(hero.artistId)}
>{hero.artist}</span>
{hero.album && (
<>
<span className="tracks-hero-meta-dot">·</span>
<span
className={hero.albumId ? 'track-artist-link' : ''}
style={{ cursor: hero.albumId ? 'pointer' : 'default' }}
onClick={() => hero.albumId && navigateToAlbum(hero.albumId)}
>{hero.album}</span>
</>
)}
</p>
<div className="tracks-hero-actions">
<button className="btn btn-primary" onClick={() => playSongNow(hero)}>
<Play size={16} fill="currentColor" /> {t('tracks.playSong')}
</button>
<button className="btn btn-surface" onClick={() => enqueue([songToTrack(hero)])}>
<ListPlus size={16} /> {t('tracks.enqueueSong')}
</button>
<button
className="btn btn-surface"
onClick={rerollHero}
disabled={heroLoading}
aria-label={t('tracks.heroReroll')}
data-tooltip={t('tracks.heroReroll')}
data-tooltip-pos="top"
>
<RefreshCw size={16} className={heroLoading ? 'is-spinning' : ''} />
</button>
</div>
</div>
</section>
)}
{!perfFlags.disableMainstageRails && !hideDiscoveryChrome && ratedSupported && (ratedLoading || rated.length > 0) && (
<SongRail
title={t('tracks.railHighlyRated')}
songs={rated}
loading={ratedLoading}
onReroll={() => { ndInvalidateSongsCache(); return reloadRated(); }}
windowArtworkByViewport={TRACKS_SONG_RAIL_WINDOWING}
initialArtworkBudget={TRACKS_SONG_RAIL_INITIAL_ARTWORK_BUDGET}
/>
)}
{!perfFlags.disableMainstageRails && !hideDiscoveryChrome && (
<SongRail
title={t('tracks.railRandom')}
songs={railSongs}
loading={randomLoading}
onReroll={rerollRandom}
windowArtworkByViewport={TRACKS_SONG_RAIL_WINDOWING}
initialArtworkBudget={TRACKS_SONG_RAIL_INITIAL_ARTWORK_BUDGET}
/>
)}
</>
);
}