Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WSTEAM1-1521: Add Autoplay #12256

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion data/mundo/live/c7dkx155e626t.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,15 @@
"serviceId": null,
"authToken": null,
"status": "LIVE",
"warnings": null
"warnings": {
"warning_text": "Contains some upsetting scenes.",
"warning": [
{
"warning_code": "D1",
"short_description": "Some upsetting scenes"
}
]
}
},
"leadMedia": true
}
Expand Down
15 changes: 11 additions & 4 deletions src/app/components/LiveMediaStream/index.styles.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { css, Theme } from '@emotion/react';
import PlayButton from '../MediaLoader/Placeholder/PlayButton';

export default {
liveMediaStreamContainer: ({ palette, spacings, mq }: Theme) =>
liveMediaStreamContainer: ({ spacings }: Theme) =>
css({
// backgroundColor: `${palette.BLACK}`,
margin: `0 ${spacings.FULL}rem`,
Expand All @@ -23,7 +22,7 @@ export default {
width: '100%',
},
}),
playButtonText: ({ spacings, palette, mq }: Theme) =>
playButtonText: ({ palette }: Theme) =>
css({
color: palette.WHITE,
}),
Expand All @@ -46,8 +45,16 @@ export default {
display: 'block',
width: '100%',
}),
mediaLoaderContainer: ({ palette, spacings, mq }: Theme) =>
mediaLoaderContainer: ({ palette }: Theme) =>
css({
backgroundColor: `${palette.POSTBOX}`,
}),
showContent: () =>
css({
display: 'unset',
}),
hideContent: () =>
css({
display: 'none',
}),
};
88 changes: 61 additions & 27 deletions src/app/components/LiveMediaStream/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
/** @jsx jsx */
/* @jsxFrag React.Fragment */
import { jsx } from '@emotion/react';
import { useContext, useState } from 'react';
import React, { FC, useContext, useEffect, useState } from 'react';
import Text from '#app/components/Text';
import { MediaCollection } from '#app/components/MediaLoader/types';
import MediaLoader, { BumpLoader } from '#app/components/MediaLoader';
import MediaLoader, {
BumpLoader,
ManualControlProps,
} from '#app/components/MediaLoader';
import filterForBlockType from '#app/lib/utilities/blockHandlers';
import { ServiceContext } from '#app/contexts/ServiceContext';
import mediaIcons from '#psammead/psammead-assets/src/svgs/mediaIcons';
Expand Down Expand Up @@ -34,46 +38,76 @@ const LiveMediaStream = ({ mediaCollection }: Props) => {
},
} = mediaItem;

