-
Notifications
You must be signed in to change notification settings - Fork 16
/
index.ts
64 lines (51 loc) · 1.52 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { CSSProperties } from 'vue';
import { Video } from '../../components/VideoPlayer';
type Maybe<T> = T | null;
type Possibly<T> = Maybe<T> | undefined;
const computedTitle = (title: Maybe<string> | undefined) => {
return title ? { title } : undefined;
};
const computedPlaybackId = (
muxPlaybackId: Possibly<string>,
playbackId: Possibly<string>,
) => {
if (!(muxPlaybackId || playbackId)) return undefined;
return { playbackId: `${muxPlaybackId || playbackId}` };
};
const computedStyle = (width: Possibly<number>, height: Possibly<number>) => {
if (!(width && height)) return undefined;
return {
style: {
aspectRatio: `${width} / ${height}`,
},
};
};
const computedPlaceholder = (blurUpThumb: Possibly<string>) => {
return blurUpThumb ? { placeholder: blurUpThumb } : undefined;
};
type Style = Maybe<CSSProperties>;
type Title = Maybe<string>;
type PlaybackId = Maybe<string>;
type Placeholder = Maybe<string>;
type AttrsForMuxPlayer = {
style?: Style;
title?: Title;
playbackId?: PlaybackId;
placeholder?: Placeholder;
};
type UseVideoPlayerArgs = {
data?: Video;
};
export const useVideoPlayer = ({
data,
}: UseVideoPlayerArgs): AttrsForMuxPlayer => {
const { title, width, height, playbackId, muxPlaybackId, blurUpThumb } =
data || {};
if (data === undefined) return {};
return {
...(computedTitle(title) || {}),
...(computedPlaybackId(muxPlaybackId, playbackId) || {}),
...(computedStyle(width, height) || {}),
...(computedPlaceholder(blurUpThumb) || {}),
};
};