diff --git a/src/renderer/features/albums/routes/album-detail-route.tsx b/src/renderer/features/albums/routes/album-detail-route.tsx index fea095161..908723b97 100644 --- a/src/renderer/features/albums/routes/album-detail-route.tsx +++ b/src/renderer/features/albums/routes/album-detail-route.tsx @@ -14,7 +14,7 @@ import { import { LibraryContainer } from '/@/renderer/features/shared/components/library-container'; import { LibraryHeaderBar } from '/@/renderer/features/shared/components/library-header-bar'; import { PageErrorBoundary } from '/@/renderer/features/shared/components/page-error-boundary'; -import { useFastAverageColor } from '/@/renderer/hooks'; +import { useFastAverageColor, useWaitForColorCalculation } from '/@/renderer/hooks'; import { useCurrentServer, useGeneralSettings } from '/@/renderer/store'; import { LibraryItem } from '/@/shared/types/domain-types'; @@ -34,7 +34,7 @@ const AlbumDetailRoute = () => { staleTime: 0, }); - const { background: backgroundColor } = useFastAverageColor({ + const { background: backgroundColor, isLoading: isColorLoading } = useFastAverageColor({ id: albumId, src: detailQuery.data?.imageUrl, srcLoaded: !detailQuery.isLoading, @@ -44,6 +44,17 @@ const AlbumDetailRoute = () => { const showBlurredImage = albumBackground; + const { isReady } = useWaitForColorCalculation({ + hasImage: !!detailQuery.data?.imageUrl, + isLoading: isColorLoading, + routeId: albumId, + showBlurredImage, + }); + + if (!isReady) { + return null; + } + return ( { staleTime: 0, }); - const { background: backgroundColor } = useFastAverageColor({ + const { background: backgroundColor, isLoading: isColorLoading } = useFastAverageColor({ id: artistId, src: detailQuery.data?.imageUrl, srcLoaded: !detailQuery.isLoading, @@ -49,6 +49,17 @@ const AlbumArtistDetailRoute = () => { const showBlurredImage = artistBackground; + const { isReady } = useWaitForColorCalculation({ + hasImage: !!detailQuery.data?.imageUrl, + isLoading: isColorLoading, + routeId, + showBlurredImage, + }); + + if (!isReady) { + return null; + } + return ( { + const { hasImage, isLoading, routeId, showBlurredImage, timeoutMs = 1000 } = args; + const [timeoutReached, setTimeoutReached] = useState(false); + + const shouldWaitForColor = hasImage && !showBlurredImage; + + useEffect(() => { + setTimeoutReached(false); + + if (!shouldWaitForColor) { + return; + } + + const timeoutId = setTimeout(() => { + setTimeoutReached(true); + }, timeoutMs); + + return () => { + clearTimeout(timeoutId); + }; + }, [shouldWaitForColor, routeId, timeoutMs]); + + const isReady = !shouldWaitForColor || !isLoading || timeoutReached; + + return { isReady }; +};