/* Copyright 2023 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, { useState } from "react"; import NewAndImprovedIcon from "../../../../../res/img/element-icons/new-and-improved.svg"; import { useMatrixClientContext } from "../../../../contexts/MatrixClientContext"; import { useNotificationSettings } from "../../../../hooks/useNotificationSettings"; import { useSettingValue } from "../../../../hooks/useSettings"; import { _t } from "../../../../languageHandler"; import { DefaultNotificationSettings, NotificationSettings, } from "../../../../models/notificationsettings/NotificationSettings"; import { RoomNotifState } from "../../../../RoomNotifs"; import { SettingLevel } from "../../../../settings/SettingLevel"; import SettingsStore from "../../../../settings/SettingsStore"; import { NotificationColor } from "../../../../stores/notifications/NotificationColor"; import { clearAllNotifications } from "../../../../utils/notifications"; import AccessibleButton from "../../elements/AccessibleButton"; import ExternalLink from "../../elements/ExternalLink"; import LabelledCheckbox from "../../elements/LabelledCheckbox"; import LabelledToggleSwitch from "../../elements/LabelledToggleSwitch"; import StyledRadioGroup from "../../elements/StyledRadioGroup"; import TagComposer from "../../elements/TagComposer"; import { StatelessNotificationBadge } from "../../rooms/NotificationBadge/StatelessNotificationBadge"; import { SettingsBanner } from "../shared/SettingsBanner"; import { SettingsSection } from "../shared/SettingsSection"; import SettingsSubsection from "../shared/SettingsSubsection"; import { NotificationPusherSettings } from "./NotificationPusherSettings"; enum NotificationDefaultLevels { AllMessages = "all_messages", PeopleMentionsKeywords = "people_mentions_keywords", MentionsKeywords = "mentions_keywords", } function toDefaultLevels(levels: NotificationSettings["defaultLevels"]): NotificationDefaultLevels { if (levels.room === RoomNotifState.AllMessages) { return NotificationDefaultLevels.AllMessages; } else if (levels.dm === RoomNotifState.AllMessages) { return NotificationDefaultLevels.PeopleMentionsKeywords; } else { return NotificationDefaultLevels.MentionsKeywords; } } const NotificationOptions = [ { value: NotificationDefaultLevels.AllMessages, label: _t("All messages"), }, { value: NotificationDefaultLevels.PeopleMentionsKeywords, label: _t("People, Mentions and Keywords"), }, { value: NotificationDefaultLevels.MentionsKeywords, label: _t("Mentions and Keywords only"), }, ]; function boldText(text: string): JSX.Element { return {text}; } function helpLink(sub: string): JSX.Element { return {sub}; } function useHasUnreadNotifications(): boolean { const cli = useMatrixClientContext(); return cli.getRooms().some((room) => room.getUnreadNotificationCount() > 0); } export default function NotificationSettings2(): JSX.Element { const cli = useMatrixClientContext(); const desktopNotifications = useSettingValue("notificationsEnabled"); const desktopShowBody = useSettingValue("notificationBodyEnabled"); const audioNotifications = useSettingValue("audioNotificationsEnabled"); const { model, hasPendingChanges, reconcile } = useNotificationSettings(cli); const disabled = model === null || hasPendingChanges; const settings = model ?? DefaultNotificationSettings; const [updatingUnread, setUpdatingUnread] = useState(false); const hasUnreadNotifications = useHasUnreadNotifications(); return (
{hasPendingChanges && model !== null && ( } action={_t("Proceed")} onAction={() => reconcile(model!)} > {_t( "Update:We’ve simplified Notifications Settings to make options easier to find. Some custom settings you’ve chosen in the past are not shown here, but they’re still active. If you proceed, some of your settings may change. Learn more", {}, { strong: boldText, a: helpLink, }, )} )}
{ reconcile({ ...model!, globalMute: !value, }); }} /> SettingsStore.setValue("notificationsEnabled", null, SettingLevel.DEVICE, value) } /> SettingsStore.setValue("notificationBodyEnabled", null, SettingLevel.DEVICE, value) } /> SettingsStore.setValue("audioNotificationsEnabled", null, SettingLevel.DEVICE, value) } />
{ reconcile({ ...model!, defaultLevels: { ...model!.defaultLevels, dm: value !== NotificationDefaultLevels.MentionsKeywords ? RoomNotifState.AllMessages : RoomNotifState.MentionsOnly, room: value === NotificationDefaultLevels.AllMessages ? RoomNotifState.AllMessages : RoomNotifState.MentionsOnly, }, }); }} /> { reconcile({ ...model!, sound: { ...model!.sound, people: value ? "default" : undefined, }, }); }} /> { reconcile({ ...model!, sound: { ...model!.sound, mentions: value ? "default" : undefined, }, }); }} /> { reconcile({ ...model!, sound: { ...model!.sound, calls: value ? "ring" : undefined, }, }); }} /> { reconcile({ ...model!, activity: { ...model!.activity, invite: value, }, }); }} /> { reconcile({ ...model!, activity: { ...model!.activity, status_event: value, }, }); }} /> { reconcile({ ...model!, activity: { ...model!.activity, bot_notices: value, }, }); }} /> when keywords are used in a room.", {}, { badge: , }, )} > { reconcile({ ...model!, mentions: { ...model!.mentions, room: value, }, }); }} /> { reconcile({ ...model!, mentions: { ...model!.mentions, user: value, }, }); }} /> { reconcile({ ...model!, mentions: { ...model!.mentions, keywords: value, }, }); }} /> { reconcile({ ...model!, keywords: [keyword, ...model!.keywords], }); }} onRemove={(keyword) => { reconcile({ ...model!, keywords: model!.keywords.filter((it) => it !== keyword), }); }} label={_t("Keyword")} placeholder={_t("New keyword")} /> {hasUnreadNotifications && ( { setUpdatingUnread(true); await clearAllNotifications(cli); setUpdatingUnread(false); }} > {_t("Mark all messages as read")} )} { reconcile(DefaultNotificationSettings); }} > {_t("Reset to default settings")}
); }