-
Notifications
You must be signed in to change notification settings - Fork 2
FCE-2803: Add permissions helpers #481
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
Merged
MiloszFilimowski
merged 8 commits into
main
from
mfilimowski/fce-2803-permission-helpers
Feb 26, 2026
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e8ce6c6
add permissions polyfill
MiloszFilimowski 35f951a
update example with the new hook
MiloszFilimowski aac49d5
implement copilot suggestions
MiloszFilimowski a01bd3b
fix docs
MiloszFilimowski d61043e
add missing jsdoc
MiloszFilimowski bc843a3
add checks
MiloszFilimowski 15ec7fe
implement nitpick
MiloszFilimowski 9c5a0ff
Merge branch 'main' into mfilimowski/fce-2803-permission-helpers
MiloszFilimowski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
examples/mobile-client/fishjam-chat/hooks/useMediaPermissions.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| import { useCallback, useEffect, useRef, useState } from "react"; | ||
| import { AppState, Linking } from "react-native"; | ||
| import { | ||
| useCameraPermissions, | ||
| useMicrophonePermissions, | ||
| } from "@fishjam-cloud/react-native-client"; | ||
|
|
||
| export function useMediaPermissions() { | ||
| const [queryCamera, requestCamera] = useCameraPermissions(); | ||
| const [queryMicrophone, requestMicrophone] = useMicrophonePermissions(); | ||
| const [permissionsGranted, setPermissionsGranted] = useState<boolean | null>( | ||
| null | ||
| ); | ||
| const hasRequested = useRef(false); | ||
|
|
||
| const checkPermissions = useCallback(async () => { | ||
| let cam = await queryCamera(); | ||
| let mic = await queryMicrophone(); | ||
|
|
||
| if (!hasRequested.current) { | ||
| hasRequested.current = true; | ||
| if (cam !== "granted") cam = await requestCamera(); | ||
| if (mic !== "granted") mic = await requestMicrophone(); | ||
| } | ||
|
|
||
| setPermissionsGranted(cam === "granted" && mic === "granted"); | ||
| }, [queryCamera, queryMicrophone, requestCamera, requestMicrophone]); | ||
MiloszFilimowski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| useEffect(() => { | ||
| checkPermissions(); | ||
| }, [checkPermissions]); | ||
|
|
||
| useEffect(() => { | ||
| const subscription = AppState.addEventListener("change", (state) => { | ||
| if (state === "active") { | ||
| checkPermissions(); | ||
| } | ||
| }); | ||
| return () => subscription.remove(); | ||
| }, [checkPermissions]); | ||
|
|
||
| const openSettings = useCallback(() => Linking.openSettings(), []); | ||
|
|
||
| return { permissionsGranted, openSettings }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| import { permissions } from '@fishjam-cloud/react-native-webrtc'; | ||
| import { useCallback } from 'react'; | ||
|
|
||
| /** | ||
| * The current status of a device permission. | ||
| * | ||
| * - `'granted'` – the user has granted the permission. | ||
| * - `'denied'` – the user has denied the permission. | ||
| * - `'prompt'` – the user has not yet been asked (or the permission can be requested again). | ||
| */ | ||
| export type PermissionStatus = 'granted' | 'denied' | 'prompt'; | ||
|
|
||
| function usePermission( | ||
| name: 'camera' | 'microphone', | ||
| ): [query: () => Promise<PermissionStatus>, request: () => Promise<PermissionStatus>] { | ||
| const query = useCallback(async (): Promise<PermissionStatus> => { | ||
| return (await permissions.query({ name })) as PermissionStatus; | ||
| }, [name]); | ||
|
|
||
| const request = useCallback(async (): Promise<PermissionStatus> => { | ||
| await permissions.request({ name }); | ||
| return (await permissions.query({ name })) as PermissionStatus; | ||
| }, [name]); | ||
|
|
||
| return [query, request]; | ||
| } | ||
|
|
||
| /** | ||
| * Hook for querying and requesting camera permission on the device. | ||
| * | ||
| * @returns A tuple of `[query, request]`: | ||
| * - `query` – checks the current camera permission status without prompting the user. | ||
| * - `request` – triggers the native permission dialog and returns the resulting status. | ||
| * | ||
| * @example | ||
| * ```tsx | ||
| * const [queryCameraPermission, requestCameraPermission] = useCameraPermissions(); | ||
| * | ||
| * const status = await queryCameraPermission(); | ||
| * if (status !== 'granted') { | ||
| * await requestCameraPermission(); | ||
| * } | ||
| * ``` | ||
| */ | ||
| export function useCameraPermissions() { | ||
| return usePermission('camera'); | ||
| } | ||
|
|
||
| /** | ||
| * Hook for querying and requesting microphone permission on the device. | ||
| * | ||
| * @returns A tuple of `[query, request]`: | ||
| * - `query` – checks the current microphone permission status without prompting the user. | ||
| * - `request` – triggers the native permission dialog and returns the resulting status. | ||
| * | ||
| * @example | ||
| * ```tsx | ||
| * const [queryMicPermission, requestMicPermission] = useMicrophonePermissions(); | ||
| * | ||
| * const status = await queryMicPermission(); | ||
| * if (status !== 'granted') { | ||
| * await requestMicPermission(); | ||
| * } | ||
| * ``` | ||
| */ | ||
| export function useMicrophonePermissions() { | ||
| return usePermission('microphone'); | ||
| } | ||
MiloszFilimowski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import { permissions } from '@fishjam-cloud/react-native-webrtc'; | ||
|
|
||
| export const patchGetUserMediaWithPermissionWarnings = () => { | ||
| const original = globalThis.navigator?.mediaDevices?.getUserMedia; | ||
| if (!original) return; | ||
|
|
||
| const boundOriginal = original.bind(globalThis.navigator.mediaDevices); | ||
|
|
||
| globalThis.navigator.mediaDevices.getUserMedia = async (constraints?: MediaStreamConstraints) => { | ||
| try { | ||
| const [cameraStatus, micStatus] = await Promise.all([ | ||
| constraints?.video ? permissions.query({ name: 'camera' }) : null, | ||
| constraints?.audio ? permissions.query({ name: 'microphone' }) : null, | ||
| ]); | ||
|
|
||
| if (cameraStatus && cameraStatus !== 'granted') { | ||
| console.warn(`Attempting to access camera with permission status: "${cameraStatus}".`); | ||
| } | ||
| if (micStatus && micStatus !== 'granted') { | ||
| console.warn(`Attempting to access microphone with permission status: "${micStatus}".`); | ||
| } | ||
| } catch (error) { | ||
| console.warn('Failed to check permissions before getUserMedia', error); | ||
| } | ||
|
|
||
| return boundOriginal(constraints); | ||
| }; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.