@@ -10,7 +10,7 @@ import addDays from 'date-fns/addDays';
10
10
import { Button , ButtonGroup , Form , FormGroup , Input , Label } from 'reactstrap' ;
11
11
12
12
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome' ;
13
- import { faThumbtack , faExchangeAlt , faHeart } from '@fortawesome/free-solid-svg-icons' ;
13
+ import { faThumbtack , faExchangeAlt , faHeart , faWandMagicSparkles } from '@fortawesome/free-solid-svg-icons' ;
14
14
import { faLightbulb } from '@fortawesome/free-regular-svg-icons' ;
15
15
16
16
import { useSettings } from 'hooks/useSettings' ;
@@ -24,12 +24,21 @@ import './SongForm.css';
24
24
25
25
import type { Scrobble } from 'utils/types/scrobble' ;
26
26
27
+ import { trackGetInfo } from 'utils/clients/lastfm/methods/trackGetInfo' ;
28
+ import { get } from 'lodash-es' ;
29
+
27
30
const DateTimePicker = lazyWithPreload ( ( ) => import ( 'components/DateTimePicker' ) ) ;
28
31
const Tooltip = lazyWithPreload ( ( ) => import ( 'components/Tooltip' ) ) ;
29
32
30
33
const reAutoPasteSplitting = / - | ? [ – — ] ? / ;
31
34
const controlOrder = [ 'artist' , 'title' , 'album' ] ; // Used for arrow navigation
32
35
36
+ enum AutoFillStatus {
37
+ Idle = '' ,
38
+ Success = 'autofill-success' ,
39
+ Fail = 'autofill-fail' ,
40
+ }
41
+
33
42
type SongMatch = {
34
43
artist : string ;
35
44
title : string ;
@@ -81,6 +90,7 @@ export function SongForm() {
81
90
const [ timestamp , setTimestamp ] = useState ( new Date ( ) ) ;
82
91
const [ title , setTitle ] = useState ( '' ) ;
83
92
const [ isCustomDate , setIsCustomDate ] = useState ( false ) ;
93
+ const [ albumAutoFillStatus , setalbumAutoFillStatus ] = useState ( AutoFillStatus . Idle ) ;
84
94
85
95
const dispatch = useDispatch ( ) ;
86
96
const { isLoading : settingsLoading , settings } = useSettings ( ) ;
@@ -290,6 +300,22 @@ export function SongForm() {
290
300
setFormValid ( artist . trim ( ) . length > 0 && title . trim ( ) . length > 0 ) ;
291
301
} ;
292
302
303
+ const autoFillAlbum = async ( ) => {
304
+ if ( ! formIsValid ) {
305
+ return ;
306
+ }
307
+ const info = await trackGetInfo ( { artist, title } ) ;
308
+ const suggestedAlbum = get ( info , 'album.title' ) ;
309
+
310
+ if ( suggestedAlbum === undefined ) {
311
+ setalbumAutoFillStatus ( AutoFillStatus . Fail ) ;
312
+ } else {
313
+ setalbumAutoFillStatus ( AutoFillStatus . Success ) ;
314
+ setAlbum ( suggestedAlbum ) ;
315
+ }
316
+ setTimeout ( ( ) => setalbumAutoFillStatus ( AutoFillStatus . Idle ) , 1200 ) ;
317
+ } ;
318
+
293
319
return (
294
320
< Form className = "SongForm" data-cy = "SongForm" >
295
321
< FormGroup className = "row" >
@@ -303,7 +329,7 @@ export function SongForm() {
303
329
name = "artist"
304
330
id = "artist"
305
331
tabIndex = { 1 }
306
- className = "hasLock "
332
+ className = "has-button "
307
333
data-cy = "SongForm-artist"
308
334
value = { artist }
309
335
onChange = { ( e ) => setArtist ( ( e . target as HTMLInputElement ) . value ) }
@@ -365,14 +391,15 @@ export function SongForm() {
365
391
name = "album"
366
392
id = "album"
367
393
tabIndex = { 3 }
368
- className = "hasLock"
394
+ className = { 'has-two-buttons ' + albumAutoFillStatus }
369
395
data-cy = "SongForm-album"
370
396
value = { album }
371
397
onChange = { ( e ) => setAlbum ( ( e . target as HTMLInputElement ) . value ) }
372
398
onKeyUp = { catchKeys }
373
399
/>
400
+
374
401
< div
375
- className = " lock-button rounded"
402
+ className = { ' lock-button rounded ' + albumAutoFillStatus }
376
403
id = "lock-album"
377
404
data-cy = "SongForm-album-lock"
378
405
onClick = { toggleLock ( 'album' ) }
@@ -382,6 +409,25 @@ export function SongForm() {
382
409
< Tooltip target = "lock-album" >
383
410
< Trans i18nKey = "lockAlbum" > Lock album</ Trans >
384
411
</ Tooltip >
412
+
413
+ < div
414
+ className = { 'autofill-button rounded ' + albumAutoFillStatus }
415
+ id = "autofill-album"
416
+ data-cy = "SongForm-autofill-button"
417
+ onClick = { autoFillAlbum }
418
+ >
419
+ < FontAwesomeIcon
420
+ icon = { faWandMagicSparkles }
421
+ className = { formIsValid || albumAutoFillStatus === AutoFillStatus . Fail ? '' : 'disabled' }
422
+ />
423
+ </ div >
424
+ < Tooltip target = "autofill-album" >
425
+ { formIsValid ? (
426
+ < Trans i18nKey = "autoFillAlbum" > Fill album name from last.fm</ Trans >
427
+ ) : (
428
+ < Trans i18nKey = "autoFillAlbumInvalidForm" > Enter artist and track for album auto-fill</ Trans >
429
+ ) }
430
+ </ Tooltip >
385
431
</ div >
386
432
</ FormGroup >
387
433
< FormGroup className = "row" >
@@ -395,7 +441,7 @@ export function SongForm() {
395
441
name = "albumArtist"
396
442
id = "albumArtist"
397
443
tabIndex = { 3 }
398
- className = "hasLock "
444
+ className = "has-button "
399
445
data-cy = "SongForm-albumArtist"
400
446
value = { albumArtist }
401
447
onChange = { ( e ) => setAlbumArtist ( ( e . target as HTMLInputElement ) . value ) }
0 commit comments