Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions CLAUDE.md
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>
"""





1 change: 1 addition & 0 deletions app/components/AudioPlayer/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const DEFAULT_VOLUME = 0.7;
81 changes: 81 additions & 0 deletions app/components/AudioPlayer/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
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 }) => {
const player = useAudioPlayer(currentSong, onNext);

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: '#ffff', 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
};

export default AudioPlayer;
68 changes: 68 additions & 0 deletions app/components/AudioPlayer/tests/index.test.js
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()
})
})
69 changes: 69 additions & 0 deletions app/components/AudioPlayer/tests/useAudioPlayer.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { renderHook, act } from '@testing-library/react'
import { useAudioPlayer } from '../useAudioPlayer'

const mockPlay = jest.fn().mockResolvedValue(undefined)
const mockPause = jest.fn()
let mockAudioInstance

beforeEach(() => {
mockPlay.mockClear()
mockPause.mockClear()
mockAudioInstance = {
play: mockPlay,
pause: mockPause,
volume: 0.7,
currentTime: 0,
duration: 0,
src: '',
addEventListener: jest.fn(),
removeEventListener: jest.fn()
}
jest.spyOn(global, 'Audio').mockImplementation(() => mockAudioInstance)
})

afterEach(() => {
global.Audio.mockRestore()
})

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 song is provided', () => {
renderHook(() => useAudioPlayer(song, jest.fn()))
expect(mockAudioInstance.src).toBe(song.previewUrl)
expect(mockPlay).toHaveBeenCalled()
})

it('should toggle play/pause', () => {
const { result } = renderHook(() => useAudioPlayer(song, jest.fn()))
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)
})
})
71 changes: 71 additions & 0 deletions app/components/AudioPlayer/useAudioPlayer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { useRef, useState, useEffect, useCallback } from 'react';
import { DEFAULT_VOLUME } from './constants';

export const useAudioPlayer = (song, onSongEnd) => {
const audioRef = useRef(null);
const [isPlaying, setIsPlaying] = useState(false);
const [currentTime, setCurrentTime] = useState(0);
const [duration, setDuration] = useState(0);
const [volume, setVolumeState] = useState(DEFAULT_VOLUME);

useEffect(() => {
const audio = new Audio();
audio.volume = DEFAULT_VOLUME;
audioRef.current = audio;

const onTime = () => setCurrentTime(audio.currentTime);
const onLoaded = () => setDuration(audio.duration);
const onEnded = () => {
setIsPlaying(false);
if (onSongEnd) {
onSongEnd();
}
};

audio.addEventListener('timeupdate', onTime);
audio.addEventListener('loadedmetadata', onLoaded);
audio.addEventListener('ended', onEnded);

return () => {
audio.removeEventListener('timeupdate', onTime);
audio.removeEventListener('loadedmetadata', onLoaded);
audio.removeEventListener('ended', onEnded);
audio.pause();
};
}, []);
Comment on lines +11 to +35
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

Stale onSongEnd closure — navigation will operate on mount-time state.

onEnded is created once in the mount-only effect and permanently captures the onSongEnd prop from that first render. onSongEnd maps to handleNext (from usePlaybackNav), which closes over songs and currentSong. After those values change (song played, list searched, etc.), handleNext gets a new reference, but the stale closure registered on the 'ended' event still calls the mount-time version — making end-of-track navigation unreliable.

Use a ref to always dispatch to the latest callback without recreating the Audio element:

🐛 Proposed fix
+  const onSongEndRef = useRef(onSongEnd);
+  useEffect(() => {
+    onSongEndRef.current = onSongEnd;
+  }); // no deps — keeps the ref current on every render

   useEffect(() => {
     const audio = new Audio();
     audio.volume = DEFAULT_VOLUME;
     audioRef.current = audio;

     const onTime = () => setCurrentTime(audio.currentTime);
     const onLoaded = () => setDuration(audio.duration);
     const onEnded = () => {
       setIsPlaying(false);
-      if (onSongEnd) {
-        onSongEnd();
-      }
+      onSongEndRef.current?.();
     };
     // ...
   }, []);
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@app/components/AudioPlayer/useAudioPlayer.js` around lines 11 - 35, The
mounted effect registers an onEnded listener that closes over the initial
onSongEnd prop, causing stale handleNext behavior; fix by adding a mutable ref
(e.g., latestOnSongEndRef) that you update whenever onSongEnd changes and change
the mounted onEnded handler (the one added/removed in the useEffect where
audioRef is created) to call latestOnSongEndRef.current() if defined — this
preserves the single Audio instance while ensuring onEnded always delegates to
the latest onSongEnd/handleNext from usePlaybackNav.


useEffect(() => {
if (song?.previewUrl && audioRef.current) {
audioRef.current.src = song.previewUrl;
audioRef.current.play().catch(() => {});
setIsPlaying(true);
}
Comment on lines +38 to +42

Choose a reason for hiding this comment

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

high

When the song prop becomes null (e.g., when a user clears the current selection), the audio continues to play in the background because there is no logic to pause it when the condition fails. This results in a poor user experience and a resource leak.

    if (song?.previewUrl && audioRef.current) {
      audioRef.current.src = song.previewUrl;
      audioRef.current.play().catch(() => {});
      setIsPlaying(true);
    } else if (audioRef.current) {
      audioRef.current.pause();
      audioRef.current.src = '';
      setIsPlaying(false);
    }

}, [song?.trackId]);

const togglePlay = useCallback(() => {
if (!audioRef.current?.src) {
return;
}
if (isPlaying) {
audioRef.current.pause();
} else {
audioRef.current.play().catch(() => {});
}
setIsPlaying(!isPlaying);
Comment on lines +37 to +54
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

Optimistic setIsPlaying(true) ignores play() rejection — UI desynchronises from audio state.

Both the song-change effect (line 40-41) and togglePlay (lines 52-54) call setIsPlaying(true) unconditionally after play(), while silently swallowing the rejection. When the browser blocks autoplay, the player UI shows a "playing" indicator with no audio output.

Move setIsPlaying(true) inside the .then() handler, and revert state in .catch():

🐛 Proposed fix
   useEffect(() => {
     if (song?.previewUrl && audioRef.current) {
       audioRef.current.src = song.previewUrl;
-      audioRef.current.play().catch(() => {});
-      setIsPlaying(true);
+      audioRef.current.play()
+        .then(() => setIsPlaying(true))
+        .catch(() => setIsPlaying(false));
     }
   }, [song?.trackId]);

   const togglePlay = useCallback(() => {
     if (!audioRef.current?.src) {
       return;
     }
     if (isPlaying) {
       audioRef.current.pause();
       setIsPlaying(false);
     } else {
-      audioRef.current.play().catch(() => {});
-      setIsPlaying(!isPlaying);
+      audioRef.current.play()
+        .then(() => setIsPlaying(true))
+        .catch(() => {});
     }
   }, [isPlaying]);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
useEffect(() => {
if (song?.previewUrl && audioRef.current) {
audioRef.current.src = song.previewUrl;
audioRef.current.play().catch(() => {});
setIsPlaying(true);
}
}, [song?.trackId]);
const togglePlay = useCallback(() => {
if (!audioRef.current?.src) {
return;
}
if (isPlaying) {
audioRef.current.pause();
} else {
audioRef.current.play().catch(() => {});
}
setIsPlaying(!isPlaying);
useEffect(() => {
if (song?.previewUrl && audioRef.current) {
audioRef.current.src = song.previewUrl;
audioRef.current.play()
.then(() => setIsPlaying(true))
.catch(() => setIsPlaying(false));
}
}, [song?.trackId]);
const togglePlay = useCallback(() => {
if (!audioRef.current?.src) {
return;
}
if (isPlaying) {
audioRef.current.pause();
setIsPlaying(false);
} else {
audioRef.current.play()
.then(() => setIsPlaying(true))
.catch(() => {});
}
setIsPlaying(!isPlaying);
}, [isPlaying]);
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@app/components/AudioPlayer/useAudioPlayer.js` around lines 37 - 54, The
effect in useEffect and the togglePlay callback optimistically call
setIsPlaying(true) regardless of whether audioRef.current.play() succeeds,
causing UI desync when play() is rejected; update both the song-change effect
(useEffect) and togglePlay to call audioRef.current.play().then(() =>
setIsPlaying(true)).catch(() => setIsPlaying(false) or leave false) so state
only flips when play succeeds and is reverted on rejection, and keep the pause
branch calling audioRef.current.pause() followed by setIsPlaying(false) as
before.

}, [isPlaying]);

const seek = useCallback((time) => {
if (audioRef.current) {
audioRef.current.currentTime = time;
}
}, []);

const setVolume = useCallback((v) => {
setVolumeState(v);
if (audioRef.current) {
audioRef.current.volume = v;
}
}, []);

return { isPlaying, currentTime, duration, volume, togglePlay, seek, setVolume };
};
29 changes: 29 additions & 0 deletions app/components/HeartButton/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import React from 'react';
import PropTypes from 'prop-types';
import { HeartFilled, HeartOutlined } from '@ant-design/icons';
import { HeartBtn } from '@components/styled/heartButton';

const HeartButton = ({ isLiked, onClick }) => {
const handleClick = (e) => {
e.stopPropagation();
onClick();
};

return (
<HeartBtn
data-testid="heart-button"
isLiked={isLiked}
onClick={handleClick}
aria-label={isLiked ? 'Unlike song' : 'Like song'}
>
{isLiked ? <HeartFilled /> : <HeartOutlined />}
</HeartBtn>
);
};

HeartButton.propTypes = {
isLiked: PropTypes.bool.isRequired,
onClick: PropTypes.func.isRequired
};

export default HeartButton;
Loading