signal-desktop/ts/notifications/getStatus.ts

59 lines
1.1 KiB
TypeScript
Raw Normal View History

2018-05-02 22:03:26 +00:00
interface Environment {
isAppFocused: boolean;
isAudioNotificationEnabled: boolean;
isEnabled: boolean;
hasNotifications: boolean;
2018-05-02 22:03:26 +00:00
userSetting: UserSetting;
}
interface Status {
shouldClearNotifications: boolean;
shouldPlayNotificationSound: boolean;
shouldShowNotifications: boolean;
type: Type;
}
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,
};
};