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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | 19x 19x 19x 19x 19x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x | import { useEffect, useState } from "react";
import { useSpotify, useToggle } from "hooks";
import { GetLyrics, IFormatLyricsResponse, LyricsAction } from "types/lyrics";
import { DisplayInFullScreen } from "types/spotify";
import {
formatLyrics,
getLyrics,
TimeOutError,
within,
WithinResult,
} from "utils";
export function useLyrics(): {
lyrics: IFormatLyricsResponse | null;
lyricsLoading: boolean;
lyricsError: string | null;
} {
const {
currentlyPlaying,
isPictureInPictureLyircsCanvas,
displayInFullScreen,
isPip,
pipWindow,
} = useSpotify();
const [lyrics, setLyrics] = useState<IFormatLyricsResponse | null>(null);
const [lyricsLoading, setLoading] = useToggle();
const [lyricsError, setLyricsError] = useState<string | null>(null);
const [res, setRes] = useState<WithinResult<GetLyrics>>({
error: null,
data: null,
});
const requestLyrics = !!(
displayInFullScreen === DisplayInFullScreen.Lyrics ||
pipWindow.current ||
(isPictureInPictureLyircsCanvas && isPip)
);
const artist = currentlyPlaying?.artists?.[0].name;
const title = currentlyPlaying?.name;
const trackId = currentlyPlaying?.id;
useEffect(() => {
if (!requestLyrics) return;
setLoading.on();
setLyricsError(null);
setLyrics(null);
setRes({ error: null, data: null });
Iif (!artist || !title) {
setLyrics(null);
setLoading.off();
setLyricsError("No artist or title provided");
return;
}
within(
getLyrics(artist, title, trackId, LyricsAction.Fullscreen),
40000,
artist + title
).then((res) => {
Iif (res) {
setRes(res);
}
});
return () => {
setLyrics(null);
setLoading.reset();
setLyricsError(null);
};
}, [artist, setLoading, setLyricsError, title, trackId, requestLyrics]);
useEffect(() => {
if (!requestLyrics) return;
Iif (!res || !artist || !title || res.id !== artist + title) return;
Iif (TimeOutError.isThisError(res.error)) {
setLyricsError(
"Sorry, This seems to be very slow and we don't want to make you wait more"
);
setLoading.off();
setLyrics(null);
return;
}
Iif (!res.error) {
setLoading.on();
}
setLoading.off();
Iif (!res.data) {
setLyricsError("No lyrics found");
return;
}
setLyricsError(null);
setLyrics(formatLyrics(res.data));
}, [artist, res, setLoading, title, requestLyrics]);
return {
lyrics,
lyricsLoading,
lyricsError,
};
}
|