Skip to content

Commit

Permalink
fix: when two tracks was played same time
Browse files Browse the repository at this point in the history
  • Loading branch information
Raul Andrade committed Mar 1, 2022
1 parent d721826 commit df70e17
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 48 deletions.
28 changes: 24 additions & 4 deletions src/components/audio-player/audio-player.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useEffect } from 'react'
import { useContext, useEffect } from 'react'
import { useAudio as ReactUseAudio } from 'react-use'
import { PlayIcon, PauseIcon } from '@heroicons/react/solid'
import { AudioContext } from 'contexts'

type AudioPlayerProps = {
src: string
Expand All @@ -16,13 +17,32 @@ export const AudioPlayer = ({
autoPlay: false
})

const { volume } = controls
const { source, setSource } = useContext(AudioContext)

const { volume, play, pause } = controls

useEffect(() => {
if (src !== source) {
pause()
}
}, [source, src, pause])

useEffect(() => {
/**
* @disclaimer
* I don't know why when I add volume function to array deps
* this component receive this error
* "Maximum update depth exceeded. This can happen when a component calls setState inside"
*/
volume(0.1)
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])

const handlePlay = () => {
play()
setSource?.(src)
}

return (
<div className="flex justify-center p-2">
{audio}
Expand All @@ -33,7 +53,7 @@ export const AudioPlayer = ({
hover:scale-110
transition duration-200 ease-out hover:ease-in"
type="button"
onClick={controls.pause}
onClick={pause}
/>
) : (
<PlayIcon
Expand All @@ -42,7 +62,7 @@ export const AudioPlayer = ({
hover:scale-110
transition duration-200 ease-out hover:ease-in"
type="button"
onClick={controls.play}
onClick={handlePlay}
/>
)}
</div>
Expand Down
5 changes: 5 additions & 0 deletions src/components/image-box/image-box.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ import userEvent from '@testing-library/user-event'
import { ImageBox } from './image-box'

describe('<ImageBox />', () => {
beforeEach(() => {
window.HTMLMediaElement.prototype.play = jest.fn()
window.HTMLMediaElement.prototype.pause = jest.fn()
})

const heartIconPath = 'div > div:nth-child(2) > svg > path'
const onLikeMock = jest.fn()

Expand Down
12 changes: 12 additions & 0 deletions src/contexts/audio-context/audio-context.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { createContext } from 'react'

type AudioContextProps = {
source: string
setSource?: (source: string) => void
}

const InitialValues: AudioContextProps = {
source: ''
}

export const AudioContext = createContext<AudioContextProps>(InitialValues)
1 change: 1 addition & 0 deletions src/contexts/audio-context/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { AudioContext } from './audio-context'
1 change: 1 addition & 0 deletions src/contexts/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { AudioContext } from './audio-context'
95 changes: 51 additions & 44 deletions src/templates/home/authenticated.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
import { usePlaylistMutation, useRecommendation } from 'queries'
import { Recommendation } from 'types'
import { MAX_RANDOM_FY_ITEMS } from '../../constants'
import { AudioContext } from 'contexts'

type AuthenticatedProps = {
artistId: string
Expand All @@ -29,6 +30,8 @@ export const Authenticated = ({ artistId }: AuthenticatedProps) => {
trackId: ''
}))

const [currentPlaying, setCurrentPlaying] = useState('')

const { data, isError, isLoading, isRefetching } = useRecommendation(
liked.id,
liked.trackId
Expand All @@ -48,52 +51,56 @@ export const Authenticated = ({ artistId }: AuthenticatedProps) => {
}

return (
<Content>
{likedArtists.length < MAX_RANDOM_FY_ITEMS && (
<>
<div
className={`mt-8 p-4 flex items-center ${
isRefetching ? 'animate-pulse' : ''
}`}
>
<Title>Randomfy</Title>
{isRefetching && <span className="ml-4">refetching...</span>}
</div>
<Grid>
{isLoading && <ImageBoxSkeleton />}
{data?.data.map((artist) => (
<ImageBox
key={artist.id}
imgSrc={artist.track.images[1].url}
audioSrc={artist.track.previewUrl}
track={artist.track.name}
artist={artist.name}
onLike={() => handleLike(artist)}
/>
))}
</Grid>
</>
)}
<AudioContext.Provider
value={{ source: currentPlaying, setSource: setCurrentPlaying }}
>
<Content>
{likedArtists.length < MAX_RANDOM_FY_ITEMS && (
<>
<div
className={`mt-8 p-4 flex items-center ${
isRefetching ? 'animate-pulse' : ''
}`}
>
<Title>Randomfy</Title>
{isRefetching && <span className="ml-4">refetching...</span>}
</div>
<Grid>
{isLoading && <ImageBoxSkeleton />}
{data?.data.map((artist) => (
<ImageBox
key={artist.id}
imgSrc={artist.track.images[1].url}
audioSrc={artist.track.previewUrl}
track={artist.track.name}
artist={artist.name}
onLike={() => handleLike(artist)}
/>
))}
</Grid>
</>
)}

<HTMLToImage artists={likedArtists} />
<HTMLToImage artists={likedArtists} />

{likedArtists.length === MAX_RANDOM_FY_ITEMS && (
<>
<div className="p-4" id="subtitle">
<SubTitle>You can make a randomfy playlist!</SubTitle>
</div>
<div className="flex justify-center p-4">
<button
className="flex justify-evenly items-center w-56 p-3 rounded-full font-semibold
{likedArtists.length === MAX_RANDOM_FY_ITEMS && (
<>
<div className="p-4" id="subtitle">
<SubTitle>You can make a randomfy playlist!</SubTitle>
</div>
<div className="flex justify-center p-4">
<button
className="flex justify-evenly items-center w-56 p-3 rounded-full font-semibold
text-center text-gray-100 dark:text-gray-800 bg-gray-900 dark:bg-gray-200"
aria-label="create playlist button"
onClick={() => mutation.mutate(likedArtists)}
>
Create Playlist
</button>
</div>
</>
)}
</Content>
aria-label="create playlist button"
onClick={() => mutation.mutate(likedArtists)}
>
Create Playlist
</button>
</div>
</>
)}
</Content>
</AudioContext.Provider>
)
}

1 comment on commit df70e17

@vercel
Copy link

@vercel vercel bot commented on df70e17 Mar 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.