Files
upstream-rocketchat/apps/meteor/client/views/account/preferences/PreferencesNotificationsSection.tsx
upstream-mirror 30810eecd7
Some checks failed
Create issues from TODOs / todos (push) Failing after 3s
Close inactive issues / close-issues (push) Has been cancelled
Deploy GitHub Pages / deploy-preview (push) Has been cancelled
CI / ⚙️ Variables Setup (push) Has been cancelled
CI / ⚙️ Test Guard (push) Has been cancelled
CI / 🚀 Notify external services - draft (push) Has been cancelled
CI / 📦 Build Packages (push) Has been cancelled
CI / 📦 Meteor Build (coverage) (push) Has been cancelled
CI / 📦 Meteor Build (production) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], fips) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Publish Docker Images (ghcr.io) (push) Has been cancelled
CI / 📦 Track Image Sizes (push) Has been cancelled
CI / 🔎 Code Check - Action Lint (push) Has been cancelled
CI / 🔎 Code Check (push) Has been cancelled
CI / 🔨 Test Storybook (push) Has been cancelled
CI / 🔨 Test Unit (push) Has been cancelled
CI / 🔨 Test API (CE) (push) Has been cancelled
CI / 🔨 Test API Livechat (CE) (push) Has been cancelled
CI / 🔨 Test UI (CE) (push) Has been cancelled
CI / 🔨 Test API (EE) (push) Has been cancelled
CI / 🔨 Test API Livechat (EE) (push) Has been cancelled
CI / 🔨 Test API Apps (deno-runtime - EE) (push) Has been cancelled
CI / 🔨 Test UI (EE) (push) Has been cancelled
CI / 🔨 Test API (FIPS) (push) Has been cancelled
CI / 🔨 Test API Livechat (FIPS) (push) Has been cancelled
CI / 🔨 Test UI (FIPS) (push) Has been cancelled
CI / 🔨 Test Federation Matrix (push) Has been cancelled
CI / 📊 Report Coverage (push) Has been cancelled
CI / ✅ Tests Done (push) Has been cancelled
CI / 🚀 Publish build assets (push) Has been cancelled
CI / 🚀 Publish Docker Images (DockerHub) (push) Has been cancelled
CI / 🚀 Notify external services (push) Has been cancelled
CI / Update Version Durability (push) Has been cancelled
Release / Release (push) Has been cancelled
Auto-close duplicate issues / auto-close-duplicates (push) Failing after 1s
Code scanning - action / CodeQL-Build (push) Failing after 2s
snapshot of https://github.com/RocketChat/Rocket.Chat.git (develop, 2026-09-16; blobs > 131072B omitted)
2026-09-16 01:21:02 +05:00

213 lines
7.9 KiB
TypeScript