const handleClick = () => {
setShowMedia(!showMedia);
};
const ManualControls = ({
mediaControls,
mediaContainer,
}: ManualControlProps) => {
const handleClick = () => {
setShowMedia(previousState => !previousState);
};

return (
<div css={styles.liveMediaStreamContainer}>
<BumpLoader />
<Text css={styles.mediaDescription}>{short}</Text>
{!showMedia && (
useEffect(() => {
if (showMedia) {
mediaControls?.play();
} else {
mediaControls?.stop();
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [mediaControls, showMedia]);

return (
<>
<div
css={[
styles.liveMediaSpan,
showMedia ? styles.showContent : styles.hideContent,
]}
>
<Text>{`${title} - ${networkName}`}</Text>
<button
type="button"
onClick={handleClick}
data-testid="close-button"
>
{mediaIcons.close}
</button>
</div>
<button
type="button"
onClick={handleClick}
data-testid="watch-now-button"
css={styles.playButton}
css={[
styles.playButton,
showMedia ? styles.hideContent : styles.showContent,
]}
>
<span css={styles.playIcon}>{mediaIcons.video}</span>
<Text
css={styles.playButtonText}
css={[styles.playButtonText]}
size="doublePica"
fontVariant="sansBold"
>
{watchNow}
</Text>
</button>
)}
{showMedia && (
<div css={styles.mediaLoaderContainer}>
<div css={styles.liveMediaSpan}>
<Text>{`${title} - ${networkName}`}</Text>
<button
type="button"
onClick={handleClick}
data-testid="close-button"
>
{mediaIcons.close}
</button>
</div>
<MediaLoader blocks={mediaCollection} />
<div css={showMedia ? styles.showContent : styles.hideContent}>
{mediaContainer}
</div>
)}
</>
);
};

return (
<div css={styles.liveMediaStreamContainer}>
<BumpLoader />
<Text css={styles.mediaDescription}>{short}</Text>
<div css={styles.mediaLoaderContainer}>
<MediaLoader
blocks={mediaCollection}
placeholderOverride={false}
ManualControls={ManualControls as FC}
/>
</div>
</div>
);
};
Expand Down
2 changes: 1 addition & 1 deletion src/app/components/MediaLoader/configs/liveMedia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export default ({
return {
playerConfig: {
...basePlayerConfig,
autoplay: true,
autoplay: false,
statsObject: {
...basePlayerConfig.statsObject,
episodePID: liveMediaBlock.id,
Expand Down
61 changes: 53 additions & 8 deletions src/app/components/MediaLoader/index.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
/** @jsx jsx */
/* @jsxFrag React.Fragment */

import { jsx } from '@emotion/react';
import React, { useContext, useEffect, useRef, useState } from 'react';
import React, {
FC,
ReactHTMLElement,
useContext,
useEffect,
useRef,
useState,
} from 'react';
import { Helmet } from 'react-helmet';
import { RequestContext } from '#contexts/RequestContext';
import { MEDIA_PLAYER_STATUS } from '#app/lib/logger.const';
Expand All @@ -16,7 +22,7 @@ import {
import filterForBlockType from '#lib/utilities/blockHandlers';
import { PageTypes } from '#app/models/types/global';
import { EventTrackingContext } from '#app/contexts/EventTrackingContext';
import { BumpType, MediaBlock, PlayerConfig } from './types';
import { BumpType, MediaBlock, Player, PlayerConfig } from './types';
import Caption from '../Caption';
import nodeLogger from '../../lib/logger.node';
import buildConfig from './utils/buildSettings';
Expand Down Expand Up @@ -93,13 +99,24 @@ const AdvertTagLoader = () => {
);
};

export type ManualControlProps = {
mediaControls?: Player;
mediaContainer: ReactHTMLElement<HTMLDivElement>;
};

type MediaContainerProps = {
playerConfig: PlayerConfig;
showAds: boolean;
ManualControls?: FC<ManualControlProps>;
};

const MediaContainer = ({ playerConfig, showAds }: MediaContainerProps) => {
const MediaContainer = ({
playerConfig,
showAds,
ManualControls,
}: MediaContainerProps) => {
const playerElementRef = useRef<HTMLDivElement>(null);
const [mediaControls, setMediaControls] = useState<Player>();

useEffect(() => {
try {
Expand All @@ -111,6 +128,7 @@ const MediaContainer = ({ playerConfig, showAds }: MediaContainerProps) => {
);

mediaPlayer.load();
setMediaControls(mediaPlayer);

if (showAds) {
const adTag = await window.dotcom.ads.getAdTag();
Expand Down Expand Up @@ -145,7 +163,7 @@ const MediaContainer = ({ playerConfig, showAds }: MediaContainerProps) => {
}
}, [playerConfig, showAds]);

return (
const container = (
<div
ref={playerElementRef}
data-e2e="media-player"
Expand All @@ -156,15 +174,36 @@ const MediaContainer = ({ playerConfig, showAds }: MediaContainerProps) => {
}
/>
);

return (
<>
{ManualControls ? (
<ManualControls
mediaControls={mediaControls}
mediaContainer={container as ManualControlProps['mediaContainer']}
/>
) : (
container
)}
</>
);
};

type Props = {
blocks: MediaBlock[];
className?: string;
embedded?: boolean;
placeholderOverride?: boolean;
ManualControls?: FC<ManualControlProps>;
};

const MediaLoader = ({ blocks, className, embedded }: Props) => {
const MediaLoader = ({
blocks,
className,
embedded,
placeholderOverride = true,
ManualControls,
}: Props) => {
const { lang, translations } = useContext(ServiceContext);
const { pageIdentifier } = useContext(EventTrackingContext);
const { enabled: adsEnabled } = useToggle('ads');
Expand Down Expand Up @@ -225,7 +264,9 @@ const MediaLoader = ({ blocks, className, embedded }: Props) => {
mediaInfo,
} = placeholderConfig ?? {};

const hasPlaceholder = Boolean(showPlaceholder && placeholderSrc);
const hasPlaceholder = Boolean(
placeholderOverride && showPlaceholder && placeholderSrc,
);

const showPortraitTitle = orientation === 'portrait' && !embedded;

Expand Down Expand Up @@ -274,7 +315,11 @@ const MediaLoader = ({ blocks, className, embedded }: Props) => {
onClick={() => setShowPlaceholder(false)}
/>
) : (
<MediaContainer playerConfig={playerConfig} showAds={showAds} />
<MediaContainer
playerConfig={playerConfig}
showAds={showAds}
ManualControls={ManualControls}
/>
)}
</>
)}
Expand Down
3 changes: 3 additions & 0 deletions src/app/components/MediaLoader/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ export type Player = {
parameters: { updatedAdTag: string },
): void;
load: () => void;
play: () => void;
pause: () => void;
stop: () => void;
bind: (event: string, callback: () => void) => void;
loadPlugin: (
pluginName: { [key: string]: string },
Expand Down
2 changes: 1 addition & 1 deletion src/app/models/types/translations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ export interface Translations {
recentEpisodes?: string;
podcastExternalLinks?: string;
download?: string;
watchNow: string;
watchNow?: string;
};
socialEmbed: {
caption?: {
Expand Down
Loading