/* Copyright 2019 - 2021 The Matrix.org Foundation C.I.C. 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, { createRef } from 'react'; import { _t } from "../../../../../languageHandler"; import { MatrixClientPeg } from "../../../../../MatrixClientPeg"; import AccessibleButton from "../../../elements/AccessibleButton"; import Notifier from "../../../../../Notifier"; import SettingsStore from '../../../../../settings/SettingsStore'; import { SettingLevel } from "../../../../../settings/SettingLevel"; import { replaceableComponent } from "../../../../../utils/replaceableComponent"; import { logger } from "matrix-js-sdk/src/logger"; import { RoomEchoChamber } from "../../../../../stores/local-echo/RoomEchoChamber"; import { EchoChamber } from '../../../../../stores/local-echo/EchoChamber'; import MatrixClientContext from "../../../../../contexts/MatrixClientContext"; import StyledRadioGroup from "../../../elements/StyledRadioGroup"; import { RoomNotifState } from '../../../../../RoomNotifs'; import defaultDispatcher from "../../../../../dispatcher/dispatcher"; import { Action } from "../../../../../dispatcher/actions"; import { UserTab } from "../../../dialogs/UserSettingsDialog"; interface IProps { roomId: string; closeSettingsFn(): void; } interface IState { currentSound: string; uploadedFile: File; } @replaceableComponent("views.settings.tabs.room.NotificationsSettingsTab") export default class NotificationsSettingsTab extends React.Component { private readonly roomProps: RoomEchoChamber; private soundUpload = createRef(); static contextType = MatrixClientContext; public context!: React.ContextType; constructor(props: IProps, context: React.ContextType) { super(props, context); this.roomProps = EchoChamber.forRoom(context.getRoom(this.props.roomId)); this.state = { currentSound: "default", uploadedFile: null, }; } // TODO: [REACT-WARNING] Replace component with real class, use constructor for refs // eslint-disable-next-line @typescript-eslint/naming-convention, camelcase public UNSAFE_componentWillMount(): void { const soundData = Notifier.getSoundForRoom(this.props.roomId); if (!soundData) { return; } this.setState({ currentSound: soundData.name || soundData.url }); } private triggerUploader = async (e: React.MouseEvent): Promise => { e.stopPropagation(); e.preventDefault(); this.soundUpload.current.click(); }; private onSoundUploadChanged = (e: React.ChangeEvent): Promise => { if (!e.target.files || !e.target.files.length) { this.setState({ uploadedFile: null, }); return; } const file = e.target.files[0]; this.setState({ uploadedFile: file, }); }; private onClickSaveSound = async (e: React.MouseEvent): Promise => { e.stopPropagation(); e.preventDefault(); try { await this.saveSound(); } catch (ex) { logger.error( `Unable to save notification sound for ${this.props.roomId}`, ); logger.error(ex); } }; private async saveSound(): Promise { 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, }, ); await SettingsStore.setValue( "notificationSound", this.props.roomId, SettingLevel.ROOM_ACCOUNT, { name: this.state.uploadedFile.name, type: type, size: this.state.uploadedFile.size, url, }, ); this.setState({ uploadedFile: null, currentSound: this.state.uploadedFile.name, }); } private clearSound = (e: React.MouseEvent): void => { e.stopPropagation(); e.preventDefault(); SettingsStore.setValue( "notificationSound", this.props.roomId, SettingLevel.ROOM_ACCOUNT, null, ); this.setState({ currentSound: "default", }); }; private onRoomNotificationChange = (value: RoomNotifState) => { this.roomProps.notificationVolume = value; this.forceUpdate(); }; private onOpenSettingsClick = () => { this.props.closeSettingsFn(); defaultDispatcher.dispatch({ action: Action.ViewUserSettings, initialTabId: UserTab.Notifications, }); }; public render(): JSX.Element { let currentUploadedFile = null; if (this.state.uploadedFile) { currentUploadedFile = (
{ _t("Uploaded sound") }: { this.state.uploadedFile.name }
); } return (
{ _t("Notifications") }
{ _t("Default") }
{ _t("Get notifications as set up in your settings", {}, { a: sub => { sub } , }) }
, }, { value: RoomNotifState.AllMessagesLoud, className: "mx_NotificationSettingsTab_allMessagesEntry", label: <> { _t("All messages") }
{ _t("Get notified for every message") }
, }, { value: RoomNotifState.MentionsOnly, className: "mx_NotificationSettingsTab_mentionsKeywordsEntry", label: <> { _t("@mentions & keywords") }
{ _t("Get notified only with mentions and keywords " + "as set up in your settings", {}, { a: sub => { sub } , }) }
, }, { value: RoomNotifState.Mute, className: "mx_NotificationSettingsTab_noneEntry", label: <> { _t("Off") }
{ _t("You won't get any notifications") }
, }, ]} onChange={this.onRoomNotificationChange} value={this.roomProps.notificationVolume} />
{ _t("Sounds") }
{ _t("Notification sound") }: { this.state.currentSound }
{ _t("Reset") }

{ _t("Set a new custom sound") }

{ currentUploadedFile }
{ _t("Browse") } { _t("Save") }
); } }