Skip to content

Commit 314cf1a

Browse files
Rename song prog component, DRY & fix the onclick handlers w/ fallback
1 parent 906e0d8 commit 314cf1a

File tree

2 files changed

+28
-26
lines changed

2 files changed

+28
-26
lines changed

src/renderer/components/SongProgressBar.tsx src/renderer/components/SongProgressAndSongDisplay.tsx

+26-24
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { LessOpaqueTinyText } from './SimpleStyledMaterialUIComponents';
77
import usePlayerStore from '../store/player';
88
import { useWindowDimensions } from '../hooks/useWindowDimensions';
99

10-
export default function SongProgressBar({
10+
export default function SongProgressAndSongDisplay({
1111
value,
1212
onManualChange,
1313
max,
@@ -33,14 +33,35 @@ export default function SongProgressBar({
3333
const artistRef = React.useRef<HTMLDivElement>(null);
3434
const artistRef2 = React.useRef<HTMLDivElement>(null);
3535

36-
function convertToMMSS(timeInSeconds: number) {
36+
const convertToMMSS = (timeInSeconds: number) => {
3737
const minutes = Math.floor(timeInSeconds / 60);
3838
const seconds = Math.floor(timeInSeconds % 60);
3939
// Ensuring the format is two-digits both for minutes and seconds
4040
return `${minutes.toString().padStart(2, '0')}:${seconds
4141
.toString()
4242
.padStart(2, '0')}`;
43-
}
43+
};
44+
45+
/**
46+
* on click, scroll to the song in the library if possible,
47+
* then try to scroll to the artist if the song is not found
48+
*/
49+
const scrollToSong = () => {
50+
const libraryArray = Object.values(filteredLibrary);
51+
let index = libraryArray.findIndex(
52+
(song) =>
53+
song.common.title === currentSongMetadata.common?.title &&
54+
song.common.artist === currentSongMetadata.common?.artist &&
55+
song.common.album === currentSongMetadata.common?.album,
56+
);
57+
58+
if (index !== -1) {
59+
index = libraryArray.findIndex(
60+
(song) => song.common.artist === currentSongMetadata.common?.artist,
61+
);
62+
}
63+
setOverrideScrollToIndex(index);
64+
};
4465

4566
React.useEffect(() => {
4667
setPosition(value);
@@ -174,18 +195,7 @@ export default function SongProgressBar({
174195
<LessOpaqueTinyText
175196
aria-label="current-title"
176197
onClick={() => {
177-
/**
178-
* on click, scroll to the song title in the library
179-
*/
180-
const libraryArray = Object.values(filteredLibrary);
181-
const index = libraryArray.findIndex(
182-
(song) =>
183-
song.common.title === currentSongMetadata.common?.title &&
184-
song.common.artist === currentSongMetadata.common?.artist &&
185-
song.common.album === currentSongMetadata.common?.album,
186-
);
187-
188-
setOverrideScrollToIndex(index);
198+
scrollToSong();
189199
}}
190200
sx={{
191201
margin: 0,
@@ -257,15 +267,7 @@ export default function SongProgressBar({
257267
<LessOpaqueTinyText
258268
aria-label="current-title"
259269
onClick={() => {
260-
/**
261-
* on click, scroll to the artist in the library
262-
*/
263-
const libraryArray = Object.values(filteredLibrary);
264-
const index = libraryArray.findIndex(
265-
(song) =>
266-
song.common.artist === currentSongMetadata.common?.artist,
267-
);
268-
setOverrideScrollToIndex(index);
270+
scrollToSong();
269271
}}
270272
sx={{
271273
margin: 0,

src/renderer/components/StaticPlayer.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import Stack from '@mui/material/Stack';
1212
import RepeatOnIcon from '@mui/icons-material/RepeatOn';
1313
import SpatialAudioIcon from '@mui/icons-material/SpatialAudio';
1414
import VolumeSliderStack from './VolumeSliderStack';
15-
import SongProgressBar from './SongProgressBar';
15+
import SongProgressAndSongDisplay from './SongProgressAndSongDisplay';
1616
import usePlayerStore from '../store/player';
1717

1818
type StaticPlayerProps = {
@@ -173,7 +173,7 @@ export default function StaticPlayer({
173173
{/**
174174
* @dev this is song progress bar on mobile and desktop
175175
*/}
176-
<SongProgressBar
176+
<SongProgressAndSongDisplay
177177
max={currentSongMetadata?.format?.duration || 0}
178178
onManualChange={(e: number) => {
179179
// manually update the player

0 commit comments

Comments
 (0)