forked from mattermost/mattermost
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchannel_notifications_modal.tsx
More file actions
596 lines (545 loc) · 29.5 KB
/
Copy pathchannel_notifications_modal.tsx
File metadata and controls
596 lines (545 loc) · 29.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import React, {useCallback, useState} from 'react';
import {Modal} from 'react-bootstrap';
import {FormattedMessage, useIntl} from 'react-intl';
import type {ValueType} from 'react-select';
import {BellOffOutlineIcon} from '@mattermost/compass-icons/components';
import type {Channel, ChannelMembership, ChannelNotifyProps} from '@mattermost/types/channels';
import type {UserNotifyProps, UserProfile} from '@mattermost/types/users';
import AlertBanner from 'components/alert_banner';
import CheckboxSettingItem from 'components/widgets/modals/components/checkbox_setting_item';
import CheckboxWithSelectSettingItem from 'components/widgets/modals/components/checkbox_with_select_item';
import ModalHeader from 'components/widgets/modals/components/modal_header';
import ModalSection from 'components/widgets/modals/components/modal_section';
import RadioSettingItem from 'components/widgets/modals/components/radio_setting_item';
import type {Option} from 'components/widgets/modals/components/react_select_item';
import {NotificationLevels, DesktopSound, IgnoreChannelMentions} from 'utils/constants';
import {convertDesktopSoundNotifyPropFromUserToDesktop, DesktopNotificationSounds, getValueOfNotificationSoundsSelect, stopTryNotificationRing, tryNotificationSound} from 'utils/notification_sounds';
import ResetToDefaultButton, {SectionName} from './reset_to_default_button';
import utils from './utils';
import type {PropsFromRedux} from './index';
import './channel_notifications_modal.scss';
export type Props = PropsFromRedux & {
/**
* Function that is called when the modal has been hidden and should be removed
*/
onExited: () => void;
/**
* Object with info about current channel
*/
channel: Channel;
/**
* Object with info about current user
*/
currentUser: UserProfile;
};
export default function ChannelNotificationsModal(props: Props) {
const {formatMessage} = useIntl();
const [show, setShow] = useState(true);
const [serverError, setServerError] = useState('');
const [settings, setSettings] = useState(getStateFromNotifyProps(props.currentUser.notify_props, props.channelMember?.notify_props));
const [desktopAndMobileSettingsDifferent, setDesktopAndMobileSettingDifferent] = useState<boolean>(areDesktopAndMobileSettingsDifferent(
props.collapsedReplyThreads,
getInitialValuesOfChannelNotifyProps(NotificationLevels.ALL, props?.channelMember?.notify_props?.desktop, props.currentUser.notify_props.desktop),
getInitialValuesOfChannelNotifyProps(NotificationLevels.ALL, props?.channelMember?.notify_props?.desktop_threads, props.currentUser.notify_props.desktop_threads),
getInitialValuesOfChannelNotifyProps(NotificationLevels.ALL, props?.channelMember?.notify_props?.push, props.currentUser.notify_props.push),
getInitialValuesOfChannelNotifyProps(NotificationLevels.ALL, props?.channelMember?.notify_props?.push_threads, props.currentUser.notify_props.push_threads),
));
function handleHide() {
setShow(false);
}
const handleChange = useCallback((values: Record<string, string>) => {
setSettings((prevSettings) => ({...prevSettings, ...values}));
}, []);
function handleUseSameMobileSettingsAsDesktopCheckboxChange(value: boolean) {
const newValueOfSettings = {...settings};
const newValueOfDesktopAndMobileSettingsDifferent = !value;
if (newValueOfDesktopAndMobileSettingsDifferent === false) {
newValueOfSettings.push = settings.desktop;
newValueOfSettings.push_threads = settings.desktop_threads;
} else {
newValueOfSettings.push = getInitialValuesOfChannelNotifyProps(NotificationLevels.ALL, props.channelMember?.notify_props?.push, props.currentUser?.notify_props?.push);
newValueOfSettings.push_threads = getInitialValuesOfChannelNotifyProps(NotificationLevels.ALL, props.channelMember?.notify_props?.push_threads, props.currentUser?.notify_props?.push_threads);
}
setSettings(newValueOfSettings);
setDesktopAndMobileSettingDifferent(newValueOfDesktopAndMobileSettingsDifferent);
}
function handleResetToDefaultClicked(channelNotifyPropsDefaultedToUserNotifyProps: ChannelMembership['notify_props'], sectionName: SectionName) {
if (sectionName === SectionName.Mobile) {
const desktopAndMobileSettingsDifferent = areDesktopAndMobileSettingsDifferent(
props.collapsedReplyThreads,
settings.desktop,
settings.desktop_threads,
channelNotifyPropsDefaultedToUserNotifyProps.push,
channelNotifyPropsDefaultedToUserNotifyProps.push_threads,
);
setDesktopAndMobileSettingDifferent(desktopAndMobileSettingsDifferent);
}
setSettings({...settings, ...channelNotifyPropsDefaultedToUserNotifyProps});
}
function handleSave() {
const channelNotifyProps = createChannelNotifyPropsFromSelectedSettings(props.currentUser.notify_props, settings, desktopAndMobileSettingsDifferent);
props.actions.updateChannelNotifyProps(props.currentUser.id, props.channel.id, channelNotifyProps).then((value) => {
const {error} = value;
if (error) {
setServerError(error.message);
} else {
handleHide();
}
});
}
const muteOrIgnoreSectionContent = (
<>
<CheckboxSettingItem
inputFieldTitle={
<FormattedMessage
id='channel_notifications.muteChannelTitle'
defaultMessage='Mute channel'
/>
}
description={formatMessage({
id: 'channel_notifications.muteChannelDesc',
defaultMessage: 'Turns off notifications for this channel. You\'ll still see badges if you\'re mentioned.',
})}
inputFieldValue={settings.mark_unread === 'mention'}
inputFieldData={utils.MuteChannelInputFieldData}
handleChange={(e) => handleChange({mark_unread: e ? 'mention' : 'all'})}
/>
<CheckboxSettingItem
inputFieldTitle={
<FormattedMessage
id='channel_notifications.ignoreMentionsTitle'
defaultMessage='Ignore mentions for @channel, @here and @all'
/>
}
description={formatMessage({
id: 'channel_notifications.ignoreMentionsDesc',
defaultMessage: 'When enabled, @channel, @here and @all will not trigger mentions or mention notifications in this channel',
})}
inputFieldValue={settings.ignore_channel_mentions === 'on'}
inputFieldData={utils.IgnoreMentionsInputFieldData}
handleChange={(e) => handleChange({ignore_channel_mentions: e ? 'on' : 'off'})}
/>
</>
);
const handleChangeForMessageNotificationSoundSelect = (selectedOption: ValueType<Option>) => {
stopTryNotificationRing();
if (selectedOption && 'value' in selectedOption) {
handleChange({desktop_notification_sound: ((selectedOption as Option).value)});
tryNotificationSound(selectedOption.value);
}
};
const desktopNotificationsSectionContent = (
<>
<RadioSettingItem
title={formatMessage({
id: 'channel_notifications.NotifyMeTitle',
defaultMessage: 'Notify me about…',
})}
inputFieldValue={settings.desktop || ''}
inputFieldData={utils.desktopNotificationInputFieldData(props.currentUser.notify_props.desktop)}
handleChange={(e) => handleChange({desktop: e.target.value})}
/>
{props.collapsedReplyThreads && settings.desktop === 'mention' &&
<CheckboxSettingItem
title={formatMessage({
id: 'channel_notifications.ThreadsReplyTitle',
defaultMessage: 'Thread reply notifications',
})}
inputFieldValue={settings.desktop_threads === 'all'}
inputFieldData={utils.DesktopReplyThreadsInputFieldData}
inputFieldTitle={
<FormattedMessage
id='channel_notifications.checkbox.threadsReplyTitle'
defaultMessage="Notify me about replies to threads I\'m following"
/>
}
handleChange={(e) => handleChange({desktop_threads: e ? 'all' : 'mention'})}
/>
}
{settings.desktop !== 'none' && (
<CheckboxWithSelectSettingItem
title={formatMessage({
id: 'channel_notifications.desktopNotifications.title',
defaultMessage: 'Sounds',
})}
checkboxFieldTitle={
<FormattedMessage
id='channel_notifications.desktopNotifications.soundEnable'
defaultMessage='Message notification sounds'
/>
}
checkboxFieldValue={settings.desktop_sound === DesktopSound.ON}
checkboxFieldData={utils.desktopNotificationSoundsCheckboxFieldData}
handleCheckboxChange={(isChecked) => handleChange({desktop_sound: isChecked ? DesktopSound.ON : DesktopSound.OFF})}
selectFieldData={utils.desktopNotificationSoundsSelectFieldData}
selectFieldValue={getValueOfNotificationSoundsSelect(settings.desktop_notification_sound)}
isSelectDisabled={settings.desktop_sound !== 'on'}
selectPlaceholder={formatMessage({
id: 'channel_notifications.desktopNotifications.soundSelectPlaceholder',
defaultMessage: 'Select a sound',
})}
handleSelectChange={handleChangeForMessageNotificationSoundSelect}
/>
)}
</>
);
const mobileNotificationsSectionContent = (
<>
<CheckboxSettingItem
inputFieldTitle={
<FormattedMessage
id='channel_notifications.checkbox.sameMobileSettingsDesktop'
defaultMessage='Use the same notification settings as desktop'
/>
}
inputFieldValue={!desktopAndMobileSettingsDifferent}
inputFieldData={utils.sameMobileSettingsDesktopInputFieldData}
handleChange={handleUseSameMobileSettingsAsDesktopCheckboxChange}
/>
{desktopAndMobileSettingsDifferent && (
<>
<RadioSettingItem
dataTestId='mobile-notify-me-radio-section'
title={formatMessage({
id: 'channel_notifications.NotifyMeTitle',
defaultMessage: 'Notify me about…',
})}
inputFieldValue={settings.push || ''}
inputFieldData={utils.mobileNotificationInputFieldData(props.currentUser.notify_props.push)}
handleChange={(e) => handleChange({push: e.target.value})}
/>
{props.collapsedReplyThreads && settings.push === 'mention' &&
<CheckboxSettingItem
dataTestId='mobile-reply-threads-checkbox-section'
title={formatMessage({
id: 'channel_notifications.ThreadsReplyTitle',
defaultMessage: 'Thread reply notifications',
})}
inputFieldTitle={
<FormattedMessage
id='channel_notifications.checkbox.threadsReplyTitle'
defaultMessage="Notify me about replies to threads I\'m following"
/>
}
inputFieldValue={settings.push_threads === 'all'}
inputFieldData={utils.MobileReplyThreadsInputFieldData}
handleChange={(e) => handleChange({push_threads: e ? 'all' : 'mention'})}
/>}
</>
)}
</>
);
const autoFollowThreadsSectionContent = (
<CheckboxSettingItem
inputFieldTitle={
<FormattedMessage
id='channel_notifications.checkbox.autoFollowThreadsTitle'
defaultMessage='Automatically follow threads in this channel'
/>
}
inputFieldValue={settings.channel_auto_follow_threads === 'on'}
inputFieldData={utils.AutoFollowThreadsInputFieldData}
handleChange={(e) => handleChange({channel_auto_follow_threads: e ? 'on' : 'off'})}
/>
);
const desktopAndMobileNotificationSectionContent = settings.mark_unread === 'all' ? (
<>
<div className='channel-notifications-settings-modal__divider'/>
<ModalSection
title={formatMessage({
id: 'channel_notifications.desktopNotificationsTitle',
defaultMessage: 'Desktop Notifications',
})}
titleSuffix={
<ResetToDefaultButton
sectionName={SectionName.Desktop}
userNotifyProps={props.currentUser.notify_props}
userSelectedChannelNotifyProps={settings}
onClick={handleResetToDefaultClicked}
/>
}
description={formatMessage({
id: 'channel_notifications.desktopNotificationsDesc',
defaultMessage: 'Available on Chrome, Edge, Firefox, and the Mattermost Desktop App.',
})}
content={desktopNotificationsSectionContent}
/>
<div className='channel-notifications-settings-modal__divider'/>
<ModalSection
title={formatMessage({
id: 'channel_notifications.mobileNotificationsTitle',
defaultMessage: 'Mobile Notifications',
})}
titleSuffix={
<ResetToDefaultButton
sectionName={SectionName.Mobile}
userNotifyProps={props.currentUser.notify_props}
userSelectedChannelNotifyProps={settings}
onClick={handleResetToDefaultClicked}
/>
}
description={formatMessage({
id: 'channel_notifications.mobileNotificationsDesc',
defaultMessage: 'Notification alerts are pushed to your mobile device when there is activity in Mattermost.',
})}
content={mobileNotificationsSectionContent}
/>
</>
) : (
<AlertBanner
id='channelNotificationsMutedBanner'
mode='info'
variant='app'
customIcon={
<BellOffOutlineIcon
size={24}
color={'currentColor'}
/>
}
title={
<FormattedMessage
id='channel_notifications.alertBanner.title'
defaultMessage='This channel is muted'
/>
}
message={
<FormattedMessage
id='channel_notifications.alertBanner.description'
defaultMessage='All other notification preferences for this channel are disabled'
/>
}
/>
);
return (
<Modal
dialogClassName='a11y__modal channel-notifications-settings-modal'
show={show}
onHide={handleHide}
onExited={props.onExited}
role='none'
aria-labelledby='channelNotificationModalLabel'
style={{display: 'flex', placeItems: 'center'}}
>
<ModalHeader
id={'channelNotificationModalLabel'}
title={formatMessage({
id: 'channel_notifications.preferences',
defaultMessage: 'Notification Preferences',
})}
subtitle={props.channel.display_name}
handleClose={handleHide}
/>
<main className='channel-notifications-settings-modal__body'>
<ModalSection
title={formatMessage({
id: 'channel_notifications.muteAndIgnore',
defaultMessage: 'Mute or ignore',
})}
content={muteOrIgnoreSectionContent}
/>
{desktopAndMobileNotificationSectionContent}
{props.collapsedReplyThreads &&
<>
<div className='channel-notifications-settings-modal__divider'/>
<ModalSection
title={formatMessage({
id: 'channel_notifications.autoFollowThreadsTitle',
defaultMessage: 'Follow all threads in this channel',
})}
description={formatMessage({
id: 'channel_notifications.autoFollowThreadsDesc',
defaultMessage: 'When enabled, all new replies in this channel will be automatically followed and will appear in your Threads view.',
})}
content={autoFollowThreadsSectionContent}
/>
</>
}
</main>
<footer className='channel-notifications-settings-modal__footer'>
{serverError &&
<span
role='alert'
className='channel-notifications-settings-modal__server-error'
>
{serverError}
</span>
}
<button
className='btn btn-tertiary btn-md'
onClick={handleHide}
>
<FormattedMessage
id='generic_btn.cancel'
defaultMessage='Cancel'
/>
</button>
<button
className='btn btn-primary btn-md'
onClick={handleSave}
>
<FormattedMessage
id='generic_btn.save'
defaultMessage='Save'
/>
</button>
</footer>
</Modal>
);
}
function getStateFromNotifyProps(currentUserNotifyProps: UserNotifyProps, channelMemberNotifyProps?: ChannelMembership['notify_props']): Required<Omit<ChannelMembership['notify_props'], 'email'>> {
return {
mark_unread: channelMemberNotifyProps?.mark_unread ?? NotificationLevels.ALL,
ignore_channel_mentions: getInitialValuesOfIgnoreChannelMentions(channelMemberNotifyProps?.mark_unread, channelMemberNotifyProps?.ignore_channel_mentions, currentUserNotifyProps?.channel),
desktop: getInitialValuesOfChannelNotifyProps<ChannelMembership['notify_props']['desktop']>(NotificationLevels.ALL, channelMemberNotifyProps?.desktop, currentUserNotifyProps?.desktop),
desktop_threads: getInitialValuesOfChannelNotifyProps<ChannelMembership['notify_props']['desktop_threads']>(NotificationLevels.ALL, channelMemberNotifyProps?.desktop_threads, currentUserNotifyProps?.desktop_threads),
desktop_sound: getInitialValuesOfChannelNotifyProps<ChannelMembership['notify_props']['desktop_sound']>(DesktopSound.ON, channelMemberNotifyProps?.desktop_sound, convertDesktopSoundNotifyPropFromUserToDesktop(currentUserNotifyProps?.desktop_sound)),
desktop_notification_sound: getInitialValuesOfChannelNotifyProps<ChannelMembership['notify_props']['desktop_notification_sound']>(DesktopNotificationSounds.BING, channelMemberNotifyProps?.desktop_notification_sound, currentUserNotifyProps?.desktop_notification_sound),
push: getInitialValuesOfChannelNotifyProps<ChannelMembership['notify_props']['push']>(NotificationLevels.ALL, channelMemberNotifyProps?.push, currentUserNotifyProps?.push),
push_threads: getInitialValuesOfChannelNotifyProps<ChannelMembership['notify_props']['push_threads']>(NotificationLevels.ALL, channelMemberNotifyProps?.push_threads, currentUserNotifyProps?.push_threads),
channel_auto_follow_threads: channelMemberNotifyProps?.channel_auto_follow_threads ?? 'off',
};
}
export function getInitialValuesOfChannelNotifyProps<KeyInNotifyProps>(defaultValue: NonNullable<KeyInNotifyProps>, channelMemberNotifyProp: KeyInNotifyProps | undefined = undefined, userNotifyProp: KeyInNotifyProps | undefined = undefined) {
let value = defaultValue;
// Check if channel_member's notify_prop is defined for the selected notify_prop
if (channelMemberNotifyProp) {
// If channel_member's notify_prop is default and user's notify_prop is defined, we should use user's notify_prop
if (channelMemberNotifyProp === NotificationLevels.DEFAULT && userNotifyProp) {
value = userNotifyProp;
} else {
// Otherwise, we should use channel_member's notify_prop as is
value = channelMemberNotifyProp;
}
} else if (userNotifyProp) {
// If channel_member's notify_prop is not defined and user's notify_prop is defined, we should use user's notify_prop as is
value = userNotifyProp;
}
return value;
}
export function getInitialValuesOfIgnoreChannelMentions(
markUnread: ChannelMembership['notify_props']['mark_unread'],
ignoreChannelMentions: ChannelMembership['notify_props']['ignore_channel_mentions'],
userNotifyPropForChannel: UserNotifyProps['channel'],
): NonNullable<ChannelMembership['notify_props']['ignore_channel_mentions']> {
let ignoreChannelMentionsDefault: ChannelNotifyProps['ignore_channel_mentions'] = IgnoreChannelMentions.OFF;
if (markUnread === NotificationLevels.MENTION || (userNotifyPropForChannel && userNotifyPropForChannel === 'false')) {
ignoreChannelMentionsDefault = IgnoreChannelMentions.ON;
}
if (ignoreChannelMentions) {
if (ignoreChannelMentions === IgnoreChannelMentions.DEFAULT) {
return ignoreChannelMentionsDefault;
}
return ignoreChannelMentions;
}
return ignoreChannelMentionsDefault;
}
export function createChannelNotifyPropsFromSelectedSettings(
userNotifyProps: UserNotifyProps,
savedChannelNotifyProps: ChannelMembership['notify_props'],
desktopAndMobileSettingsDifferent: boolean,
) {
const channelNotifyProps: ChannelMembership['notify_props'] = {
mark_unread: savedChannelNotifyProps.mark_unread,
ignore_channel_mentions: savedChannelNotifyProps.ignore_channel_mentions,
channel_auto_follow_threads: savedChannelNotifyProps.channel_auto_follow_threads,
};
if (savedChannelNotifyProps.desktop === userNotifyProps.desktop) {
channelNotifyProps.desktop = NotificationLevels.DEFAULT;
} else {
channelNotifyProps.desktop = savedChannelNotifyProps.desktop;
}
// Check if USER's desktop_thread setting are defined
if (userNotifyProps?.desktop_threads?.length) {
// If USER's desktop_thread setting is same as CHANNEL's new desktop_threads setting, we should set it to default
if (userNotifyProps.desktop_threads === savedChannelNotifyProps.desktop_threads) {
channelNotifyProps.desktop_threads = NotificationLevels.DEFAULT;
} else {
// Otherwise, we should use the CHANNEL's new desktop_threads setting as is
channelNotifyProps.desktop_threads = savedChannelNotifyProps.desktop_threads;
}
} else if (savedChannelNotifyProps.desktop_threads === NotificationLevels.MENTION || savedChannelNotifyProps.desktop_threads === NotificationLevels.DEFAULT) {
// If USER's desktop_thread setting is not defined and CHANNEL's new desktop_threads setting is MENTION or DEFAULT, then save it as default
channelNotifyProps.desktop_threads = NotificationLevels.DEFAULT;
} else {
// Otherwise, we should use the CHANNEL's new desktop_threads setting as is
channelNotifyProps.desktop_threads = savedChannelNotifyProps.desktop_threads;
}
if (convertDesktopSoundNotifyPropFromUserToDesktop(userNotifyProps?.desktop_sound) === savedChannelNotifyProps.desktop_sound) {
channelNotifyProps.desktop_sound = DesktopSound.DEFAULT;
} else {
channelNotifyProps.desktop_sound = savedChannelNotifyProps.desktop_sound;
}
// Check if USER's desktop_notification_sound setting is defined
if (userNotifyProps?.desktop_notification_sound?.length) {
// If USER's desktop_notification_sound setting is same as CHANNEL's new desktop_notification_sound setting, we should set it to default
if (userNotifyProps.desktop_notification_sound === savedChannelNotifyProps.desktop_notification_sound) {
channelNotifyProps.desktop_notification_sound = DesktopNotificationSounds.DEFAULT;
} else {
// Otherwise, we should use the CHANNEL's new desktop_notification_sound setting as is
channelNotifyProps.desktop_notification_sound = savedChannelNotifyProps.desktop_notification_sound;
}
} else if (savedChannelNotifyProps.desktop_notification_sound === DesktopNotificationSounds.BING || savedChannelNotifyProps.desktop_notification_sound === DesktopNotificationSounds.DEFAULT) {
// If USER's desktop_notification_sound setting is not defined and CHANNEL's new desktop_notification_sound setting is either BING or DEFAULT, then save it as default
channelNotifyProps.desktop_notification_sound = DesktopNotificationSounds.DEFAULT;
} else {
// Otherwise, we should use the CHANNEL's new desktop_notification_sound setting as is
channelNotifyProps.desktop_notification_sound = savedChannelNotifyProps.desktop_notification_sound;
}
if (savedChannelNotifyProps.push === userNotifyProps.push) {
channelNotifyProps.push = NotificationLevels.DEFAULT;
} else {
channelNotifyProps.push = savedChannelNotifyProps.push;
}
// Check if USER's push_threads setting are defined
if (userNotifyProps?.push_threads?.length) {
// If USER's push_threads setting is same as CHANNEL's new push_threads setting, we should set it to default
if (userNotifyProps.push_threads === savedChannelNotifyProps.push_threads) {
channelNotifyProps.push_threads = NotificationLevels.DEFAULT;
} else {
// Otherwise, we should use the CHANNEL's new push_threads setting as is
channelNotifyProps.push_threads = savedChannelNotifyProps.push_threads;
}
} else if (savedChannelNotifyProps.push_threads === NotificationLevels.MENTION || savedChannelNotifyProps.push_threads === NotificationLevels.DEFAULT) {
// If USER's push_threads setting is not defined and CHANNEL's new push_threads setting is MENTION or DEFAULT, then save it as default
channelNotifyProps.push_threads = NotificationLevels.DEFAULT;
} else {
// Otherwise, we should use the CHANNEL's new push_threads setting as is
channelNotifyProps.push_threads = savedChannelNotifyProps.push_threads;
}
// If desktop and mobile settings are checked to be same, then same settings should be applied to push and push_threads
// as that of desktop and desktop_threads
if (desktopAndMobileSettingsDifferent === false) {
// If desktop is set to default, it means it is synced to the user's notification settings.
// Since we checked the box to use the same settings for mobile, we need to set channel's mobile to match channel's desktop.
// Setting mobile to default would sync it to the user's notification settings, which we want to avoid.
if (channelNotifyProps.desktop === NotificationLevels.DEFAULT) {
channelNotifyProps.push = userNotifyProps.desktop;
} else {
// Otherwise, we should use the CHANNEL's desktop setting as is to match mobile settings
channelNotifyProps.push = channelNotifyProps.desktop;
}
if (channelNotifyProps.desktop_threads === NotificationLevels.DEFAULT) {
channelNotifyProps.push_threads = userNotifyProps.desktop_threads;
} else {
channelNotifyProps.push_threads = channelNotifyProps.desktop_threads;
}
}
return channelNotifyProps;
}
/**
* Check's if channel's notification settings for desktop and mobile are different
*/
export function areDesktopAndMobileSettingsDifferent(
isCollapsedThreadsEnabled: boolean,
desktop: UserNotifyProps['desktop'],
desktopThreads: UserNotifyProps['desktop_threads'],
push?: UserNotifyProps['push'],
pushThreads?: UserNotifyProps['push_threads'],
): boolean {
if (push === NotificationLevels.DEFAULT || push === desktop) {
return isCollapsedThreadsEnabled && desktopThreads !== pushThreads;
}
return true;
}