signal-desktop/ts/notifications/getStatus.ts

61 lines
1.3 KiB
TypeScript
Raw Normal View History

2018-05-02 22:03:26 +00:00
interface Environment {
isAppFocused: boolean;
isAudioNotificationEnabled: boolean;
isAudioNotificationSupported: boolean;
isEnabled: boolean;
numNotifications: number;
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 = (environment: Environment): Status => {
const type = ((): Type => {
if (!environment.isEnabled) {
return 'disabled';
}
const hasNotifications = environment.numNotifications > 0;
if (!hasNotifications) {
return 'noNotifications';
}
if (environment.isAppFocused) {
return 'appIsFocused';
}
if (environment.userSetting === 'off') {
return 'userSetting';
}
return 'ok';
})();
const shouldPlayNotificationSound =
environment.isAudioNotificationSupported &&
environment.isAudioNotificationEnabled;
const shouldShowNotifications = type === 'ok';
const shouldClearNotifications = type === 'appIsFocused';
return {
shouldClearNotifications,
shouldPlayNotificationSound,
shouldShowNotifications,
type,
};
};