@sendbird/uikit-react
Version:
Sendbird UIKit for React: A feature-rich and customizable chat UI kit with messaging, channel management, and user authentication.
81 lines (78 loc) • 4.1 kB
JavaScript
import { useRef, useEffect } from 'react';
import { u as useVoicePlayerContext, a as AudioUnitDefaultValue, V as VOICE_PLAYER_STATUS, g as generateGroupKey } from '../chunks/bundle-DWURNKdQ.js';
import { m as VOICE_PLAYER_AUDIO_ID, k as VOICE_MESSAGE_MIME_TYPE } from '../chunks/bundle-JhKiWlXT.js';
import { useVoiceRecorderContext } from '../VoiceRecorder/context.js';
import '../chunks/bundle-Bpofr334.js';
import '../chunks/bundle-CglqREVl.js';
import '../chunks/bundle-4clodtJA.js';
import '../chunks/bundle-B56O1y8C.js';
import '@sendbird/chat';
import '@sendbird/chat/groupChannel';
import '@sendbird/chat/openChannel';
import '../chunks/bundle-Cdqsdoa8.js';
import '../chunks/bundle-oM0Fxt9G.js';
import '../chunks/bundle-DmnXeBdU.js';
import '../chunks/bundle-C4anRHWY.js';
import 'react-dom';
import '../chunks/bundle-DX6fRIJl.js';
import '../chunks/bundle-C2ARCMSL.js';
import '../ui/IconButton.js';
import '../ui/Button.js';
import '../chunks/bundle-Cdplrrlw.js';
import '../ui/Icon.js';
import '../chunks/bundle-LLA95Pqf.js';
var useVoicePlayer = function (_a) {
var _b;
var _c = _a.key, key = _c === void 0 ? '' : _c, _d = _a.channelUrl, channelUrl = _d === void 0 ? '' : _d, audioFile = _a.audioFile, _e = _a.audioFileUrl, audioFileUrl = _e === void 0 ? '' : _e, _f = _a.audioFileMimeType, audioFileMimeType = _f === void 0 ? VOICE_MESSAGE_MIME_TYPE : _f;
var groupKey = generateGroupKey(channelUrl, key);
var _g = useVoicePlayerContext(), play = _g.play, pause = _g.pause, stop = _g.stop, reset = _g.reset, voicePlayerStore = _g.voicePlayerStore;
var isRecordable = useVoiceRecorderContext().isRecordable;
var currentAudioUnit = ((_b = voicePlayerStore === null || voicePlayerStore === void 0 ? void 0 : voicePlayerStore.audioStorage) === null || _b === void 0 ? void 0 : _b[groupKey]) || AudioUnitDefaultValue();
var currentAudioUnitRef = useRef(currentAudioUnit);
currentAudioUnitRef.current = currentAudioUnit;
var playVoicePlayer = function () {
if (!isRecordable) {
play === null || play === void 0 ? void 0 : play({
groupKey: groupKey,
audioFile: audioFile,
audioFileUrl: audioFileUrl,
audioFileMimeType: audioFileMimeType,
});
}
};
var pauseVoicePlayer = function () {
pause === null || pause === void 0 ? void 0 : pause(groupKey);
};
var stopVoicePlayer = function (text) {
if (text === void 0) { text = ''; }
stop === null || stop === void 0 ? void 0 : stop(text);
};
useEffect(function () {
return function () {
var _a, _b;
if (audioFile || audioFileUrl) {
// Pause via DOM because reset() captured in this closure has stale currentPlayer
var voiceAudioPlayerElement = document.getElementById(VOICE_PLAYER_AUDIO_ID);
(_a = voiceAudioPlayerElement === null || voiceAudioPlayerElement === void 0 ? void 0 : voiceAudioPlayerElement.pause) === null || _a === void 0 ? void 0 : _a.call(voiceAudioPlayerElement);
var status_1 = (_b = currentAudioUnitRef.current) === null || _b === void 0 ? void 0 : _b.playingStatus;
if (status_1 && status_1 !== VOICE_PLAYER_STATUS.IDLE) {
reset === null || reset === void 0 ? void 0 : reset(groupKey);
}
}
};
}, []);
return ({
play: playVoicePlayer,
pause: pauseVoicePlayer,
stop: stopVoicePlayer,
/**
* The reason why we multiply this by *1000 is,
* The unit of playbackTime and duration should be millisecond
*/
playbackTime: ((currentAudioUnit === null || currentAudioUnit === void 0 ? void 0 : currentAudioUnit.playbackTime) || 0) * 1000,
duration: ((currentAudioUnit === null || currentAudioUnit === void 0 ? void 0 : currentAudioUnit.duration) || 0) * 1000,
playingStatus: currentAudioUnit.playingStatus,
});
};
export { useVoicePlayer };
//# sourceMappingURL=useVoicePlayer.js.map