|
| 1 | +/* |
| 2 | +Copyright 2021 The Matrix.org Foundation C.I.C. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +import AccessibleTooltipButton from "../elements/AccessibleTooltipButton"; |
| 18 | +import {_t} from "../../../languageHandler"; |
| 19 | +import React from "react"; |
| 20 | +import {VoiceRecorder} from "../../../voice/VoiceRecorder"; |
| 21 | +import {Room} from "matrix-js-sdk/src/models/room"; |
| 22 | +import {MatrixClientPeg} from "../../../MatrixClientPeg"; |
| 23 | +import classNames from "classnames"; |
| 24 | + |
| 25 | +interface IProps { |
| 26 | + room: Room; |
| 27 | + onRecording: (haveRecording: boolean) => void; |
| 28 | +} |
| 29 | + |
| 30 | +interface IState { |
| 31 | + recorder?: VoiceRecorder; |
| 32 | +} |
| 33 | + |
| 34 | +export default class VoiceRecordComposerTile extends React.PureComponent<IProps, IState> { |
| 35 | + public constructor(props) { |
| 36 | + super(props); |
| 37 | + |
| 38 | + this.state = { |
| 39 | + recorder: null, // not recording by default |
| 40 | + }; |
| 41 | + } |
| 42 | + |
| 43 | + private onStartStopVoiceMessage = async () => { |
| 44 | + // TODO: @@ TravisR: We do not want to auto-send on stop. |
| 45 | + if (this.state.recorder) { |
| 46 | + await this.state.recorder.stop(); |
| 47 | + const mxc = await this.state.recorder.upload(); |
| 48 | + MatrixClientPeg.get().sendMessage(this.props.room.roomId, { |
| 49 | + body: "Voice message", |
| 50 | + msgtype: "org.matrix.msc2516.voice", |
| 51 | + url: mxc, |
| 52 | + }); |
| 53 | + this.setState({recorder: null}); |
| 54 | + this.props.onRecording(false); |
| 55 | + return; |
| 56 | + } |
| 57 | + const recorder = new VoiceRecorder(MatrixClientPeg.get()); |
| 58 | + await recorder.start(); |
| 59 | + this.props.onRecording(true); |
| 60 | + // TODO: @@ TravisR: Run through EQ component |
| 61 | + // recorder.frequencyData.onUpdate((freq) => { |
| 62 | + // console.log('@@ UPDATE', freq); |
| 63 | + // }); |
| 64 | + this.setState({recorder}); |
| 65 | + }; |
| 66 | + |
| 67 | + public render() { |
| 68 | + const classes = classNames({ |
| 69 | + 'mx_MessageComposer_button': !this.state.recorder, |
| 70 | + 'mx_MessageComposer_voiceMessage': !this.state.recorder, |
| 71 | + 'mx_VoiceRecordComposerTile_stop': !!this.state.recorder, |
| 72 | + }); |
| 73 | + |
| 74 | + let tooltip = _t("Record a voice message"); |
| 75 | + if (!!this.state.recorder) { |
| 76 | + // TODO: @@ TravisR: Change to match behaviour |
| 77 | + tooltip = _t("Stop & send recording"); |
| 78 | + } |
| 79 | + |
| 80 | + return ( |
| 81 | + <AccessibleTooltipButton |
| 82 | + className={classes} |
| 83 | + onClick={this.onStartStopVoiceMessage} |
| 84 | + title={tooltip} |
| 85 | + /> |
| 86 | + ); |
| 87 | + } |
| 88 | +} |
0 commit comments