Skip to content

Commit 7f0e0c6

Browse files
committed
Make the Mux Playback ID more explicit.
1 parent 1046955 commit 7f0e0c6

1 file changed

Lines changed: 92 additions & 65 deletions

File tree

src/VideoPlayer/index.tsx

Lines changed: 92 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -8,48 +8,75 @@
88
//
99
// [1]: https://www.mux.com/player
1010

11-
import React, { forwardRef } from 'react';
11+
import React, {forwardRef} from 'react';
1212

1313
// We use and extend Typescript types defined in the MUX player.
14-
1514
import type MuxPlayerElement from '@mux/mux-player';
16-
import type { MuxPlayerProps } from '@mux/mux-player-react';
15+
import type {MuxPlayerProps} from '@mux/mux-player-react';
1716

1817
// React MUX player is made available in two flavours: eager and lazy loaded. We
1918
// choose to use the lazy version to avoid loading the web component uselessly.
2019
// MUX player lazy version loads internally the eager version using
2120
// `React.lazy()`.
22-
2321
import MuxPlayer from '@mux/mux-player-react/lazy';
2422

2523
// The core of this component is the `useVideoPlayer` hook: it takes
2624
// data from DatoCMS GraphQL API and returns props as expected by the
2725
// `<MuxPlayer />` component.
28-
29-
import { useVideoPlayer } from '../useVideoPlayer/index.js';
26+
import {useVideoPlayer} from '../useVideoPlayer/index.js';
3027

3128
type Maybe<T> = T | null;
3229
type Possibly<T> = Maybe<T> | undefined;
3330

31+
/**
32+
* The playback ID is how the player knows which video to play.
33+
* At least one of these is required.
34+
* If both are provided, `muxPlaybackId` takes precedence.
35+
* This is for backward compatibility.
36+
*/
37+
type PlaybackIdSources =
38+
| { muxPlaybackId: Possibly<string>; }
39+
| { playbackId: Possibly<string>; }
40+
3441
// `Video` represents a fragment of data regarding a video as returned from
3542
// DatoCMS GraphQL API.
36-
37-
export type Video = {
38-
/** Title attribute (`title`) for the video */
39-
title?: Possibly<string>;
40-
/** The height of the video */
41-
height?: Possibly<number>;
42-
/** The width of the video */
43-
width?: Possibly<number>;
44-
/** The MUX playbaack ID */
45-
muxPlaybackId?: Possibly<string>;
46-
/** The MUX playbaack ID */
47-
playbackId?: Possibly<string>;
48-
/** A data: URI containing a blurhash for the video */
49-
blurUpThumb?: Possibly<string>;
50-
/** Other data can be passed, but they have no effect on rendering the player */
51-
// biome-ignore lint/suspicious/noExplicitAny: we intentionally want to allow to add any other value to this video object
52-
[k: string]: any;
43+
/**
44+
* Video data as returned by the DatoCMS GraphQL Content Delivery API.
45+
*
46+
* You MUST provide `muxPlaybackId`. The `streamingUrl` property is not used.
47+
*
48+
* @property muxPlaybackId - Mux playback ID
49+
* @property playbackId - (Optional, fallback only) A playback ID alias for backward compatibility.
50+
* @property title - Title attribute (`title`) for the video.
51+
* @property height - The height of the video in pixels.
52+
* @property width - The width of the video in pixels.
53+
* @property blurUpThumb - A `data:` URI containing a blurhash placeholder image.
54+
* @property [k: string] - Any additional properties are accepted but not used
55+
* by the renderer.
56+
*
57+
* @example
58+
* ```ts
59+
* const video: Video = {
60+
* muxPlaybackId: "abcdef123456",
61+
* title: "Example video",
62+
* width: 1920,
63+
* height: 1080,
64+
* blurUpThumb: "data:image/png;base64,..."
65+
* };
66+
* ```
67+
*/
68+
export type Video = PlaybackIdSources & {
69+
/** Title attribute (`title`) for the video */
70+
title?: Possibly<string>;
71+
/** The height of the video */
72+
height?: Possibly<number>;
73+
/** The width of the video */
74+
width?: Possibly<number>;
75+
/** A data: URI containing a blurhash for the video */
76+
blurUpThumb?: Possibly<string>;
77+
/** Other data can be passed, but they have no effect on rendering the player */
78+
// biome-ignore lint/suspicious/noExplicitAny: we intentionally want to allow to add any other value to this video object
79+
[k: string]: any;
5380
};
5481

5582
// The component supports [all the props][1] allowed by the `<MuxPlayer />`
@@ -59,51 +86,51 @@ export type Video = {
5986
// [1]: https://github.com/muxinc/elements/blob/main/packages/mux-player-react/REFERENCE.md
6087

6188
export type VideoPlayerProps = MuxPlayerProps & {
62-
/** The actual response you get from a DatoCMS `video` GraphQL query */
63-
data?: Video;
89+
/** The actual response you get from a DatoCMS `video` GraphQL query */
90+
data?: Video;
6491
};
6592

6693
export const VideoPlayer: (
67-
props: VideoPlayerProps,
94+
props: VideoPlayerProps,
6895
) => ReturnType<typeof MuxPlayer> = forwardRef<
69-
MuxPlayerElement,
70-
VideoPlayerProps
96+
MuxPlayerElement,
97+
VideoPlayerProps
7198
>((props, ref) => {
72-
const {
73-
data = {},
74-
disableCookies = true,
75-
disableTracking = true,
76-
preload = 'metadata',
77-
style: styleFromProps,
78-
...rest
79-
} = props;
80-
81-
const {
82-
title,
83-
playbackId,
84-
style: styleFromHook,
85-
placeholder,
86-
} = useVideoPlayer({
87-
data,
88-
});
89-
90-
const style = {
91-
...styleFromHook,
92-
...styleFromProps,
93-
};
94-
95-
return (
96-
<MuxPlayer
97-
ref={ref}
98-
streamType="on-demand"
99-
preload={preload}
100-
title={title}
101-
disableCookies={disableCookies}
102-
disableTracking={disableTracking}
103-
playbackId={playbackId}
104-
style={style}
105-
placeholder={placeholder}
106-
{...rest}
107-
/>
108-
);
99+
const {
100+
data = {muxPlaybackId: undefined},
101+
disableCookies = true,
102+
disableTracking = true,
103+
preload = 'metadata',
104+
style: styleFromProps,
105+
...rest
106+
} = props;
107+
108+
const {
109+
title,
110+
playbackId,
111+
style: styleFromHook,
112+
placeholder,
113+
} = useVideoPlayer({
114+
data,
115+
});
116+
117+
const style = {
118+
...styleFromHook,
119+
...styleFromProps,
120+
};
121+
122+
return (
123+
<MuxPlayer
124+
ref={ref}
125+
streamType="on-demand"
126+
preload={preload}
127+
title={title}
128+
disableCookies={disableCookies}
129+
disableTracking={disableTracking}
130+
playbackId={playbackId}
131+
style={style}
132+
placeholder={placeholder}
133+
{...rest}
134+
/>
135+
);
109136
});

0 commit comments

Comments
 (0)