Files
cinny/src/app/hooks/useEmailNotifications.ts
T
Lotus Bot 61a1f008d0 chore: upgrade i18next 26, prettier 3, fontsource-variable, domhandler 6, lint-staged 17
- i18next 23->26 + react-i18next 15->17
- prettier 2->3, reformat all files
- replace @fontsource/inter with @fontsource-variable/inter 5, update import path
- domhandler 5->6 (aligns with transitive deps)
- lint-staged 16->17
2026-05-21 23:30:50 -04:00

56 lines
1.4 KiB
TypeScript

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];
};