|
| 1 | +/* |
| 2 | +Copyright 2023 The Matrix.org Foundation C.I.C. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +import { ThreepidMedium } from "matrix-js-sdk/src/@types/threepids"; |
| 18 | +import { IPusher } from "matrix-js-sdk/src/matrix"; |
| 19 | +import React, { useCallback } from "react"; |
| 20 | + |
| 21 | +import { useMatrixClientContext } from "../../../../contexts/MatrixClientContext"; |
| 22 | +import { Action } from "../../../../dispatcher/actions"; |
| 23 | +import dispatcher from "../../../../dispatcher/dispatcher"; |
| 24 | +import { usePushers } from "../../../../hooks/usePushers"; |
| 25 | +import { useThreepids } from "../../../../hooks/useThreepids"; |
| 26 | +import { _t } from "../../../../languageHandler"; |
| 27 | +import SdkConfig from "../../../../SdkConfig"; |
| 28 | +import { UserTab } from "../../dialogs/UserTab"; |
| 29 | +import AccessibleButton from "../../elements/AccessibleButton"; |
| 30 | +import LabelledCheckbox from "../../elements/LabelledCheckbox"; |
| 31 | +import { SettingsIndent } from "../shared/SettingsIndent"; |
| 32 | +import SettingsSubsection, { SettingsSubsectionText } from "../shared/SettingsSubsection"; |
| 33 | + |
| 34 | +const EmailPusherTemplate: Omit<IPusher, "pushkey" | "device_display_name" | "append"> = { |
| 35 | + kind: "email", |
| 36 | + app_id: "m.email", |
| 37 | + app_display_name: "Email Notifications", |
| 38 | + lang: navigator.language, |
| 39 | + data: { |
| 40 | + brand: SdkConfig.get().brand, |
| 41 | + }, |
| 42 | +}; |
| 43 | + |
| 44 | +function generalTabButton(content: string): JSX.Element { |
| 45 | + return ( |
| 46 | + <AccessibleButton |
| 47 | + kind="link_inline" |
| 48 | + onClick={() => { |
| 49 | + dispatcher.dispatch({ |
| 50 | + action: Action.ViewUserSettings, |
| 51 | + initialTabId: UserTab.General, |
| 52 | + }); |
| 53 | + }} |
| 54 | + > |
| 55 | + {content} |
| 56 | + </AccessibleButton> |
| 57 | + ); |
| 58 | +} |
| 59 | + |
| 60 | +export function NotificationPusherSettings(): JSX.Element { |
| 61 | + const cli = useMatrixClientContext(); |
| 62 | + const [pushers, refreshPushers] = usePushers(cli); |
| 63 | + const [threepids, refreshThreepids] = useThreepids(cli); |
| 64 | + |
| 65 | + const setEmailEnabled = useCallback( |
| 66 | + (email: string, enabled: boolean) => { |
| 67 | + if (enabled) { |
| 68 | + cli.setPusher({ |
| 69 | + ...EmailPusherTemplate, |
| 70 | + pushkey: email, |
| 71 | + device_display_name: email, |
| 72 | + // We always append for email pushers since we don't want to stop other |
| 73 | + // accounts notifying to the same email address |
| 74 | + append: true, |
| 75 | + }).catch((err) => console.error(err)); |
| 76 | + } else { |
| 77 | + const pusher = pushers.find((p) => p.kind === "email" && p.pushkey === email); |
| 78 | + if (pusher) { |
| 79 | + cli.removePusher(pusher.pushkey, pusher.app_id).catch((err) => console.error(err)); |
| 80 | + } |
| 81 | + } |
| 82 | + refreshThreepids(); |
| 83 | + refreshPushers(); |
| 84 | + }, |
| 85 | + [cli, pushers, refreshPushers, refreshThreepids], |
| 86 | + ); |
| 87 | + |
| 88 | + const notificationTargets = pushers.filter((it) => it.kind !== "email"); |
| 89 | + |
| 90 | + return ( |
| 91 | + <> |
| 92 | + <SettingsSubsection className="mx_NotificationPusherSettings" heading={_t("Email summary")}> |
| 93 | + <SettingsSubsectionText className="mx_NotificationPusherSettings_description"> |
| 94 | + {_t("Receive an email summary of missed notifications")} |
| 95 | + </SettingsSubsectionText> |
| 96 | + <div className="mx_SettingsSubsection_description mx_NotificationPusherSettings_detail"> |
| 97 | + <SettingsSubsectionText> |
| 98 | + {_t( |
| 99 | + "Select which emails you want to send summaries to. Manage your emails in <button>General</button>.", |
| 100 | + {}, |
| 101 | + { button: generalTabButton }, |
| 102 | + )} |
| 103 | + </SettingsSubsectionText> |
| 104 | + </div> |
| 105 | + <SettingsIndent> |
| 106 | + {threepids |
| 107 | + .filter((t) => t.medium === ThreepidMedium.Email) |
| 108 | + .map((email) => ( |
| 109 | + <LabelledCheckbox |
| 110 | + key={email.address} |
| 111 | + label={email.address} |
| 112 | + value={pushers.find((it) => it.pushkey === email.address) !== undefined} |
| 113 | + onChange={(value) => setEmailEnabled(email.address, value)} |
| 114 | + /> |
| 115 | + ))} |
| 116 | + </SettingsIndent> |
| 117 | + </SettingsSubsection> |
| 118 | + {notificationTargets.length > 0 && ( |
| 119 | + <SettingsSubsection heading={_t("Notification targets")}> |
| 120 | + <ul> |
| 121 | + {pushers |
| 122 | + .filter((it) => it.kind !== "email") |
| 123 | + .map((pusher) => ( |
| 124 | + <li key={pusher.pushkey}>{pusher.device_display_name || pusher.app_display_name}</li> |
| 125 | + ))} |
| 126 | + </ul> |
| 127 | + </SettingsSubsection> |
| 128 | + )} |
| 129 | + </> |
| 130 | + ); |
| 131 | +} |
0 commit comments