Promise that resolves when the update is complete
// Refresh recent tracks history
async function refreshHistory() {
await updateRecentTracks();
console.log('Recent tracks history updated from Spotify');
}
export async function updateRecentTracks(): Promise<void> {
try {
// Get recently played tracks from Spotify API
const recentlyPlayed = await spotifyApi.getRecentlyPlayedTracks();
if (
recentlyPlayed &&
recentlyPlayed.items &&
recentlyPlayed.items.length > 0
) {
// Extract track IDs from the response
const trackIds = recentlyPlayed.items.map((item) => item.track.id);
// Update the state with the new list of recently played tracks
setRecentTracks(trackIds);
store.saveLog(
`Updated recent tracks list (${trackIds.length} tracks)`,
"DEBUG",
);
} else {
store.saveLog(
"No recent tracks found or API response was empty",
"DEBUG",
);
}
} catch (error) {
store.saveLog(`Failed to update recent tracks: ${error}`, "ERROR");
}
}
Updates the list of recently played tracks from Spotify
Fetches the user's recently played tracks from the Spotify API and updates the application state to reflect the current listening history. This provides a baseline of listening data for skip detection and backward navigation identification.