/**
* Item Module of contents list
*
* @module MediaItem
*/
import React, { useCallback, useEffect, useMemo } from "react";
import classNames from "classnames";
import compose from "ramda/src/compose";
import { shallowEqual, useSelector } from "react-redux";
import { Image } from "@enact/sandstone/Image";
import { Marquee, MarqueeController } from "@enact/sandstone/Marquee";
import Spottable from "@enact/spotlight/Spottable";
import { Cell, Column } from "@enact/ui/Layout";
import { VoiceControlDecorator } from "@enact/webos/speech";
import * as Config from "../../utils/Config";
// import YOUTUBE_BADGE_LOGO from "../../../assets/list/icon/icon_youtube.png";
import * as ContentType from "../../utils/Config";
import * as Utils from "../../utils/helperMethods";
import { $L } from "../../utils/helperMethods";
import { SpotlightIds } from "../../utils/SpotlightIds";
import css from "./MediaItem.module.less";
/**
*
* @param {itemSize} "small", "big", 'plan'
* @returns
*/
const MediaItemBase = ({
itemSize,
supportFavBtn,
favBtnFocused,
caption,
onItemClick,
forceFocus,
listspotlightid,
style,
onFavBtnFocused,
itemInfo,
playing,
selectMode,
...rest
}) => {
const contentsMyFavorites = useSelector(
(state) => state.contentsMyFavorites,
shallowEqual
);
const contentInfos = useSelector((state) => state.contentInfos);
const panelInfo = useSelector((state) => state.panels);
const updatedContentInfo = useSelector(
(state) => state.updatedContentInfo,
(newState) => {
if (!itemInfo || !newState) {
return true;
}
if (itemInfo.contentId === newState.contentId) {
Utils.jsonConcat(itemInfo, newState);
return false;
}
if (itemInfo.youtubeId && itemInfo.youtubeId === newState.youtubeId) {
Utils.jsonConcat(itemInfo, newState);
return false;
}
if (itemInfo.contentType === ContentType.PLAN) {
let matched = false;
if (itemInfo.items) {
for (let j = 0; j < itemInfo.items.length; j++) {
if (itemInfo.items[j].contentId === newState.contentId) {
Utils.jsonConcat(itemInfo.items[j], newState);
matched = true;
}
}
if (itemInfo.items && itemInfo.items[0]) {
itemInfo.thumbnailImageUrl = itemInfo.items[0].thumbnailImageUrl;
itemInfo.postImageUrl = itemInfo.items[0].postImageUrl;
}
if (matched) {
return false;
}
}
}
return true; //it's same item. Will not update.
}
);
useEffect(() => {
if (itemInfo && itemInfo.contentType === ContentType.PLAN) {
if (itemInfo.items) {
for (let j in itemInfo.items) {
if (
itemInfo.items[j].contentId &&
contentInfos[itemInfo.items[j].contentId]
) {
Utils.jsonConcat(
itemInfo.items[j],
contentInfos[itemInfo.items[j].contentId]
);
}
}
if (itemInfo.items && itemInfo.items[0]) {
itemInfo.thumbnailImageUrl = itemInfo.items[0].thumbnailImageUrl;
itemInfo.postImageUrl = itemInfo.items[0].postImageUrl;
}
}
} else if (
itemInfo &&
itemInfo.contentId &&
contentInfos[itemInfo.contentId]
) {
Utils.jsonConcat(itemInfo, contentInfos[itemInfo.contentId]);
}
}, [itemInfo, contentInfos, updatedContentInfo]);
const iamFavContent = useMemo(() => {
let ret = false;
if (listspotlightid === SpotlightIds.LIST_MYFAVORITE) {
ret = true;
} else if (
itemInfo &&
contentsMyFavorites &&
contentsMyFavorites.length > 0
) {
const id = itemInfo.contentId;
for (let index in contentsMyFavorites) {
if (id === contentsMyFavorites[index].contentId) {
ret = true;
break;
}
}
}
return ret;
}, [contentsMyFavorites, itemInfo, listspotlightid]);
const onFavMouseEnter = useCallback(() => {
if (!supportFavBtn) {
return;
}
onFavBtnFocused(true);
}, [onFavBtnFocused, supportFavBtn]);
const onFavMouseLeave = useCallback(() => {
onFavBtnFocused(false);
}, [onFavBtnFocused]);
const onClick = useCallback(
(ev) => {
console.log("onClick..", ev);
if (onItemClick) {
onItemClick(ev);
}
ev.stopPropagation();
ev.preventDefault();
},
[onItemClick]
);
let thumbnail = "",
placeholder = "";
if (itemSize !== "small" && itemInfo && itemInfo.postImageUrl) {
thumbnail = itemInfo.postImageUrl;
} else {
thumbnail = itemInfo && itemInfo.thumbnailImageUrl;
}
placeholder = itemInfo && itemInfo.thumbnailImageUrl;
const captionContainer = useCallback(
(_isYoutubeChannel, _isApp, _isSpecialButton) => {
const marqueeAlign =
_isYoutubeChannel || _isApp || _isSpecialButton ? "center" : "left";
const replaceHtmlEntitesCaption = Utils.replaceHtmlEntites(caption);
if (!itemInfo || itemInfo.contentType === ContentType.PLAN) {
return null;
}
let splitTitle = [];
if (Object.keys(itemInfo).includes("dayTitle")) {
splitTitle = replaceHtmlEntitesCaption.split(":");
}
return (