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:
@@ -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
|
||||
// disappears (e.g. just deleted) drop to the next available, or clear when none remain.
|
||||
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 (selectedId != null) setSelectedId(null);
|
||||
return;
|
||||
@@ -236,7 +239,7 @@ export default function Playlists({ canWrite }: { canWrite: boolean }) {
|
||||
if (selectedId == null || !playlists.some((p) => p.id === selectedId)) {
|
||||
setSelectedId(playlists[0].id);
|
||||
}
|
||||
}, [playlists, selectedId]);
|
||||
}, [playlists, selectedId, listQuery.data]);
|
||||
|
||||
const detailQuery = useQuery({
|
||||
queryKey: ["playlist", selectedId],
|
||||
|
||||
Reference in New Issue
Block a user