Skip to content

Commit

Permalink
feat: use context for audio related info
Browse files Browse the repository at this point in the history
  • Loading branch information
bamdadfr committed Oct 9, 2024
1 parent 765b06b commit 4015b61
Show file tree
Hide file tree
Showing 38 changed files with 288 additions and 327 deletions.
8 changes: 0 additions & 8 deletions src/atoms/buffered.atoms.ts

This file was deleted.

8 changes: 0 additions & 8 deletions src/atoms/duration.atoms.ts

This file was deleted.

8 changes: 0 additions & 8 deletions src/atoms/load.atoms.ts

This file was deleted.

8 changes: 0 additions & 8 deletions src/atoms/mute.atoms.ts

This file was deleted.

18 changes: 0 additions & 18 deletions src/atoms/play-pause.atoms.ts

This file was deleted.

8 changes: 0 additions & 8 deletions src/atoms/progress.atoms.ts

This file was deleted.

8 changes: 0 additions & 8 deletions src/atoms/repeat.atoms.ts

This file was deleted.

8 changes: 0 additions & 8 deletions src/atoms/seek.atoms.ts

This file was deleted.

18 changes: 0 additions & 18 deletions src/atoms/speed.atoms.ts

This file was deleted.

8 changes: 0 additions & 8 deletions src/atoms/volume.atoms.ts

This file was deleted.

40 changes: 29 additions & 11 deletions src/components/audio/audio.component.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,40 @@
import React from 'react';
import {NextSeo} from 'next-seo';
import React, {useEffect} from 'react';
import {useAudioPlayerContext} from 'src/contexts/audio-player-context';
import {useAudioRefContext} from 'src/contexts/audio-ref-context';
import {useDynamicTitle} from 'src/hooks/use-dynamic-title';

import {Invisible} from './audio.component.styles';
import {useAudioModule} from './hooks/use-audio-component';

export function AudioComponent() {
interface Props {
defaultSpeed: string;
}

export function AudioComponent({defaultSpeed}: Props) {
const audioRef = useAudioRefContext();
const {dynamicTitle} = useDynamicTitle();
const {setSpeed} = useAudioPlayerContext();

useEffect(() => {
setSpeed(defaultSpeed);
}, [defaultSpeed, setSpeed]);

useAudioModule();

return (
<Invisible>
<audio
ref={audioRef}
aria-label="player"
// controls
>
<track kind="captions" />
</audio>
</Invisible>
<>
<NextSeo title={dynamicTitle} />

<Invisible>
<audio
ref={audioRef}
aria-label="player"
// controls
>
<track kind="captions" />
</audio>
</Invisible>
</>
);
}
5 changes: 2 additions & 3 deletions src/components/audio/hooks/use-audio-buffered.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {useAtom} from 'jotai';
import {useEffect} from 'react';
import {setBufferedAtom} from 'src/atoms/buffered.atoms';
import {useAudioPlayerContext} from 'src/contexts/audio-player-context';
import {useAudioRefContext} from 'src/contexts/audio-ref-context';

const getBufferedAmount = (audio: HTMLAudioElement) => {
Expand All @@ -13,7 +12,7 @@ const getBufferedAmount = (audio: HTMLAudioElement) => {

export function useAudioBuffered() {
const ref = useAudioRefContext();
const [, setBuffered] = useAtom(setBufferedAtom);
const {setBuffered} = useAudioPlayerContext();

useEffect(() => {
if (ref.current === null) {
Expand Down
24 changes: 11 additions & 13 deletions src/components/audio/hooks/use-audio-load.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
import {useAtom} from 'jotai';
import {useCallback, useEffect, useState} from 'react';
import {setDurationAtom} from 'src/atoms/duration.atoms';
import {setLoadedAtom} from 'src/atoms/load.atoms';
import {useAudioPlayerContext} from 'src/contexts/audio-player-context';
import {useAudioRefContext} from 'src/contexts/audio-ref-context';
import {useAudioUrlContext} from 'src/contexts/audio-url-context';

export function useAudioLoad() {
const ref = useAudioRefContext();
const {url} = useAudioUrlContext();
const {setDuration, setReady} = useAudioPlayerContext();
const [savedUrl, setSavedUrl] = useState<string>();
const [, setLoaded] = useAtom(setLoadedAtom);
const [, setDuration] = useAtom(setDurationAtom);

const handleCanPlay = useCallback(() => {
setLoaded(true);
}, [setLoaded]);
setReady(true);
}, [setReady]);

const handleMetadata = useCallback(() => {
if (ref.current === null) {
Expand All @@ -38,12 +35,13 @@ export function useAudioLoad() {
audio.src = url;
setSavedUrl(url);

audio.addEventListener('canplay', () => handleCanPlay());
audio.addEventListener('loadedmetadata', () => handleMetadata());
audio.oncanplay = handleCanPlay;
audio.onloadedmetadata = handleMetadata;

return () => {
audio.removeEventListener('canplay', () => handleCanPlay());
audio.removeEventListener('loadedmetadata', () => handleMetadata());
};
// todo: fix me
// return () => {
// audio.oncanplay = null;
// audio.onloadedmetadata = null;
// };
}, [ref, handleCanPlay, handleMetadata, savedUrl, setDuration, url]);
}
5 changes: 2 additions & 3 deletions src/components/audio/hooks/use-audio-loop.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import {useAtom} from 'jotai';
import {useEffect} from 'react';
import {isRepeatingAtom} from 'src/atoms/repeat.atoms';
import {useAudioPlayerContext} from 'src/contexts/audio-player-context';
import {useAudioRefContext} from 'src/contexts/audio-ref-context';

export function useAudioLoop() {
const ref = useAudioRefContext();
const [isRepeating] = useAtom(isRepeatingAtom);
const {isRepeating} = useAudioPlayerContext();

useEffect(() => {
if (ref.current === null) {
Expand Down
5 changes: 2 additions & 3 deletions src/components/audio/hooks/use-audio-play-pause.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import {useAtom} from 'jotai';
import {useEffect} from 'react';
import {isPlayingAtom} from 'src/atoms/play-pause.atoms';
import {useAudioPlayerContext} from 'src/contexts/audio-player-context';
import {useAudioRefContext} from 'src/contexts/audio-ref-context';

export function useAudioPlayPause() {
const ref = useAudioRefContext();
const [isPlaying] = useAtom(isPlayingAtom);
const {isPlaying} = useAudioPlayerContext();

useEffect(() => {
if (ref.current === null) {
Expand Down
5 changes: 2 additions & 3 deletions src/components/audio/hooks/use-audio-playback-rate.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import {useAtom} from 'jotai';
import {useEffect} from 'react';
import {speedAtom} from 'src/atoms/speed.atoms';
import {useAudioPlayerContext} from 'src/contexts/audio-player-context';
import {useAudioRefContext} from 'src/contexts/audio-ref-context';

export function useAudioPlaybackRate() {
const ref = useAudioRefContext();
const [speed] = useAtom(speedAtom);
const {speed} = useAudioPlayerContext();

useEffect(() => {
if (ref.current === null) {
Expand Down
7 changes: 2 additions & 5 deletions src/components/audio/hooks/use-audio-progress.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import {useAtom} from 'jotai';
import {useEffect} from 'react';
import {isPlayingAtom} from 'src/atoms/play-pause.atoms';
import {setProgressAtom} from 'src/atoms/progress.atoms';
import {useAudioPlayerContext} from 'src/contexts/audio-player-context';
import {useAudioRefContext} from 'src/contexts/audio-ref-context';

export function useAudioProgress() {
const ref = useAudioRefContext();
const [isPlaying] = useAtom(isPlayingAtom);
const [, setProgress] = useAtom(setProgressAtom);
const {isPlaying, setProgress} = useAudioPlayerContext();
const fps = 10;

useEffect(() => {
Expand Down
5 changes: 2 additions & 3 deletions src/components/audio/hooks/use-audio-seek.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import {useAtom} from 'jotai';
import {useEffect} from 'react';
import {seekAtom} from 'src/atoms/seek.atoms';
import {useAudioPlayerContext} from 'src/contexts/audio-player-context';
import {useAudioRefContext} from 'src/contexts/audio-ref-context';

export function useAudioSeek() {
const ref = useAudioRefContext();
const [seek] = useAtom(seekAtom);
const {seek} = useAudioPlayerContext();

useEffect(() => {
if (ref.current === null) {
Expand Down
6 changes: 2 additions & 4 deletions src/components/audio/hooks/use-audio-volume.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import {useAtom} from 'jotai';
import {useEffect} from 'react';
import {setVolumeAtom, volumeAtom} from 'src/atoms/volume.atoms';
import {useAudioPlayerContext} from 'src/contexts/audio-player-context';
import {useAudioRefContext} from 'src/contexts/audio-ref-context';

export function useAudioVolume() {
const ref = useAudioRefContext();
const [volume] = useAtom(volumeAtom);
const [, setVolume] = useAtom(setVolumeAtom);
const {volume, setVolume} = useAudioPlayerContext();

useEffect(() => {
if (ref.current === null) {
Expand Down
35 changes: 11 additions & 24 deletions src/components/audio/hooks/use-keyboard-toggle.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,20 @@
import {useAtom} from 'jotai';
import {useEffect} from 'react';
import {useAudioPlayerContext} from 'src/contexts/audio-player-context';

import {
isPlayingAtom,
setPauseAtom,
setPlayAtom,
} from '../../../atoms/play-pause.atoms';

/**
* Hook to toggle play/pause state with keyboard
* @param keyCode
*/
export function useKeyboardToggle(keyCode = 'Space'): void {
const [isPlaying] = useAtom(isPlayingAtom);
const [, setPlay] = useAtom(setPlayAtom);
const [, setPause] = useAtom(setPauseAtom);
const {togglePlaying} = useAudioPlayerContext();

useEffect(() => {
const handleKeyboard = (event) => {
if (event.code === keyCode) {
if (isPlaying) {
return setPause();
}

setPlay();
const handler = (e: KeyboardEvent) => {
if (e.code === keyCode) {
togglePlaying();
}
};

document.addEventListener('keypress', handleKeyboard);
return () => document.removeEventListener('keypress', handleKeyboard);
}, [isPlaying, keyCode, setPause, setPlay]);
document.addEventListener('keypress', handler);

return () => {
document.removeEventListener('keypress', handler);
};
}, [keyCode, togglePlaying]);
}
20 changes: 11 additions & 9 deletions src/components/indicators/hooks/use-indicators-component.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
import {useAtom} from 'jotai';
import {useMemo} from 'react';
import speedToPercentage from 'speed-to-percentage';
import speedToSemitones from 'speed-to-semitones';

import {speedAtom} from '../../../atoms/speed.atoms';
import {useAudioPlayerContext} from 'src/contexts/audio-player-context';

interface UseIndicatorsComponent {
percentage: string;
semitones: string;
}

/**
* Entry hook for the indicators component
*/
export function useIndicatorsComponent(): UseIndicatorsComponent {
const [speed] = useAtom(speedAtom);
const percentage = useMemo(() => speedToPercentage(speed, 1).toString(), [speed]);
const semitones = useMemo(() => speedToSemitones(speed, 1).toString(), [speed]);
const {speed} = useAudioPlayerContext();

const percentage = useMemo(
() => speedToPercentage(speed, 1).toString(),
[speed],
);
const semitones = useMemo(
() => speedToSemitones(speed, 1).toString(),
[speed],
);

return {
percentage,
Expand Down
Loading

0 comments on commit 4015b61

Please sign in to comment.