import React from 'react'; import { generatePath } from 'react-router'; import { Link } from 'react-router-dom'; import styled from 'styled-components'; import { Album, AlbumArtist, Artist, Playlist, Song } from '/@/renderer/api/types'; import { Text } from '/@/renderer/components/text'; import { AppRoute } from '/@/renderer/router/routes'; import { CardRow } from '/@/renderer/types'; const Row = styled.div<{ $secondary?: boolean }>` width: 100%; max-width: 100%; height: 22px; padding: 0 0.2rem; overflow: hidden; color: ${({ $secondary }) => ($secondary ? 'var(--main-fg-secondary)' : 'var(--main-fg)')}; text-overflow: ellipsis; white-space: nowrap; user-select: none; `; interface CardRowsProps { data: any; rows: CardRow[] | CardRow[] | CardRow[]; } export const CardRows = ({ data, rows }: CardRowsProps) => { return ( <> {rows.map((row, index: number) => { if (row.arrayProperty && row.route) { return ( 0} > {data[row.property].map((item: any, itemIndex: number) => ( {itemIndex > 0 && ( , )}{' '} 0} component={Link} overflow="hidden" size={index > 0 ? 'sm' : 'md'} to={generatePath( row.route!.route, row.route!.slugs?.reduce((acc, slug) => { return { ...acc, [slug.slugProperty]: data[row.property][itemIndex][ slug.idProperty ], }; }, {}), )} onClick={(e) => e.stopPropagation()} > {row.arrayProperty && item[row.arrayProperty]} ))} ); } if (row.arrayProperty) { return ( {data[row.property].map((item: any) => ( 0} overflow="hidden" size={index > 0 ? 'sm' : 'md'} > {row.arrayProperty && item[row.arrayProperty]} ))} ); } return ( {row.route ? ( { return { ...acc, [slug.slugProperty]: data[slug.idProperty], }; }, {}), )} onClick={(e) => e.stopPropagation()} > {data && data[row.property]} ) : ( 0} overflow="hidden" size={index > 0 ? 'sm' : 'md'} > {data && data[row.property]} )} ); })} ); }; export const ALBUM_CARD_ROWS: { [key: string]: CardRow } = { albumArtists: { arrayProperty: 'name', property: 'albumArtists', route: { route: AppRoute.LIBRARY_ALBUM_ARTISTS_DETAIL, slugs: [{ idProperty: 'id', slugProperty: 'albumArtistId' }], }, }, artists: { arrayProperty: 'name', property: 'artists', route: { route: AppRoute.LIBRARY_ALBUM_ARTISTS_DETAIL, slugs: [{ idProperty: 'id', slugProperty: 'albumArtistId' }], }, }, createdAt: { property: 'createdAt', }, duration: { property: 'duration', }, lastPlayedAt: { property: 'lastPlayedAt', }, name: { property: 'name', route: { route: AppRoute.LIBRARY_ALBUMS_DETAIL, slugs: [{ idProperty: 'id', slugProperty: 'albumId' }], }, }, playCount: { property: 'playCount', }, rating: { property: 'userRating', }, releaseDate: { property: 'releaseDate', }, releaseYear: { property: 'releaseYear', }, songCount: { property: 'songCount', }, }; export const SONG_CARD_ROWS: { [key: string]: CardRow } = { album: { property: 'album', route: { route: AppRoute.LIBRARY_ALBUMS_DETAIL, slugs: [{ idProperty: 'albumId', slugProperty: 'albumId' }], }, }, albumArtists: { arrayProperty: 'name', property: 'albumArtists', route: { route: AppRoute.LIBRARY_ALBUM_ARTISTS_DETAIL, slugs: [{ idProperty: 'id', slugProperty: 'albumArtistId' }], }, }, artists: { arrayProperty: 'name', property: 'artists', route: { route: AppRoute.LIBRARY_ALBUM_ARTISTS_DETAIL, slugs: [{ idProperty: 'id', slugProperty: 'albumArtistId' }], }, }, createdAt: { property: 'createdAt', }, duration: { property: 'duration', }, lastPlayedAt: { property: 'lastPlayedAt', }, name: { property: 'name', route: { route: AppRoute.LIBRARY_ALBUMS_DETAIL, slugs: [{ idProperty: 'albumId', slugProperty: 'albumId' }], }, }, playCount: { property: 'playCount', }, rating: { property: 'userRating', }, releaseDate: { property: 'releaseDate', }, releaseYear: { property: 'releaseYear', }, }; export const ALBUMARTIST_CARD_ROWS: { [key: string]: CardRow } = { albumCount: { property: 'albumCount', }, duration: { property: 'duration', }, genres: { property: 'genres', }, lastPlayedAt: { property: 'lastPlayedAt', }, name: { property: 'name', route: { route: AppRoute.LIBRARY_ALBUM_ARTISTS_DETAIL, slugs: [{ idProperty: 'id', slugProperty: 'albumArtistId' }], }, }, playCount: { property: 'playCount', }, rating: { property: 'userRating', }, songCount: { property: 'songCount', }, }; export const PLAYLIST_CARD_ROWS: { [key: string]: CardRow } = { duration: { property: 'duration', }, name: { property: 'name', route: { route: AppRoute.PLAYLISTS_DETAIL, slugs: [{ idProperty: 'id', slugProperty: 'playlistId' }], }, }, nameFull: { property: 'name', route: { route: AppRoute.PLAYLISTS_DETAIL_SONGS, slugs: [{ idProperty: 'id', slugProperty: 'playlistId' }], }, }, owner: { property: 'owner', }, public: { property: 'public', }, songCount: { property: 'songCount', }, updatedAt: { property: 'songCount', }, };