Skip to content

Commit adac8d5

Browse files
committed
fix: currently playing album is added to recently played on page load again
1 parent 322c499 commit adac8d5

File tree

1 file changed

+66
-6
lines changed

1 file changed

+66
-6
lines changed

src/hooks/useSpotifyData.js

Lines changed: 66 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ export function useSpotifyData(activeSection, skipInitialFetch = false) {
8181
const retryTimeoutRef = useRef(null);
8282
const abortControllerRef = useRef(null);
8383
const lazyLoadTimeoutRef = useRef(null);
84-
8584
const sectionTimeoutRefs = useRef({
8685
playlists: null,
8786
artists: null,
@@ -134,6 +133,7 @@ export function useSpotifyData(activeSection, skipInitialFetch = false) {
134133
getRecentlyPlayed,
135134
getNextRecentlyPlayed,
136135
getUserShows,
136+
getPlayerState,
137137
} = useSpotifyWebSocket();
138138

139139
const scheduleLazyLoad = useCallback(() => {
@@ -308,11 +308,6 @@ export function useSpotifyData(activeSection, skipInitialFetch = false) {
308308
setNextTokens((prev) => ({ ...prev, recentTracks: data.next }));
309309
}
310310

311-
if (currentlyPlayingAlbum?.id) {
312-
albumIds.add(currentlyPlayingAlbum.id);
313-
uniqueAlbums.push(currentlyPlayingAlbum);
314-
}
315-
316311
const items = data.items || [];
317312
items.forEach((item) => {
318313
if (
@@ -349,6 +344,7 @@ export function useSpotifyData(activeSection, skipInitialFetch = false) {
349344
return limitedAlbums;
350345
});
351346
} else {
347+
console.log('Setting recentAlbums to:', uniqueAlbums.map(a => a.name));
352348
setRecentAlbums(uniqueAlbums);
353349
setItemCounts((prev) => ({
354350
...prev,
@@ -1186,6 +1182,69 @@ export function useSpotifyData(activeSection, skipInitialFetch = false) {
11861182
failedRequests.push("fetchUserShows");
11871183
}
11881184

1185+
try {
1186+
console.log("Final step: Fetching current player state to add currently playing album...");
1187+
const playerStateResponse = await getPlayerState();
1188+
console.log("Player state response:", playerStateResponse);
1189+
1190+
const playerState = playerStateResponse?.result?.result || playerStateResponse?.result || playerStateResponse;
1191+
console.log("Extracted player state:", playerState);
1192+
1193+
if (playerState?.item) {
1194+
console.log("Item found:", playerState.item);
1195+
console.log("Item type:", playerState.item.type);
1196+
console.log("Has album:", !!playerState.item.album);
1197+
}
1198+
1199+
if (playerState?.item && (playerState.item.type === "track" || playerState.item.album)) {
1200+
const currentAlbum = playerState.item.is_local
1201+
? {
1202+
id: `local-${playerState.item.uri}`,
1203+
name: playerState.item.album?.name || playerState.item.name,
1204+
images: [{ url: "/images/not-playing.webp" }],
1205+
artists: playerState.item.artists,
1206+
type: "local-track",
1207+
uri: playerState.item.uri,
1208+
}
1209+
: playerState.item.album;
1210+
1211+
console.log("Found currently playing album:", currentAlbum?.name);
1212+
1213+
setRecentAlbums((prevAlbums) => {
1214+
if (prevAlbums.length === 0 || prevAlbums[0]?.id !== currentAlbum.id) {
1215+
console.log("Adding currently playing album to front of recently played carousel");
1216+
const filteredAlbums = prevAlbums.filter(
1217+
(album) => album.id !== currentAlbum.id,
1218+
);
1219+
return [currentAlbum, ...filteredAlbums].slice(0, 50);
1220+
} else {
1221+
console.log("Currently playing album is already at front, no update needed");
1222+
return prevAlbums;
1223+
}
1224+
});
1225+
} else if (playerState?.item && playerState.item.type === "episode") {
1226+
const currentShow = playerState.item.show;
1227+
console.log("Found currently playing show:", currentShow?.name);
1228+
1229+
setRecentAlbums((prevAlbums) => {
1230+
if (prevAlbums.length === 0 || prevAlbums[0]?.id !== currentShow.id) {
1231+
console.log("Adding currently playing show to front of recently played carousel");
1232+
const filteredAlbums = prevAlbums.filter(
1233+
(album) => album.id !== currentShow.id,
1234+
);
1235+
return [currentShow, ...filteredAlbums].slice(0, 50);
1236+
} else {
1237+
console.log("Currently playing show is already at front, no update needed");
1238+
return prevAlbums;
1239+
}
1240+
});
1241+
} else {
1242+
console.log("No currently playing item found or unsupported type. Player state:", playerState);
1243+
}
1244+
} catch (error) {
1245+
console.error("Failed to fetch player state for currently playing album:", error);
1246+
}
1247+
11891248
if (failedRequests.length > 0) {
11901249
console.error("Some data fetching operations failed:", failedRequests);
11911250

@@ -1235,6 +1294,7 @@ export function useSpotifyData(activeSection, skipInitialFetch = false) {
12351294
fetchRadioMixes,
12361295
fetchUserShows,
12371296
skipInitialFetch,
1297+
getPlayerState,
12381298
]);
12391299

12401300
useEffect(() => {

0 commit comments

Comments
 (0)