Skip to content

Commit a0db4e0

Browse files
authored
refactor: conditionally render MuxPlayer only when data is ready (#10710)
1 parent 00fb323 commit a0db4e0

1 file changed

Lines changed: 25 additions & 15 deletions

File tree

apps/mux/frontend/src/index.tsx

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,16 @@ export class App extends React.Component<AppProps, AppState> {
313313
return this.state.value && this.state.value.drmPlaybackId ? true : false;
314314
};
315315

316+
isPlayerReady = (): boolean => {
317+
if (!this.state.playerPlaybackId) return false;
318+
319+
const isPublicReady = !!this.state.value?.ready && !!this.state.value?.playbackId;
320+
const isSignedReady =
321+
this.isUsingSigned() && !this.state.isTokenLoading && !!this.state.playbackToken;
322+
323+
return isPublicReady || isSignedReady;
324+
};
325+
316326
getSwitchCheckedState = (): boolean => {
317327
// If there are pending actions of playback, use the state of the pending action
318328
if (this.state.value?.pendingActions?.create) {
@@ -538,12 +548,9 @@ export class App extends React.Component<AppProps, AppState> {
538548
const parsedBody = JSON.parse(body);
539549
if (!parsedBody.ok) throw new Error(parsedBody.error);
540550

541-
const tokens = parsedBody.data as SignedTokens;
542-
this.setState({ isTokenLoading: false });
543-
return tokens;
551+
return parsedBody.data as SignedTokens;
544552
} catch (e) {
545553
console.error(e);
546-
this.setState({ isTokenLoading: false });
547554
return {
548555
licenseToken: undefined,
549556
playbackToken: 'playback-token-not-found',
@@ -568,7 +575,13 @@ export class App extends React.Component<AppProps, AppState> {
568575
const { playbackToken, posterToken, storyboardToken, licenseToken } =
569576
await this.fetchSignedTokens(signedPlaybackId, isDRM);
570577

571-
this.setState({ playbackToken, posterToken, storyboardToken, drmLicenseToken: licenseToken });
578+
this.setState({
579+
playbackToken,
580+
posterToken,
581+
storyboardToken,
582+
drmLicenseToken: licenseToken,
583+
isTokenLoading: false,
584+
});
572585
};
573586

574587
getAsset = async (assetId: string) => {
@@ -1331,9 +1344,7 @@ export class App extends React.Component<AppProps, AppState> {
13311344
) {
13321345
const { muxDomain } = this.props.sdk.parameters.installation as InstallationParams;
13331346

1334-
const showPlayer =
1335-
(this.state.value.ready && this.state.value.playbackId) ||
1336-
(this.isUsingSigned() && !this.state.isTokenLoading);
1347+
const showPlayer = this.isPlayerReady();
13371348

13381349
return (
13391350
<>
@@ -1419,17 +1430,16 @@ export class App extends React.Component<AppProps, AppState> {
14191430
</Box>
14201431
)}
14211432

1422-
<section
1423-
className="player"
1424-
style={{ ...this.getPlayerAspectRatio(), display: showPlayer ? undefined : 'none' }}>
1425-
{this.state.playerPlaybackId !== 'playback-test-123' && (
1433+
{showPlayer && (
1434+
<section
1435+
className="player"
1436+
style={this.getPlayerAspectRatio()}>
14261437
<MuxPlayer
14271438
ref={this.muxPlayerRef}
14281439
data-testid="muxplayer"
14291440
style={{ height: '100%', width: '100%' }}
14301441
playbackId={this.state.playerPlaybackId}
14311442
streamType={this.getPlayerType()}
1432-
poster={this.state.value.audioOnly ? '#' : undefined}
14331443
customDomain={muxDomain && muxDomain !== 'mux.com' ? muxDomain : undefined}
14341444
audio={this.state.value.audioOnly}
14351445
metadata={{
@@ -1448,8 +1458,8 @@ export class App extends React.Component<AppProps, AppState> {
14481458
: undefined
14491459
}
14501460
/>
1451-
)}
1452-
</section>
1461+
</section>
1462+
)}
14531463

14541464
{showPlayer && this.isLive() && (
14551465
<Box marginBottom="spacingM" marginTop="spacingM">

0 commit comments

Comments
 (0)