/** * 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 ( {Object.keys(itemInfo).includes("dayTitle") ? ( <> {" "} {splitTitle[0] + " :"}{" "} {splitTitle[1]} ) : ( replaceHtmlEntitesCaption )} ); }, [itemInfo, caption, forceFocus, favBtnFocused] ); const planItemDetail = useCallback( (itemSize) => { if (itemInfo && itemSize === "plan") { return (
{Utils.replaceHtmlEntites(itemInfo.title)} {itemInfo.items && itemInfo.items.map((item, index) => (
{item && item.repeatCount}
{item && item.title}
))}
); } else { return ( <>
{Utils.replaceHtmlEntites(itemInfo.title)}
{itemInfo.items.length}
); } }, [itemInfo, itemSize, forceFocus, onClick] ); const playerRepeat = useCallback(() => { if (itemInfo) { return (
{itemInfo.repeatCount}
); } else { return null; } }, [itemInfo]); const supportBadge = useMemo(() => { let supported = false, badgeStyle = {}; if (itemInfo) { if (itemInfo.contentType === ContentType.YOUTUBE_VIDEO) { supported = true; // badgeStyle = { backgroundImage: `url(${YOUTUBE_BADGE_LOGO})` }; } else if (itemInfo.contentType === ContentType.CP_CONTENT) { supported = true; // badgeStyle = { backgroundImage: `url(${itemInfo.badgeIconUrl})` }; } } return { supported, badgeStyle }; }, [itemInfo]); const isYoutubeChannel = itemInfo && itemInfo.contentType === ContentType.YOUTUBE_CHANNEL; const isYoutubeVideo = itemInfo && itemInfo.contentType === ContentType.YOUTUBE_VIDEO; const isMoreButton = itemInfo && itemInfo.contentType === ContentType.MORE; const isAddPlanButton = itemInfo && itemInfo.contentType === ContentType.ADD_PLAN; const isSpecialButton = isMoreButton || isAddPlanButton; const isApp = itemInfo && itemInfo.contentType === ContentType.APP; const isYoutubeAppMoveIcon = panelInfo.length > 0 && panelInfo[0].name === "moreformedlist"; const moveAppIcon = itemInfo && (itemInfo.contentType === ContentType.APP || itemInfo.contentType === ContentType.CP_CONTENT); const playtime = itemInfo && itemInfo.playtime ? Utils.transSecToText(itemInfo.playtime) : null; const useNewTodayIcon = Config.USE_NEW_TODAY_ICON; return (
{thumbnail && ( )} {listspotlightid === SpotlightIds.LIST_TODAYTOP && (
)} {itemInfo && itemInfo.isToday && (
)} {captionContainer(isYoutubeChannel, isApp, isSpecialButton)} {!forceFocus && playtime && (
{playtime}
)} {supportBadge.supported && (
)} {itemInfo && (!selectMode || isAddPlanButton) && (
)} {itemInfo && itemInfo.contentType === ContentType.PLAN && planItemDetail(itemSize)} {itemInfo && itemInfo.repeatCount && playerRepeat()} {supportFavBtn && (
{iamFavContent ? $L("Remove from Favorite").toUpperCase() : $L("Add to Favorite").toUpperCase()}
)}
); }; const ImageItemDecorator = compose( MarqueeController({ marqueeOnFocus: true }), VoiceControlDecorator, Spottable ); const MediaItem = ImageItemDecorator(MediaItemBase); export default MediaItem;