Skip to content

Commit d576cce

Browse files
committed
restore last playlist
1 parent a51ce50 commit d576cce

File tree

6 files changed

+123
-82
lines changed

6 files changed

+123
-82
lines changed

apps/pwa/src/i18n/en.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,4 +231,5 @@ export default {
231231
remove_stop_timer_question: 'are you sure to remove stop-timer',
232232
timeout_while_fetching_data: 'timeout while fetching data',
233233
timeout: 'timeout of %s1 milliseconds',
234+
question_restore_playlist: 'do you want to restore last playlist ?',
234235
};

apps/pwa/src/i18n/zh_hans.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ const zhCN: {
219219
remove_stop_timer_question: '确定移除停止定时器?',
220220
timeout_while_fetching_data: '获取数据超时',
221221
timeout: '超时 %s1 毫秒',
222+
question_restore_playlist: '是否恢复上一次的播放列表',
222223
};
223224

224225
export default zhCN;

apps/pwa/src/index.tsx

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,8 @@
1-
import './polyfill'; // make sure that polyfill is first
1+
import './polyfill'; // make sure that polyfill is at first
22
import './updater';
33
import './devtool';
44
import { createRoot } from 'react-dom/client';
55
import 'cropperjs/dist/cropper.min.css';
66
import App from './app';
7-
import Unsupported from './unsupported';
87

9-
function findUnsupportedList(): string[] {
10-
const unsupportedList: string[] = [];
11-
12-
if (!window.CSS.supports('height', '1dvb')) {
13-
unsupportedList.push('https://caniuse.com/viewport-unit-variants');
14-
}
15-
16-
return unsupportedList;
17-
}
18-
19-
const unsupportedList = findUnsupportedList();
20-
const root = createRoot(document.querySelector('#root')!);
21-
if (unsupportedList.length) {
22-
root.render(<Unsupported unsupportedList={unsupportedList} />);
23-
} else {
24-
root.render(<App />);
25-
}
8+
createRoot(document.querySelector('#root')!).render(<App />);

apps/pwa/src/pages/player/use_playlist.ts renamed to apps/pwa/src/pages/player/use_playlist/index.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,13 @@ 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 { PlaylistMusic } from './constants';
7-
import eventemitter, { EventType } from './eventemitter';
8-
import storage, { Key } from './storage';
6+
import { PlaylistMusic } from '../constants';
7+
import eventemitter, { EventType } from '../eventemitter';
8+
import usePlaylistRestore from './use_playlist_restore';
99

1010
export default () => {
1111
const [playlist, setPlaylist] = useState<PlaylistMusic[]>([]);
12-
13-
useEffect(
14-
() =>
15-
playlist && playlist.length > 0
16-
? void storage.setItem(Key.PLAYLIST, playlist)
17-
: undefined,
18-
[playlist],
19-
);
12+
usePlaylistRestore(playlist);
2013

2114
useEffect(() => {
2215
const unlistenActionPlayMusic = eventemitter.listen(
@@ -116,7 +109,15 @@ export default () => {
116109
);
117110
const unlistenMusicDeleted = eventemitter.listen(
118111
EventType.MUSIC_DELETED,
119-
(data) => setPlaylist((pl) => pl.filter((m) => m.id !== data.id)),
112+
(data) =>
113+
setPlaylist((pl) =>
114+
pl
115+
.filter((m) => m.id !== data.id)
116+
.map((music, index, { length }) => ({
117+
...music,
118+
index: length - index,
119+
})),
120+
),
120121
);
121122
return () => {
122123
unlistenActionPlayMusic();
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import { useEffect } from 'react';
2+
import { PlaylistMusic } from '../constants';
3+
import storage, { Key } from '../storage';
4+
import logger from '@/utils/logger';
5+
import notice from '@/utils/notice';
6+
import styled from 'styled-components';
7+
import IconButton from '@/components/icon_button';
8+
import { MdCheck, MdClose } from 'react-icons/md';
9+
import upperCaseFirstLetter from '@/style/upper_case_first_letter';
10+
import eventemitter, { EventType } from '../eventemitter';
11+
import { t } from '@/i18n';
12+
13+
const Restore = styled.div`
14+
> .text {
15+
padding-top: 10px;
16+
${upperCaseFirstLetter}
17+
}
18+
19+
> .actions {
20+
margin-top: 5px;
21+
22+
display: flex;
23+
align-items: center;
24+
justify-content: flex-end;
25+
gap: 5px;
26+
27+
> .action {
28+
color: #fff;
29+
}
30+
}
31+
`;
32+
33+
function usePlaylistRestore(playlist: PlaylistMusic[]) {
34+
useEffect(
35+
() =>
36+
playlist && playlist.length > 0
37+
? void storage.setItem(Key.PLAYLIST, playlist)
38+
: undefined,
39+
[playlist],
40+
);
41+
42+
useEffect(() => {
43+
let noticeId: string | undefined;
44+
storage
45+
.getItem(Key.PLAYLIST)
46+
.then((cachedPlaylist) => {
47+
if (cachedPlaylist && cachedPlaylist.length > 0) {
48+
noticeId = notice.info(
49+
<Restore>
50+
<div className="text">{t('question_restore_playlist')}</div>
51+
<div className="actions">
52+
<IconButton
53+
className="action"
54+
onClick={() => {
55+
notice.close(noticeId!);
56+
return eventemitter.emit(
57+
EventType.ACTION_ADD_MUSIC_LIST_TO_PLAYLIST,
58+
{
59+
musicList: cachedPlaylist,
60+
},
61+
);
62+
}}
63+
>
64+
<MdCheck />
65+
</IconButton>
66+
<IconButton
67+
className="action"
68+
onClick={() => notice.close(noticeId!)}
69+
>
70+
<MdClose />
71+
</IconButton>
72+
</div>
73+
</Restore>,
74+
{ duration: 0, closable: false },
75+
);
76+
}
77+
})
78+
.catch((error) => logger.error(error, 'Failed to get cached playlist'));
79+
80+
const closeNotice = () => {
81+
if (noticeId) {
82+
notice.close(noticeId);
83+
noticeId = undefined;
84+
}
85+
};
86+
const unlistenActionPlayMusic = eventemitter.listen(
87+
EventType.ACTION_PLAY_MUSIC,
88+
closeNotice,
89+
);
90+
const unlistenActionAddMusicListToPlaylist = eventemitter.listen(
91+
EventType.ACTION_ADD_MUSIC_LIST_TO_PLAYLIST,
92+
closeNotice,
93+
);
94+
const unlistenActionInsertMusicToPlayqueue = eventemitter.listen(
95+
EventType.ACTION_INSERT_MUSIC_TO_PLAYQUEUE,
96+
closeNotice,
97+
);
98+
return () => {
99+
unlistenActionPlayMusic();
100+
unlistenActionAddMusicListToPlaylist();
101+
unlistenActionInsertMusicToPlayqueue();
102+
};
103+
}, []);
104+
}
105+
106+
export default usePlaylistRestore;

apps/pwa/src/unsupported.tsx

Lines changed: 0 additions & 51 deletions
This file was deleted.

0 commit comments

Comments
 (0)