/* Copyright 2024 New Vector Ltd. Copyright 2023 The Matrix.org Foundation C.I.C. SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial Please see LICENSE files in the repository root for full details. */ import React, { type JSX, 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, type NotificationSettings, } from "../../../../models/notificationsettings/NotificationSettings"; import { RoomNotifState } from "../../../../RoomNotifs"; import { SettingLevel } from "../../../../settings/SettingLevel"; import SettingsStore from "../../../../settings/SettingsStore"; import { NotificationLevel } from "../../../../stores/notifications/NotificationLevel"; 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"; import SettingsFlag from "../../elements/SettingsFlag"; import { SettingsSubsectionHeading } from "../shared/SettingsSubsectionHeading"; 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; } } 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); } /** * The new notification settings tab view, only displayed if the user has Features.NotificationSettings2 enabled */ 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(); const NotificationOptions = [ { value: NotificationDefaultLevels.AllMessages, label: _t("notifications|all_messages"), }, { value: NotificationDefaultLevels.PeopleMentionsKeywords, label: _t("settings|notifications|people_mentions_keywords"), }, { value: NotificationDefaultLevels.MentionsKeywords, label: _t("settings|notifications|mentions_keywords_only"), }, ]; return (
{hasPendingChanges && model !== null && ( } action={_t("action|proceed")} onAction={() => reconcile(model!)} > {_t( "settings|notifications|labs_notice_prompt", {}, { 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) } />
} description={_t("settings|notifications|default_setting_description")} > { reconcile({ ...model!, defaultLevels: { ...model!.defaultLevels, dm: value !== NotificationDefaultLevels.MentionsKeywords ? RoomNotifState.AllMessages : RoomNotifState.MentionsOnly, room: value === NotificationDefaultLevels.AllMessages ? RoomNotifState.AllMessages : RoomNotifState.MentionsOnly, }, }); }} /> } description={_t("settings|notifications|play_sound_for_description")} > { 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, }, }); }} /> } description={_t( "settings|notifications|keywords", {}, { 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("notifications|keyword")} placeholder={_t("notifications|keyword_new")} /> {hasUnreadNotifications && ( { setUpdatingUnread(true); await clearAllNotifications(cli); setUpdatingUnread(false); }} > {_t("settings|notifications|quick_actions_mark_all_read")} )} { reconcile(DefaultNotificationSettings); }} > {_t("settings|notifications|quick_actions_reset")}
); }