Skip to content

Commit

Permalink
perf: prevent unnecessary rerender of Playlist
Browse files Browse the repository at this point in the history
  • Loading branch information
SevenOutman committed Apr 15, 2023
1 parent 75317b1 commit 4ebc0a6
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/components/player.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useRef, useState } from "react";
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import cx from "clsx";

import { ReactComponent as IconPlay } from "../assets/play.svg";
Expand Down Expand Up @@ -93,6 +93,16 @@ export function APlayer({
const [isPlaylistOpen, setPlaylistOpen] = useState(() => hasPlaylist);

const themeColor = useThemeColor(playlist.currentSong, theme);
const playlistAudioProp = useMemo(
() => (Array.isArray(audio) ? audio : [audio]),
[audio]
);

const { prioritize } = playlist;
const handlePlayAudioFromList = useCallback(
(audioInfo: AudioInfo) => prioritize(audioInfo),
[prioritize]
);

return (
<div
Expand Down Expand Up @@ -157,9 +167,9 @@ export function APlayer({
<Playlist
themeColor={themeColor}
open={isPlaylistOpen}
audio={Array.isArray(audio) ? audio : [audio]}
audio={playlistAudioProp}
playingAudioUrl={playlist.currentSong.url}
onPlayAudio={(audioInfo) => playlist.prioritize(audioInfo)}
onPlayAudio={handlePlayAudioFromList}
/>
) : null}
</div>
Expand Down

0 comments on commit 4ebc0a6

Please # to comment.