( state: Pick<QueueStore, 'items' | 'currentIndex'>, direction: 'forward' | 'backward', )
| 42 | }); |
| 43 | |
| 44 | const getDirectionalIndex = ( |
| 45 | state: Pick<QueueStore, 'items' | 'currentIndex'>, |
| 46 | direction: 'forward' | 'backward', |
| 47 | ): number => { |
| 48 | const { items, currentIndex } = state; |
| 49 | const shuffleEnabled = |
| 50 | (getSetting('core.playback.shuffle') as boolean) ?? false; |
| 51 | const repeatMode = (getSetting('core.playback.repeat') as string) ?? 'off'; |
| 52 | |
| 53 | if (items.length === 0) { |
| 54 | return currentIndex; |
| 55 | } |
| 56 | |
| 57 | if (shuffleEnabled) { |
| 58 | return getShuffledIndex(items.length, currentIndex); |
| 59 | } |
| 60 | |
| 61 | if (direction === 'forward') { |
| 62 | if (currentIndex < items.length - 1) { |
| 63 | return currentIndex + 1; |
| 64 | } |
| 65 | return repeatMode === 'all' ? 0 : currentIndex; |
| 66 | } |
| 67 | |
| 68 | if (currentIndex > 0) { |
| 69 | return currentIndex - 1; |
| 70 | } |
| 71 | |
| 72 | return repeatMode === 'all' ? items.length - 1 : currentIndex; |
| 73 | }; |
| 74 | |
| 75 | const getShuffledIndex = (length: number, currentIndex: number): number => { |
| 76 | if (length <= 1) { |
no test coverage detected