notification.ts 982 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. import { promisified } from './tauri'
  2. export interface Options {
  3. title: string
  4. body?: string
  5. icon?: string
  6. }
  7. export type PartialOptions = Omit<Options, 'title'>
  8. export type Permission = 'granted' | 'denied' | 'default'
  9. async function isPermissionGranted(): Promise<boolean | null> {
  10. if (window.Notification.permission !== 'default') {
  11. return await Promise.resolve(window.Notification.permission === 'granted')
  12. }
  13. return await promisified({
  14. cmd: 'isNotificationPermissionGranted'
  15. })
  16. }
  17. async function requestPermission(): Promise<Permission> {
  18. return await window.Notification.requestPermission()
  19. }
  20. function sendNotification(options: Options | string): void {
  21. if (typeof options === 'string') {
  22. // eslint-disable-next-line no-new
  23. new window.Notification(options)
  24. } else {
  25. // eslint-disable-next-line no-new
  26. new window.Notification(options.title, options)
  27. }
  28. }
  29. export {
  30. sendNotification,
  31. requestPermission,
  32. isPermissionGranted
  33. }