Wait for load before setting background color
This commit is contained in:
parent
b8b8ca9f66
commit
b0bc4c3cf3
3 changed files with 11 additions and 9 deletions
|
@ -18,7 +18,7 @@ const AlbumDetailRoute = () => {
|
||||||
|
|
||||||
const { albumId } = useParams() as { albumId: string };
|
const { albumId } = useParams() as { albumId: string };
|
||||||
const detailQuery = useAlbumDetail({ id: albumId });
|
const detailQuery = useAlbumDetail({ id: albumId });
|
||||||
const background = useFastAverageColor(detailQuery.data?.imageUrl);
|
const background = useFastAverageColor(detailQuery.data?.imageUrl, !detailQuery.isLoading);
|
||||||
const handlePlayQueueAdd = usePlayQueueAdd();
|
const handlePlayQueueAdd = usePlayQueueAdd();
|
||||||
const playButtonBehavior = usePlayButtonBehavior();
|
const playButtonBehavior = usePlayButtonBehavior();
|
||||||
|
|
||||||
|
|
|
@ -18,7 +18,11 @@ const PlaylistDetailRoute = () => {
|
||||||
const { playlistId } = useParams() as { playlistId: string };
|
const { playlistId } = useParams() as { playlistId: string };
|
||||||
|
|
||||||
const detailQuery = usePlaylistDetail({ id: playlistId });
|
const detailQuery = usePlaylistDetail({ id: playlistId });
|
||||||
const background = useFastAverageColor(detailQuery?.data?.imageUrl, 'dominant');
|
const background = useFastAverageColor(
|
||||||
|
detailQuery?.data?.imageUrl,
|
||||||
|
!detailQuery?.isLoading,
|
||||||
|
'dominant',
|
||||||
|
);
|
||||||
|
|
||||||
const handlePlayQueueAdd = usePlayQueueAdd();
|
const handlePlayQueueAdd = usePlayQueueAdd();
|
||||||
const playButtonBehavior = usePlayButtonBehavior();
|
const playButtonBehavior = usePlayButtonBehavior();
|
||||||
|
|
|
@ -1,17 +1,17 @@
|
||||||
import { useEffect, useRef, useState } from 'react';
|
import { useEffect, useState } from 'react';
|
||||||
import { FastAverageColor } from 'fast-average-color';
|
import { FastAverageColor } from 'fast-average-color';
|
||||||
|
|
||||||
export const useFastAverageColor = (
|
export const useFastAverageColor = (
|
||||||
src?: string | null,
|
src?: string | null,
|
||||||
|
srcLoaded?: boolean,
|
||||||
aglorithm?: 'dominant' | 'simple' | 'sqrt',
|
aglorithm?: 'dominant' | 'simple' | 'sqrt',
|
||||||
) => {
|
) => {
|
||||||
const isMountedRef = useRef<boolean | null>(null);
|
|
||||||
const [color, setColor] = useState<string | undefined>(undefined);
|
const [color, setColor] = useState<string | undefined>(undefined);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const fac = new FastAverageColor();
|
const fac = new FastAverageColor();
|
||||||
|
|
||||||
if (src) {
|
if (src && srcLoaded) {
|
||||||
fac
|
fac
|
||||||
.getColorAsync(src, {
|
.getColorAsync(src, {
|
||||||
algorithm: aglorithm || 'dominant',
|
algorithm: aglorithm || 'dominant',
|
||||||
|
@ -28,16 +28,14 @@ export const useFastAverageColor = (
|
||||||
console.log('Error fetching average color', e);
|
console.log('Error fetching average color', e);
|
||||||
return setColor('rgba(0, 0, 0, 0)');
|
return setColor('rgba(0, 0, 0, 0)');
|
||||||
});
|
});
|
||||||
} else if (isMountedRef.current) {
|
} else if (srcLoaded) {
|
||||||
return setColor('var(--placeholder-bg)');
|
return setColor('var(--placeholder-bg)');
|
||||||
} else {
|
|
||||||
isMountedRef.current = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
fac.destroy();
|
fac.destroy();
|
||||||
};
|
};
|
||||||
}, [aglorithm, src]);
|
}, [aglorithm, srcLoaded, src]);
|
||||||
|
|
||||||
return color;
|
return color;
|
||||||
};
|
};
|
||||||
|
|
Reference in a new issue