mattermost-mobile/app/screens/settings/notifications/notifications.tsx
Daniel Espino García e0992c0bf6
Add test notification tool (#8271)
* Add test notification menu

* Reduce the minimum version for testing purposes

* Address feedback

* Add missing strings

* Address feedback

* Fix snapshots

* Bump version limit and use correct link

* Fix tests

* Fix URL issues
2024-11-11 10:22:57 +01:00

176 lines
6.8 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import React, {useCallback, useMemo} from 'react';
import {defineMessages, useIntl} from 'react-intl';
import {getCallsConfig} from '@calls/state';
import SettingContainer from '@components/settings/container';
import SettingItem from '@components/settings/item';
import {General, Screens} from '@constants';
import {useServerUrl} from '@context/server';
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 SendTestNotificationNotice from './send_test_notification_notice';
import type UserModel from '@typings/database/models/servers/user';
import type {AvailableScreens} from '@typings/screens/navigation';
const mentionTexts = defineMessages({
crtOn: {
id: 'notification_settings.mentions',
defaultMessage: 'Mentions',
},
crtOff: {
id: 'notification_settings.mentions_replies',
defaultMessage: 'Mentions and Replies',
},
callsOn: {
id: 'notification_settings.calls_on',
defaultMessage: 'On',
},
callsOff: {
id: 'notification_settings.calls_off',
defaultMessage: 'Off',
},
});
type NotificationsProps = {
componentId: AvailableScreens;
currentUser?: UserModel;
emailInterval: string;
enableAutoResponder: boolean;
enableEmailBatching: boolean;
isCRTEnabled: boolean;
sendEmailNotifications: boolean;
serverVersion: string;
}
const Notifications = ({
componentId,
currentUser,
emailInterval,
enableAutoResponder,
enableEmailBatching,
isCRTEnabled,
sendEmailNotifications,
serverVersion,
}: NotificationsProps) => {
const intl = useIntl();
const serverUrl = useServerUrl();
const notifyProps = useMemo(() => getNotificationProps(currentUser), [currentUser?.notifyProps]);
const callsRingingEnabled = useMemo(() => getCallsConfig(serverUrl).EnableRinging, [serverUrl]);
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);
}, [intl, 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);
}, [intl]);
const callsNotificationsOn = useMemo(() => Boolean(notifyProps?.calls_mobile_sound ? notifyProps.calls_mobile_sound === 'true' : notifyProps?.calls_desktop_sound === 'true'),
[notifyProps]);
const goToNotificationSettingsCall = useCallback(() => {
const screen = Screens.SETTINGS_NOTIFICATION_CALL;
const title = intl.formatMessage({
id: 'notification_settings.call_notification',
defaultMessage: 'Call Notifications',
});
gotoSettingsScreen(screen, title);
}, [intl]);
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);
}, [intl]);
const goToEmailSettings = useCallback(() => {
const screen = Screens.SETTINGS_NOTIFICATION_EMAIL;
const title = intl.formatMessage({id: 'notification_settings.email', defaultMessage: 'Email Notifications'});
gotoSettingsScreen(screen, title);
}, [intl]);
const close = useCallback(() => {
popTopScreen(componentId);
}, [componentId]);
useAndroidHardwareBackHandler(componentId, close);
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'
/>
{callsRingingEnabled &&
<SettingItem
optionName='call_notification'
onPress={goToNotificationSettingsCall}
info={intl.formatMessage({
id: callsNotificationsOn ? mentionTexts.callsOn.id : mentionTexts.callsOff.id,
defaultMessage: callsNotificationsOn ? mentionTexts.callsOn.defaultMessage : mentionTexts.callsOff.defaultMessage,
})}
testID='notification_settings.call_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'
/>
)}
<SendTestNotificationNotice
serverVersion={serverVersion}
userId={currentUser?.id || ''}
/>
</SettingContainer>
);
};
export default Notifications;