Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | 19x 19x 19x 41x 41x 41x 41x 41x 41x 7x 7x 7x 7x | import { Dispatch, SetStateAction, useEffect } from "react";
import { useAuth } from "hooks";
import { AudioPlayer } from "hooks/useSpotifyPlayer";
import { ITrack } from "types/spotify";
import { configuration } from "utils";
export function useRecentlyPlayed({
setRecentlyPlayed,
currentlyPlaying,
playedSource,
setVolume,
player,
}: {
setRecentlyPlayed: Dispatch<SetStateAction<ITrack[]>>;
currentlyPlaying: ITrack | undefined;
playedSource: string | undefined;
player: Spotify.Player | AudioPlayer | undefined;
setVolume: Dispatch<SetStateAction<number>>;
}): void {
const { user, isPremium } = useAuth();
useEffect(() => {
if (!playedSource || !user?.id) return;
const type = playedSource.split(":")[1];
Iif (type === "track" && currentlyPlaying) {
setRecentlyPlayed((prev) => {
Iif (prev.some((el) => el.uri === currentlyPlaying.uri)) {
localStorage.setItem(
`${user.id}:recentlyPlayed`,
JSON.stringify(prev)
);
return prev;
}
Iif (prev.length === 10) {
const newRecentlyPlayedwithLimit = [
currentlyPlaying,
...prev.slice(0, -1),
];
localStorage.setItem(
`${user.id}:recentlyPlayed`,
JSON.stringify(newRecentlyPlayedwithLimit)
);
return newRecentlyPlayedwithLimit;
}
const newRecentlyPlayed = [currentlyPlaying, ...prev];
localStorage.setItem(
`${user.id}:recentlyPlayed`,
JSON.stringify(newRecentlyPlayed)
);
return newRecentlyPlayed;
});
}
}, [playedSource, currentlyPlaying, user?.id, setRecentlyPlayed]);
useEffect(() => {
if (!user?.id) return;
const playback = localStorage.getItem("playback");
const recentlyPlayedFromLocal = localStorage.getItem(
`${user.id}:recentlyPlayed`
);
Iif (playback) {
const volume = configuration.get("volume");
setVolume(volume);
if (isPremium && player) {
(player as Spotify.Player).on("ready", () => {
player?.setVolume(volume);
});
} else {
player?.setVolume(volume);
}
}
Iif (recentlyPlayedFromLocal) {
const recentlyPlayedObj = JSON.parse(recentlyPlayedFromLocal) as ITrack[];
setRecentlyPlayed(recentlyPlayedObj);
}
}, [isPremium, player, setRecentlyPlayed, setVolume, user?.id]);
}
|