Skip to content

Commit

Permalink
Updated to hook
Browse files Browse the repository at this point in the history
  • Loading branch information
Harsh-D-2004 committed Mar 4, 2025
1 parent e6f52e1 commit 51a437d
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 30 deletions.
44 changes: 25 additions & 19 deletions src/Utils/cameraPermissionHandler.ts
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 };
};
9 changes: 7 additions & 2 deletions src/components/Common/AvatarEditModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ const AvatarEditModal = ({
);
const { t } = useTranslation();
const [isDragging, setIsDragging] = useState(false);
const { requestPermission } = handleCameraPermission();

const handleSwitchCamera = useCallback(() => {
setConstraint(
Expand Down Expand Up @@ -394,9 +395,13 @@ const AvatarEditModal = ({
ref={webRef}
videoConstraints={constraint}
onUserMediaError={async () => {
await handleCameraPermission("user", () =>
setIsCameraOpen(false),
const hasPermission = await requestPermission(
"camera",
"user",
);
if (!hasPermission) {
setIsCameraOpen(false);
}
}}
/>
</>
Expand Down
30 changes: 21 additions & 9 deletions src/components/Files/CameraCaptureDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import {

import useBreakpoints from "@/hooks/useBreakpoints";

import { handleCameraPermission } from "@/Utils/cameraPermissionHandler";

export interface CameraCaptureDialogProps {
open: boolean;
onOpenChange: (open: boolean) => void;
Expand All @@ -26,6 +28,8 @@ export interface CameraCaptureDialogProps {
export default function CameraCaptureDialog(props: CameraCaptureDialogProps) {
const { open, onOpenChange, onCapture, onResetCapture, setPreview } = props;
const isLaptopScreen = useBreakpoints({ lg: true, default: false });
const { requestPermission } = handleCameraPermission();
const [stream, setStream] = useState<MediaStream | null>(null);

const [cameraFacingMode, setCameraFacingMode] = useState(
isLaptopScreen ? "user" : "environment",
Expand All @@ -41,17 +45,25 @@ export default function CameraCaptureDialog(props: CameraCaptureDialogProps) {

useEffect(() => {
if (!open) return;
let stream: MediaStream | null = null;

navigator.mediaDevices
.getUserMedia({ video: { facingMode: cameraFacingMode } })
.then((mediaStream) => {
stream = mediaStream;
})
.catch(() => {
toast.warning(t("camera_permission_denied"));
const getCameraStream = async () => {
const hasPermission = await requestPermission("camera", cameraFacingMode);
if (!hasPermission) {
onOpenChange(false);
});
return;
}

try {
const mediaStream = await navigator.mediaDevices.getUserMedia({
video: { facingMode: cameraFacingMode },
});
setStream(mediaStream);
} catch (error) {
console.error("Error accessing camera:", error);
}
};

getCameraStream();

return () => {
if (stream) {
Expand Down

0 comments on commit 51a437d

Please sign in to comment.