Prevent wrong initial color on navigation on the same route
This commit is contained in:
parent
48eaddbeda
commit
853770ea8e
7 changed files with 62 additions and 38 deletions
|
@ -1,4 +1,4 @@
|
||||||
import { NativeScrollArea } from '/@/renderer/components';
|
import { NativeScrollArea, Spinner } from '/@/renderer/components';
|
||||||
import { AnimatedPage, LibraryHeaderBar } from '/@/renderer/features/shared';
|
import { AnimatedPage, LibraryHeaderBar } from '/@/renderer/features/shared';
|
||||||
import { useRef } from 'react';
|
import { useRef } from 'react';
|
||||||
import type { AgGridReact as AgGridReactType } from '@ag-grid-community/react/lib/agGridReact';
|
import type { AgGridReact as AgGridReactType } from '@ag-grid-community/react/lib/agGridReact';
|
||||||
|
@ -20,7 +20,11 @@ const AlbumDetailRoute = () => {
|
||||||
const { albumId } = useParams() as { albumId: string };
|
const { albumId } = useParams() as { albumId: string };
|
||||||
const server = useCurrentServer();
|
const server = useCurrentServer();
|
||||||
const detailQuery = useAlbumDetail({ query: { id: albumId }, serverId: server?.id });
|
const detailQuery = useAlbumDetail({ query: { id: albumId }, serverId: server?.id });
|
||||||
const background = useFastAverageColor(detailQuery.data?.imageUrl, !detailQuery.isLoading);
|
const { color: background, colorId } = useFastAverageColor({
|
||||||
|
id: albumId,
|
||||||
|
src: detailQuery.data?.imageUrl,
|
||||||
|
srcLoaded: !detailQuery.isLoading,
|
||||||
|
});
|
||||||
const handlePlayQueueAdd = usePlayQueueAdd();
|
const handlePlayQueueAdd = usePlayQueueAdd();
|
||||||
const playButtonBehavior = usePlayButtonBehavior();
|
const playButtonBehavior = usePlayButtonBehavior();
|
||||||
|
|
||||||
|
@ -34,7 +38,9 @@ const AlbumDetailRoute = () => {
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
if (!background) return null;
|
if (!background || colorId !== albumId) {
|
||||||
|
return <Spinner container />;
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<AnimatedPage key={`album-detail-${albumId}`}>
|
<AnimatedPage key={`album-detail-${albumId}`}>
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { NativeScrollArea } from '/@/renderer/components';
|
import { NativeScrollArea, Spinner } from '/@/renderer/components';
|
||||||
import { AnimatedPage, LibraryHeaderBar } from '/@/renderer/features/shared';
|
import { AnimatedPage, LibraryHeaderBar } from '/@/renderer/features/shared';
|
||||||
import { useRef } from 'react';
|
import { useRef } from 'react';
|
||||||
import { useParams } from 'react-router';
|
import { useParams } from 'react-router';
|
||||||
|
@ -23,7 +23,11 @@ const AlbumArtistDetailRoute = () => {
|
||||||
query: { id: albumArtistId },
|
query: { id: albumArtistId },
|
||||||
serverId: server?.id,
|
serverId: server?.id,
|
||||||
});
|
});
|
||||||
const background = useFastAverageColor(detailQuery.data?.imageUrl, !detailQuery.isLoading);
|
const { color, colorId } = useFastAverageColor({
|
||||||
|
id: albumArtistId,
|
||||||
|
src: detailQuery.data?.imageUrl,
|
||||||
|
srcLoaded: !detailQuery.isLoading,
|
||||||
|
});
|
||||||
|
|
||||||
const handlePlay = () => {
|
const handlePlay = () => {
|
||||||
handlePlayQueueAdd?.({
|
handlePlayQueueAdd?.({
|
||||||
|
@ -35,14 +39,16 @@ const AlbumArtistDetailRoute = () => {
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
if (detailQuery.isLoading || !background) return null;
|
if (detailQuery.isLoading || !color || colorId !== albumArtistId) {
|
||||||
|
return <Spinner container />;
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<AnimatedPage key={`album-artist-detail-${albumArtistId}`}>
|
<AnimatedPage key={`album-artist-detail-${albumArtistId}`}>
|
||||||
<NativeScrollArea
|
<NativeScrollArea
|
||||||
ref={scrollAreaRef}
|
ref={scrollAreaRef}
|
||||||
pageHeaderProps={{
|
pageHeaderProps={{
|
||||||
backgroundColor: background,
|
backgroundColor: color,
|
||||||
children: (
|
children: (
|
||||||
<LibraryHeaderBar>
|
<LibraryHeaderBar>
|
||||||
<LibraryHeaderBar.PlayButton onClick={handlePlay} />
|
<LibraryHeaderBar.PlayButton onClick={handlePlay} />
|
||||||
|
@ -57,9 +63,9 @@ const AlbumArtistDetailRoute = () => {
|
||||||
>
|
>
|
||||||
<AlbumArtistDetailHeader
|
<AlbumArtistDetailHeader
|
||||||
ref={headerRef}
|
ref={headerRef}
|
||||||
background={background}
|
background={color}
|
||||||
/>
|
/>
|
||||||
<AlbumArtistDetailContent background={background} />
|
<AlbumArtistDetailContent background={color} />
|
||||||
</NativeScrollArea>
|
</NativeScrollArea>
|
||||||
</AnimatedPage>
|
</AnimatedPage>
|
||||||
);
|
);
|
||||||
|
|
|
@ -122,7 +122,11 @@ export const FullScreenPlayerImage = () => {
|
||||||
const { queue } = usePlayerData();
|
const { queue } = usePlayerData();
|
||||||
const useImageAspectRatio = useFullScreenPlayerStore((state) => state.useImageAspectRatio);
|
const useImageAspectRatio = useFullScreenPlayerStore((state) => state.useImageAspectRatio);
|
||||||
const currentSong = queue.current;
|
const currentSong = queue.current;
|
||||||
const background = useFastAverageColor(queue.current?.imageUrl, true, 'dominant');
|
const { color: background } = useFastAverageColor({
|
||||||
|
algorithm: 'dominant',
|
||||||
|
src: queue.current?.imageUrl,
|
||||||
|
srcLoaded: true,
|
||||||
|
});
|
||||||
const imageKey = `image-${background}`;
|
const imageKey = `image-${background}`;
|
||||||
|
|
||||||
const [imageState, setImageState] = useSetState({
|
const [imageState, setImageState] = useSetState({
|
||||||
|
|
|
@ -193,7 +193,11 @@ export const FullScreenPlayer = () => {
|
||||||
}, [location, setStore]);
|
}, [location, setStore]);
|
||||||
|
|
||||||
const currentSong = useCurrentSong();
|
const currentSong = useCurrentSong();
|
||||||
const background = useFastAverageColor(currentSong?.imageUrl, true, 'dominant');
|
const { color: background } = useFastAverageColor({
|
||||||
|
algorithm: 'dominant',
|
||||||
|
src: currentSong?.imageUrl,
|
||||||
|
srcLoaded: true,
|
||||||
|
});
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Container
|
<Container
|
||||||
|
|
|
@ -1,19 +1,16 @@
|
||||||
import { MutableRefObject, useMemo, useRef } from 'react';
|
|
||||||
import { ColDef, RowDoubleClickedEvent } from '@ag-grid-community/core';
|
import { ColDef, RowDoubleClickedEvent } from '@ag-grid-community/core';
|
||||||
import type { AgGridReact as AgGridReactType } from '@ag-grid-community/react/lib/agGridReact';
|
import type { AgGridReact as AgGridReactType } from '@ag-grid-community/react/lib/agGridReact';
|
||||||
import { Box, Group } from '@mantine/core';
|
import { Box, Group } from '@mantine/core';
|
||||||
import { closeAllModals, openModal } from '@mantine/modals';
|
import { closeAllModals, openModal } from '@mantine/modals';
|
||||||
|
import { MutableRefObject, useMemo, useRef } from 'react';
|
||||||
import { RiMoreFill } from 'react-icons/ri';
|
import { RiMoreFill } from 'react-icons/ri';
|
||||||
import { generatePath, useNavigate, useParams } from 'react-router';
|
import { generatePath, useNavigate, useParams } from 'react-router';
|
||||||
import { Link } from 'react-router-dom';
|
import { Link } from 'react-router-dom';
|
||||||
import styled from 'styled-components';
|
import styled from 'styled-components';
|
||||||
|
import { useListStoreByKey } from '../../../store/list.store';
|
||||||
import { LibraryItem, QueueSong } from '/@/renderer/api/types';
|
import { LibraryItem, QueueSong } from '/@/renderer/api/types';
|
||||||
import { Button, ConfirmModal, DropdownMenu, MotionGroup, toast } from '/@/renderer/components';
|
import { Button, ConfirmModal, DropdownMenu, MotionGroup, toast } from '/@/renderer/components';
|
||||||
import {
|
import { getColumnDefs, VirtualTable } from '/@/renderer/components/virtual-table';
|
||||||
getColumnDefs,
|
|
||||||
useFixedTableHeader,
|
|
||||||
VirtualTable,
|
|
||||||
} from '/@/renderer/components/virtual-table';
|
|
||||||
import { useCurrentSongRowStyles } from '/@/renderer/components/virtual-table/hooks/use-current-song-row-styles';
|
import { useCurrentSongRowStyles } from '/@/renderer/components/virtual-table/hooks/use-current-song-row-styles';
|
||||||
import { useHandleTableContextMenu } from '/@/renderer/features/context-menu';
|
import { useHandleTableContextMenu } from '/@/renderer/features/context-menu';
|
||||||
import {
|
import {
|
||||||
|
@ -30,7 +27,6 @@ import { AppRoute } from '/@/renderer/router/routes';
|
||||||
import { useCurrentServer } from '/@/renderer/store';
|
import { useCurrentServer } from '/@/renderer/store';
|
||||||
import { usePlayButtonBehavior } from '/@/renderer/store/settings.store';
|
import { usePlayButtonBehavior } from '/@/renderer/store/settings.store';
|
||||||
import { Play } from '/@/renderer/types';
|
import { Play } from '/@/renderer/types';
|
||||||
import { useListStoreByKey } from '../../../store/list.store';
|
|
||||||
|
|
||||||
const ContentContainer = styled.div`
|
const ContentContainer = styled.div`
|
||||||
position: relative;
|
position: relative;
|
||||||
|
@ -97,8 +93,6 @@ export const PlaylistDetailContent = ({ tableRef }: PlaylistDetailContentProps)
|
||||||
[playlistSongsQueryInfinite.data?.pages],
|
[playlistSongsQueryInfinite.data?.pages],
|
||||||
);
|
);
|
||||||
|
|
||||||
const { intersectRef, tableContainerRef } = useFixedTableHeader();
|
|
||||||
|
|
||||||
const deletePlaylistMutation = useDeletePlaylist({});
|
const deletePlaylistMutation = useDeletePlaylist({});
|
||||||
|
|
||||||
const handleDeletePlaylist = () => {
|
const handleDeletePlaylist = () => {
|
||||||
|
@ -166,7 +160,6 @@ export const PlaylistDetailContent = ({ tableRef }: PlaylistDetailContentProps)
|
||||||
return (
|
return (
|
||||||
<ContentContainer>
|
<ContentContainer>
|
||||||
<Group
|
<Group
|
||||||
ref={intersectRef}
|
|
||||||
p="1rem"
|
p="1rem"
|
||||||
position="apart"
|
position="apart"
|
||||||
>
|
>
|
||||||
|
@ -217,12 +210,13 @@ export const PlaylistDetailContent = ({ tableRef }: PlaylistDetailContentProps)
|
||||||
</Button>
|
</Button>
|
||||||
</Group>
|
</Group>
|
||||||
</Group>
|
</Group>
|
||||||
<Box ref={tableContainerRef}>
|
<Box>
|
||||||
<VirtualTable
|
<VirtualTable
|
||||||
ref={tableRef}
|
ref={tableRef}
|
||||||
autoFitColumns
|
autoFitColumns
|
||||||
autoHeight
|
autoHeight
|
||||||
deselectOnClickOutside
|
deselectOnClickOutside
|
||||||
|
stickyHeader
|
||||||
suppressCellFocus
|
suppressCellFocus
|
||||||
suppressHorizontalScroll
|
suppressHorizontalScroll
|
||||||
suppressLoadingOverlay
|
suppressLoadingOverlay
|
||||||
|
|
|
@ -2,7 +2,7 @@ import { useRef } from 'react';
|
||||||
import type { AgGridReact as AgGridReactType } from '@ag-grid-community/react/lib/agGridReact';
|
import type { AgGridReact as AgGridReactType } from '@ag-grid-community/react/lib/agGridReact';
|
||||||
import { useParams } from 'react-router';
|
import { useParams } from 'react-router';
|
||||||
import { LibraryItem } from '/@/renderer/api/types';
|
import { LibraryItem } from '/@/renderer/api/types';
|
||||||
import { NativeScrollArea } from '/@/renderer/components';
|
import { NativeScrollArea, Spinner } from '/@/renderer/components';
|
||||||
import { usePlayQueueAdd } from '/@/renderer/features/player';
|
import { usePlayQueueAdd } from '/@/renderer/features/player';
|
||||||
import { PlaylistDetailContent } from '/@/renderer/features/playlists/components/playlist-detail-content';
|
import { PlaylistDetailContent } from '/@/renderer/features/playlists/components/playlist-detail-content';
|
||||||
import { PlaylistDetailHeader } from '/@/renderer/features/playlists/components/playlist-detail-header';
|
import { PlaylistDetailHeader } from '/@/renderer/features/playlists/components/playlist-detail-header';
|
||||||
|
@ -20,11 +20,12 @@ const PlaylistDetailRoute = () => {
|
||||||
const server = useCurrentServer();
|
const server = useCurrentServer();
|
||||||
|
|
||||||
const detailQuery = usePlaylistDetail({ query: { id: playlistId }, serverId: server?.id });
|
const detailQuery = usePlaylistDetail({ query: { id: playlistId }, serverId: server?.id });
|
||||||
const background = useFastAverageColor(
|
const { color: background, colorId } = useFastAverageColor({
|
||||||
detailQuery?.data?.imageUrl,
|
algorithm: 'sqrt',
|
||||||
!detailQuery?.isLoading,
|
id: playlistId,
|
||||||
'sqrt',
|
src: detailQuery?.data?.imageUrl,
|
||||||
);
|
srcLoaded: !detailQuery?.isLoading,
|
||||||
|
});
|
||||||
|
|
||||||
const handlePlayQueueAdd = usePlayQueueAdd();
|
const handlePlayQueueAdd = usePlayQueueAdd();
|
||||||
const playButtonBehavior = usePlayButtonBehavior();
|
const playButtonBehavior = usePlayButtonBehavior();
|
||||||
|
@ -39,7 +40,9 @@ const PlaylistDetailRoute = () => {
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
if (!background) return null;
|
if (!background || colorId !== playlistId) {
|
||||||
|
return <Spinner container />;
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<AnimatedPage key={`playlist-detail-${playlistId}`}>
|
<AnimatedPage key={`playlist-detail-${playlistId}`}>
|
||||||
|
@ -55,6 +58,7 @@ const PlaylistDetailRoute = () => {
|
||||||
</LibraryHeaderBar.Title>
|
</LibraryHeaderBar.Title>
|
||||||
</LibraryHeaderBar>
|
</LibraryHeaderBar>
|
||||||
),
|
),
|
||||||
|
offset: 200,
|
||||||
target: headerRef,
|
target: headerRef,
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
|
|
|
@ -1,11 +1,15 @@
|
||||||
import { useEffect, useState } from 'react';
|
import { useEffect, useRef, useState } from 'react';
|
||||||
import { FastAverageColor } from 'fast-average-color';
|
import { FastAverageColor } from 'fast-average-color';
|
||||||
|
|
||||||
export const useFastAverageColor = (
|
export const useFastAverageColor = (args: {
|
||||||
src?: string | null,
|
algorithm?: 'dominant' | 'simple' | 'sqrt';
|
||||||
srcLoaded?: boolean,
|
id?: string;
|
||||||
aglorithm?: 'dominant' | 'simple' | 'sqrt',
|
src?: string | null;
|
||||||
) => {
|
srcLoaded?: boolean;
|
||||||
|
}) => {
|
||||||
|
const { algorithm, src, srcLoaded, id } = args;
|
||||||
|
const idRef = useRef<string | undefined>(id);
|
||||||
|
|
||||||
const [color, setColor] = useState<string | undefined>(undefined);
|
const [color, setColor] = useState<string | undefined>(undefined);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
@ -13,7 +17,7 @@ export const useFastAverageColor = (
|
||||||
|
|
||||||
if (src && srcLoaded) {
|
if (src && srcLoaded) {
|
||||||
fac.getColorAsync(src, {
|
fac.getColorAsync(src, {
|
||||||
algorithm: aglorithm || 'dominant',
|
algorithm: algorithm || 'dominant',
|
||||||
ignoredColor: [
|
ignoredColor: [
|
||||||
[255, 255, 255, 255, 90], // White
|
[255, 255, 255, 255, 90], // White
|
||||||
[0, 0, 0, 255, 30], // Black
|
[0, 0, 0, 255, 30], // Black
|
||||||
|
@ -22,10 +26,12 @@ export const useFastAverageColor = (
|
||||||
mode: 'precision',
|
mode: 'precision',
|
||||||
})
|
})
|
||||||
.then((color) => {
|
.then((color) => {
|
||||||
|
idRef.current = id;
|
||||||
return setColor(color.rgb);
|
return setColor(color.rgb);
|
||||||
})
|
})
|
||||||
.catch((e) => {
|
.catch((e) => {
|
||||||
console.log('Error fetching average color', e);
|
console.log('Error fetching average color', e);
|
||||||
|
idRef.current = id;
|
||||||
return setColor('rgba(0, 0, 0, 0)');
|
return setColor('rgba(0, 0, 0, 0)');
|
||||||
});
|
});
|
||||||
} else if (srcLoaded) {
|
} else if (srcLoaded) {
|
||||||
|
@ -35,7 +41,7 @@ export const useFastAverageColor = (
|
||||||
return () => {
|
return () => {
|
||||||
fac.destroy();
|
fac.destroy();
|
||||||
};
|
};
|
||||||
}, [aglorithm, srcLoaded, src]);
|
}, [algorithm, srcLoaded, src, id]);
|
||||||
|
|
||||||
return color;
|
return { color, colorId: idRef.current };
|
||||||
};
|
};
|
||||||
|
|
Reference in a new issue