mirror of
https://github.com/jeffvli/feishin.git
synced 2026-05-16 05:36:00 +02:00
add utility to wait for detail bg color before page render
This commit is contained in:
@@ -14,7 +14,7 @@ import {
|
|||||||
import { LibraryContainer } from '/@/renderer/features/shared/components/library-container';
|
import { LibraryContainer } from '/@/renderer/features/shared/components/library-container';
|
||||||
import { LibraryHeaderBar } from '/@/renderer/features/shared/components/library-header-bar';
|
import { LibraryHeaderBar } from '/@/renderer/features/shared/components/library-header-bar';
|
||||||
import { PageErrorBoundary } from '/@/renderer/features/shared/components/page-error-boundary';
|
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 { useCurrentServer, useGeneralSettings } from '/@/renderer/store';
|
||||||
import { LibraryItem } from '/@/shared/types/domain-types';
|
import { LibraryItem } from '/@/shared/types/domain-types';
|
||||||
|
|
||||||
@@ -34,7 +34,7 @@ const AlbumDetailRoute = () => {
|
|||||||
staleTime: 0,
|
staleTime: 0,
|
||||||
});
|
});
|
||||||
|
|
||||||
const { background: backgroundColor } = useFastAverageColor({
|
const { background: backgroundColor, isLoading: isColorLoading } = useFastAverageColor({
|
||||||
id: albumId,
|
id: albumId,
|
||||||
src: detailQuery.data?.imageUrl,
|
src: detailQuery.data?.imageUrl,
|
||||||
srcLoaded: !detailQuery.isLoading,
|
srcLoaded: !detailQuery.isLoading,
|
||||||
@@ -44,6 +44,17 @@ const AlbumDetailRoute = () => {
|
|||||||
|
|
||||||
const showBlurredImage = albumBackground;
|
const showBlurredImage = albumBackground;
|
||||||
|
|
||||||
|
const { isReady } = useWaitForColorCalculation({
|
||||||
|
hasImage: !!detailQuery.data?.imageUrl,
|
||||||
|
isLoading: isColorLoading,
|
||||||
|
routeId: albumId,
|
||||||
|
showBlurredImage,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!isReady) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<AnimatedPage key={`album-detail-${albumId}`}>
|
<AnimatedPage key={`album-detail-${albumId}`}>
|
||||||
<NativeScrollArea
|
<NativeScrollArea
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ import {
|
|||||||
import { LibraryContainer } from '/@/renderer/features/shared/components/library-container';
|
import { LibraryContainer } from '/@/renderer/features/shared/components/library-container';
|
||||||
import { LibraryHeaderBar } from '/@/renderer/features/shared/components/library-header-bar';
|
import { LibraryHeaderBar } from '/@/renderer/features/shared/components/library-header-bar';
|
||||||
import { PageErrorBoundary } from '/@/renderer/features/shared/components/page-error-boundary';
|
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 { useCurrentServer, useGeneralSettings } from '/@/renderer/store';
|
||||||
import { LibraryItem } from '/@/shared/types/domain-types';
|
import { LibraryItem } from '/@/shared/types/domain-types';
|
||||||
|
|
||||||
@@ -39,7 +39,7 @@ const AlbumArtistDetailRoute = () => {
|
|||||||
staleTime: 0,
|
staleTime: 0,
|
||||||
});
|
});
|
||||||
|
|
||||||
const { background: backgroundColor } = useFastAverageColor({
|
const { background: backgroundColor, isLoading: isColorLoading } = useFastAverageColor({
|
||||||
id: artistId,
|
id: artistId,
|
||||||
src: detailQuery.data?.imageUrl,
|
src: detailQuery.data?.imageUrl,
|
||||||
srcLoaded: !detailQuery.isLoading,
|
srcLoaded: !detailQuery.isLoading,
|
||||||
@@ -49,6 +49,17 @@ const AlbumArtistDetailRoute = () => {
|
|||||||
|
|
||||||
const showBlurredImage = artistBackground;
|
const showBlurredImage = artistBackground;
|
||||||
|
|
||||||
|
const { isReady } = useWaitForColorCalculation({
|
||||||
|
hasImage: !!detailQuery.data?.imageUrl,
|
||||||
|
isLoading: isColorLoading,
|
||||||
|
routeId,
|
||||||
|
showBlurredImage,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!isReady) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<AnimatedPage key={`album-artist-detail-${routeId}`}>
|
<AnimatedPage key={`album-artist-detail-${routeId}`}>
|
||||||
<NativeScrollArea
|
<NativeScrollArea
|
||||||
|
|||||||
@@ -155,3 +155,36 @@ export const useFastAverageColor = (args: {
|
|||||||
isLoading,
|
isLoading,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const useWaitForColorCalculation = (args: {
|
||||||
|
hasImage: boolean;
|
||||||
|
isLoading: boolean;
|
||||||
|
routeId: string;
|
||||||
|
showBlurredImage: boolean;
|
||||||
|
timeoutMs?: number;
|
||||||
|
}) => {
|
||||||
|
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 };
|
||||||
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user