signal-desktop/ts/notifications/getStatus.ts

62 lines
1.2 KiB
TypeScript
Raw Normal View History

// Copyright 2018-2021 Signal Messenger, LLC
2020-10-30 20:34:04 +00:00
// SPDX-License-Identifier: AGPL-3.0-only
type Environment = {
2018-05-02 22:03:26 +00:00
isAppFocused: boolean;
isAudioNotificationEnabled: boolean;
isEnabled: boolean;
hasNotifications: boolean;
2018-05-02 22:03:26 +00:00
userSetting: UserSetting;
};
2018-05-02 22:03:26 +00:00
type Status = {
2018-05-02 22:03:26 +00:00
shouldClearNotifications: boolean;
shouldPlayNotificationSound: boolean;
shouldShowNotifications: boolean;
type: Type;
};
2018-05-02 22:03:26 +00:00
type UserSetting = 'off' | 'count' | 'name' | 'message';
type Type =
| 'ok'
| 'disabled'
| 'appIsFocused'
| 'noNotifications'
| 'userSetting';
export const getStatus = ({
isAppFocused,
isAudioNotificationEnabled,
isEnabled,
hasNotifications,
userSetting,
}: Environment): Status => {
2018-05-02 22:03:26 +00:00
const type = ((): Type => {
if (!isEnabled) {
2018-05-02 22:03:26 +00:00
return 'disabled';
}
if (!hasNotifications) {
return 'noNotifications';
}
if (isAppFocused) {
2018-05-02 22:03:26 +00:00
return 'appIsFocused';
}
if (userSetting === 'off') {
2018-05-02 22:03:26 +00:00
return 'userSetting';
}
return 'ok';
})();
return {
shouldClearNotifications: type === 'appIsFocused',
shouldPlayNotificationSound: isAudioNotificationEnabled,
shouldShowNotifications: type === 'ok',
2018-05-02 22:03:26 +00:00
type,
};
};