-
Notifications
You must be signed in to change notification settings - Fork 13
Fix/audio state across page #29
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
base: master
Are you sure you want to change the base?
Changes from all commits
a4e3baf
0cbd77c
7e9d374
c73faf3
d58fca5
a8940ea
74ecc3c
5507528
b303ad4
8337628
6cb0042
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
|
|
||
| ## Code Rules | ||
| - Follow ESLint config defined in `./eslint.config.mjs` | ||
| - Max cyclomatic complexity per function: **5** | ||
| - Max parameters per function: **3** | ||
| - Max file length: **100 lines** — break into smaller files if exceeded | ||
| - Extract shared/common logic into utils | ||
| - Keep constants in a separate constants file | ||
| - Write proper test cases for every feature | ||
| - Build small features first before moving forward | ||
|
|
||
|
|
||
| DISTILLED_AESTHETICS_PROMPT = """ | ||
| <frontend_aesthetics> | ||
| You tend to converge toward generic, "on distribution" outputs. In frontend design, this creates what users call the "AI slop" aesthetic. Avoid this: make creative, distinctive frontends that surprise and delight. Focus on: | ||
|
|
||
| Typography: Choose fonts that are beautiful, unique, and interesting. Avoid generic fonts like Arial and Inter; opt instead for distinctive choices that elevate the frontend's aesthetics. | ||
|
|
||
| Color & Theme: Commit to a cohesive aesthetic. Use CSS variables for consistency. Dominant colors with sharp accents outperform timid, evenly-distributed palettes. Draw from Music Application themes and cultural aesthetics for inspiration. | ||
|
|
||
| Motion: Use animations for effects and micro-interactions. Prioritize CSS-only solutions for HTML. Use Motion library for React when available. Focus on high-impact moments: one well-orchestrated page load with staggered reveals (animation-delay) creates more delight than scattered micro-interactions. | ||
|
|
||
| Backgrounds: Create atmosphere and depth rather than defaulting to solid colors. Layer CSS gradients, use geometric patterns, or add contextual effects that match the overall aesthetic. | ||
|
|
||
| Avoid generic AI-generated aesthetics: | ||
| - Overused font families (Inter, Roboto, Arial, system fonts) | ||
| - Clichéd color schemes (particularly purple gradients on white backgrounds) | ||
| - Predictable layouts and component patterns | ||
| - Cookie-cutter design that lacks context-specific character | ||
|
|
||
| Interpret creatively and make unexpected choices that feel genuinely designed for the context. Vary between light and dark themes, different fonts, different aesthetics. You still tend to converge on common choices (Space Grotesk, for example) across generations. Avoid this: it is critical that you think outside the box! | ||
| </frontend_aesthetics> | ||
| """ | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import React from 'react'; | ||
| import PropTypes from 'prop-types'; | ||
| import { PlayCircleFilled, PauseCircleFilled } from '@ant-design/icons'; | ||
| import { ArtworkWrapper, Artwork, PlayOverlay } from '@components/styled/artworkPlayButton'; | ||
|
|
||
| const ArtworkPlayButton = ({ src, alt, isPlaying, isActive, onClick }) => { | ||
| const handleClick = (e) => { | ||
| e.stopPropagation(); | ||
| onClick(); | ||
| }; | ||
|
|
||
| return ( | ||
| <ArtworkWrapper onClick={handleClick} data-testid="artwork-play-btn"> | ||
| <Artwork src={src} alt={alt} /> | ||
| <PlayOverlay isActive={isActive} data-overlay data-testid="play-overlay"> | ||
| {isActive && isPlaying ? <PauseCircleFilled /> : <PlayCircleFilled />} | ||
| </PlayOverlay> | ||
| </ArtworkWrapper> | ||
| ); | ||
| }; | ||
|
|
||
| ArtworkPlayButton.propTypes = { | ||
| src: PropTypes.string.isRequired, | ||
| alt: PropTypes.string.isRequired, | ||
| isPlaying: PropTypes.bool.isRequired, | ||
| isActive: PropTypes.bool.isRequired, | ||
| onClick: PropTypes.func.isRequired | ||
| }; | ||
|
|
||
| export default ArtworkPlayButton; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| import React from 'react' | ||
| import { fireEvent } from '@testing-library/react' | ||
| import { renderProvider } from '@utils/testUtils' | ||
| import ArtworkPlayButton from '../index' | ||
|
|
||
| describe('<ArtworkPlayButton />', () => { | ||
| const defaultProps = { | ||
| src: 'art.jpg', | ||
| alt: 'Song Art', | ||
| isPlaying: false, | ||
| isActive: false, | ||
| onClick: jest.fn() | ||
| } | ||
|
|
||
| beforeEach(() => jest.clearAllMocks()) | ||
|
|
||
| it('should render the artwork image', () => { | ||
| const { getByAltText } = renderProvider( | ||
| <ArtworkPlayButton {...defaultProps} /> | ||
| ) | ||
| expect(getByAltText('Song Art')).toBeTruthy() | ||
| }) | ||
|
|
||
| it('should show PlayCircleFilled when not active', () => { | ||
| const { getByTestId } = renderProvider( | ||
| <ArtworkPlayButton {...defaultProps} /> | ||
| ) | ||
| expect(getByTestId('play-overlay')).toBeTruthy() | ||
| }) | ||
|
|
||
| it('should show PauseCircleFilled when active and playing', () => { | ||
| const props = { ...defaultProps, isActive: true, isPlaying: true } | ||
| const { getByTestId } = renderProvider(<ArtworkPlayButton {...props} />) | ||
| expect(getByTestId('play-overlay')).toBeTruthy() | ||
| }) | ||
|
|
||
| it('should call onClick and stop propagation', () => { | ||
| const { getByTestId } = renderProvider( | ||
| <ArtworkPlayButton {...defaultProps} /> | ||
| ) | ||
| const wrapper = getByTestId('artwork-play-btn') | ||
| const parentClick = jest.fn() | ||
| wrapper.parentElement.addEventListener('click', parentClick) | ||
| fireEvent.click(wrapper) | ||
| expect(defaultProps.onClick).toHaveBeenCalledTimes(1) | ||
| }) | ||
| }) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| import { DEFAULT_VOLUME } from './constants'; | ||
|
|
||
| let audioInstance = null; | ||
|
|
||
| export const getAudioInstance = () => { | ||
| if (!audioInstance) { | ||
| audioInstance = new Audio(); | ||
| audioInstance.volume = DEFAULT_VOLUME; | ||
| } | ||
| return audioInstance; | ||
|
Comment on lines
+5
to
+10
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. 🧩 Analysis chain🏁 Script executed: # Find the audioSingleton.js file and examine its contents
find . -name "audioSingleton.js" -type f 2>/dev/null | head -5Repository: wednesday-solutions/nextjs-template Length of output: 125 🏁 Script executed: # Search for all calls to getAudioInstance to see the usage patterns
rg -n "getAudioInstance" --type js --type jsx --type ts --type tsx -A 3 -B 3 2>/dev/null | head -100Repository: wednesday-solutions/nextjs-template Length of output: 61 🏁 Script executed: # Check if this is a Next.js project
ls -la | grep -E "next.config|package.json" && head -30 package.json 2>/dev/nullRepository: wednesday-solutions/nextjs-template Length of output: 1411 🏁 Script executed: # Read the audioSingleton.js file
cat -n ./app/components/AudioPlayer/audioSingleton.jsRepository: wednesday-solutions/nextjs-template Length of output: 400 🏁 Script executed: # Search more broadly for getAudioInstance, audioInstance, or AudioPlayer imports
rg "getAudioInstance|audioInstance|audioSingleton" --type js --type jsx --type ts --type tsx 2>/dev/nullRepository: wednesday-solutions/nextjs-template Length of output: 61 🏁 Script executed: # Check what's in the AudioPlayer directory
ls -la ./app/components/AudioPlayer/Repository: wednesday-solutions/nextjs-template Length of output: 573 🏁 Script executed: # Check useAudioSetup.js
cat -n ./app/components/AudioPlayer/useAudioSetup.jsRepository: wednesday-solutions/nextjs-template Length of output: 1520 🏁 Script executed: # Check useAudioPlayer.js
cat -n ./app/components/AudioPlayer/useAudioPlayer.jsRepository: wednesday-solutions/nextjs-template Length of output: 1983 🏁 Script executed: # Check the main index.js component
cat -n ./app/components/AudioPlayer/index.jsRepository: wednesday-solutions/nextjs-template Length of output: 3369 🏁 Script executed: # Search for dynamic imports or 'use client' directives
rg "dynamic|'use client'|\"use client\"|ssr: false" app/components/AudioPlayer --type js --type jsxRepository: wednesday-solutions/nextjs-template Length of output: 106 🏁 Script executed: # Check if AudioPlayer is imported dynamically anywhere
rg "AudioPlayer|audioSingleton" --type js --type jsx -B 2 | head -50Repository: wednesday-solutions/nextjs-template Length of output: 106 🏁 Script executed: # Search for dynamic imports and 'use client' with correct file types
rg "dynamic|use client|ssr:" app/components/AudioPlayer --type js -B 2Repository: wednesday-solutions/nextjs-template Length of output: 61 🏁 Script executed: # Broader search for AudioPlayer imports in the codebase
rg "AudioPlayer|from.*audioSingleton" --type js -B 1 -A 1Repository: wednesday-solutions/nextjs-template Length of output: 8269
The proposed fix ( A complete fix requires one of these approaches:
🤖 Prompt for AI Agents |
||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export const DEFAULT_VOLUME = 0.7; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| import React from 'react'; | ||
| import PropTypes from 'prop-types'; | ||
| import { | ||
| StepBackwardFilled, | ||
| PlayCircleFilled, | ||
| PauseCircleFilled, | ||
| StepForwardFilled, | ||
| SoundFilled | ||
| } from '@ant-design/icons'; | ||
| import { useAudioPlayer } from './useAudioPlayer'; | ||
| import { | ||
| PlayerContainer, | ||
| NowPlayingArt, | ||
| PlayerTrackInfo, | ||
| TrackTitle, | ||
| TrackArtist, | ||
| PlayerControls, | ||
| ControlButton, | ||
| ProgressSlider, | ||
| VolumeGroup, | ||
| VolumeSlider | ||
| } from '@components/styled/playerBar'; | ||
|
|
||
| const AudioPlayer = ({ currentSong, onNext, onPrev, onPlayStateChange, onRegisterToggle }) => { | ||
| const player = useAudioPlayer(currentSong, onNext, onPlayStateChange); | ||
|
|
||
| React.useEffect(() => { | ||
| if (onRegisterToggle) { | ||
| onRegisterToggle(player.togglePlay); | ||
| } | ||
| }, [player.togglePlay, onRegisterToggle]); | ||
|
|
||
| if (!currentSong) { | ||
| return null; | ||
| } | ||
|
|
||
| return ( | ||
| <PlayerContainer data-testid="audio-player"> | ||
| <NowPlayingArt src={currentSong.artworkUrl} alt={currentSong.trackName} /> | ||
| <PlayerTrackInfo> | ||
| <TrackTitle>{currentSong.trackName}</TrackTitle> | ||
| <TrackArtist>{currentSong.artistName}</TrackArtist> | ||
| </PlayerTrackInfo> | ||
| <PlayerControls> | ||
| <ControlButton data-testid="prev-btn" onClick={onPrev}> | ||
| <StepBackwardFilled /> | ||
| </ControlButton> | ||
| <ControlButton data-testid="play-btn" primary onClick={player.togglePlay}> | ||
| {player.isPlaying ? <PauseCircleFilled /> : <PlayCircleFilled />} | ||
| </ControlButton> | ||
| <ControlButton data-testid="next-btn" onClick={onNext}> | ||
| <StepForwardFilled /> | ||
| </ControlButton> | ||
| </PlayerControls> | ||
| <ProgressSlider | ||
| data-testid="progress-slider" | ||
| type="range" | ||
| min={0} | ||
| max={player.duration || 0} | ||
| value={player.currentTime} | ||
| onChange={(e) => player.seek(Number(e.target.value))} | ||
| style={{ '--fill': `${player.duration ? (player.currentTime / player.duration) * 100 : 0}%` }} | ||
| /> | ||
| <VolumeGroup> | ||
| <SoundFilled data-testid="volume-icon" style={{ color: '#ff6b35', fontSize: '1rem' }} /> | ||
| <VolumeSlider | ||
| data-testid="volume-slider" | ||
| type="range" | ||
| min={0} | ||
| max={1} | ||
| step={0.05} | ||
| value={player.volume} | ||
| onChange={(e) => player.setVolume(Number(e.target.value))} | ||
| style={{ '--fill': `${player.volume * 100}%` }} | ||
| /> | ||
| </VolumeGroup> | ||
| </PlayerContainer> | ||
| ); | ||
| }; | ||
|
|
||
| AudioPlayer.propTypes = { | ||
| currentSong: PropTypes.object, | ||
| onNext: PropTypes.func.isRequired, | ||
| onPrev: PropTypes.func.isRequired, | ||
| onPlayStateChange: PropTypes.func, | ||
| onRegisterToggle: PropTypes.func | ||
| }; | ||
|
|
||
| export default AudioPlayer; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| import React from 'react' | ||
| import { renderProvider } from '@utils/testUtils' | ||
| import AudioPlayer from '../index' | ||
|
|
||
| jest.mock('../useAudioPlayer', () => ({ | ||
| useAudioPlayer: () => ({ | ||
| isPlaying: false, | ||
| currentTime: 0, | ||
| duration: 30, | ||
| volume: 0.7, | ||
| togglePlay: jest.fn(), | ||
| seek: jest.fn(), | ||
| setVolume: jest.fn() | ||
| }) | ||
| })) | ||
|
|
||
| const mockSong = { | ||
| trackId: 1, | ||
| trackName: 'Test Song', | ||
| artistName: 'Test Artist', | ||
| artworkUrl: 'test.jpg', | ||
| previewUrl: 'test.mp3' | ||
| } | ||
|
|
||
| describe('<AudioPlayer />', () => { | ||
| const mockNext = jest.fn() | ||
| const mockPrev = jest.fn() | ||
|
|
||
| it('should render nothing when no currentSong', () => { | ||
| const { container } = renderProvider( | ||
| <AudioPlayer currentSong={null} onNext={mockNext} onPrev={mockPrev} /> | ||
| ) | ||
| expect(container.innerHTML).toBe('') | ||
| }) | ||
|
|
||
| it('should render the player when a song is provided', () => { | ||
| const { getByTestId } = renderProvider( | ||
| <AudioPlayer currentSong={mockSong} onNext={mockNext} onPrev={mockPrev} /> | ||
| ) | ||
| expect(getByTestId('audio-player')).toBeTruthy() | ||
| }) | ||
|
|
||
| it('should display track info', () => { | ||
| const { getByText } = renderProvider( | ||
| <AudioPlayer currentSong={mockSong} onNext={mockNext} onPrev={mockPrev} /> | ||
| ) | ||
| expect(getByText('Test Song')).toBeTruthy() | ||
| expect(getByText('Test Artist')).toBeTruthy() | ||
| }) | ||
|
|
||
| it('should render all control buttons', () => { | ||
| const { getByTestId } = renderProvider( | ||
| <AudioPlayer currentSong={mockSong} onNext={mockNext} onPrev={mockPrev} /> | ||
| ) | ||
| expect(getByTestId('prev-btn')).toBeTruthy() | ||
| expect(getByTestId('play-btn')).toBeTruthy() | ||
| expect(getByTestId('next-btn')).toBeTruthy() | ||
| }) | ||
|
|
||
| it('should render progress and volume sliders', () => { | ||
| const { getByTestId } = renderProvider( | ||
| <AudioPlayer currentSong={mockSong} onNext={mockNext} onPrev={mockPrev} /> | ||
| ) | ||
| expect(getByTestId('progress-slider')).toBeTruthy() | ||
| expect(getByTestId('volume-icon')).toBeTruthy() | ||
| expect(getByTestId('volume-slider')).toBeTruthy() | ||
| }) | ||
| }) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| import { renderHook, act } from '@testing-library/react' | ||
|
|
||
| const mockPlay = jest.fn().mockResolvedValue(undefined) | ||
| const mockPause = jest.fn() | ||
| let mockAudioInstance | ||
|
|
||
| jest.mock('../audioSingleton', () => ({ | ||
| getAudioInstance: () => mockAudioInstance | ||
| })) | ||
|
|
||
| const { useAudioPlayer } = require('../useAudioPlayer') | ||
|
|
||
| beforeEach(() => { | ||
| mockPlay.mockClear() | ||
| mockPause.mockClear() | ||
| mockAudioInstance = { | ||
| play: mockPlay, | ||
| pause: mockPause, | ||
| volume: 0.7, | ||
| currentTime: 0, | ||
| duration: 0, | ||
| src: '', | ||
| paused: true, | ||
| addEventListener: jest.fn(), | ||
| removeEventListener: jest.fn() | ||
| } | ||
| }) | ||
|
|
||
| describe('useAudioPlayer', () => { | ||
| const song = { trackId: 1, previewUrl: 'http://test.mp3', trackName: 'Test' } | ||
|
|
||
| it('should initialize with default values', () => { | ||
| const { result } = renderHook(() => useAudioPlayer(null, jest.fn())) | ||
| expect(result.current.isPlaying).toBe(false) | ||
| expect(result.current.volume).toBe(0.7) | ||
| expect(result.current.currentTime).toBe(0) | ||
| expect(result.current.duration).toBe(0) | ||
| }) | ||
|
|
||
| it('should play when a new song is selected', () => { | ||
| const { rerender } = renderHook(({ s }) => useAudioPlayer(s, jest.fn()), { | ||
| initialProps: { s: null } | ||
| }) | ||
| rerender({ s: song }) | ||
| expect(mockAudioInstance.src).toBe(song.previewUrl) | ||
| expect(mockPlay).toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('should not auto-play on remount with same song', () => { | ||
| renderHook(() => useAudioPlayer(song, jest.fn())) | ||
| expect(mockPlay).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('should toggle play/pause', () => { | ||
| const { result, rerender } = renderHook( | ||
| ({ s }) => useAudioPlayer(s, jest.fn()), | ||
| { | ||
| initialProps: { s: null } | ||
| } | ||
| ) | ||
| rerender({ s: song }) | ||
| act(() => { | ||
| result.current.togglePlay() | ||
| }) | ||
| expect(mockPause).toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('should update volume', () => { | ||
| const { result } = renderHook(() => useAudioPlayer(null, jest.fn())) | ||
| act(() => { | ||
| result.current.setVolume(0.5) | ||
| }) | ||
| expect(result.current.volume).toBe(0.5) | ||
| expect(mockAudioInstance.volume).toBe(0.5) | ||
| }) | ||
|
|
||
| it('should seek to a given time', () => { | ||
| const { result } = renderHook(() => useAudioPlayer(null, jest.fn())) | ||
| act(() => { | ||
| result.current.seek(15) | ||
| }) | ||
| expect(mockAudioInstance.currentTime).toBe(15) | ||
| }) | ||
| }) |
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.
The
Audioconstructor is a browser-only API. In a Next.js environment, this code will execute on the server during pre-rendering and throw aReferenceError: Audio is not defined. You should add a check for thewindowobject before instantiation.