From 4cb4432816c93492088f270926406a162f1473d5 Mon Sep 17 00:00:00 2001 From: Bokdol11859 <2019147551@yonsei.ac.kr> Date: Mon, 4 Mar 2024 20:37:04 +0900 Subject: [PATCH] fix: fix usePermission to not break on environments without navigator.permissions --- src/usePermission.ts | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/usePermission.ts b/src/usePermission.ts index 0aae85f55c..d7157c2040 100644 --- a/src/usePermission.ts +++ b/src/usePermission.ts @@ -1,7 +1,7 @@ import { useEffect, useState } from 'react'; import { noop, off, on } from './misc/util'; -export type IState = PermissionState | ''; +export type IState = PermissionState | 'unavailable' | ''; interface IPushPermissionDescriptor extends PermissionDescriptor { name: 'push'; @@ -39,14 +39,18 @@ const usePermission = (permissionDesc: IPermissionDescriptor): IState => { setState(() => permissionStatus?.state ?? ''); }; - navigator.permissions - .query(permissionDesc) - .then((status) => { - permissionStatus = status; - on(permissionStatus, 'change', onChange); - onChange(); - }) - .catch(noop); + if ('permissions' in navigator) { + navigator.permissions + .query(permissionDesc) + .then((status) => { + permissionStatus = status; + on(permissionStatus, 'change', onChange); + onChange(); + }) + .catch(noop); + } else { + setState('unavailable'); + } return () => { permissionStatus && off(permissionStatus, 'change', onChange);