Ver código fonte

feat: activate first song by artist/album when hitting enter

Fela Maslen 5 anos atrás
pai
commit
99856dc24d

+ 26 - 0
gmus-web/src/components/ui/cmus/reducer/keypress.spec.ts

@@ -178,6 +178,32 @@ describe(ActionTypeKeyPressed, () => {
     const action: ActionKeyPressed = { type: ActionTypeKeyPressed, key: Keys.enter };
 
     describe('when in library view', () => {
+      describe('when in the artist list mode', () => {
+        it('should set the globalAction to play the first song by the artist', () => {
+          expect.assertions(1);
+
+          const result = cmusUIReducer(
+            {
+              ...stateWithActiveArtist,
+              library: {
+                ...stateWithActiveArtist.library,
+                modeWindow: LibraryModeWindow.ArtistList,
+              },
+            },
+            action,
+          );
+
+          expect(result.globalAction).toStrictEqual(
+            stateSet({
+              playing: true,
+              songId: 184,
+              currentTime: 0,
+              seekTime: 0,
+            }),
+          );
+        });
+      });
+
       describe('when in the songs list mode', () => {
         const state: CmusUIState = {
           ...initialCmusUIState,

+ 18 - 16
gmus-web/src/components/ui/cmus/reducer/keypress.ts

@@ -35,28 +35,30 @@ function toggleExpandArtist(library: CmusUIState['library']): CmusUIState['libra
   return { ...library, expandedArtists: [...library.expandedArtists, library.activeArtist] };
 }
 
-const activateSong = (state: CmusUIState, songId: number): CmusUIState =>
-  withGlobalAction(
-    state,
-    stateSet({
-      playing: true,
-      songId,
-      currentTime: 0,
-      seekTime: 0,
-    }),
-  );
+const activateSong = (state: CmusUIState, songId: number | null): CmusUIState =>
+  songId
+    ? withGlobalAction(
+        state,
+        stateSet({
+          playing: true,
+          songId,
+          currentTime: 0,
+          seekTime: 0,
+        }),
+      )
+    : state;
 
 function handleActivate(state: CmusUIState): CmusUIState {
   switch (state.view) {
     case View.Library:
-      if (state.library.modeWindow === LibraryModeWindow.SongList) {
-        if (!state.library.activeSongId) {
+      switch (state.library.modeWindow) {
+        case LibraryModeWindow.SongList:
+          return activateSong(state, state.library.activeSongId);
+        case LibraryModeWindow.ArtistList:
+          return activateSong(state, getFilteredSongs(state)[0]?.id ?? null);
+        default:
           return state;
-        }
-
-        return activateSong(state, state.library.activeSongId);
       }
-      return state;
 
     case View.Queue:
       if (!state.queue.active) {