refactor: remove deprecated `KonchatNotification` (#35589)
parent
bda3fcd14f
commit
dcff0fcbee
@ -1,24 +1 @@ |
||||
import { ReactiveVar } from 'meteor/reactive-var'; |
||||
|
||||
declare global { |
||||
// eslint-disable-next-line @typescript-eslint/naming-convention
|
||||
interface NotificationEventMap { |
||||
reply: { response: string }; |
||||
} |
||||
} |
||||
|
||||
class KonchatNotification { |
||||
public notificationStatus = new ReactiveVar<NotificationPermission | undefined>(undefined); |
||||
|
||||
public getDesktopPermission() { |
||||
if (window.Notification && Notification.permission !== 'granted') { |
||||
return Notification.requestPermission((status) => { |
||||
this.notificationStatus.set(status); |
||||
}); |
||||
} |
||||
} |
||||
} |
||||
|
||||
const instance = new KonchatNotification(); |
||||
|
||||
export { instance as KonchatNotification }; |
||||
// KonchatNotification in memoriam
|
||||
|
||||
@ -0,0 +1,19 @@ |
||||
import { useCallback, useSyncExternalStore } from 'react'; |
||||
|
||||
import { notificationManager } from '../../lib/notificationManager'; |
||||
|
||||
export const useNotificationAllowed = (): boolean => { |
||||
const allowed = useSyncExternalStore( |
||||
useCallback( |
||||
(callback): (() => void) => |
||||
notificationManager.on('change', () => { |
||||
notificationManager.allowed = Notification.permission === 'granted'; |
||||
callback(); |
||||
}), |
||||
[], |
||||
), |
||||
(): boolean => notificationManager.allowed, |
||||
); |
||||
|
||||
return allowed; |
||||
}; |
||||
@ -0,0 +1,21 @@ |
||||
import { useCallback } from 'react'; |
||||
|
||||
import { notificationManager } from '../../lib/notificationManager'; |
||||
|
||||
export const useNotificationPermission = () => { |
||||
const requestPermission = useCallback(async () => { |
||||
const response = await Notification.requestPermission(); |
||||
notificationManager.allowed = response === 'granted'; |
||||
notificationManager.emit('change'); |
||||
|
||||
const notifications = await navigator.permissions.query({ name: 'notifications' }); |
||||
notifications.onchange = () => { |
||||
notificationManager.allowed = notifications.state === 'granted'; |
||||
notificationManager.emit('change'); |
||||
}; |
||||
}, []); |
||||
|
||||
if ('Notification' in window) { |
||||
requestPermission(); |
||||
} |
||||
}; |
||||
@ -0,0 +1,6 @@ |
||||
import { Emitter } from '@rocket.chat/emitter'; |
||||
|
||||
class NotificationPermissionEmitter extends Emitter { |
||||
allowed: boolean; |
||||
} |
||||
export const notificationManager = new NotificationPermissionEmitter(); |
||||
Loading…
Reference in new issue