Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 134
[C-3668] Improve artist previews#7308
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -2,14 +2,15 @@ import { createContext, useCallback, useEffect, useState } from 'react' | ||
| import { | ||
| ID, | ||
| encodeHashId, | ||
| UserTrackMetadata, | ||
| playerActions, | ||
| useGetUserById, | ||
| useGetUserTracksByHandle | ||
| } from '@audius/common' | ||
| import { useDispatch } from 'react-redux' | ||
| import { useUnmount } from 'react-use' | ||
| import { audioPlayer } from 'services/audio-player' | ||
| import { apiClient } from 'services/audius-api-client' | ||
| type PreviewContextProps = { | ||
| isPlaying: boolean | ||
| @@ -32,7 +33,8 @@ export const SelectArtistsPreviewContextProvider = (props: { | ||
| }) => { | ||
| const [isPlaying, setIsPlaying] = useState(false) | ||
| const [nowPlayingArtistId, setNowPlayingArtistId] = useState<number>(-1) | ||
| const [trackId, setTrackId] = useState<string | null>(null) | ||
| const [track, setTrack] = useState<UserTrackMetadata | null>(null) | ||
| const dispatch = useDispatch() | ||
| const { data: artist } = useGetUserById({ | ||
| id: nowPlayingArtistId, | ||
| @@ -41,64 +43,73 @@ export const SelectArtistsPreviewContextProvider = (props: { | ||
| const { data: artistTracks } = useGetUserTracksByHandle( | ||
| { | ||
| handle: artist?.handle, | ||
| currentUserId: null | ||
| currentUserId: null, | ||
| // Unlikely we cant play an artist's first 3 tracks. | ||
| limit: 3 | ||
Comment on lines
+47
to
+48
Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice add! | ||
| }, | ||
| { disabled: !artist?.handle } | ||
| ) | ||
| useEffect(() => { | ||
| const trackId = artistTracks?.find((track) => track.is_available)?.track_id | ||
| trackId && setTrackId(encodeHashId(trackId)) | ||
| const track = artistTracks?.find((track) => track.is_available) | ||
| if (track) { | ||
| setTrack(track) | ||
| } | ||
| }, [artistTracks]) | ||
| const togglePlayback = useCallback(() => { | ||
| if (audioPlayer.isPlaying()) { | ||
| audioPlayer.pause() | ||
| dispatch(playerActions.pause()) | ||
Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. interesting, for my own learning; what was the difference here? ContributorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for pause i don't know how much it matters, but i wanted to make sure the redux store stayed in sync with the common user actions rather than having play go through redux, and pause go through audioplayer directly | ||
| setIsPlaying(false) | ||
| } else { | ||
| audioPlayer.play() | ||
| dispatch(playerActions.play()) | ||
| setIsPlaying(true) | ||
| } | ||
| }, []) | ||
| }, [dispatch]) | ||
| const stopPreview = useCallback(() => { | ||
| audioPlayer.stop() | ||
| setNowPlayingArtistId(-1) | ||
| setTrackId(null) | ||
| setTrack(null) | ||
| setIsPlaying(false) | ||
| }, []) | ||
| const playPreview = useCallback((artistId: ID) => { | ||
| if (audioPlayer.isPlaying()) { | ||
| audioPlayer.stop() | ||
| } | ||
| setNowPlayingArtistId(artistId) | ||
| setIsPlaying(true) | ||
| }, []) | ||
| const playPreview = useCallback( | ||
| (artistId: ID) => { | ||
| if (audioPlayer.isPlaying()) { | ||
| dispatch(playerActions.stop({})) | ||
| } | ||
| setNowPlayingArtistId(artistId) | ||
| setIsPlaying(true) | ||
| }, | ||
| [dispatch] | ||
| ) | ||
| const togglePreview = useCallback( | ||
| (artistId: ID) => { | ||
| if (artistId === nowPlayingArtistId) { | ||
| togglePlayback() | ||
| } else { | ||
| audioPlayer.stop() | ||
| dispatch(playerActions.stop({})) | ||
| setIsPlaying(false) | ||
| setNowPlayingArtistId(artistId) | ||
| } | ||
| }, | ||
| [nowPlayingArtistId, togglePlayback] | ||
| [nowPlayingArtistId, togglePlayback, dispatch] | ||
| ) | ||
| useEffect(() => { | ||
| if (!trackId) return | ||
| audioPlayer.load( | ||
| 0, | ||
| stopPreview, | ||
| apiClient.makeUrl(`/tracks/${trackId}/stream`) | ||
| ) | ||
| audioPlayer.play() | ||
| if (!track) return | ||
| const { track_id, preview_cid, duration } = track | ||
| const isPreview = !!preview_cid | ||
| const startTime = isPreview | ||
| ? undefined | ||
| : Math.min(30, Math.max(0, duration - 30)) | ||
| dispatch(playerActions.play({ trackId: track_id, startTime, isPreview })) | ||
| setIsPlaying(true) | ||
| }, [nowPlayingArtistId, stopPreview, trackId]) | ||
| }, [nowPlayingArtistId, stopPreview, track, dispatch]) | ||
| useUnmount(stopPreview) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🙌
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks yeah this was that callout reed had