Skip to content

Commit

Permalink
MOSIP-34887 (#331)
Browse files Browse the repository at this point in the history
* fix: handle camera is not plugged in mid-verification

Signed-off-by: bunsy-0900 <[email protected]>

* fix: fix camera icon not hiding

Signed-off-by: bunsy-0900 <[email protected]>

* fix: fix webcam icon still showing

Signed-off-by: bunsy-0900 <[email protected]>

* fix: revert deprecated changes

Signed-off-by: bunsy-0900 <[email protected]>

---------

Signed-off-by: bunsy-0900 <[email protected]>
  • Loading branch information
bunsy-0900 authored Aug 30, 2024
1 parent 183b940 commit 4ff4c99
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useEffect } from "react";
import { useTranslation } from "react-i18next";

import { useL2Hash } from "~hooks/useL2Hash";
Expand All @@ -22,6 +23,12 @@ export const IdentityVerificationStatus = ({
const retriableErrorCodes =
settings.configs["status.request.retry.error.codes"].split(",");

useEffect(() => {
if (window.videoLocalStream) {
window.videoLocalStream.getTracks().forEach(track => track.stop());
}
}, [])

// isError occurs when the query encounters a network error or the request limit attempts is reached
const {
data: identityVerificationStatus,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,35 @@ export const VerificationScreen = ({
setStep(EkycVerificationStep.SlotCheckingScreen);
};

const stopEkycVerificationProcess = useCallback(() => {
if (connected) {
unsubscribe();
client.deactivate();
}
setStep(EkycVerificationStep.IdentityVerificationStatus);
}, [])

useEffect(() => {
const checkWebcamInputSource = () => {
if (webcamRef && webcamRef.current) {
const webcamStreamState = (webcamRef.current as Webcam).stream?.active;
if (webcamStreamState === false) {
stopEkycVerificationProcess();
}
}
}

// checking camera permission in every 1 second
const webcamInputSourceCheckInterval = setInterval(
checkWebcamInputSource,
1000
);

return () => {
clearInterval(webcamInputSourceCheckInterval);
};
}, []);

// timer useEffect
useEffect(() => {
if (timer && timer > 0) {
Expand Down Expand Up @@ -444,6 +473,8 @@ export const VerificationScreen = ({
};
}, []);

const handleWebcamUserMediaError = (error: string | DOMException) => stopEkycVerificationProcess();

return alertConfig !== null ? (
<EkycStatusAlert config={alertConfig} />
) : (
Expand Down Expand Up @@ -479,6 +510,7 @@ export const VerificationScreen = ({
}
videoConstraints={videoConstraints}
screenshotFormat="image/jpeg"
onUserMediaError={handleWebcamUserMediaError}
/>
</div>
{/* temporary button for socket connection */}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export const VideoPreview = ({
*/
const handleContinue = (e: any) => {
e.preventDefault();
webcamRef.current = null;
setStep(EkycVerificationStep.SlotCheckingScreen);
};

Expand All @@ -78,23 +79,37 @@ export const VideoPreview = ({
};

useEffect(() => {
const cameraPermissionCheck = () => {
navigator.mediaDevices
.getUserMedia({ video: true })
.then(cameraPermissionAllowed)
.catch(cameraPermissionDenied);
};

// checking camera permission in every 1 second
const cameraPermissionCheckInterval = setInterval(
cameraPermissionCheck,
1000
);
return () => clearInterval(cameraPermissionCheckInterval);

return () => {
clearInterval(cameraPermissionCheckInterval);
if (window.videoLocalStream) {
window.videoLocalStream.getTracks().forEach(track => track.stop());
}
};
}, [permissionGranted]);

// if camera permission granted then set the state
const cameraPermissionAllowed = (stream: MediaStream) => {
const cameraPermissionAllowed = useCallback((stream: MediaStream) => {
window.videoLocalStream = stream;
if (!permissionGranted) {
setPermissionGranted(true);
}
};
}, []);

// if camera permission denied then set the state
const cameraPermissionDenied = (error: any) => {
const cameraPermissionDenied = useCallback((error: any) => {
if (permissionGranted) {
setPermissionGranted(false);
if (error.name === "NotReadableError") {
Expand All @@ -111,16 +126,7 @@ export const VideoPreview = ({
});
}
}
};

// check the camera permission, if camera permission granted then set the state
// it will work for chrome & firefox as well
const cameraPermissionCheck = () => {
navigator.mediaDevices
.getUserMedia({ video: true })
.then(cameraPermissionAllowed)
.catch(cameraPermissionDenied);
};
}, []);

// video preview div, it will show the video preview if camera permission granted
// otherwise it will show a card asking for camera permission
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import { isEqual } from "lodash";
import { create } from "zustand";
import { devtools } from "zustand/middleware";

import { Error, KycProvider, KycProviderDetail, SignupHashCode } from "~typings/types";
import {
Error,
KycProvider,
KycProviderDetail,
SignupHashCode,
} from "~typings/types";

export enum EkycVerificationStep {
VerificationSteps,
Expand Down Expand Up @@ -157,7 +162,8 @@ export const errorBannerMessageSelector = (

export const setErrorBannerMessageSelector = (
state: EkycVerificationStore
): EkycVerificationStore["setErrorBannerMessage"] => state.setErrorBannerMessage;
): EkycVerificationStore["setErrorBannerMessage"] =>
state.setErrorBannerMessage;

export const slotIdSelector = (
state: EkycVerificationStore
Expand Down
8 changes: 8 additions & 0 deletions signup-ui/src/react-app-env.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
/// <reference types="react-scripts" />

declare global {
interface Window {
videoLocalStream: MediaStream
}
}

export {}

0 comments on commit 4ff4c99

Please sign in to comment.