diff --git a/src/app/features/settings/notifications/SystemNotification.tsx b/src/app/features/settings/notifications/SystemNotification.tsx index 8001eb31..515fa27e 100644 --- a/src/app/features/settings/notifications/SystemNotification.tsx +++ b/src/app/features/settings/notifications/SystemNotification.tsx @@ -1,14 +1,11 @@ import React, { useCallback } from 'react'; import { useTranslation } from 'react-i18next'; import { Text, Switch, Button, color, Spinner } from 'folds'; -import { IPusherRequest } from 'matrix-js-sdk'; import { SettingTile } from '../../../components/setting-tile'; import { SettingsSection } from '../SettingsSection'; import { useSetting } from '../../../state/hooks/settings'; import { settingsAtom } from '../../../state/settings'; -import { useEmailNotifications } from '../../../hooks/useEmailNotifications'; import { AsyncStatus, useAsyncCallback } from '../../../hooks/useAsyncCallback'; -import { useMatrixClient } from '../../../hooks/useMatrixClient'; import { useRegisterPushNotifications, useDisablePushNotifications, @@ -18,80 +15,6 @@ import { const noop = (): void => undefined; -function EmailNotification() { - const { t } = useTranslation(); - const mx = useMatrixClient(); - const [result, refreshResult] = useEmailNotifications(); - - const [setState, setEnable] = useAsyncCallback( - useCallback( - async (email: string, enable: boolean) => { - if (enable) { - await mx.setPusher({ - kind: 'email', - app_id: 'm.email', - pushkey: email, - app_display_name: 'Email Notifications', - device_display_name: email, - lang: 'en', - data: { - brand: 'Vojo', - }, - append: true, - }); - return; - } - await mx.setPusher({ - pushkey: email, - app_id: 'm.email', - kind: null, - } as unknown as IPusherRequest); - }, - [mx] - ) - ); - - const handleChange = (value: boolean) => { - if (result && result.email) { - setEnable(result.email, value).then(() => { - refreshResult(); - }); - } - }; - - return ( - - {result && !result.email && ( - - {t('Settings.email_no_email')} - - )} - {result && result.email && t('Settings.email_send_notif_to', { email: result.email })} - {result === null && ( - - {t('Settings.unexpected_error')} - - )} - {result === undefined && t('Settings.email_send_notif')} - - } - after={ - <> - {setState.status !== AsyncStatus.Loading && - typeof result === 'object' && - result?.email && } - {(setState.status === AsyncStatus.Loading || result === undefined) && ( - - )} - - } - /> - ); -} - function PushNotification() { const { t } = useTranslation(); const status = usePushNotificationStatus(); @@ -194,7 +117,6 @@ export function SystemNotification() { description={t('Settings.invite_spam_filter_desc')} after={} /> - ); } diff --git a/src/app/hooks/useEmailNotifications.ts b/src/app/hooks/useEmailNotifications.ts deleted file mode 100644 index 58639394..00000000 --- a/src/app/hooks/useEmailNotifications.ts +++ /dev/null @@ -1,55 +0,0 @@ -import { useCallback } from 'react'; -import { AsyncStatus, useAsyncCallbackValue } from './useAsyncCallback'; -import { useMatrixClient } from './useMatrixClient'; - -type RefreshHandler = () => void; - -type EmailNotificationResult = { - enabled: boolean; - email?: string; -}; - -export const useEmailNotifications = (): [ - EmailNotificationResult | undefined | null, - RefreshHandler -] => { - const mx = useMatrixClient(); - - const [emailState, refresh] = useAsyncCallbackValue( - useCallback(async () => { - const tpIDs = (await mx.getThreePids())?.threepids; - const emailAddresses = tpIDs.filter((id) => id.medium === 'email').map((id) => id.address); - if (emailAddresses.length === 0) - return { - enabled: false, - }; - - const pushers = (await mx.getPushers())?.pushers; - const emailPusher = pushers.find( - (pusher) => pusher.app_id === 'm.email' && emailAddresses.includes(pusher.pushkey) - ); - - if (emailPusher?.pushkey) { - return { - enabled: true, - email: emailPusher.pushkey, - }; - } - - return { - enabled: false, - email: emailAddresses[0], - }; - }, [mx]) - ); - - if (emailState.status === AsyncStatus.Success) { - return [emailState.data, refresh]; - } - - if (emailState.status === AsyncStatus.Error) { - return [null, refresh]; - } - - return [undefined, refresh]; -};