import React from "react"; import handleViewport from 'react-in-viewport'; import { connect } from 'react-redux'; import { bindActionCreators } from 'redux'; import { scrollTo, sourceIcon } from '../util/helpers'; import Link from './Link'; import Icon from './Icon'; import Thumbnail from './Thumbnail'; import LinksSentence from './LinksSentence'; import { I18n } from '../locale'; import { encodeUri } from '../util/format'; import * as uiActions from '../services/ui/actions'; import * as lastfmActions from '../services/lastfm/actions'; import * as mopidyActions from '../services/mopidy/actions'; import * as spotifyActions from '../services/spotify/actions'; const SecondaryLine = ({ item: { type, tracks_total, tracks = [], followers, albums_uris, artists, } = {}, }) => { let trackCount = 0; if (tracks) trackCount = tracks.length; if (tracks_total) trackCount = tracks_total; switch (type) { case 'playlist': return ( ); case 'artist': return ( {followers && } {albums_uris && } ); case 'album': return ( {artists && } ); default: return ( {artists && } {followers && } ); } }; const GridItemComponent = ({ item: itemProp, itemHeight, getLink, show_source_icon, grid_glow_enabled, spotify_available, isFirstInGrid, uiActions: { showContextMenu, }, mopidyActions: { getImages: getMopidyImages, }, spotifyActions: { getArtistImages: getSpotifyImages, }, inViewport, forwardedRef, setItemHeight, }) => { if (!itemProp) return null; let item = itemProp; if (item.album) item = { ...item, ...item.album }; // Listen for changes to our height, and pass it up to our Grid. This is then used to build the // placeholder elements when out of viewport. We only care about the first item because this // represents the same heights for everything else (in almost all circumstances). if (isFirstInGrid && forwardedRef.current) { const { current: { clientHeight } } = forwardedRef; if (clientHeight !== itemHeight) { setItemHeight(clientHeight); } } const onContextMenu = (e) => { e.preventDefault(); showContextMenu({ e, context: item.type, uris: [item.uri], items: [item], tracklist_uri: item.uri, // not needed? }); }; // Load images if (!item.images && inViewport) { switch (item.type) { case 'artist': if (spotify_available) getSpotifyImages(item); break; case 'album': getMopidyImages([item.uri]); break; default: break; } } // Build link let to = ''; if (getLink) { to = getLink(item); } else if (item.link) { to = item.link; } else { to = `/${item.type}/${encodeUri(item.uri)}`; } return ( {inViewport || isFirstInGrid ? (
{item.name ? item.name : {item.uri}}
{show_source_icon && ( )}
) : (
)} ); }; const mapStateToProps = (state) => { const { ui: { grid_glow_enabled, }, spotify: { access_token: spotify_available, }, } = state; return { grid_glow_enabled, spotify_available, }; }; const mapDispatchToProps = (dispatch) => ({ uiActions: bindActionCreators(uiActions, dispatch), lastfmActions: bindActionCreators(lastfmActions, dispatch), mopidyActions: bindActionCreators(mopidyActions, dispatch), spotifyActions: bindActionCreators(spotifyActions, dispatch), }); const GridItem = connect(mapStateToProps, mapDispatchToProps)(handleViewport(GridItemComponent)); export { GridItem, }; export default { GridItem, };