Skip to content

Commit

Permalink
main 🧊 add enabled for use permission
Browse files Browse the repository at this point in the history
  • Loading branch information
debabin committed Oct 31, 2024
1 parent ba06635 commit f8e5879
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/hooks/usePermission/usePermission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ export type UsePermissionName =
| 'push'
| 'speaker';

/** The use permission options type */
export interface UsePermissionOptions {
/** Whether the permission is enabled */
enabled: boolean;
}

/** The use permission return type */
export interface UsePermissionReturn {
/** The permission state */
Expand All @@ -41,9 +47,13 @@ export interface UsePermissionReturn {
* @example
* const { state, supported, query } = usePermission('microphone');
*/
export const usePermission = (permissionDescriptorName: UsePermissionName) => {
export const usePermission = (
permissionDescriptorName: UsePermissionName,
options?: UsePermissionOptions
) => {
const [state, setState] = useState<PermissionState>('prompt');
const supported = navigator && 'permissions' in navigator;
const enabled = options?.enabled ?? true;

const permissionDescriptor = { name: permissionDescriptorName };

Expand All @@ -61,13 +71,13 @@ export const usePermission = (permissionDescriptorName: UsePermissionName) => {
});

useEffect(() => {
if (!supported) return;
if (!supported || !enabled) return;
query();
window.addEventListener('change', query);
return () => {
window.removeEventListener('change', query);
};
}, [permissionDescriptorName]);
}, [permissionDescriptorName, enabled]);

return {
state,
Expand Down

0 comments on commit f8e5879

Please sign in to comment.