Skip to content

Commit 7c9413c

Browse files
committed
cache playlist
1 parent 50e24fd commit 7c9413c

File tree

5 files changed

+44
-8
lines changed

5 files changed

+44
-8
lines changed

apps/pwa/src/pages/player/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ export interface MusicWithSingerAliases extends Omit<Music, 'singers'> {
2727
singers: SingerWithAliases[];
2828
}
2929

30+
export type PlaylistMusic = MusicWithSingerAliases & { index: number };
31+
3032
export interface QueueMusic extends MusicWithSingerAliases {
3133
index: number;
3234
pid: string;

apps/pwa/src/pages/player/index.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,10 @@ function Wrapper() {
8484

8585
useKeyboard({ paused: audioPaused, queueMusic, musicbillList });
8686
useMediaSession(queueMusic);
87-
88-
useEffect(() => {
89-
e.emit(EventType.CURRENT_MUSIC_CHANGE, { queueMusic });
90-
}, [queueMusic]);
87+
useEffect(
88+
() => e.emit(EventType.CURRENT_MUSIC_CHANGE, { queueMusic }),
89+
[queueMusic],
90+
);
9191

9292
const contextValue = useMemo(
9393
() => ({

apps/pwa/src/pages/player/musicbill_music_drawer/musicbill.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ function Musicbill({
8282
PlayerEventType.ADD_MUSIC_TO_MUSICBILL,
8383
{
8484
musicbill,
85-
music: music!,
85+
music,
8686
},
8787
);
8888
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import Storage from '@/utils/storage';
2+
import { MusicWithSingerAliases } from './constants';
3+
4+
export enum Key {
5+
PLAYLIST = 'playlist',
6+
}
7+
8+
const storage = new Storage<
9+
Key,
10+
{
11+
[Key.PLAYLIST]: {
12+
userId: string;
13+
musicList: MusicWithSingerAliases[];
14+
};
15+
}
16+
>('player');
17+
18+
export default storage;

apps/pwa/src/pages/player/use_playlist.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,30 @@ import notice from '@/utils/notice';
33
import getMusic from '@/server/api/get_music';
44
import logger from '@/utils/logger';
55
import { t } from '@/i18n';
6-
import { MusicWithSingerAliases } from './constants';
6+
import { PlaylistMusic } from './constants';
77
import eventemitter, { EventType } from './eventemitter';
8-
9-
type PlaylistMusic = MusicWithSingerAliases & { index: number };
8+
import storage, { Key } from './storage';
9+
import {
10+
getSelectedServer,
11+
getSelectedUser,
12+
useServer,
13+
} from '@/global_states/server';
1014

1115
export default () => {
1216
const [playlist, setPlaylist] = useState<PlaylistMusic[]>([]);
1317

18+
useEffect(
19+
() =>
20+
playlist && playlist.length > 0
21+
? void storage.setItem(Key.PLAYLIST, {
22+
userId: getSelectedUser(getSelectedServer(useServer.getState())!)!
23+
.id,
24+
musicList: playlist,
25+
})
26+
: undefined,
27+
[playlist],
28+
);
29+
1430
useEffect(() => {
1531
const unlistenActionPlayMusic = eventemitter.listen(
1632
EventType.ACTION_PLAY_MUSIC,

0 commit comments

Comments
 (0)