refactor(notifications): cut the email-pusher row and its orphaned hook
This commit is contained in:
parent
81c57eccdd
commit
94bc35092a
2 changed files with 0 additions and 133 deletions
|
|
@ -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 (
|
||||
<SettingTile
|
||||
title={t('Settings.email_notification')}
|
||||
description={
|
||||
<>
|
||||
{result && !result.email && (
|
||||
<Text as="span" style={{ color: color.Critical.Main }} size="T200">
|
||||
{t('Settings.email_no_email')}
|
||||
</Text>
|
||||
)}
|
||||
{result && result.email && t('Settings.email_send_notif_to', { email: result.email })}
|
||||
{result === null && (
|
||||
<Text as="span" style={{ color: color.Critical.Main }} size="T200">
|
||||
{t('Settings.unexpected_error')}
|
||||
</Text>
|
||||
)}
|
||||
{result === undefined && t('Settings.email_send_notif')}
|
||||
</>
|
||||
}
|
||||
after={
|
||||
<>
|
||||
{setState.status !== AsyncStatus.Loading &&
|
||||
typeof result === 'object' &&
|
||||
result?.email && <Switch value={result.enabled} onChange={handleChange} />}
|
||||
{(setState.status === AsyncStatus.Loading || result === undefined) && (
|
||||
<Spinner variant="Secondary" />
|
||||
)}
|
||||
</>
|
||||
}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
function PushNotification() {
|
||||
const { t } = useTranslation();
|
||||
const status = usePushNotificationStatus();
|
||||
|
|
@ -194,7 +117,6 @@ export function SystemNotification() {
|
|||
description={t('Settings.invite_spam_filter_desc')}
|
||||
after={<Switch value={inviteSpamFilter} onChange={setInviteSpamFilter} />}
|
||||
/>
|
||||
<EmailNotification />
|
||||
</SettingsSection>
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<EmailNotificationResult, Error>(
|
||||
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];
|
||||
};
|
||||
Loading…
Add table
Reference in a new issue