Skip to content

Commit

Permalink
Converted to hook and added MediaStream
Browse files Browse the repository at this point in the history
  • Loading branch information
Harsh-D-2004 committed Mar 7, 2025
1 parent 4ef57ec commit ac0c657
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 45 deletions.
32 changes: 0 additions & 32 deletions src/Utils/cameraPermissionHandler.ts

This file was deleted.

37 changes: 37 additions & 0 deletions src/Utils/useCamera.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { useCallback, useRef } from "react";
import { useTranslation } from "react-i18next";
import { toast } from "sonner";

export const useCamera = () => {
const toastShownRef = useRef(false);
const { t } = useTranslation();

const requestPermission = useCallback(
async (cameraFacingMode: string = "user") => {
try {
toastShownRef.current = false;
const constraints: MediaStreamConstraints = {
video: { facingMode: cameraFacingMode },
};

const mediaStream =
await navigator.mediaDevices.getUserMedia(constraints);

if (mediaStream == null) {
return { hasPermission: false, mediaStream: null };
}

return { hasPermission: true, mediaStream: mediaStream };
} catch (_error) {
if (!toastShownRef.current) {
toastShownRef.current = true;
toast.warning(t("camera_permission_denied"));
}
return { hasPermission: false, mediaStream: null };
}
},
[t],
);

return { requestPermission };
};
9 changes: 3 additions & 6 deletions src/components/Common/AvatarEditModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {

import useDragAndDrop from "@/hooks/useDragAndDrop";

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

interface Props {
title: string;
Expand Down Expand Up @@ -79,7 +79,7 @@ const AvatarEditModal = ({
);
const { t } = useTranslation();
const [isDragging, setIsDragging] = useState(false);
const { requestPermission } = handleCameraPermission();
const { requestPermission } = useCamera();

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

import useBreakpoints from "@/hooks/useBreakpoints";

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

export interface CameraCaptureDialogProps {
open: boolean;
Expand All @@ -28,7 +28,7 @@ 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 { requestPermission } = useCamera();
const [stream, setStream] = useState<MediaStream | null>(null);

const [cameraFacingMode, setCameraFacingMode] = useState(
Expand All @@ -47,16 +47,14 @@ export default function CameraCaptureDialog(props: CameraCaptureDialogProps) {
if (!open) return;

const getCameraStream = async () => {
const hasPermission = await requestPermission("camera", cameraFacingMode);
if (!hasPermission) {
const hasPermission = await requestPermission(cameraFacingMode);
if (!hasPermission.hasPermission) {
onOpenChange(false);
return;
}

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

0 comments on commit ac0c657

Please sign in to comment.