Skip to content

Commit

Permalink
refactor: modify usePermission for compatible with TypeScript 4.5
Browse files Browse the repository at this point in the history
  • Loading branch information
fintechs committed Mar 1, 2024
1 parent ade8d39 commit 7b01217
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 32 deletions.
3 changes: 2 additions & 1 deletion docs/usePermission.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ React side-effect hook to query permission status of browser APIs.
import {usePermission} from 'react-use';

const Demo = () => {
const state = usePermission({ name: 'microphone' });
// type PermissionName = "geolocation" | "notifications" | "persistent-storage" | "push" | "screen-wake-lock" | "xr-spatial-tracking";
const state = usePermission({ name: 'notifications' });

return (
<pre>
Expand Down
38 changes: 8 additions & 30 deletions src/usePermission.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,10 @@
import { useEffect, useState } from 'react';
import { noop, off, on } from './misc/util';
import { useEffect, useState } from "react";

Check failure on line 1 in src/usePermission.ts

View workflow job for this annotation

GitHub Actions / Linting

Replace `"react"` with `'react'`
import { noop, off, on } from "./misc/util";

Check failure on line 2 in src/usePermission.ts

View workflow job for this annotation

GitHub Actions / Linting

Replace `"./misc/util"` with `'./misc/util'`

export type IState = PermissionState | '';
export type IState = PermissionState | "";

Check failure on line 4 in src/usePermission.ts

View workflow job for this annotation

GitHub Actions / Linting

Replace `""` with `''`

interface IPushPermissionDescriptor extends PermissionDescriptor {
name: 'push';
userVisibleOnly?: boolean;
}

interface IMidiPermissionDescriptor extends PermissionDescriptor {
name: 'midi';
sysex?: boolean;
}

interface IDevicePermissionDescriptor extends PermissionDescriptor {
name: 'camera' | 'microphone' | 'speaker';
deviceId?: string;
}

export type IPermissionDescriptor =
| PermissionDescriptor
| IPushPermissionDescriptor
| IMidiPermissionDescriptor
| IDevicePermissionDescriptor;

// const usePermission = <T extends PermissionDescriptor>(permissionDesc: T): IState => {
const usePermission = (permissionDesc: IPermissionDescriptor): IState => {
const [state, setState] = useState<IState>('');
const usePermission = (permissionDesc: PermissionDescriptor): IState => {
const [state, setState] = useState<IState>("");

Check failure on line 7 in src/usePermission.ts

View workflow job for this annotation

GitHub Actions / Linting

Replace `""` with `''`

useEffect(() => {
let mounted = true;
Expand All @@ -36,20 +14,20 @@ const usePermission = (permissionDesc: IPermissionDescriptor): IState => {
if (!mounted) {
return;
}
setState(() => permissionStatus?.state ?? '');
setState(() => permissionStatus?.state ?? "");

Check failure on line 17 in src/usePermission.ts

View workflow job for this annotation

GitHub Actions / Linting

Replace `""` with `''`
};

navigator.permissions
.query(permissionDesc)
.then((status) => {
permissionStatus = status;
on(permissionStatus, 'change', onChange);
on(permissionStatus, "change", onChange);

Check failure on line 24 in src/usePermission.ts

View workflow job for this annotation

GitHub Actions / Linting

Replace `"change"` with `'change'`
onChange();
})
.catch(noop);

return () => {
permissionStatus && off(permissionStatus, 'change', onChange);
permissionStatus && off(permissionStatus, "change", onChange);

Check failure on line 30 in src/usePermission.ts

View workflow job for this annotation

GitHub Actions / Linting

Replace `"change"` with `'change'`
mounted = false;
permissionStatus = null;
};
Expand Down
3 changes: 2 additions & 1 deletion stories/usePermission.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { usePermission } from '../src';
import ShowDocs from './util/ShowDocs';

const Demo = () => {
const state = usePermission({ name: 'microphone' });
// type PermissionName = "geolocation" | "notifications" | "persistent-storage" | "push" | "screen-wake-lock" | "xr-spatial-tracking";
const state = usePermission({ name: 'notifications' });

return <pre>{JSON.stringify(state, null, 2)}</pre>;
};
Expand Down

0 comments on commit 7b01217

Please sign in to comment.