Minor refactor to simplify the getStateFromNotifyProps (#24635)

Этот коммит содержится в:
Daniel Espino García
2023-10-05 16:25:08 +02:00
коммит произвёл GitHub
родитель d61364a24a
Коммит 4409dc083b

Просмотреть файл

@@ -105,11 +105,16 @@ const getDefaultPushNotifyLevel = (currentUserNotifyProps: UserNotifyProps, isGM
return NotificationLevels.ALL; return NotificationLevels.ALL;
}; };
const getDefaultPushThreadsNotifyLevel = (currentUserNotifyProps: UserNotifyProps): Exclude<ChannelMemberNotifyProps['push_threads'], undefined> => { const getDefaultPushThreadsNotifyLevel = (currentUserNotifyProps: UserNotifyProps, isGM: boolean): Exclude<ChannelMemberNotifyProps['push_threads'], undefined> => {
if (currentUserNotifyProps?.push_threads) { if (currentUserNotifyProps?.push_threads) {
if (currentUserNotifyProps.push_threads === 'default') { if (currentUserNotifyProps.push_threads === 'default') {
return NotificationLevels.ALL; return NotificationLevels.ALL;
} }
if (isGM && currentUserNotifyProps.push_threads === NotificationLevels.MENTION) {
return NotificationLevels.ALL;
}
return currentUserNotifyProps.push_threads; return currentUserNotifyProps.push_threads;
} }
return NotificationLevels.ALL; return NotificationLevels.ALL;
@@ -119,7 +124,7 @@ export default class ChannelNotificationsModal extends React.PureComponent<Props
constructor(props: Props) { constructor(props: Props) {
super(props); super(props);
const channelNotifyProps = props.channelMember && props.channelMember.notify_props; const channelNotifyProps = props.channelMember?.notify_props;
this.state = { this.state = {
show: true, show: true,
@@ -173,7 +178,7 @@ export default class ChannelNotificationsModal extends React.PureComponent<Props
if ( if (
pushNotifyLevel === getDefaultPushNotifyLevel(currentUserNotifyProps, this.isGM()) && pushNotifyLevel === getDefaultPushNotifyLevel(currentUserNotifyProps, this.isGM()) &&
pushThreadsNotifyLevel === getDefaultPushThreadsNotifyLevel(currentUserNotifyProps) pushThreadsNotifyLevel === getDefaultPushThreadsNotifyLevel(currentUserNotifyProps, this.isGM())
) { ) {
return true; return true;
} }
@@ -183,28 +188,26 @@ export default class ChannelNotificationsModal extends React.PureComponent<Props
getStateFromNotifyProps(currentUserNotifyProps: UserNotifyProps, channelMemberNotifyProps?: ChannelMemberNotifyProps) { getStateFromNotifyProps(currentUserNotifyProps: UserNotifyProps, channelMemberNotifyProps?: ChannelMemberNotifyProps) {
let ignoreChannelMentionsDefault: ChannelNotifyProps['ignore_channel_mentions'] = IgnoreChannelMentions.OFF; let ignoreChannelMentionsDefault: ChannelNotifyProps['ignore_channel_mentions'] = IgnoreChannelMentions.OFF;
let desktopNotifyLevelDefault: ChannelNotifyProps['desktop'] = getDefaultDesktopNotificationLevel(currentUserNotifyProps, this.isGM()); const desktopNotifyLevelDefault: ChannelNotifyProps['desktop'] = getDefaultDesktopNotificationLevel(currentUserNotifyProps, this.isGM());
let pushNotifyLevelDefault: ChannelMemberNotifyProps['push'] = getDefaultPushNotifyLevel(currentUserNotifyProps, this.isGM()); const pushNotifyLevelDefault: ChannelMemberNotifyProps['push'] = getDefaultPushNotifyLevel(currentUserNotifyProps, this.isGM());
let pushThreadsNotifyLevelDefault: ChannelMemberNotifyProps['push_threads'] = getDefaultPushThreadsNotifyLevel(currentUserNotifyProps); const pushThreadsNotifyLevelDefault: ChannelMemberNotifyProps['push_threads'] = getDefaultPushThreadsNotifyLevel(currentUserNotifyProps, this.isGM());
if (channelMemberNotifyProps?.desktop) { const channelDesktopNotifyProps = channelMemberNotifyProps?.desktop || NotificationLevels.DEFAULT;
if (channelMemberNotifyProps.desktop !== 'default') { let desktopNotifyLevel = desktopNotifyLevelDefault;
desktopNotifyLevelDefault = channelMemberNotifyProps.desktop; if (channelDesktopNotifyProps !== NotificationLevels.DEFAULT) {
} else if (this.isGM()) { desktopNotifyLevel = channelDesktopNotifyProps;
desktopNotifyLevelDefault = NotificationLevels.ALL;
}
} }
if (channelMemberNotifyProps?.push) {
if (channelMemberNotifyProps.push !== 'default') { const channelPushNotifyProps = channelMemberNotifyProps?.push || NotificationLevels.DEFAULT;
pushNotifyLevelDefault = channelMemberNotifyProps.push; let pushNotifyLevel = pushNotifyLevelDefault;
} if (channelPushNotifyProps !== 'default') {
pushNotifyLevel = channelPushNotifyProps;
} }
if (channelMemberNotifyProps?.push_threads) {
if (channelMemberNotifyProps.push_threads !== 'default') { const channelPushThreadsNotifyProps = channelMemberNotifyProps?.push_threads || NotificationLevels.DEFAULT;
pushThreadsNotifyLevelDefault = channelMemberNotifyProps.push_threads; let pushThreadsNotifyLevel = pushThreadsNotifyLevelDefault;
} else if (this.isGM()) { if (channelPushThreadsNotifyProps !== 'default') {
pushThreadsNotifyLevelDefault = NotificationLevels.ALL; pushThreadsNotifyLevel = channelPushThreadsNotifyProps;
}
} }
if (channelMemberNotifyProps?.mark_unread === NotificationLevels.MENTION || (currentUserNotifyProps.channel && currentUserNotifyProps.channel === 'false')) { if (channelMemberNotifyProps?.mark_unread === NotificationLevels.MENTION || (currentUserNotifyProps.channel && currentUserNotifyProps.channel === 'false')) {
@@ -217,13 +220,13 @@ export default class ChannelNotificationsModal extends React.PureComponent<Props
} }
return { return {
desktopNotifyLevel: desktopNotifyLevelDefault, desktopNotifyLevel,
desktopSound: channelMemberNotifyProps?.desktop_sound || getDefaultDesktopSound(currentUserNotifyProps), desktopSound: channelMemberNotifyProps?.desktop_sound || getDefaultDesktopSound(currentUserNotifyProps),
desktopNotifySound: channelMemberNotifyProps?.desktop_notification_sound || getDefaultDesktopNotificationSound(currentUserNotifyProps), desktopNotifySound: channelMemberNotifyProps?.desktop_notification_sound || getDefaultDesktopNotificationSound(currentUserNotifyProps),
desktopThreadsNotifyLevel: channelMemberNotifyProps?.desktop_threads || getDefaultDesktopThreadsNotifyLevel(currentUserNotifyProps), desktopThreadsNotifyLevel: channelMemberNotifyProps?.desktop_threads || getDefaultDesktopThreadsNotifyLevel(currentUserNotifyProps),
markUnreadNotifyLevel: channelMemberNotifyProps?.mark_unread || NotificationLevels.ALL, markUnreadNotifyLevel: channelMemberNotifyProps?.mark_unread || NotificationLevels.ALL,
pushNotifyLevel: pushNotifyLevelDefault, pushNotifyLevel,
pushThreadsNotifyLevel: pushThreadsNotifyLevelDefault, pushThreadsNotifyLevel,
ignoreChannelMentions, ignoreChannelMentions,
channelAutoFollowThreads: channelMemberNotifyProps?.channel_auto_follow_threads || ChannelAutoFollowThreads.OFF, channelAutoFollowThreads: channelMemberNotifyProps?.channel_auto_follow_threads || ChannelAutoFollowThreads.OFF,
}; };
@@ -277,7 +280,7 @@ export default class ChannelNotificationsModal extends React.PureComponent<Props
const userPushNotificationDefaults = { const userPushNotificationDefaults = {
pushNotifyLevel: getDefaultPushNotifyLevel(currentUserNotifyProps, this.isGM()), pushNotifyLevel: getDefaultPushNotifyLevel(currentUserNotifyProps, this.isGM()),
pushThreadsNotifyLevel: getDefaultPushThreadsNotifyLevel(currentUserNotifyProps), pushThreadsNotifyLevel: getDefaultPushThreadsNotifyLevel(currentUserNotifyProps, this.isGM()),
}; };
this.setState(userPushNotificationDefaults); this.setState(userPushNotificationDefaults);