-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseSongInfo.js
34 lines (30 loc) · 1.06 KB
/
useSongInfo.js
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
import { useEffect, useState } from 'react'
import { useRecoilState } from 'recoil'
import { currentTrackIdState } from '../atoms/songAtom'
import useSpotify from './useSpotify'
function useSongInfo() {
const spotifyApi = useSpotify()
const [currentTrackId, setCurrentTrackId] =
useRecoilState(currentTrackIdState)
const [songInfo, setSongInfo] = useState(null)
useEffect(() => {
async function fetchSong() {
if (currentTrackId) {
const trackInfo = await fetch(
`https://api.spotify.com/v1/tracks/${currentTrackId}`,
{
headers: {
Authorization: `Bearer ${spotifyApi.getAccessToken()}`,
},
}
)
.then((res) => res.json())
.catch((err) => console.error(err))
setSongInfo(trackInfo)
}
}
fetchSong()
}, [currentTrackId, spotifyApi])
return songInfo
}
export default useSongInfo