-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Co-authored-by: JD Francis <jd@audius.co>
- Loading branch information
1 parent
fdc7966
commit 482e382
Showing
6 changed files
with
231 additions
and
28 deletions.
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
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
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
118 changes: 118 additions & 0 deletions
118
packages/web/src/pages/sign-up-page/utils/selectArtistsPreviewContext.tsx
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,118 @@ | ||
import { createContext, useCallback, useEffect, useState } from 'react' | ||
|
||
import { | ||
ID, | ||
encodeHashId, | ||
useGetUserById, | ||
useGetUserTracksByHandle | ||
} from '@audius/common' | ||
import { useUnmount } from 'react-use' | ||
|
||
import { audioPlayer } from 'services/audio-player' | ||
import { apiClient } from 'services/audius-api-client' | ||
|
||
type PreviewContextProps = { | ||
isPlaying: boolean | ||
nowPlayingArtistId: number | ||
playPreview: (artistId: ID) => void | ||
stopPreview: () => void | ||
togglePreview: (artistId: ID) => void | ||
} | ||
|
||
export const SelectArtistsPreviewContext = createContext<PreviewContextProps>({ | ||
isPlaying: false, | ||
nowPlayingArtistId: -1, | ||
playPreview: () => {}, | ||
stopPreview: () => {}, | ||
togglePreview: () => {} | ||
}) | ||
|
||
export const SelectArtistsPreviewContextProvider = (props: { | ||
children: JSX.Element | ||
}) => { | ||
const [isPlaying, setIsPlaying] = useState(false) | ||
const [nowPlayingArtistId, setNowPlayingArtistId] = useState<number>(-1) | ||
const [trackId, setTrackId] = useState<string | null>(null) | ||
|
||
const { data: artist } = useGetUserById({ | ||
id: nowPlayingArtistId, | ||
currentUserId: null | ||
}) | ||
const { data: artistTracks } = useGetUserTracksByHandle( | ||
{ | ||
handle: artist?.handle, | ||
currentUserId: null | ||
}, | ||
{ disabled: !artist?.handle } | ||
) | ||
|
||
useEffect(() => { | ||
const trackId = artistTracks?.find((track) => track.is_available)?.track_id | ||
trackId && setTrackId(encodeHashId(trackId)) | ||
}, [artistTracks]) | ||
|
||
const togglePlayback = useCallback(() => { | ||
if (audioPlayer.isPlaying()) { | ||
audioPlayer.pause() | ||
setIsPlaying(false) | ||
} else { | ||
audioPlayer.play() | ||
setIsPlaying(true) | ||
} | ||
}, []) | ||
|
||
const stopPreview = useCallback(() => { | ||
audioPlayer.stop() | ||
setNowPlayingArtistId(-1) | ||
setTrackId(null) | ||
setIsPlaying(false) | ||
}, []) | ||
|
||
const playPreview = useCallback((artistId: ID) => { | ||
if (audioPlayer.isPlaying()) { | ||
audioPlayer.stop() | ||
} | ||
setNowPlayingArtistId(artistId) | ||
setIsPlaying(true) | ||
}, []) | ||
|
||
const togglePreview = useCallback( | ||
(artistId: ID) => { | ||
if (artistId === nowPlayingArtistId) { | ||
togglePlayback() | ||
} else { | ||
audioPlayer.stop() | ||
setIsPlaying(false) | ||
setNowPlayingArtistId(artistId) | ||
} | ||
}, | ||
[nowPlayingArtistId, togglePlayback] | ||
) | ||
|
||
useEffect(() => { | ||
if (!trackId) return | ||
audioPlayer.load( | ||
0, | ||
stopPreview, | ||
apiClient.makeUrl(`/tracks/${trackId}/stream`) | ||
) | ||
audioPlayer.play() | ||
setIsPlaying(true) | ||
}, [nowPlayingArtistId, stopPreview, trackId]) | ||
|
||
useUnmount(stopPreview) | ||
|
||
return ( | ||
<SelectArtistsPreviewContext.Provider | ||
value={{ | ||
isPlaying, | ||
nowPlayingArtistId, | ||
playPreview, | ||
stopPreview, | ||
togglePreview | ||
}} | ||
> | ||
{props.children} | ||
</SelectArtistsPreviewContext.Provider> | ||
) | ||
} |