import type { INotificationDesktop } from '@rocket.chat/core-typings';
import type { SelectOption } from '@rocket.chat/fuselage';
import { AccordionItem, Button } from '@rocket.chat/fuselage';
import { Field, FieldGroup, FieldHint, FieldLabel, FieldRow, Select, ToggleSwitch } from '@rocket.chat/fuselage-forms';
import type { TranslationKey } from '@rocket.chat/ui-contexts';
import { useSetting, useUserPreference, useUser } from '@rocket.chat/ui-contexts';
import { useCallback, useEffect, useId, useMemo, useState } from 'react';
import { Controller, useFormContext } from 'react-hook-form';
import { useTranslation } from 'react-i18next';
import { useNotification } from '../../../hooks/notification/useNotification';
const notificationOptionsLabelMap = {
all: 'All_messages',
mentions: 'Mentions',
nothing: 'Nothing',
};
const emailNotificationOptionsLabelMap = {
mentions: 'Email_Notification_Mode_All',
nothing: 'Email_Notification_Mode_Disabled',
};
const PreferencesNotificationsSection = () => {
const { t, i18n } = useTranslation();
const user = useUser();
const [notificationsPermission, setNotificationsPermission] = useState<NotificationPermission>();
const defaultDesktopNotifications = useSetting(
'Accounts_Default_User_Preferences_desktopNotifications',
) as keyof typeof notificationOptionsLabelMap;
const defaultMobileNotifications = useSetting(
'Accounts_Default_User_Preferences_pushNotifications',
) as keyof typeof notificationOptionsLabelMap;
const canChangeEmailNotification = useSetting('Accounts_AllowEmailNotifications');
const loginEmailEnabled = useSetting('Device_Management_Enable_Login_Emails');
const allowLoginEmailPreference = useSetting('Device_Management_Allow_Login_Email_preference');
const showNewLoginEmailPreference = loginEmailEnabled && allowLoginEmailPreference;
const showVideoConfMobileRinging = useSetting('VideoConf_Mobile_Ringing');
const showVoipMobileRinging = useSetting('VoIP_TeamCollab_Mobile_Ringing_Enabled');
const showMobileRinging = showVideoConfMobileRinging || showVoipMobileRinging;
const notify = useNotification();
const userEmailNotificationMode = useUserPreference('emailNotificationMode') as keyof typeof emailNotificationOptionsLabelMap;
useEffect(() => setNotificationsPermission(window.Notification && Notification.permission), []);
const onSendNotification = useCallback(() => {
notify({
payload: {
sender: { _id: 'rocket.cat', username: 'rocket.cat' },
rid: 'GENERAL',
} as INotificationDesktop['payload'],
title: t('Desktop_Notification_Test'),
text: t('This_is_a_desktop_notification'),
});
}, [notify, t]);
const onAskNotificationPermission = useCallback(() => {
window.Notification && Notification.requestPermission().then((val) => setNotificationsPermission(val));
}, []);
const notificationOptions = useMemo(
() => Object.entries(notificationOptionsLabelMap).map(([key, val]) => i18n.exists(val) && [key, t(val)]),
[i18n, t],
) as SelectOption[];
const desktopNotificationOptions = useMemo<SelectOption[]>((): SelectOption[] => {
const optionsCp = notificationOptions.slice();
optionsCp.unshift(['default', `${t('Default')} (${t(notificationOptionsLabelMap[defaultDesktopNotifications] as TranslationKey)})`]);
return optionsCp;
}, [defaultDesktopNotifications, notificationOptions, t]);
const mobileNotificationOptions = useMemo(() => {
const optionsCp = notificationOptions.slice();
optionsCp.unshift(['default', `${t('Default')} (${t(notificationOptionsLabelMap[defaultMobileNotifications] as TranslationKey)})`]);
return optionsCp;
}, [defaultMobileNotifications, notificationOptions, t]);
const emailNotificationOptions = useMemo(() => {
const options = Object.entries(emailNotificationOptionsLabelMap).map(
([key, val]) => i18n.exists(val) && [key, t(val)],
) as SelectOption[];
options.unshift(['default', `${t('Default')} (${t(emailNotificationOptionsLabelMap[userEmailNotificationMode] as TranslationKey)})`]);
return options;
}, [i18n, t, userEmailNotificationMode]);
const { control } = useFormContext();
const desktopNotificationsLabelId = useId();
const showCalendarPreference = user?.settings?.calendar?.outlook?.Enabled;
return (
<AccordionItem title={t('Notifications')}>
<FieldGroup>
<Field>
<FieldLabel>{t('Desktop_Notifications')}</FieldLabel>
<FieldRow>
{notificationsPermission === 'denied' && t('Desktop_Notifications_Disabled')}
{notificationsPermission === 'granted' && (
<Button primary onClick={onSendNotification} aria-labelledby={desktopNotificationsLabelId}>
{t('Test_Desktop_Notifications')}
</Button>
)}
{notificationsPermission !== 'denied' && notificationsPermission !== 'granted' && (
<Button primary onClick={onAskNotificationPermission} aria-labelledby={desktopNotificationsLabelId}>
{t('Enable_Desktop_Notifications')}
</Button>
)}
</FieldRow>
</Field>
<Field>
<FieldRow>
<FieldLabel>{t('Notification_RequireInteraction')}</FieldLabel>
<Controller
name='desktopNotificationRequireInteraction'
control={control}
render={({ field: { value, ...field } }) => <ToggleSwitch {...field} checked={value} />}
/>
</FieldRow>
<FieldHint>{t('Only_works_with_chrome_version_greater_50')}</FieldHint>
</Field>
<Field>
<FieldLabel>{t('Notification_Desktop_Default_For')}</FieldLabel>
<FieldRow>
<Controller
name='desktopNotifications'
control={control}
render={({ field }) => <Select {...field} options={desktopNotificationOptions} />}
/>
</FieldRow>
</Field>
<Field>
<FieldRow>
<FieldLabel>{t('Notification_Desktop_show_voice_calls')}</FieldLabel>
<Controller
name='desktopNotificationVoiceCalls'
control={control}
render={({ field: { value, ...field } }) => <ToggleSwitch {...field} checked={value} />}
/>
</FieldRow>
</Field>
<Field>
<FieldLabel>{t('Notification_Push_Default_For')}</FieldLabel>
<FieldRow>
<Controller
name='pushNotifications'
control={control}
render={({ field }) => <Select {...field} options={mobileNotificationOptions} />}
/>
</FieldRow>
</Field>
<Field>
<FieldLabel>{t('Email_Notification_Mode')}</FieldLabel>
<FieldRow>
<Controller
name='emailNotificationMode'
control={control}
render={({ field }) => <Select disabled={!canChangeEmailNotification} {...field} options={emailNotificationOptions} />}
/>
</FieldRow>
<FieldHint>
{canChangeEmailNotification && t('You_need_to_verifiy_your_email_address_to_get_notications')}
{!canChangeEmailNotification && t('Email_Notifications_Change_Disabled')}
</FieldHint>
</Field>
{showNewLoginEmailPreference && (
<Field>
<FieldRow>
<FieldLabel>{t('Receive_Login_Detection_Emails')}</FieldLabel>
<Controller
name='receiveLoginDetectionEmail'
control={control}
render={({ field: { value, ...field } }) => <ToggleSwitch {...field} checked={value} />}
/>
</FieldRow>
<FieldHint>{t('Receive_Login_Detection_Emails_Description')}</FieldHint>
</Field>
)}
{showCalendarPreference && (
<Field>
<FieldRow>
<FieldLabel>{t('Notify_Calendar_Events')}</FieldLabel>
<Controller
name='notifyCalendarEvents'
control={control}
render={({ field: { value, ...field } }) => <ToggleSwitch {...field} checked={value} />}
/>
</FieldRow>
</Field>
)}
{showMobileRinging && (
<Field>
<FieldRow>
<FieldLabel>{t('VideoConf_Mobile_Ringing')}</FieldLabel>
<Controller
name='enableMobileRinging'
control={control}
render={({ field: { value, ...field } }) => <ToggleSwitch {...field} checked={value} />}
/>
</FieldRow>
</Field>
)}
</FieldGroup>
</AccordionItem>
);
};
export default PreferencesNotificationsSection;