-
Notifications
You must be signed in to change notification settings - Fork 132
[C-3371] FollowArtistTile Play Track Preview #6769
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
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
22ad806
Revert "revert dev setup"
amendelsohn ce9be0c
wip
amendelsohn 803f750
Merge branch 'main' into am-artist-tile-preview
amendelsohn b9b1021
fix Avatar usage
amendelsohn 20ca296
Merge branch 'main' into am-artist-tile-preview
amendelsohn 68de3ff
Merge branch 'main' into am-artist-tile-preview
amendelsohn cbb6ade
add preview player context
amendelsohn 13e41fc
disable header for dev
amendelsohn 7e507f2
Merge branch 'main' into am-artist-tile-preview
amendelsohn db4f02b
working playback controls
amendelsohn 7cd0efd
audio fetching works!!
amendelsohn 421e78e
basic overlay; fix playback switching behavior
amendelsohn 4882232
revert dev changes
amendelsohn f74fc04
Merge branch 'main' into am-artist-tile-preview
amendelsohn b46abac
properly stop playback on unmount
amendelsohn 15aa635
Merge branch 'main' of github.com:AudiusProject/audius-protocol into …
DejayJD File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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> | ||
| ) | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
nit: I wonder if we're taking the inline css a little too far here 😅, this is about ~13 style rules and not just "quick one-off" things. IMO this is still a good use case for a class but maybe thats just me
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.
Good point. Wondering what the meta is here post-harmony. I'm down for either, and please feel free to push changes to this if you see fit