@sendbird/uikit-react
Version:
Sendbird UIKit for React: A feature-rich and customizable chat UI kit with messaging, channel management, and user authentication.
83 lines (79 loc) • 4.14 kB
JavaScript
;
var React = require('react');
var VoicePlayer_context = require('../chunks/bundle-iWa9rWFV.js');
var consts = require('../chunks/bundle-DZN-28o5.js');
var VoiceRecorder_context = require('../VoiceRecorder/context.js');
require('../chunks/bundle-jAsAzWpU.js');
require('../chunks/bundle-BVn2UMtk.js');
require('../chunks/bundle-on0zTbLT.js');
require('../chunks/bundle-Buar9ys-.js');
require('@sendbird/chat');
require('@sendbird/chat/groupChannel');
require('@sendbird/chat/openChannel');
require('../chunks/bundle-ClT0IexP.js');
require('../chunks/bundle-D-_6Kk3L.js');
require('../chunks/bundle-BXuNw6bR.js');
require('../chunks/bundle-BFmC2V1o.js');
require('react-dom');
require('../chunks/bundle-1F9guuKw.js');
require('../chunks/bundle-DDUAkmTu.js');
require('../ui/IconButton.js');
require('../ui/Button.js');
require('../chunks/bundle-DxZzcGya.js');
require('../ui/Icon.js');
require('../chunks/bundle-BNgfU9I_.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 ? consts.VOICE_MESSAGE_MIME_TYPE : _f;
var groupKey = VoicePlayer_context.generateGroupKey(channelUrl, key);
var _g = VoicePlayer_context.useVoicePlayerContext(), play = _g.play, pause = _g.pause, stop = _g.stop, reset = _g.reset, voicePlayerStore = _g.voicePlayerStore;
var isRecordable = VoiceRecorder_context.useVoiceRecorderContext().isRecordable;
var currentAudioUnit = ((_b = voicePlayerStore === null || voicePlayerStore === void 0 ? void 0 : voicePlayerStore.audioStorage) === null || _b === void 0 ? void 0 : _b[groupKey]) || VoicePlayer_context.AudioUnitDefaultValue();
var currentAudioUnitRef = React.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);
};
React.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(consts.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 !== VoicePlayer_context.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,
});
};
exports.useVoicePlayer = useVoicePlayer;
//# sourceMappingURL=useVoicePlayer.js.map