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

194 lines
6.6 KiB
TypeScript
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.
*/
2021-06-29 13:11:58 +01:00
import React, { createRef } from 'react';
import { _t } from "../../../../../languageHandler";
import { MatrixClientPeg } from "../../../../../MatrixClientPeg";
2019-04-19 16:27:30 +01:00
import AccessibleButton from "../../../elements/AccessibleButton";
import Notifier from "../../../../../Notifier";
2019-05-15 15:52:42 +01:00
import SettingsStore from '../../../../../settings/SettingsStore';
2021-06-29 13:11:58 +01:00
import { SettingLevel } from "../../../../../settings/SettingLevel";
import { replaceableComponent } from "../../../../../utils/replaceableComponent";
2019-04-19 16:27:30 +01:00
2021-10-15 16:30:53 +02:00
import { logger } from "matrix-js-sdk/src/logger";
2021-09-21 12:51:47 +02:00
interface IProps {
roomId: string;
}
interface IState {
currentSound: string;
uploadedFile: File;
}
@replaceableComponent("views.settings.tabs.room.NotificationsSettingsTab")
2021-09-21 12:51:47 +02:00
export default class NotificationsSettingsTab extends React.Component<IProps, IState> {
private soundUpload = createRef<HTMLInputElement>();
2019-04-19 16:27:30 +01:00
2021-09-21 12:51:47 +02:00
constructor(props: IProps) {
super(props);
2019-04-19 16:27:30 +01:00
this.state = {
currentSound: "default",
uploadedFile: null,
};
}
// TODO: [REACT-WARNING] Replace component with real class, use constructor for refs
2021-09-21 12:51:47 +02:00
// eslint-disable-next-line @typescript-eslint/naming-convention, camelcase
public UNSAFE_componentWillMount(): void {
2020-06-25 08:43:35 +01:00
const soundData = Notifier.getSoundForRoom(this.props.roomId);
if (!soundData) {
return;
}
2021-06-29 13:11:58 +01:00
this.setState({ currentSound: soundData.name || soundData.url });
2019-04-19 16:27:30 +01:00
}
2021-09-21 12:51:47 +02:00
private triggerUploader = async (e: React.MouseEvent): Promise<void> => {
2019-05-14 21:05:22 +01:00
e.stopPropagation();
e.preventDefault();
2021-09-21 12:51:47 +02:00
this.soundUpload.current.click();
};
2019-05-14 21:05:22 +01:00
2021-09-21 12:51:47 +02:00
private onSoundUploadChanged = (e: React.ChangeEvent<HTMLInputElement>): Promise<void> => {
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,
});
2021-09-21 12:51:47 +02:00
};
2019-05-15 15:52:42 +01:00
2021-09-21 12:51:47 +02:00
private onClickSaveSound = async (e: React.MouseEvent): Promise<void> => {
2019-05-15 15:52:42 +01:00
e.stopPropagation();
e.preventDefault();
2019-05-12 17:14:21 +01:00
try {
2021-09-21 12:51:47 +02:00
await this.saveSound();
2019-05-12 17:14:21 +01:00
} catch (ex) {
2021-10-15 16:30:53 +02:00
logger.error(
2019-05-12 17:14:21 +01:00
`Unable to save notification sound for ${this.props.roomId}`,
);
2021-10-15 16:30:53 +02:00
logger.error(ex);
2019-05-12 17:14:21 +01:00
}
2021-09-21 12:51:47 +02:00
};
2019-04-19 16:27:30 +01:00
2021-09-21 12:51:47 +02:00
private async saveSound(): Promise<void> {
2019-04-19 16:27:30 +01:00
if (!this.state.uploadedFile) {
return;
}
2019-06-03 17:35:15 +01:00
2019-04-19 16:27:30 +01:00
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";
}
2019-05-15 15:52:42 +01:00
2019-04-19 16:27:30 +01:00
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
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,
currentSound: this.state.uploadedFile.name,
});
}
2021-09-21 12:51:47 +02:00
private clearSound = (e: React.MouseEvent): void => {
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",
});
2021-09-21 12:51:47 +02:00
};
2019-04-19 16:27:30 +01:00
2021-09-21 12:51:47 +02:00
public render(): JSX.Element {
2019-05-15 15:52:42 +01:00
let currentUploadedFile = null;
if (this.state.uploadedFile) {
currentUploadedFile = (
<div>
<span>{ _t("Uploaded sound") }: <code>{ this.state.uploadedFile.name }</code></span>
2019-05-15 15:52:42 +01:00
</div>
);
}
2019-04-19 16:27:30 +01:00
return (
<div className="mx_SettingsTab">
<div className="mx_SettingsTab_heading">{ _t("Notifications") }</div>
2019-04-19 16:27:30 +01:00
<div className='mx_SettingsTab_section mx_SettingsTab_subsectionText'>
<span className='mx_SettingsTab_subheading'>{ _t("Sounds") }</span>
2019-04-19 16:27:30 +01:00
<div>
2021-10-16 14:58:10 +01:00
<div className="mx_SettingsTab_subsectionText">
<span>{ _t("Notification sound") }: <code>{ this.state.currentSound }</code></span>
</div>
2021-09-21 12:51:47 +02:00
<AccessibleButton className="mx_NotificationSound_resetSound" disabled={this.state.currentSound == "default"} onClick={this.clearSound} kind="primary">
{ _t("Reset") }
2019-05-15 15:52:42 +01:00
</AccessibleButton>
2019-04-19 16:27:30 +01:00
</div>
<div>
<h3>{ _t("Set a new custom sound") }</h3>
2021-10-16 14:58:10 +01:00
<div className="mx_SettingsFlag">
<form autoComplete="off" noValidate={true}>
<input ref={this.soundUpload} className="mx_NotificationSound_soundUpload" type="file" onChange={this.onSoundUploadChanged} accept="audio/*" />
</form>
2021-10-16 14:58:10 +01:00
{ currentUploadedFile }
</div>
2019-05-15 15:52:42 +01:00
2021-09-21 12:51:47 +02:00
<AccessibleButton className="mx_NotificationSound_browse" onClick={this.triggerUploader} kind="primary">
{ _t("Browse") }
2019-05-14 21:05:22 +01:00
</AccessibleButton>
2021-09-21 12:51:47 +02:00
<AccessibleButton className="mx_NotificationSound_save" disabled={this.state.uploadedFile == null} onClick={this.onClickSaveSound} kind="primary">
{ _t("Save") }
2019-04-19 16:27:30 +01:00
</AccessibleButton>
2019-05-15 15:52:42 +01:00
<br />
2019-04-19 16:27:30 +01:00
</div>
</div>
</div>
);
}
}