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

476 lines
14 KiB
TypeScript
Raw Normal View History

2023-01-12 18:43:25 -08:00
import { useMemo } from 'react';
2023-05-17 17:12:23 -07:00
import { Button, Text, TextTitle } from '/@/renderer/components';
2023-01-12 18:43:25 -08:00
import { ColDef, RowDoubleClickedEvent } from '@ag-grid-community/core';
import { Box, Group, Stack } from '@mantine/core';
import { RiHeartFill, RiHeartLine, RiMoreFill } from 'react-icons/ri';
2023-01-12 18:43:25 -08:00
import { generatePath, useParams } from 'react-router';
import { useCurrentServer } from '/@/renderer/store';
2023-01-15 16:22:07 -08:00
import { createSearchParams, Link } from 'react-router-dom';
2023-01-12 18:43:25 -08:00
import styled from 'styled-components';
import { AppRoute } from '/@/renderer/router/routes';
import { useContainerQuery } from '/@/renderer/hooks';
import { usePlayButtonBehavior } from '/@/renderer/store/settings.store';
import {
useHandleGeneralContextMenu,
useHandleTableContextMenu,
} from '/@/renderer/features/context-menu';
2023-05-17 17:12:23 -07:00
import { CardRow, Play, TableColumn } from '/@/renderer/types';
2023-01-12 18:43:25 -08:00
import {
ARTIST_CONTEXT_MENU_ITEMS,
SONG_CONTEXT_MENU_ITEMS,
} from '/@/renderer/features/context-menu/context-menu-items';
import { PlayButton, useCreateFavorite, useDeleteFavorite } from '/@/renderer/features/shared';
2023-01-12 18:43:25 -08:00
import { useAlbumList } from '/@/renderer/features/albums/queries/album-list-query';
import {
2023-05-17 17:12:23 -07:00
Album,
AlbumArtist,
2023-01-12 18:43:25 -08:00
AlbumListSort,
LibraryItem,
QueueSong,
ServerType,
SortOrder,
} from '/@/renderer/api/types';
import { usePlayQueueAdd } from '/@/renderer/features/player';
import { useAlbumArtistDetail } from '/@/renderer/features/artists/queries/album-artist-detail-query';
import { useTopSongsList } from '/@/renderer/features/artists/queries/top-songs-list-query';
import { getColumnDefs, VirtualTable } from '/@/renderer/components/virtual-table';
2023-05-17 17:12:23 -07:00
import { SwiperGridCarousel } from '/@/renderer/components/grid-carousel';
2023-01-12 18:43:25 -08:00
const ContentContainer = styled.div`
position: relative;
display: flex;
flex-direction: column;
gap: 3rem;
padding: 1rem 2rem 5rem;
overflow: hidden;
.ag-theme-alpine-dark {
--ag-header-background-color: rgba(0, 0, 0, 0%) !important;
}
.ag-header {
margin-bottom: 0.5rem;
}
`;
export const AlbumArtistDetailContent = () => {
const { albumArtistId } = useParams() as { albumArtistId: string };
const cq = useContainerQuery();
const handlePlayQueueAdd = usePlayQueueAdd();
const server = useCurrentServer();
const detailQuery = useAlbumArtistDetail({ query: { id: albumArtistId }, serverId: server?.id });
2023-01-12 18:43:25 -08:00
2023-01-15 16:22:07 -08:00
const artistDiscographyLink = `${generatePath(AppRoute.LIBRARY_ALBUM_ARTISTS_DETAIL_DISCOGRAPHY, {
albumArtistId,
})}?${createSearchParams({
artistId: albumArtistId,
artistName: detailQuery?.data?.name || '',
})}`;
2023-01-15 20:39:43 -08:00
const artistSongsLink = `${generatePath(AppRoute.LIBRARY_ALBUM_ARTISTS_DETAIL_SONGS, {
albumArtistId,
})}?${createSearchParams({
artistId: albumArtistId,
artistName: detailQuery?.data?.name || '',
})}`;
2023-01-12 18:43:25 -08:00
const recentAlbumsQuery = useAlbumList({
query: {
_custom: {
jellyfin: {
2023-05-17 17:12:23 -07:00
...(server?.type === ServerType.JELLYFIN ? { ArtistIds: albumArtistId } : undefined),
},
navidrome: {
...(server?.type === ServerType.NAVIDROME
? { artist_id: albumArtistId, compilation: false }
: undefined),
},
},
2023-05-17 17:12:23 -07:00
// limit: 10,
sortBy: AlbumListSort.RELEASE_DATE,
sortOrder: SortOrder.DESC,
startIndex: 0,
},
serverId: server?.id,
2023-01-12 18:43:25 -08:00
});
2023-01-15 20:39:43 -08:00
const compilationAlbumsQuery = useAlbumList({
query: {
_custom: {
jellyfin: {
...(server?.type === ServerType.JELLYFIN
2023-05-17 17:12:23 -07:00
? { ContributingArtistIds: albumArtistId }
: undefined),
},
navidrome: {
...(server?.type === ServerType.NAVIDROME
? { artist_id: albumArtistId, compilation: true }
: undefined),
},
},
2023-05-17 17:12:23 -07:00
// limit: 10,
sortBy: AlbumListSort.RELEASE_DATE,
sortOrder: SortOrder.DESC,
startIndex: 0,
},
serverId: server?.id,
2023-01-15 20:39:43 -08:00
});
const topSongsQuery = useTopSongsList({
options: {
enabled: !!detailQuery?.data?.name,
},
query: {
artist: detailQuery?.data?.name || '',
artistId: albumArtistId,
},
serverId: server?.id,
});
2023-01-12 18:43:25 -08:00
const topSongsColumnDefs: ColDef[] = useMemo(
() =>
getColumnDefs([
{ column: TableColumn.ROW_INDEX, width: 0 },
{ column: TableColumn.TITLE_COMBINED, width: 0 },
{ column: TableColumn.DURATION, width: 0 },
{ column: TableColumn.ALBUM, width: 0 },
{ column: TableColumn.YEAR, width: 0 },
{ column: TableColumn.PLAY_COUNT, width: 0 },
{ column: TableColumn.USER_FAVORITE, width: 0 },
]),
[],
);
2023-05-17 17:12:23 -07:00
const cardRows: Record<string, CardRow<Album>[] | CardRow<AlbumArtist>[]> = {
2023-01-12 18:43:25 -08:00
album: [
{
property: 'name',
route: {
route: AppRoute.LIBRARY_ALBUMS_DETAIL,
slugs: [{ idProperty: 'id', slugProperty: 'albumId' }],
},
},
{
arrayProperty: 'name',
property: 'albumArtists',
route: {
route: AppRoute.LIBRARY_ALBUM_ARTISTS_DETAIL,
slugs: [{ idProperty: 'id', slugProperty: 'albumArtistId' }],
},
},
],
albumArtist: [
{
property: 'name',
route: {
route: AppRoute.LIBRARY_ALBUM_ARTISTS_DETAIL,
slugs: [{ idProperty: 'id', slugProperty: 'albumArtistId' }],
},
},
],
};
2023-05-17 17:12:23 -07:00
const cardRoutes = {
album: {
route: AppRoute.LIBRARY_ALBUMS_DETAIL,
slugs: [{ idProperty: 'id', slugProperty: 'albumId' }],
},
albumArtist: {
route: AppRoute.LIBRARY_ALBUM_ARTISTS_DETAIL,
slugs: [{ idProperty: 'id', slugProperty: 'albumArtistId' }],
},
};
2023-01-12 18:43:25 -08:00
const carousels = [
{
data: recentAlbumsQuery?.data?.items,
2023-01-15 20:39:43 -08:00
isHidden: !recentAlbumsQuery?.data?.items?.length,
2023-01-12 18:43:25 -08:00
itemType: LibraryItem.ALBUM,
loading: recentAlbumsQuery?.isLoading || recentAlbumsQuery.isFetching,
title: (
2023-05-17 17:12:23 -07:00
<Group align="flex-end">
2023-01-12 18:43:25 -08:00
<TextTitle
2023-01-30 01:36:36 -08:00
order={2}
weight={700}
2023-01-12 18:43:25 -08:00
>
2023-01-15 20:39:43 -08:00
Recent releases
2023-01-12 18:43:25 -08:00
</TextTitle>
<Button
compact
uppercase
component={Link}
2023-01-15 16:22:07 -08:00
to={artistDiscographyLink}
2023-01-12 18:43:25 -08:00
variant="subtle"
>
View discography
</Button>
2023-05-17 17:12:23 -07:00
</Group>
2023-01-12 18:43:25 -08:00
),
2023-01-15 20:39:43 -08:00
uniqueId: 'recentReleases',
},
{
data: compilationAlbumsQuery?.data?.items,
isHidden: !compilationAlbumsQuery?.data?.items?.length,
itemType: LibraryItem.ALBUM,
loading: compilationAlbumsQuery?.isLoading || compilationAlbumsQuery.isFetching,
title: (
<TextTitle
2023-01-30 01:36:36 -08:00
order={2}
weight={700}
2023-01-15 20:39:43 -08:00
>
Appears on
</TextTitle>
),
uniqueId: 'compilationAlbums',
2023-01-12 18:43:25 -08:00
},
{
2023-05-17 17:12:23 -07:00
data: detailQuery?.data?.similarArtists || [],
2023-01-12 18:43:25 -08:00
isHidden: !detailQuery?.data?.similarArtists,
itemType: LibraryItem.ALBUM_ARTIST,
loading: detailQuery?.isLoading || detailQuery.isFetching,
title: (
<TextTitle
2023-01-30 01:36:36 -08:00
order={2}
weight={700}
2023-01-12 18:43:25 -08:00
>
Related artists
</TextTitle>
),
uniqueId: 'similarArtists',
},
];
const playButtonBehavior = usePlayButtonBehavior();
const handlePlay = async (playType?: Play) => {
handlePlayQueueAdd?.({
byItemType: {
id: [albumArtistId],
type: LibraryItem.ALBUM_ARTIST,
},
play: playType || playButtonBehavior,
});
};
const handleContextMenu = useHandleTableContextMenu(LibraryItem.SONG, SONG_CONTEXT_MENU_ITEMS);
const handleRowDoubleClick = (e: RowDoubleClickedEvent<QueueSong>) => {
if (!e.data) return;
handlePlayQueueAdd?.({
byData: [e.data],
play: playButtonBehavior,
});
};
const createFavoriteMutation = useCreateFavorite({});
const deleteFavoriteMutation = useDeleteFavorite({});
2023-01-12 18:43:25 -08:00
const handleFavorite = () => {
if (!detailQuery?.data) return;
if (detailQuery.data.userFavorite) {
deleteFavoriteMutation.mutate({
query: {
id: [detailQuery.data.id],
type: LibraryItem.ALBUM_ARTIST,
},
2023-05-13 22:54:24 -07:00
serverId: detailQuery.data.serverId,
2023-01-12 18:43:25 -08:00
});
} else {
createFavoriteMutation.mutate({
query: {
id: [detailQuery.data.id],
type: LibraryItem.ALBUM_ARTIST,
},
2023-05-13 22:54:24 -07:00
serverId: detailQuery.data.serverId,
2023-01-12 18:43:25 -08:00
});
}
};
const handleGeneralContextMenu = useHandleGeneralContextMenu(
LibraryItem.ALBUM_ARTIST,
ARTIST_CONTEXT_MENU_ITEMS,
);
2023-01-12 18:43:25 -08:00
const topSongs = topSongsQuery?.data?.items?.slice(0, 10);
const showBiography =
detailQuery?.data?.biography !== undefined && detailQuery?.data?.biography !== null;
2023-02-27 12:17:22 -08:00
const showTopSongs = topSongsQuery?.data?.items?.length;
2023-01-15 20:39:43 -08:00
const showGenres = detailQuery?.data?.genres ? detailQuery?.data?.genres.length !== 0 : false;
2023-01-12 18:43:25 -08:00
const isLoading =
2023-01-15 20:39:43 -08:00
detailQuery?.isLoading || (server?.type === ServerType.NAVIDROME && topSongsQuery?.isLoading);
2023-01-12 18:43:25 -08:00
if (isLoading) return <ContentContainer ref={cq.ref} />;
return (
<ContentContainer ref={cq.ref}>
<Box component="section">
<Group spacing="md">
2023-01-12 18:43:25 -08:00
<PlayButton onClick={() => handlePlay(playButtonBehavior)} />
<Group spacing="xs">
<Button
compact
loading={createFavoriteMutation.isLoading || deleteFavoriteMutation.isLoading}
variant="subtle"
onClick={handleFavorite}
>
{detailQuery?.data?.userFavorite ? (
<RiHeartFill
color="red"
size={20}
/>
) : (
<RiHeartLine size={20} />
)}
</Button>
<Button
compact
variant="subtle"
onClick={(e) => {
if (!detailQuery?.data) return;
handleGeneralContextMenu(e, [detailQuery.data!]);
}}
>
<RiMoreFill size={20} />
</Button>
2023-01-13 01:44:47 -08:00
<Button
compact
uppercase
component={Link}
2023-01-15 16:22:07 -08:00
to={artistDiscographyLink}
2023-01-13 01:44:47 -08:00
variant="subtle"
>
View discography
</Button>
<Button
compact
uppercase
component={Link}
2023-01-15 20:39:43 -08:00
to={artistSongsLink}
2023-01-13 01:44:47 -08:00
variant="subtle"
>
View all songs
</Button>
2023-01-12 18:43:25 -08:00
</Group>
</Group>
</Box>
{showGenres ? (
2023-01-12 18:43:25 -08:00
<Box component="section">
2023-03-09 18:14:40 -08:00
<Group spacing="sm">
2023-01-12 18:43:25 -08:00
{detailQuery?.data?.genres?.map((genre) => (
<Button
key={`genre-${genre.id}`}
compact
component={Link}
radius="md"
2023-03-09 18:14:40 -08:00
size="md"
2023-01-12 18:43:25 -08:00
to={generatePath(`${AppRoute.LIBRARY_ALBUM_ARTISTS}?genre=${genre.id}`, {
albumArtistId,
})}
2023-03-09 18:14:40 -08:00
variant="outline"
2023-01-12 18:43:25 -08:00
>
{genre.name}
</Button>
))}
</Group>
</Box>
) : null}
2023-01-12 18:43:25 -08:00
{showBiography ? (
<Box
component="section"
maw="1280px"
>
<TextTitle
2023-01-30 01:36:36 -08:00
order={2}
weight={700}
2023-01-12 18:43:25 -08:00
>
About {detailQuery?.data?.name}
</TextTitle>
<Text
$secondary
component="p"
dangerouslySetInnerHTML={{ __html: detailQuery?.data?.biography || '' }}
sx={{ textAlign: 'justify' }}
/>
</Box>
) : null}
{showTopSongs ? (
2023-01-12 18:43:25 -08:00
<Box component="section">
<Group
noWrap
position="apart"
>
<Group
noWrap
align="flex-end"
>
<TextTitle
2023-01-30 01:36:36 -08:00
order={2}
weight={700}
2023-01-12 18:43:25 -08:00
>
Top Songs
</TextTitle>
<Button
compact
uppercase
component={Link}
to={generatePath(AppRoute.LIBRARY_ALBUM_ARTISTS_DETAIL_TOP_SONGS, {
albumArtistId,
})}
variant="subtle"
>
View all
</Button>
</Group>
</Group>
<VirtualTable
autoFitColumns
autoHeight
deselectOnClickOutside
suppressCellFocus
suppressHorizontalScroll
suppressLoadingOverlay
suppressRowDrag
columnDefs={topSongsColumnDefs}
enableCellChangeFlash={false}
getRowId={(data) => data.data.uniqueId}
rowData={topSongs}
rowHeight={60}
rowSelection="multiple"
onCellContextMenu={handleContextMenu}
onRowDoubleClicked={handleRowDoubleClick}
/>
</Box>
) : null}
2023-01-12 18:43:25 -08:00
<Box component="section">
<Stack spacing="xl">
{carousels
.filter((c) => !c.isHidden)
.map((carousel) => (
2023-05-17 17:12:23 -07:00
<SwiperGridCarousel
2023-01-12 18:43:25 -08:00
key={`carousel-${carousel.uniqueId}`}
cardRows={cardRows[carousel.itemType as keyof typeof cardRows]}
data={carousel.data}
2023-05-17 17:12:23 -07:00
isLoading={carousel.loading}
2023-01-12 18:43:25 -08:00
itemType={carousel.itemType}
2023-05-17 17:12:23 -07:00
route={cardRoutes[carousel.itemType as keyof typeof cardRoutes]}
swiperProps={{
grid: {
rows: 2,
},
}}
title={{
label: carousel.title,
}}
2023-01-12 18:43:25 -08:00
uniqueId={carousel.uniqueId}
2023-05-17 17:12:23 -07:00
/>
2023-01-12 18:43:25 -08:00
))}
</Stack>
</Box>
</ContentContainer>
);
};