Skip to content

Commit 24cf8e7

Browse files
authored
Merge pull request #140 from mebtte/beta
improve playlist restoring
2 parents 74407f2 + cf71b91 commit 24cf8e7

File tree

4 files changed

+28
-5
lines changed

4 files changed

+28
-5
lines changed

apps/pwa/src/global_states/server.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ export function getSelectedServer(ss: ServerState) {
1111
: undefined;
1212
}
1313

14-
export function getSelectedUser(server: Server) {
15-
return server.selectedUserId
14+
export function getSelectedUser(server: Server | undefined) {
15+
return server?.selectedUserId
1616
? server.users.find((u) => u.id === server.selectedUserId)
1717
: undefined;
1818
}

apps/pwa/src/pages/player/use_playlist/use_playlist_restore.tsx

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { MdCheck, MdClose } from 'react-icons/md';
99
import upperCaseFirstLetter from '@/style/upper_case_first_letter';
1010
import eventemitter, { EventType } from '../eventemitter';
1111
import { t } from '@/i18n';
12+
import useUnmount from '@/utils/use_unmount';
1213

1314
const Restore = styled.div`
1415
> .text {
@@ -33,11 +34,23 @@ const Restore = styled.div`
3334
function usePlaylistRestore(playlist: PlaylistMusic[]) {
3435
useEffect(
3536
() =>
36-
playlist && playlist.length > 0
37-
? void storage.setItem(Key.PLAYLIST, playlist)
37+
playlist.length
38+
? void storage
39+
.setItem(Key.PLAYLIST, playlist)
40+
.catch((error) =>
41+
logger.error(error, 'Failed to save playlist to storage'),
42+
)
3843
: undefined,
3944
[playlist],
4045
);
46+
useUnmount(
47+
() =>
48+
void storage
49+
.removeItem(Key.PLAYLIST)
50+
.catch((error) =>
51+
logger.error(error, 'Failed to remove playlist from storage'),
52+
),
53+
);
4154

4255
useEffect(() => {
4356
let noticeId: string | undefined;
@@ -99,6 +112,7 @@ function usePlaylistRestore(playlist: PlaylistMusic[]) {
99112
unlistenActionPlayMusic();
100113
unlistenActionAddMusicListToPlaylist();
101114
unlistenActionInsertMusicToPlayqueue();
115+
closeNotice();
102116
};
103117
}, []);
104118
}

apps/pwa/src/utils/use_event.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ function useEvent<Callback extends (...args: unknown[]) => unknown>(
77
callbackRef.current = callback;
88

99
const memoCallback: Callback = useCallback(
10-
// @ts-expect-error
10+
// @ts-expect-error: known types
1111
(...args) => callbackRef.current!(...args),
1212
[],
1313
);

apps/pwa/src/utils/use_unmount.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { useEffect, useRef } from 'react';
2+
3+
function useUnmount<Callback extends () => void>(callback: Callback) {
4+
const callbackRef = useRef<Callback>(callback);
5+
callbackRef.current = callback;
6+
useEffect(() => callbackRef.current, []);
7+
}
8+
9+
export default useUnmount;

0 commit comments

Comments
 (0)