mattermost-mobile/app/screens/settings/notifications/notifications.tsx
Daniel Espino García 162cdfb21d
Handle currentUser not available throught the app and refetch it (#7333)
* Handle currentUser not available throught the app and refetch it

* Address feedback
2023-05-05 15:40:59 -04:00

131 lines
4.9 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import React, {useCallback, useMemo} from 'react';
import {useIntl} from 'react-intl';
import SettingContainer from '@components/settings/container';
import SettingItem from '@components/settings/item';
import {General, Screens} from '@constants';
import useAndroidHardwareBackHandler from '@hooks/android_back_handler';
import {t} from '@i18n';
import {popTopScreen} from '@screens/navigation';
import {gotoSettingsScreen} from '@screens/settings/config';
import {getEmailInterval, getEmailIntervalTexts, getNotificationProps} from '@utils/user';
import type UserModel from '@typings/database/models/servers/user';
import type {AvailableScreens} from '@typings/screens/navigation';
const mentionTexts = {
crtOn: {
id: t('notification_settings.mentions'),
defaultMessage: 'Mentions',
},
crtOff: {
id: t('notification_settings.mentions_replies'),
defaultMessage: 'Mentions and Replies',
},
};
type NotificationsProps = {
componentId: AvailableScreens;
currentUser?: UserModel;
emailInterval: string;
enableAutoResponder: boolean;
enableEmailBatching: boolean;
isCRTEnabled: boolean;
sendEmailNotifications: boolean;
}
const Notifications = ({
componentId,
currentUser,
emailInterval,
enableAutoResponder,
enableEmailBatching,
isCRTEnabled,
sendEmailNotifications,
}: NotificationsProps) => {
const intl = useIntl();
const notifyProps = useMemo(() => getNotificationProps(currentUser), [currentUser?.notifyProps]);
const emailIntervalPref = useMemo(() =>
getEmailInterval(
sendEmailNotifications && notifyProps?.email === 'true',
enableEmailBatching,
parseInt(emailInterval, 10),
).toString(),
[emailInterval, enableEmailBatching, notifyProps, sendEmailNotifications]);
const goToNotificationSettingsMentions = useCallback(() => {
const screen = Screens.SETTINGS_NOTIFICATION_MENTION;
const id = isCRTEnabled ? t('notification_settings.mentions') : t('notification_settings.mentions_replies');
const defaultMessage = isCRTEnabled ? 'Mentions' : 'Mentions and Replies';
const title = intl.formatMessage({id, defaultMessage});
gotoSettingsScreen(screen, title);
}, [isCRTEnabled]);
const goToNotificationSettingsPush = useCallback(() => {
const screen = Screens.SETTINGS_NOTIFICATION_PUSH;
const title = intl.formatMessage({
id: 'notification_settings.push_notification',
defaultMessage: 'Push Notifications',
});
gotoSettingsScreen(screen, title);
}, []);
const goToNotificationAutoResponder = useCallback(() => {
const screen = Screens.SETTINGS_NOTIFICATION_AUTO_RESPONDER;
const title = intl.formatMessage({
id: 'notification_settings.auto_responder',
defaultMessage: 'Automatic Replies',
});
gotoSettingsScreen(screen, title);
}, []);
const goToEmailSettings = useCallback(() => {
const screen = Screens.SETTINGS_NOTIFICATION_EMAIL;
const title = intl.formatMessage({id: 'notification_settings.email', defaultMessage: 'Email Notifications'});
gotoSettingsScreen(screen, title);
}, []);
useAndroidHardwareBackHandler(componentId, () => {
popTopScreen(componentId);
});
return (
<SettingContainer testID='notification_settings'>
<SettingItem
onPress={goToNotificationSettingsMentions}
optionName='mentions'
label={intl.formatMessage({
id: isCRTEnabled ? mentionTexts.crtOn.id : mentionTexts.crtOff.id,
defaultMessage: isCRTEnabled ? mentionTexts.crtOn.defaultMessage : mentionTexts.crtOff.defaultMessage,
})}
testID='notification_settings.mentions.option'
/>
<SettingItem
optionName='push_notification'
onPress={goToNotificationSettingsPush}
testID='notification_settings.push_notifications.option'
/>
<SettingItem
optionName='email'
onPress={goToEmailSettings}
info={intl.formatMessage(getEmailIntervalTexts(emailIntervalPref))}
testID='notification_settings.email_notifications.option'
/>
{enableAutoResponder && (
<SettingItem
onPress={goToNotificationAutoResponder}
optionName='automatic_dm_replies'
info={currentUser?.status === General.OUT_OF_OFFICE && notifyProps.auto_responder_active === 'true' ? 'On' : 'Off'}
testID='notification_settings.automatic_replies.option'
/>
)}
</SettingContainer>
);
};
export default Notifications;