Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: modify usePermission for compatible with TypeScript 4.5 #2547

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@
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