Skip to content

Commit

Permalink
Scroll Wheel: Add back click handler
Browse files Browse the repository at this point in the history
Clicking back either restarts the song or goes to the next song
depending on the audio's current time.
  • Loading branch information
tvillarete committed Dec 31, 2019
1 parent 8c0d5f1 commit 800ef91
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 6 deletions.
22 changes: 21 additions & 1 deletion src/App/Audio.tsx
@@ -1,9 +1,17 @@
import React, { useCallback, useEffect, useRef } from 'react';

import { useEventListener } from 'hooks';
import { useAudioService } from 'services/audio';

const Audio = () => {
const { source, playing, nextSong, loading, setLoading } = useAudioService();
const {
source,
playing,
nextSong,
prevSong,
loading,
setLoading
} = useAudioService();
const audioRef = useRef<HTMLAudioElement>(null);

const handlePlay = useCallback(async () => {
Expand All @@ -18,6 +26,16 @@ const Audio = () => {
}
}, []);

const handleBackClick = useCallback(() => {
if (source && audioRef.current) {
if (audioRef.current.currentTime < 3) {
prevSong();
} else {
audioRef.current.currentTime = 0;
}
}
}, [prevSong, source]);

const handleLoadStart = useCallback(() => {
setLoading(true);
}, [setLoading]);
Expand All @@ -34,6 +52,8 @@ const Audio = () => {
}
}, [handlePause, handlePlay, loading, playing]);

useEventListener("backclick", handleBackClick);

return source ? (
<audio
ref={audioRef}
Expand Down
1 change: 0 additions & 1 deletion src/App/index.tsx
Expand Up @@ -51,7 +51,6 @@ const Shell = styled.div`
animation: none;
border-radius: 0;
-webkit-box-reflect: unset;
max-height: unset;
}
@keyframes descend {
Expand Down
3 changes: 2 additions & 1 deletion src/components/ScrollWheel/index.tsx
Expand Up @@ -27,6 +27,7 @@ const forwardScrollEvent = new Event("forwardscroll");
const backwardScrollEvent = new Event("backwardscroll");
const wheelClickEvent = new Event("wheelclick");
const menuClickEvent = new Event("menuclick");
const backClickEvent = new Event("backclick");

const ScrollWheel = () => {
const [count, setCount] = useState(0);
Expand Down Expand Up @@ -58,7 +59,7 @@ const ScrollWheel = () => {
togglePause();
break;
case WHEEL_QUADRANT.LEFT:
console.log("CLICKED REWIND");
window.dispatchEvent(backClickEvent);
break;
case WHEEL_QUADRANT.RIGHT:
nextSong();
Expand Down
10 changes: 7 additions & 3 deletions src/hooks/useScrollHandler.ts
Expand Up @@ -24,11 +24,10 @@ const useScrollHandler = (
/** Wait until the user stops scrolling to check for a new preview to display. */
const checkForPreview = useCallback(
(i: number) => {
if (!isActive || !options[i]) return;

if (timeoutIdRef.current) {
clearTimeout(timeoutIdRef.current);
}
if (!isActive || !options[i]) return;
timeoutIdRef.current = setTimeout(() => {
const preview = options[i].preview;
if (preview) {
Expand Down Expand Up @@ -91,7 +90,12 @@ const useScrollHandler = (
}, [index, isActive, options, play, showWindow]);

useEffect(() => {
checkForPreview(0);
if (!options || !options[0]) return;

const preview = options[0].preview;
if (preview) {
setPreview(preview);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

Expand Down
19 changes: 19 additions & 0 deletions src/services/audio.tsx
Expand Up @@ -45,6 +45,7 @@ export interface AudioServiceHook {
play: (playlist: Song[], index?: number) => void;
togglePause: () => void;
nextSong: () => void;
prevSong: () => void;
setLoading: (value: boolean) => void;
}

Expand Down Expand Up @@ -94,6 +95,23 @@ export const useAudioService = (): AudioServiceHook => {
}
}, [audioState.source, setAudioState]);

const prevSong = useCallback(() => {
if (audioState.source && audioState.songIndex > 0) {
setAudioState(prevState => {
const newIndex = prevState.songIndex - 1;
const newSource = prevState.playlist[newIndex];
setDocumentSongTitle(newSource);

return {
...prevState,
playing: true,
songIndex: newIndex,
source: newSource
};
});
}
}, [audioState.songIndex, audioState.source, setAudioState]);

const setLoading = useCallback(
(value: boolean) => setAudioState({ ...audioState, loading: value }),
[audioState, setAudioState]
Expand All @@ -108,6 +126,7 @@ export const useAudioService = (): AudioServiceHook => {
play,
togglePause,
nextSong,
prevSong,
setLoading
};
};
Expand Down

0 comments on commit 800ef91

Please sign in to comment.