-
Notifications
You must be signed in to change notification settings - Fork 649
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e6f52e1
commit 51a437d
Showing
3 changed files
with
53 additions
and
30 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,24 +1,30 @@ | ||
import { useTranslation } from "react-i18next"; | ||
import { useCallback, useState } from "react"; | ||
import { toast } from "sonner"; | ||
|
||
const { t } = useTranslation(); | ||
export const handleCameraPermission = () => { | ||
const [toastShown, setToastShown] = useState(false); | ||
|
||
let toastShown = false; | ||
const requestPermission = useCallback( | ||
async (device: "camera", cameraFacingMode: string = "user") => { | ||
try { | ||
setToastShown(false); | ||
const constraints = | ||
device === "camera" | ||
? { video: { facingMode: cameraFacingMode } } | ||
: { audio: true }; | ||
|
||
export const handleCameraPermission = async ( | ||
cameraFacingMode: string, | ||
onPermissionDenied: () => void, | ||
) => { | ||
toastShown = false; | ||
try { | ||
await navigator.mediaDevices.getUserMedia({ | ||
video: { facingMode: cameraFacingMode }, | ||
}); | ||
} catch (_error) { | ||
if (!toastShown) { | ||
toastShown = true; | ||
toast.warning(t("camera_permission_denied")); | ||
} | ||
onPermissionDenied(); | ||
} | ||
await navigator.mediaDevices.getUserMedia(constraints); | ||
return true; | ||
} catch (_error) { | ||
if (!toastShown) { | ||
setToastShown(true); | ||
toast.warning(`${device} permission denied`); | ||
} | ||
return false; // Permission denied | ||
} | ||
}, | ||
[toastShown], | ||
); | ||
|
||
return { requestPermission }; | ||
}; |
This file contains 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 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