feishin/src/renderer/features/artists/components/album-artist-detail-header.tsx

120 lines
4.6 KiB
TypeScript
Raw Normal View History

import { useQuery } from '@tanstack/react-query';
import { forwardRef, Fragment, Ref } from 'react';
import { useTranslation } from 'react-i18next';
2023-01-12 18:43:25 -08:00
import { useParams } from 'react-router';
import { artistsQueries } from '/@/renderer/features/artists/api/artists-api';
2025-09-22 22:39:46 -07:00
import { LibraryHeader } from '/@/renderer/features/shared/components/library-header';
import { useSetRating } from '/@/renderer/features/shared/mutations/set-rating-mutation';
2023-01-12 18:43:25 -08:00
import { AppRoute } from '/@/renderer/router/routes';
2025-05-20 19:23:36 -07:00
import { useCurrentServer } from '/@/renderer/store';
2023-01-12 18:43:25 -08:00
import { formatDurationString } from '/@/renderer/utils';
import { Group } from '/@/shared/components/group/group';
import { Rating } from '/@/shared/components/rating/rating';
import { Stack } from '/@/shared/components/stack/stack';
import { Text } from '/@/shared/components/text/text';
2025-05-20 19:23:36 -07:00
import { LibraryItem, ServerType } from '/@/shared/types/domain-types';
2023-01-12 18:43:25 -08:00
interface AlbumArtistDetailHeaderProps {
background: {
background?: string;
blur: number;
loading: boolean;
};
2023-01-12 18:43:25 -08:00
}
export const AlbumArtistDetailHeader = forwardRef(
({ background }: AlbumArtistDetailHeaderProps, ref: Ref<HTMLDivElement>) => {
2025-05-06 14:43:42 -07:00
const { albumArtistId, artistId } = useParams() as {
albumArtistId?: string;
artistId?: string;
};
const routeId = (artistId || albumArtistId) as string;
2023-07-01 19:10:05 -07:00
const server = useCurrentServer();
const { t } = useTranslation();
const detailQuery = useQuery(
artistsQueries.albumArtistDetail({
query: { id: routeId },
serverId: server?.id,
}),
);
2023-01-12 18:43:25 -08:00
2025-05-07 19:53:23 -07:00
const albumCount = detailQuery?.data?.albumCount;
const songCount = detailQuery?.data?.songCount;
const duration = detailQuery?.data?.duration;
const durationEnabled = duration !== null && duration !== undefined;
2023-07-01 19:10:05 -07:00
const metadataItems = [
{
2025-05-07 19:53:23 -07:00
enabled: albumCount !== null && albumCount !== undefined,
2023-07-01 19:10:05 -07:00
id: 'albumCount',
secondary: false,
2025-05-07 19:53:23 -07:00
value: t('entity.albumWithCount', { count: albumCount || 0 }),
2023-07-01 19:10:05 -07:00
},
{
2025-05-07 19:53:23 -07:00
enabled: songCount !== null && songCount !== undefined,
2023-07-01 19:10:05 -07:00
id: 'songCount',
secondary: false,
2025-05-07 19:53:23 -07:00
value: t('entity.trackWithCount', { count: songCount || 0 }),
2023-07-01 19:10:05 -07:00
},
{
2025-05-07 19:53:23 -07:00
enabled: durationEnabled,
2023-07-01 19:10:05 -07:00
id: 'duration',
secondary: true,
2025-05-07 19:53:23 -07:00
value: durationEnabled && formatDurationString(duration),
2023-07-01 19:10:05 -07:00
},
];
2023-01-12 18:43:25 -08:00
2023-07-01 19:10:05 -07:00
const updateRatingMutation = useSetRating({});
2023-07-01 19:10:05 -07:00
const handleUpdateRating = (rating: number) => {
if (!detailQuery?.data) return;
2023-07-01 19:10:05 -07:00
updateRatingMutation.mutate({
query: {
item: [detailQuery.data],
rating,
},
serverId: detailQuery?.data.serverId,
});
};
2023-07-01 19:10:05 -07:00
const showRating = detailQuery?.data?.serverType === ServerType.NAVIDROME;
2023-07-01 19:10:05 -07:00
return (
<LibraryHeader
imageUrl={detailQuery?.data?.imageUrl}
item={{ route: AppRoute.LIBRARY_ALBUM_ARTISTS, type: LibraryItem.ALBUM_ARTIST }}
ref={ref}
title={detailQuery?.data?.name || ''}
{...background}
>
<Stack>
<Group>
{metadataItems
2024-09-26 04:23:08 +00:00
.filter((i) => i.enabled)
.map((item, index) => (
<Fragment key={`item-${item.id}-${index}`}>
{index > 0 && <Text isNoSelect></Text>}
<Text isMuted={item.secondary}>{item.value}</Text>
</Fragment>
))}
{showRating && (
<>
<Text isNoSelect></Text>
<Rating
onChange={handleUpdateRating}
readOnly={
detailQuery?.isFetching || updateRatingMutation.isPending
}
value={detailQuery?.data?.userRating || 0}
/>
</>
)}
</Group>
</Stack>
</LibraryHeader>
2023-07-01 19:10:05 -07:00
);
},
2023-01-12 18:43:25 -08:00
);