|
@@ -1,6 +1,7 @@
|
|
|
|
|
+import { useThrottleCallback } from '@react-hook/throttle';
|
|
|
import { AxiosInstance, AxiosResponse } from 'axios';
|
|
import { AxiosInstance, AxiosResponse } from 'axios';
|
|
|
-import { useCallback, useContext, useEffect } from 'react';
|
|
|
|
|
-import { stateSet } from '../actions';
|
|
|
|
|
|
|
+import { Dispatch, useCallback, useContext, useEffect } from 'react';
|
|
|
|
|
+import { LocalAction, stateSet } from '../actions';
|
|
|
import { DispatchContext } from '../context/state';
|
|
import { DispatchContext } from '../context/state';
|
|
|
import { NullSong, Song, songExists } from '../types';
|
|
import { NullSong, Song, songExists } from '../types';
|
|
|
import { getApiUrl } from '../utils/url';
|
|
import { getApiUrl } from '../utils/url';
|
|
@@ -9,29 +10,18 @@ import { useRequestCallback } from './request';
|
|
|
|
|
|
|
|
function useNextOrPrevSong(
|
|
function useNextOrPrevSong(
|
|
|
key: 'next' | 'prev',
|
|
key: 'next' | 'prev',
|
|
|
-): [(songId: number) => void, Song | NullSong | null, boolean] {
|
|
|
|
|
|
|
+ dispatch: Dispatch<LocalAction>,
|
|
|
|
|
+): [(songId: number) => void, boolean] {
|
|
|
const sendRequest = useCallback(
|
|
const sendRequest = useCallback(
|
|
|
(axios: AxiosInstance, id: number): Promise<AxiosResponse<Song | NullSong>> =>
|
|
(axios: AxiosInstance, id: number): Promise<AxiosResponse<Song | NullSong>> =>
|
|
|
axios.get(`${getApiUrl()}/${key}-song?id=${id}`),
|
|
axios.get(`${getApiUrl()}/${key}-song?id=${id}`),
|
|
|
[key],
|
|
[key],
|
|
|
);
|
|
);
|
|
|
|
|
|
|
|
- return useRequestCallback<number, Song | NullSong>({
|
|
|
|
|
|
|
+ const [onRequest, response, loading] = useRequestCallback<number, Song | NullSong>({
|
|
|
sendRequest,
|
|
sendRequest,
|
|
|
});
|
|
});
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-export function usePlayQueue(): {
|
|
|
|
|
- onNext: (songId: number) => void;
|
|
|
|
|
- onPrev: (songId: number) => void;
|
|
|
|
|
- loading: boolean;
|
|
|
|
|
-} {
|
|
|
|
|
- const dispatch = useContext(DispatchContext);
|
|
|
|
|
|
|
|
|
|
- const [onRequestNext, nextSong, loadingNext] = useNextOrPrevSong('next');
|
|
|
|
|
- const [onRequestPrev, prevSong, loadingPrev] = useNextOrPrevSong('prev');
|
|
|
|
|
-
|
|
|
|
|
- const response = nextSong ?? prevSong;
|
|
|
|
|
useEffect(() => {
|
|
useEffect(() => {
|
|
|
if (response) {
|
|
if (response) {
|
|
|
if (songExists(response)) {
|
|
if (songExists(response)) {
|
|
@@ -42,5 +32,20 @@ export function usePlayQueue(): {
|
|
|
}
|
|
}
|
|
|
}, [dispatch, response]);
|
|
}, [dispatch, response]);
|
|
|
|
|
|
|
|
|
|
+ const debouncedRequest = useThrottleCallback(onRequest, 5, true);
|
|
|
|
|
+
|
|
|
|
|
+ return [debouncedRequest, loading];
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+export function usePlayQueue(): {
|
|
|
|
|
+ onNext: (songId: number) => void;
|
|
|
|
|
+ onPrev: (songId: number) => void;
|
|
|
|
|
+ loading: boolean;
|
|
|
|
|
+} {
|
|
|
|
|
+ const dispatch = useContext(DispatchContext);
|
|
|
|
|
+
|
|
|
|
|
+ const [onRequestNext, loadingNext] = useNextOrPrevSong('next', dispatch);
|
|
|
|
|
+ const [onRequestPrev, loadingPrev] = useNextOrPrevSong('prev', dispatch);
|
|
|
|
|
+
|
|
|
return { onNext: onRequestNext, onPrev: onRequestPrev, loading: loadingNext || loadingPrev };
|
|
return { onNext: onRequestNext, onPrev: onRequestPrev, loading: loadingNext || loadingPrev };
|
|
|
}
|
|
}
|