generated from scaffold-eth/scaffold-eth-2
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c9a0106
commit dd9a154
Showing
5 changed files
with
184 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import { createContext, useContext, useEffect, useState } from "react"; | ||
import { Track } from "../types/spotify"; | ||
|
||
const PlayerContext = createContext({} as any); | ||
|
||
export default function PlayerProvider({ children }: any) { | ||
const [currentTrack, setCurrentTrack] = useState<Track | null>(null); | ||
const [currentTrackAudio, setCurrentTrackAudio] = useState<HTMLAudioElement | null>(null); | ||
const [isPlaying, setIsPlaying] = useState(false); | ||
const [duration, setDuration] = useState(0); | ||
const [currentTime, setCurrentTime] = useState(0); | ||
const [slider, setSlider] = useState(1); | ||
const [drag, setDrag] = useState(0); | ||
|
||
useEffect(() => { | ||
if (!currentTrack) return; | ||
if (isPlaying) { | ||
pause(); | ||
setCurrentTrackAudio(null); | ||
} | ||
const tempAudio = new Audio(currentTrack.preview_url); | ||
|
||
const setAudioData = () => { | ||
setDuration(tempAudio.duration); | ||
setCurrentTime(tempAudio.currentTime); | ||
}; | ||
|
||
const setAudioTime = () => { | ||
const currTime = tempAudio.currentTime; | ||
setCurrentTime(currTime); | ||
setSlider(currTime ? Number(((currTime * 100) / tempAudio.duration).toFixed(1)) : 0); | ||
}; | ||
|
||
tempAudio.addEventListener("loadeddata", setAudioData); | ||
tempAudio.addEventListener("timeupdate", setAudioTime); | ||
tempAudio.preload = "none"; | ||
|
||
setCurrentTrackAudio(tempAudio); | ||
console.log("audio set"); | ||
|
||
return () => { | ||
pause(); | ||
setCurrentTrackAudio(null); | ||
}; | ||
}, [currentTrack]); | ||
|
||
useEffect(() => { | ||
const handlePlay = async () => { | ||
if (currentTrackAudio) { | ||
await play(); | ||
} | ||
}; | ||
handlePlay(); | ||
}, [currentTrackAudio]); | ||
|
||
const togglePlay = async () => { | ||
if (isPlaying) pause(); | ||
else await play(); | ||
}; | ||
|
||
const play = async () => { | ||
setIsPlaying(true); | ||
await currentTrackAudio?.play(); | ||
}; | ||
|
||
const pause = () => { | ||
setIsPlaying(false); | ||
currentTrackAudio?.pause(); | ||
}; | ||
|
||
useEffect(() => { | ||
if (currentTrackAudio && drag) { | ||
currentTrackAudio.currentTime = Math.round((drag * currentTrackAudio.duration) / 100); | ||
} | ||
}, [drag]); | ||
|
||
return ( | ||
<PlayerContext.Provider | ||
value={{ | ||
currentTrack, | ||
currentTrackAudio, | ||
setCurrentTrack, | ||
isPlaying, | ||
play, | ||
pause, | ||
togglePlay, | ||
duration, | ||
currentTime, | ||
slider, | ||
setSlider, | ||
drag, | ||
setDrag, | ||
}} | ||
> | ||
{children} | ||
</PlayerContext.Provider> | ||
); | ||
} | ||
|
||
export const usePlayer = () => useContext(PlayerContext); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { Dispatch, SetStateAction, createContext, useContext, useState } from "react"; | ||
import axios from "axios"; | ||
import { PlaylistType, SearchResults, Track } from "~~/types/spotify"; | ||
|
||
interface ContextProps { | ||
playlists: PlaylistType[]; | ||
searchResults: SearchResults | null; | ||
query: string; | ||
setQuery: Dispatch<SetStateAction<string>>; | ||
fetchPlaylists: () => void; | ||
fetchSearchResults: (query: string) => void; | ||
currentTrack: Track | null; | ||
setCurrentTrack: Dispatch<SetStateAction<Track | null>>; | ||
tracksQueue: Track[]; | ||
setTracksQueue: Dispatch<SetStateAction<Track[]>>; | ||
} | ||
|
||
const SpotifyContext = createContext({} as ContextProps); | ||
|
||
export const SpotifyProvider = ({ children }: any) => { | ||
const [playlists, setPlaylists] = useState<PlaylistType[]>([]); | ||
const [searchResults, setSearchResults] = useState<SearchResults | null>(null); | ||
const [tracksQueue, setTracksQueue] = useState<Track[]>([]); | ||
const [currentTrack, setCurrentTrack] = useState<Track | null>(null); | ||
const [query, setQuery] = useState(""); | ||
|
||
const fetchPlaylists = async () => { | ||
try { | ||
const resp = await axios.get("/api/playlists"); | ||
const data = resp.data; | ||
setPlaylists(data.items); | ||
} catch (err) { | ||
console.log(err); | ||
} | ||
}; | ||
|
||
const fetchSearchResults = async () => { | ||
try { | ||
const resp = await axios.get(`/api/search?q=${query}`); | ||
setSearchResults(resp.data); | ||
} catch (err) { | ||
console.error(err); | ||
} | ||
}; | ||
|
||
return ( | ||
<SpotifyContext.Provider | ||
value={{ | ||
playlists, | ||
fetchPlaylists, | ||
query, | ||
setQuery, | ||
searchResults, | ||
fetchSearchResults, | ||
currentTrack, | ||
setCurrentTrack, | ||
tracksQueue, | ||
setTracksQueue, | ||
}} | ||
> | ||
{children} | ||
</SpotifyContext.Provider> | ||
); | ||
}; | ||
|
||
export const useSpotify = () => useContext(SpotifyContext); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters