Skip to content

Commit

Permalink
feat: extract audio state into custom hook
Browse files Browse the repository at this point in the history
  • Loading branch information
AnthonyPorthouse committed Nov 14, 2024
1 parent 2fef727 commit ab6bd8a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/Components/MediaPlayer/MediaControls.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useAudio } from "@hooks/useAudio.js";
import { useAudioState } from "@hooks/useAudio.js";
import { useCallback } from "react";

import FullscreenButton from "../MediaControls/FullscreenButton.js";
import PauseButton from "../MediaControls/PauseButton.js";
Expand All @@ -7,15 +8,15 @@ import SkipButton from "../MediaControls/SkipButton.js";
import StopButton from "../MediaControls/StopButton.js";

function MediaControls() {
const audio = useAudio();
const { isPaused } = useAudioState();

const playPauseButton = () => {
if (audio?.paused) {
const playPauseButton = useCallback(() => {
if (isPaused) {
return <PlayButton />;
}

return <PauseButton />;
};
}, [isPaused]);

return (
<div className={`flex w-full gap-2`}>
Expand Down
8 changes: 8 additions & 0 deletions src/hooks/useAudio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,11 @@ import { useContext } from "react";
export function useAudio() {
return useContext(AudioContext);
}

export function useAudioState() {
const audio = useAudio();

return {
isPaused: audio?.paused ?? false,
};
}

0 comments on commit ab6bd8a

Please sign in to comment.