Files
Iris/src/js/components/GridItem.js

137 lines
3.4 KiB
JavaScript
Raw Normal View History

2016-10-25 14:09:53 +13:00
import React from 'react';
import {
uriType,
scrollTo,
sourceIcon,
} from '../util/helpers';
import Link from './Link';
2019-01-15 16:57:50 +13:00
import Icon from './Icon';
import Thumbnail from './Thumbnail';
import LinksSentence from './LinksSentence';
import { I18n } from '../locale';
2016-10-25 14:09:53 +13:00
2019-09-05 15:36:11 +12:00
export default class GridItem extends React.Component {
componentDidMount() {
const {
mopidyActions,
spotifyActions,
spotifyAvailable,
item,
} = this.props;
if (!item || item.images) return;
switch (uriType(item.uri)) {
case 'artist':
if (spotifyActions && spotifyAvailable) {
//spotifyActions.getArtistImages(item);
}
break;
case 'album':
// If Mopidy doesn't find any images, then it will pass on the call to LastFM
if (mopidyActions) {
mopidyActions.getImages([item.uri]);
}
break;
default:
break;
2019-09-05 15:36:11 +12:00
}
}
shouldComponentUpdate = (nextProps) => {
const { item } = this.props;
return nextProps.item !== item;
2019-09-05 15:36:11 +12:00
}
onContextMenu = (e) => {
const { onContextMenu } = this.props;
if (onContextMenu) {
onContextMenu(e);
}
2019-09-05 15:36:11 +12:00
}
renderSecondary = ({
uri,
tracks_total,
followers,
albums_uris,
artists,
}) => {
switch (uriType(uri)) {
2019-09-05 15:36:11 +12:00
case 'playlist':
2020-09-11 13:53:54 +12:00
return (
<span className="grid__item__secondary__content">
2020-09-11 13:53:54 +12:00
<I18n path="specs.tracks" count={tracks_total || 0} />
</span>
2020-09-11 13:53:54 +12:00
);
2019-09-05 15:36:11 +12:00
case 'artist':
return (
<span className="grid__item__secondary__content">
{followers && <I18n path="specs.followers" count={followers.toLocaleString()} />}
{albums_uris && <I18n path="specs.albums" count={albums_uris.length} />}
2019-09-05 15:36:11 +12:00
</span>
);
case 'album':
return (
<span className="grid__item__secondary__content">
{artists && <LinksSentence nolinks items={artists} />}
2019-09-05 15:36:11 +12:00
</span>
);
default:
return (
<span className="grid__item__secondary__content">
{artists && <LinksSentence nolinks items={item.artists} /> }
{followers && <I18n path="specs.followers" count={followers.toLocaleString()} />}
2019-09-05 15:36:11 +12:00
</span>
);
}
}
render = () => {
const {
item: { album },
link: customLink,
type,
show_source_icon,
} = this.props;
let { item } = this.props;
if (!item) return null;
if (album) item = { ...item, ...album };
2019-09-05 15:36:11 +12:00
const link = customLink || `/${type}/${encodeURIComponent(item.uri)}`;
2019-09-05 15:36:11 +12:00
return (
<Link
className={`grid__item grid__item--${type}`}
2019-09-05 15:36:11 +12:00
to={link}
onClick={scrollTo}
onContextMenu={this.onContextMenu}
2019-09-05 15:36:11 +12:00
>
<Thumbnail
glow
size="medium"
className="grid__item__thumbnail"
images={item.images || item.icons}
type={type}
/>
2019-09-05 15:36:11 +12:00
<div className="grid__item__name">
{item.name ? item.name : <span className="opaque-text">{item.uri}</span>}
</div>
<div className="grid__item__secondary">
{show_source_icon && (
<Icon name={sourceIcon(item.uri)} type="fontawesome" className="source" />
)}
2019-09-05 15:36:11 +12:00
{this.renderSecondary(item)}
</div>
</Link>
);
}
2016-10-25 14:09:53 +13:00
}