import React, { useEffect } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { useDrag } from 'react-dnd';
import Link from './Link';
import { SourceIcon } from './Icon';
import Thumbnail from './Thumbnail';
import LinksSentence from './LinksSentence';
import { I18n } from '../locale';
import { encodeUri } from '../util/format';
import { isTouchDevice } from '../util/helpers';
import * as uiActions from '../services/ui/actions';
import * as mopidyActions from '../services/mopidy/actions';
import * as spotifyActions from '../services/spotify/actions';
const SecondaryLine = ({
sourceIcon = true,
item: {
uri,
type,
tracks_total,
tracks = [],
followers,
albums_uris = [],
artists,
} = {},
}) => {
let trackCount = 0;
if (tracks) trackCount = tracks.length;
if (tracks_total) trackCount = tracks_total;
const items = () => {
switch (type) {
case 'playlist':
return
;
case 'artist':
return (
<>
{followers > 0 && }
{albums_uris.length > 0 && }
>
);
case 'album':
return {artists && };
default:
return (
<>
{artists && }
{followers && }
>
);
}
};
return (
{sourceIcon && }
{items()}
);
};
const GridItem = ({
item: itemProp,
getLink,
sourceIcon,
}) => {
let item = itemProp;
if (item.album) item = { ...item, ...item.album };
const dispatch = useDispatch();
const grid_glow_enabled = useSelector((state) => state.ui.grid_glow_enabled);
const spotify_available = useSelector((state) => state.spotify.access_token);
const [_, drag] = useDrag({
type: item?.type?.toUpperCase() || 'UNKNOWN',
item: { item, context: item },
});
const onContextMenu = (e) => {
e.preventDefault();
dispatch(
uiActions.showContextMenu({
e,
type: item.type,
item,
}),
);
};
// Load images
useEffect(() => {
if (!item.images) {
switch (item.type) {
case 'artist':
if (spotify_available) {
dispatch(spotifyActions.getArtistImages(item));
}
break;
case 'album':
dispatch(mopidyActions.getImages([item.uri]));
break;
default:
break;
}
}
}, [item.images]);
// Build link
let to = '';
const getLinkResult = getLink ? getLink(item) : undefined;
if (getLinkResult) {
to = getLinkResult;
} else if (item.link) {
to = item.link;
} else {
to = `/${item.type}/${encodeUri(item.uri)}`;
if (item.name && item.type !== 'artist') {
// Strip out "%"; this causes conflicts with our uri decoder
to += `/${encodeURIComponent(item.name.replace('%', ''))}`;
}
}
return (
{item.name ? item.name : {item.uri}}
);
};
export {
GridItem,
};
export default {
GridItem,
};