show: update shuffle as the list continue loading
Massimo Melina committed
Jul 5, 2024 at 00:43 UTC
56eec6285f1792ea787436de29bed5a412771c2f
1 file changed
+9
-2
frontend/src/show.ts
+9
-2
@@ -1,5 +1,5 @@
1
import { DirEntry, DirList, ext2type, state, useSnapState } from './state'
2
-import { createElement as h, Fragment, useEffect, useRef, useState } from 'react'
2
+import { createElement as h, Fragment, useEffect, useMemo, useRef, useState } from 'react'
3
import {
4
basename, dirname, domOn, hfsEvent, hIcon, isMac, newDialog, pathEncode, restartAnimation, useStateMounted,
5
} from './misc'
@@ -11,6 +11,7 @@ import { t, useI18N } from './i18n'
11
import { alertDialog } from './dialog'
12
import _ from 'lodash'
13
import { getId3Tags } from './id3'
14
+import { subscribeKey } from 'valtio/utils'
15
16
enum ZoomMode {
17
fullWidth,
@@ -28,12 +29,18 @@ export function fileShow(entry: DirEntry, { startPlaying=false } = {}) {
29
const lastGood = useRef(entry)
30
const [mode, setMode] = useState(ZoomMode.contain)
31
const [shuffle, setShuffle] = useState<undefined|DirList>()
32
+ // shuffle the rest of the list as we continue getting entries, leaving intact the part we've already played/being through
33
+ const shuffleIdx = useMemo(() => shuffle?.findIndex(x => x.n === cur.n), [cur])
34
+ useEffect(() => subscribeKey(state, 'list', list => {
35
+ const n = 1 + (shuffleIdx ?? Infinity)
36
+ setShuffle(x => list && x?.slice(0, n).concat(_.shuffle(list.slice(n))))
37
+ }), [shuffleIdx])
38
const [repeat, setRepeat, { get: getRepeat }] = useStateMounted(false)
39
const [cover, setCover] = useState('')
40
useEffect(() => {
41
if (shuffle)
42
goTo(shuffle[0])
36
- }, [shuffle])
43
+ }, [Boolean(shuffle)])
44
useEventListener('keydown', ({ key }) => {
45
if (key === 'Escape') return close()
46
if (key === 'ArrowLeft') return goPrev()