@@ -7,7 +7,7 @@ import { LessOpaqueTinyText } from './SimpleStyledMaterialUIComponents';
7
7
import usePlayerStore from '../store/player' ;
8
8
import { useWindowDimensions } from '../hooks/useWindowDimensions' ;
9
9
10
- export default function SongProgressBar ( {
10
+ export default function SongProgressAndSongDisplay ( {
11
11
value,
12
12
onManualChange,
13
13
max,
@@ -33,14 +33,35 @@ export default function SongProgressBar({
33
33
const artistRef = React . useRef < HTMLDivElement > ( null ) ;
34
34
const artistRef2 = React . useRef < HTMLDivElement > ( null ) ;
35
35
36
- function convertToMMSS ( timeInSeconds : number ) {
36
+ const convertToMMSS = ( timeInSeconds : number ) => {
37
37
const minutes = Math . floor ( timeInSeconds / 60 ) ;
38
38
const seconds = Math . floor ( timeInSeconds % 60 ) ;
39
39
// Ensuring the format is two-digits both for minutes and seconds
40
40
return `${ minutes . toString ( ) . padStart ( 2 , '0' ) } :${ seconds
41
41
. toString ( )
42
42
. 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
+ } ;
44
65
45
66
React . useEffect ( ( ) => {
46
67
setPosition ( value ) ;
@@ -174,18 +195,7 @@ export default function SongProgressBar({
174
195
< LessOpaqueTinyText
175
196
aria-label = "current-title"
176
197
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 ( ) ;
189
199
} }
190
200
sx = { {
191
201
margin : 0 ,
@@ -257,15 +267,7 @@ export default function SongProgressBar({
257
267
< LessOpaqueTinyText
258
268
aria-label = "current-title"
259
269
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 ( ) ;
269
271
} }
270
272
sx = { {
271
273
margin : 0 ,
0 commit comments