2024-09-15 21:48:32 -07:00
|
|
|
import { forwardRef, Fragment, Ref, useCallback, useMemo } from 'react';
|
2024-08-25 19:52:44 -07:00
|
|
|
import { Group, Stack } from '@mantine/core';
|
|
|
|
|
import { useTranslation } from 'react-i18next';
|
2022-12-29 18:45:01 -08:00
|
|
|
import { generatePath, useParams } from 'react-router';
|
|
|
|
|
import { Link } from 'react-router-dom';
|
2024-09-15 21:48:32 -07:00
|
|
|
import { AlbumDetailResponse, LibraryItem, ServerType } from '/@/renderer/api/types';
|
2023-05-21 15:36:15 -07:00
|
|
|
import { Rating, Text } from '/@/renderer/components';
|
2022-12-29 18:45:01 -08:00
|
|
|
import { useAlbumDetail } from '/@/renderer/features/albums/queries/album-detail-query';
|
2023-04-30 22:01:52 -07:00
|
|
|
import { LibraryHeader, useSetRating } from '/@/renderer/features/shared';
|
2022-12-29 18:45:01 -08:00
|
|
|
import { useContainerQuery } from '/@/renderer/hooks';
|
|
|
|
|
import { AppRoute } from '/@/renderer/router/routes';
|
2023-04-30 22:01:52 -07:00
|
|
|
import { useCurrentServer } from '/@/renderer/store';
|
2024-09-12 20:35:57 -05:00
|
|
|
import { formatDateAbsoluteUTC, formatDurationString } from '/@/renderer/utils';
|
2024-09-15 21:48:32 -07:00
|
|
|
import { useSongChange } from '/@/renderer/hooks/use-song-change';
|
|
|
|
|
import { queryKeys } from '/@/renderer/api/query-keys';
|
|
|
|
|
import { queryClient } from '/@/renderer/lib/react-query';
|
2022-12-29 18:45:01 -08:00
|
|
|
|
|
|
|
|
interface AlbumDetailHeaderProps {
|
2024-09-01 19:42:01 -04:00
|
|
|
background: {
|
|
|
|
|
background: string;
|
|
|
|
|
blur: number;
|
|
|
|
|
};
|
2022-12-29 18:45:01 -08:00
|
|
|
}
|
|
|
|
|
|
2022-12-31 17:50:05 -08:00
|
|
|
export const AlbumDetailHeader = forwardRef(
|
2023-07-01 19:10:05 -07:00
|
|
|
({ background }: AlbumDetailHeaderProps, ref: Ref<HTMLDivElement>) => {
|
|
|
|
|
const { albumId } = useParams() as { albumId: string };
|
|
|
|
|
const server = useCurrentServer();
|
|
|
|
|
const detailQuery = useAlbumDetail({ query: { id: albumId }, serverId: server?.id });
|
|
|
|
|
const cq = useContainerQuery();
|
2024-08-25 19:52:44 -07:00
|
|
|
const { t } = useTranslation();
|
|
|
|
|
|
2024-09-01 19:42:01 -04:00
|
|
|
const showRating = detailQuery?.data?.serverType === ServerType.NAVIDROME;
|
|
|
|
|
|
2024-08-25 19:52:44 -07:00
|
|
|
const originalDifferentFromRelease =
|
|
|
|
|
detailQuery.data?.originalDate &&
|
|
|
|
|
detailQuery.data.originalDate !== detailQuery.data.releaseDate;
|
|
|
|
|
|
|
|
|
|
const releasePrefix = originalDifferentFromRelease
|
|
|
|
|
? t('page.albumDetail.released', { postProcess: 'sentenceCase' })
|
|
|
|
|
: '♫';
|
2022-12-29 18:45:01 -08:00
|
|
|
|
2024-09-15 21:48:32 -07:00
|
|
|
const songIds = useMemo(() => {
|
|
|
|
|
return new Set(detailQuery.data?.songs?.map((song) => song.id));
|
|
|
|
|
}, [detailQuery.data?.songs]);
|
|
|
|
|
|
|
|
|
|
const handleSongChange = useCallback(
|
|
|
|
|
(id: string) => {
|
|
|
|
|
if (songIds.has(id)) {
|
|
|
|
|
const queryKey = queryKeys.albums.detail(server?.id, { id: albumId });
|
|
|
|
|
queryClient.setQueryData<AlbumDetailResponse | undefined>(
|
|
|
|
|
queryKey,
|
|
|
|
|
(previous) => {
|
|
|
|
|
if (!previous) return undefined;
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
...previous,
|
|
|
|
|
playCount: previous.playCount ? previous.playCount + 1 : 1,
|
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
[albumId, server?.id, songIds],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
useSongChange((ids, event) => {
|
|
|
|
|
if (event.event === 'play') {
|
|
|
|
|
handleSongChange(ids[0]);
|
|
|
|
|
}
|
|
|
|
|
}, detailQuery.data !== undefined);
|
|
|
|
|
|
2023-07-01 19:10:05 -07:00
|
|
|
const metadataItems = [
|
|
|
|
|
{
|
2024-08-25 19:52:44 -07:00
|
|
|
id: 'releaseDate',
|
|
|
|
|
value:
|
|
|
|
|
detailQuery?.data?.releaseDate &&
|
2024-09-12 20:35:57 -05:00
|
|
|
`${releasePrefix} ${formatDateAbsoluteUTC(detailQuery?.data?.releaseDate)}`,
|
2023-07-01 19:10:05 -07:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
id: 'songCount',
|
|
|
|
|
value: `${detailQuery?.data?.songCount} songs`,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
id: 'duration',
|
|
|
|
|
value:
|
2023-08-11 21:08:13 -07:00
|
|
|
detailQuery?.data?.duration && formatDurationString(detailQuery.data.duration),
|
2023-07-01 19:10:05 -07:00
|
|
|
},
|
2024-08-29 23:03:00 -04:00
|
|
|
{
|
|
|
|
|
id: 'playCount',
|
2024-09-01 12:48:11 -07:00
|
|
|
value: t('entity.play', {
|
|
|
|
|
count: detailQuery?.data?.playCount as number,
|
|
|
|
|
}),
|
2024-08-29 23:03:00 -04:00
|
|
|
},
|
2023-07-01 19:10:05 -07:00
|
|
|
];
|
2023-01-02 17:57:49 -08:00
|
|
|
|
2024-08-25 19:52:44 -07:00
|
|
|
if (originalDifferentFromRelease) {
|
2024-09-12 20:35:57 -05:00
|
|
|
const formatted = `♫ ${formatDateAbsoluteUTC(detailQuery!.data!.originalDate)}`;
|
2024-08-25 19:52:44 -07:00
|
|
|
metadataItems.splice(0, 0, {
|
|
|
|
|
id: 'originalDate',
|
|
|
|
|
value: formatted,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-01 19:10:05 -07:00
|
|
|
const updateRatingMutation = useSetRating({});
|
2023-02-05 05:19:01 -08:00
|
|
|
|
2023-07-01 19:10:05 -07:00
|
|
|
const handleUpdateRating = (rating: number) => {
|
|
|
|
|
if (!detailQuery?.data) return;
|
2023-02-05 05:19:01 -08:00
|
|
|
|
2023-07-01 19:10:05 -07:00
|
|
|
updateRatingMutation.mutate({
|
|
|
|
|
query: {
|
|
|
|
|
item: [detailQuery.data],
|
|
|
|
|
rating,
|
|
|
|
|
},
|
|
|
|
|
serverId: detailQuery.data.serverId,
|
|
|
|
|
});
|
|
|
|
|
};
|
2023-02-05 05:19:01 -08:00
|
|
|
|
2023-07-01 19:10:05 -07:00
|
|
|
return (
|
|
|
|
|
<Stack ref={cq.ref}>
|
|
|
|
|
<LibraryHeader
|
|
|
|
|
ref={ref}
|
|
|
|
|
imageUrl={detailQuery?.data?.imageUrl}
|
|
|
|
|
item={{ route: AppRoute.LIBRARY_ALBUMS, type: LibraryItem.ALBUM }}
|
|
|
|
|
title={detailQuery?.data?.name || ''}
|
2024-09-01 19:42:01 -04:00
|
|
|
{...background}
|
2023-03-09 18:16:57 -08:00
|
|
|
>
|
2023-07-01 19:10:05 -07:00
|
|
|
<Stack spacing="sm">
|
|
|
|
|
<Group spacing="sm">
|
|
|
|
|
{metadataItems.map((item, index) => (
|
|
|
|
|
<Fragment key={`item-${item.id}-${index}`}>
|
|
|
|
|
{index > 0 && <Text $noSelect>•</Text>}
|
2024-08-25 19:52:44 -07:00
|
|
|
<Text>{item.value}</Text>
|
2023-07-01 19:10:05 -07:00
|
|
|
</Fragment>
|
|
|
|
|
))}
|
|
|
|
|
{showRating && (
|
|
|
|
|
<>
|
|
|
|
|
<Text $noSelect>•</Text>
|
|
|
|
|
<Rating
|
|
|
|
|
readOnly={
|
|
|
|
|
detailQuery?.isFetching ||
|
|
|
|
|
updateRatingMutation.isLoading
|
|
|
|
|
}
|
|
|
|
|
value={detailQuery?.data?.userRating || 0}
|
|
|
|
|
onChange={handleUpdateRating}
|
|
|
|
|
/>
|
|
|
|
|
</>
|
|
|
|
|
)}
|
|
|
|
|
</Group>
|
|
|
|
|
<Group
|
2023-07-19 01:39:19 -07:00
|
|
|
mah="4rem"
|
2023-07-01 19:10:05 -07:00
|
|
|
spacing="md"
|
|
|
|
|
sx={{
|
|
|
|
|
WebkitBoxOrient: 'vertical',
|
|
|
|
|
WebkitLineClamp: 2,
|
|
|
|
|
overflow: 'hidden',
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{detailQuery?.data?.albumArtists.map((artist) => (
|
|
|
|
|
<Text
|
|
|
|
|
key={`artist-${artist.id}`}
|
|
|
|
|
$link
|
|
|
|
|
component={Link}
|
|
|
|
|
fw={600}
|
|
|
|
|
to={generatePath(AppRoute.LIBRARY_ALBUM_ARTISTS_DETAIL, {
|
|
|
|
|
albumArtistId: artist.id,
|
|
|
|
|
})}
|
|
|
|
|
variant="subtle"
|
|
|
|
|
>
|
|
|
|
|
{artist.name}
|
|
|
|
|
</Text>
|
|
|
|
|
))}
|
|
|
|
|
</Group>
|
|
|
|
|
</Stack>
|
|
|
|
|
</LibraryHeader>
|
|
|
|
|
</Stack>
|
|
|
|
|
);
|
|
|
|
|
},
|
2022-12-31 17:50:05 -08:00
|
|
|
);
|