-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVoicemailPlayer.tsx
173 lines (156 loc) · 4.26 KB
/
VoicemailPlayer.tsx
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import React, { useState } from "react";
import useAudioPlayback from "./hooks/useAudioPlayback";
import AudioPeaksBar from "./components/AudioPeaksBar";
import { PlayIcon, PauseIcon } from "./components/icons";
import { AudioPlaybackStatus } from "./audio-playback";
/**
* `VoicemailPlayer` component props
* @public
* */
export interface VoicemailPlayerProps {
/**
* A function that renders <audio> element
*
* @param ref - A callback to be set as `ref` on the <audio> element
* @returns React element
* @example
* ```ts
* <VoicemailPlayer>{(ref) => <audio ref={ref} />}</VoicemailPlayer>
* ```
*
* @public
*/
children: (ref: React.RefCallback<HTMLAudioElement>) => React.ReactElement;
/**
* Optional CSS class to add to the player's root element
*/
className?: string;
/**
* Vertical alignment of bars in waveform
* @defaultValue `"bottom"`
*/
barAlignment?: "top" | "middle" | "bottom";
/**
* Width of a single bar in waveform in pixels
* @defaultValue `2`
*/
barWidth?: number;
/**
* Spacing between bars in waveform in pixels
* @defaultValue `2`
*/
barGap?: number;
/**
* Corner radius of bars in waveform in pixels
* @defaultValue `barWidth / 2`
*/
barRadius?: number;
}
/**
* Given a function that renders an <audio> element as `children`, renders a
* React element that displays the audio's amplitude peaks, current time / duration,
* and allows to control the audio playback (currently Play / Pause, and Seek)
*
* @param props - {@link VoicemailPlayerProps}
* @returns React element
* @example
* ```ts
* <VoicemailPlayer>{(ref) => <audio ref={ref} />}</VoicemailPlayer>
* ```
*
* @public
*/
export default function VoicemailPlayer({
children,
className,
barAlignment,
barWidth,
barGap,
barRadius,
}: VoicemailPlayerProps) {
const [audioElement, setAudioElement] = useState<HTMLAudioElement | null>(
null
);
const [playback, commands] = useAudioPlayback(audioElement);
const onProgressChange = (relativeX: number) => {
if (playback.isDurationUnknown) {
return;
}
commands.seek(relativeX * playback.duration);
};
const renderAudio = children;
const renderStatus = () => {
if (playback.status === "error") {
return <span title={playback.error?.message}>Error</span>;
}
return (
<>
<span role="timer" aria-label="Current Time">
{formatTime(playback.currentTime)}
</span>
{!playback.isDurationUnknown && (
<>
/
<span aria-label="Duration">{formatTime(playback.duration)}</span>
</>
)}
</>
);
};
return (
<div className={rootClassName(playback.status, className)}>
{playback.status === "playing" ? (
<button
aria-label="Pause"
className={prefixClassName("playButton")}
onClick={commands.pause}
>
<PauseIcon className={prefixClassName("playButton-icon")} />
</button>
) : (
<button
aria-label="Play"
className={prefixClassName("playButton")}
onClick={commands.play}
disabled={playback.status !== "ready"}
>
<PlayIcon className={prefixClassName("playButton-icon")} />
</button>
)}
<div className={prefixClassName("content")}>
<AudioPeaksBar
audioData={playback.data}
progress={playback.progress}
barAlignment={barAlignment}
barWidth={barWidth}
barGap={barGap}
barRadius={barRadius}
onProgressChange={onProgressChange}
/>
<div>{renderStatus()}</div>
</div>
{renderAudio(setAudioElement)}
</div>
);
}
function rootClassName(status: AudioPlaybackStatus, userClassName?: string) {
return [
prefixClassName("root"),
prefixClassName("root--" + status),
userClassName,
]
.filter(Boolean)
.join(" ");
}
function prefixClassName(name: string) {
return `VoicemailPlayer-${name}`;
}
function formatTime(timeInSeconds: number) {
let minutes = Math.floor(timeInSeconds / 60);
let seconds = Math.round(timeInSeconds % 60);
if (seconds === 60) {
seconds = 0;
minutes += 1;
}
return `${minutes}:${String(seconds).padStart(2, "0")}`;
}