fix(playlists): don't drop the restored selection during initial load

The auto-select effect ran while the playlist query was still loading (empty list),
nulling the localStorage-restored selection so F5 fell back to the first playlist.
Wait for the first load (listQuery.data) before adjusting the selection.
This commit is contained in:
npeter83 2026-06-15 22:19:05 +02:00
parent c1d43d8961
commit 380ad39ad4

View file

@ -229,6 +229,9 @@ export default function Playlists({ canWrite }: { canWrite: boolean }) {
// Keep the selection valid: default to the first playlist, and if the selected one // Keep the selection valid: default to the first playlist, and if the selected one
// disappears (e.g. just deleted) drop to the next available, or clear when none remain. // disappears (e.g. just deleted) drop to the next available, or clear when none remain.
useEffect(() => { useEffect(() => {
// Wait for the first load before adjusting the selection — otherwise the empty
// placeholder list would null the restored selection and we'd fall back to the first.
if (!listQuery.data) return;
if (!playlists.length) { if (!playlists.length) {
if (selectedId != null) setSelectedId(null); if (selectedId != null) setSelectedId(null);
return; return;
@ -236,7 +239,7 @@ export default function Playlists({ canWrite }: { canWrite: boolean }) {
if (selectedId == null || !playlists.some((p) => p.id === selectedId)) { if (selectedId == null || !playlists.some((p) => p.id === selectedId)) {
setSelectedId(playlists[0].id); setSelectedId(playlists[0].id);
} }
}, [playlists, selectedId]); }, [playlists, selectedId, listQuery.data]);
const detailQuery = useQuery({ const detailQuery = useQuery({
queryKey: ["playlist", selectedId], queryKey: ["playlist", selectedId],