Files
ThreadNet-Web/src/components/views/settings/tabs/room/NotificationSettingsTab.js
T

159 lines
4.9 KiB
JavaScript
Raw Normal View History

2019-04-19 16:27:30 +01:00
/*
Copyright 2019 New Vector Ltd
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import React from 'react';
import PropTypes from 'prop-types';
import {_t} from "../../../../../languageHandler";
import MatrixClientPeg from "../../../../../MatrixClientPeg";
import AccessibleButton from "../../../elements/AccessibleButton";
import Notifier from "../../../../../Notifier";
2019-05-12 17:14:21 +01:00
import SettingsStore from '../../../../../settings/SettingsStore';
import { SettingLevel } from '../../../../../settings/SettingsStore';
2019-04-19 16:27:30 +01:00
export default class NotificationsSettingsTab extends React.Component {
static propTypes = {
roomId: PropTypes.string.isRequired,
};
constructor() {
super();
this.state = {
currentSound: "default",
uploadedFile: null,
};
}
componentWillMount() {
2019-04-19 22:31:51 +01:00
Notifier.getSoundForRoom(this.props.roomId).then((soundData) => {
2019-04-19 16:27:30 +01:00
if (!soundData) {
return;
}
2019-04-19 21:42:18 +01:00
this.setState({currentSound: soundData.name || soundData.url});
});
2019-04-19 16:27:30 +01:00
}
2019-05-14 21:05:22 +01:00
async _triggerUploader(e) {
e.stopPropagation();
e.preventDefault();
this.refs.soundUpload.click();
}
2019-05-12 17:14:21 +01:00
async _onSoundUploadChanged(e) {
2019-04-19 16:27:30 +01:00
if (!e.target.files || !e.target.files.length) {
this.setState({
uploadedFile: null,
});
return;
}
const file = e.target.files[0];
this.setState({
uploadedFile: file,
});
2019-05-12 17:14:21 +01:00
try {
await this._saveSound();
} catch (ex) {
console.error(
`Unable to save notification sound for ${this.props.roomId}`,
ex,
);
}
2019-04-19 16:27:30 +01:00
}
2019-05-12 17:14:21 +01:00
async _saveSound() {
2019-04-19 16:27:30 +01:00
if (!this.state.uploadedFile) {
return;
}
let type = this.state.uploadedFile.type;
if (type === "video/ogg") {
// XXX: I've observed browsers allowing users to pick a audio/ogg files,
// and then calling it a video/ogg. This is a lame hack, but man browsers
// suck at detecting mimetypes.
type = "audio/ogg";
}
const url = await MatrixClientPeg.get().uploadContent(
this.state.uploadedFile, {
type,
},
);
2019-04-19 22:31:51 +01:00
await SettingsStore.setValue(
"notificationSound",
this.props.roomId,
2019-05-12 17:14:21 +01:00
SettingsStore.
SettingLevel.ROOM_ACCOUNT,
2019-04-19 22:31:51 +01:00
{
name: this.state.uploadedFile.name,
type: type,
size: this.state.uploadedFile.size,
url,
},
);
2019-04-19 16:27:30 +01:00
this.setState({
uploadedFile: null,
uploadedFileUrl: null,
currentSound: this.state.uploadedFile.name,
});
}
2019-04-19 21:42:18 +01:00
_clearSound(e) {
2019-04-19 16:27:30 +01:00
e.stopPropagation();
e.preventDefault();
2019-05-12 17:14:21 +01:00
SettingsStore.setValue(
"notificationSound",
this.props.roomId,
SettingLevel.ROOM_ACCOUNT,
null,
);
2019-04-19 16:27:30 +01:00
this.setState({
currentSound: "default",
});
}
render() {
return (
<div className="mx_SettingsTab">
<div className="mx_SettingsTab_heading">{_t("Notifications")}</div>
<div className='mx_SettingsTab_section mx_SettingsTab_subsectionText'>
<span className='mx_SettingsTab_subheading'>{_t("Sounds")}</span>
<div>
2019-04-21 18:01:26 +01:00
<span>{_t("Custom Notification Sounds")}: <code>{this.state.currentSound}</code></span>
2019-04-19 16:27:30 +01:00
</div>
<div>
<h3>{_t("Set a new custom sound")}</h3>
2019-05-12 17:14:21 +01:00
<form autoComplete={false} noValidate={true}>
2019-05-14 21:05:22 +01:00
<input ref="soundUpload" className="mx_NotificationSound_soundUpload" type="file" onChange={this._onSoundUploadChanged.bind(this)} accept="audio/*" />
2019-04-19 16:27:30 +01:00
</form>
2019-05-14 21:05:22 +01:00
<AccessibleButton onClick={this._triggerUploader.bind(this)} kind="primary">
{_t("Browse")}
</AccessibleButton>
<AccessibleButton className="mx_NotificationSound_resetSound" onClick={this._clearSound.bind(this)} kind="primary">
{_t("Reset")}
2019-04-19 16:27:30 +01:00
</AccessibleButton>
</div>
</div>
</div>
);
}
}