@smileid/web-components 10.0.6 → 11.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +15 -15
- package/dist/README.md +15 -0
- package/dist/components/README.md +14 -0
- package/dist/components/document/src/README.md +111 -0
- package/dist/components/document/src/document-capture/README.md +90 -0
- package/dist/components/document/src/document-capture-instructions/README.md +56 -0
- package/dist/components/document/src/document-capture-review/README.md +79 -0
- package/dist/components/selfie/README.md +225 -0
- package/dist/components/smart-camera-web/src/README.md +207 -0
- package/dist/domain/camera/src/README.md +38 -0
- package/dist/domain/file-upload/README.md +35 -0
- package/dist/esm/{DocumentCaptureScreens-BjATTDqu.js → DocumentCaptureScreens-DmH2JZDA.js} +3 -3
- package/dist/esm/DocumentCaptureScreens-DmH2JZDA.js.map +1 -0
- package/dist/esm/EndUserConsent-D4fd1ovG.js.map +1 -1
- package/dist/esm/Navigation-CTjK6tLU.js.map +1 -1
- package/dist/esm/PoweredBySmileId-CxbaihMu.js.map +1 -1
- package/dist/esm/SelfieCaptureScreens-DbdN2zNk.js +7901 -0
- package/dist/esm/SelfieCaptureScreens-DbdN2zNk.js.map +1 -0
- package/dist/esm/SignaturePad-C7MtmT8m.js.map +1 -1
- package/dist/esm/TotpConsent-CQU5jQi4.js.map +1 -1
- package/dist/esm/combobox.js.map +1 -1
- package/dist/esm/document.js +1 -1
- package/dist/esm/main.js +2 -2
- package/dist/esm/{package-CZlW6BZn.js → package-bgeQiff6.js} +2 -2
- package/dist/esm/package-bgeQiff6.js.map +1 -0
- package/dist/esm/selfie.js +1 -1
- package/dist/esm/smart-camera-web.js +3 -3
- package/dist/esm/smart-camera-web.js.map +1 -1
- package/dist/esm/styles-BOEZtbuc.js.map +1 -1
- package/dist/package-lock.json +4948 -0
- package/dist/package.json +59 -0
- package/dist/smart-camera-web.js +72 -98
- package/dist/smart-camera-web.js.gz +0 -0
- package/dist/smart-camera-web.js.map +1 -1
- package/dist/src/components/combobox/src/index.js +2 -0
- package/dist/src/components/combobox/src/index.js.map +7 -0
- package/dist/src/components/document/src/index.js +2 -0
- package/dist/src/components/document/src/index.js.map +7 -0
- package/dist/src/components/end-user-consent/src/index.js +14 -0
- package/dist/src/components/end-user-consent/src/index.js.map +7 -0
- package/dist/src/components/selfie/src/index.js +2 -0
- package/dist/src/components/selfie/src/index.js.map +7 -0
- package/dist/src/components/signature-pad/src/index.js +10 -0
- package/dist/src/components/signature-pad/src/index.js.map +7 -0
- package/dist/src/components/smart-camera-web/src/SmartCameraWeb.js +2 -0
- package/dist/src/components/smart-camera-web/src/SmartCameraWeb.js.map +7 -0
- package/dist/src/components/totp-consent/src/index.js +14 -0
- package/dist/src/components/totp-consent/src/index.js.map +7 -0
- package/dist/src/index.js.map +7 -0
- package/dist/styles/README.md +3 -0
- package/dist/types/combobox.d.ts +19 -19
- package/dist/types/document.d.ts +19 -19
- package/dist/types/end-user-consent.d.ts +19 -19
- package/dist/types/main.d.ts +24 -20
- package/dist/types/navigation.d.ts +19 -19
- package/dist/types/selfie.d.ts +19 -19
- package/dist/types/signature-pad.d.ts +19 -19
- package/dist/types/smart-camera-web.d.ts +19 -19
- package/dist/types/totp-consent.d.ts +19 -19
- package/lib/components/README.md +14 -14
- package/lib/components/attribution/PoweredBySmileId.js +42 -42
- package/lib/components/camera-permission/CameraPermission.js +139 -139
- package/lib/components/camera-permission/CameraPermission.stories.js +27 -27
- package/lib/components/combobox/src/Combobox.js +589 -589
- package/lib/components/combobox/src/index.js +1 -1
- package/lib/components/document/src/DocumentCaptureScreens.js +410 -409
- package/lib/components/document/src/DocumentCaptureScreens.stories.js +57 -57
- package/lib/components/document/src/README.md +111 -111
- package/lib/components/document/src/document-capture/DocumentCapture.js +760 -760
- package/lib/components/document/src/document-capture/DocumentCapture.stories.js +78 -78
- package/lib/components/document/src/document-capture/README.md +90 -90
- package/lib/components/document/src/document-capture/index.js +3 -3
- package/lib/components/document/src/document-capture-instructions/DocumentCaptureInstructions.js +545 -545
- package/lib/components/document/src/document-capture-instructions/DocumentCaptureInstructions.stories.js +24 -24
- package/lib/components/document/src/document-capture-instructions/README.md +56 -56
- package/lib/components/document/src/document-capture-instructions/index.js +3 -3
- package/lib/components/document/src/document-capture-review/DocumentCaptureReview.js +360 -360
- package/lib/components/document/src/document-capture-review/DocumentCaptureReview.stories.js +24 -24
- package/lib/components/document/src/document-capture-review/README.md +79 -79
- package/lib/components/document/src/document-capture-review/index.js +3 -3
- package/lib/components/document/src/index.js +3 -3
- package/lib/components/end-user-consent/src/EndUserConsent.js +795 -795
- package/lib/components/end-user-consent/src/EndUserConsent.stories.js +29 -29
- package/lib/components/end-user-consent/src/index.js +4 -4
- package/lib/components/navigation/src/Navigation.js +171 -171
- package/lib/components/navigation/src/Navigation.stories.js +24 -24
- package/lib/components/navigation/src/index.js +3 -3
- package/lib/components/selfie/README.md +225 -225
- package/lib/components/selfie/src/SelfieCaptureScreens.js +420 -431
- package/lib/components/selfie/src/SelfieCaptureScreens.stories.js +29 -29
- package/lib/components/selfie/src/index.js +3 -3
- package/lib/components/selfie/src/selfie-capture/SelfieCapture.js +1099 -1084
- package/lib/components/selfie/src/selfie-capture/SelfieCapture.stories.js +36 -36
- package/lib/components/selfie/src/selfie-capture/index.js +3 -3
- package/lib/components/selfie/src/selfie-capture-instructions/SelfieCaptureInstructions.js +689 -689
- package/lib/components/selfie/src/selfie-capture-instructions/SelfieCaptureInstructions.stories.js +23 -23
- package/lib/components/selfie/src/selfie-capture-instructions/index.js +3 -3
- package/lib/components/selfie/src/selfie-capture-review/SelfieCaptureReview.js +209 -209
- package/lib/components/selfie/src/selfie-capture-review/SelfieCaptureReview.stories.js +24 -24
- package/lib/components/selfie/src/selfie-capture-review/index.js +3 -3
- package/lib/components/selfie/src/selfie-capture-wrapper/SelfieCaptureWrapper.tsx +256 -239
- package/lib/components/selfie/src/selfie-capture-wrapper/index.ts +1 -1
- package/lib/components/selfie/src/smartselfie-capture/OvalProgress.tsx +81 -81
- package/lib/components/selfie/src/smartselfie-capture/SmartSelfieCapture.tsx +265 -283
- package/lib/components/selfie/src/smartselfie-capture/components/AlertDisplay.tsx +34 -34
- package/lib/components/selfie/src/smartselfie-capture/components/CameraPreview.tsx +97 -97
- package/lib/components/selfie/src/smartselfie-capture/components/CaptureControls.tsx +78 -76
- package/lib/components/selfie/src/smartselfie-capture/components/index.ts +3 -3
- package/lib/components/selfie/src/smartselfie-capture/constants.ts +23 -23
- package/lib/components/selfie/src/smartselfie-capture/hooks/index.ts +2 -2
- package/lib/components/selfie/src/smartselfie-capture/hooks/useCamera.ts +238 -238
- package/lib/components/selfie/src/smartselfie-capture/hooks/useFaceCapture.ts +618 -617
- package/lib/components/selfie/src/smartselfie-capture/index.ts +1 -1
- package/lib/components/selfie/src/smartselfie-capture/utils/alertMessages.ts +13 -13
- package/lib/components/selfie/src/smartselfie-capture/utils/canvas.ts +105 -105
- package/lib/components/selfie/src/smartselfie-capture/utils/faceDetection.ts +129 -129
- package/lib/components/selfie/src/smartselfie-capture/utils/imageCapture.ts +64 -64
- package/lib/components/selfie/src/smartselfie-capture/utils/index.ts +4 -4
- package/lib/components/selfie/src/smartselfie-capture/utils/mediapipeManager.ts +77 -77
- package/lib/components/signature-pad/package-lock.json +3009 -3009
- package/lib/components/signature-pad/package.json +30 -30
- package/lib/components/signature-pad/src/SignaturePad.js +484 -484
- package/lib/components/signature-pad/src/SignaturePad.stories.js +32 -32
- package/lib/components/signature-pad/src/index.js +3 -3
- package/lib/components/smart-camera-web/src/README.md +206 -206
- package/lib/components/smart-camera-web/src/SmartCameraWeb.js +305 -305
- package/lib/components/smart-camera-web/src/SmartCameraWeb.stories.js +57 -57
- package/lib/components/totp-consent/src/TotpConsent.js +949 -949
- package/lib/components/totp-consent/src/index.js +4 -4
- package/lib/domain/camera/src/README.md +38 -38
- package/lib/domain/camera/src/SmartCamera.js +109 -109
- package/lib/domain/constants/src/Constants.js +27 -27
- package/lib/domain/file-upload/README.md +35 -35
- package/lib/domain/file-upload/src/SmartFileUpload.js +65 -65
- package/lib/styles/README.md +3 -3
- package/lib/styles/src/styles.js +372 -372
- package/lib/styles/src/typography.js +52 -52
- package/package.json +111 -112
- package/dist/esm/DocumentCaptureScreens-BjATTDqu.js.map +0 -1
- package/dist/esm/SelfieCaptureScreens-UUzZzl1A.js +0 -11361
- package/dist/esm/SelfieCaptureScreens-UUzZzl1A.js.map +0 -1
- package/dist/esm/package-CZlW6BZn.js.map +0 -1
|
@@ -1,238 +1,238 @@
|
|
|
1
|
-
import { useRef, useState, useEffect } from 'preact/hooks';
|
|
2
|
-
|
|
3
|
-
export const useCamera = (initialFacingMode: CameraFacingMode = 'user') => {
|
|
4
|
-
const videoRef = useRef<HTMLVideoElement>(null);
|
|
5
|
-
const streamRef = useRef<MediaStream | null>(null);
|
|
6
|
-
const [facingMode, setFacingMode] = useState(initialFacingMode);
|
|
7
|
-
const [agentSupported, setAgentSupported] = useState(false);
|
|
8
|
-
const onCameraSwitchCallbackRef = useRef<(() => void) | null>(null);
|
|
9
|
-
const isSwitchingCameraRef = useRef(false);
|
|
10
|
-
const timeoutIdsRef = useRef<Set<NodeJS.Timeout>>(new Set());
|
|
11
|
-
|
|
12
|
-
const registerCameraSwitchCallback = (callback: () => void) => {
|
|
13
|
-
onCameraSwitchCallbackRef.current = callback;
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
useEffect(() => {
|
|
17
|
-
const video = videoRef.current;
|
|
18
|
-
if (!video) return undefined;
|
|
19
|
-
|
|
20
|
-
const handleVideoReady = () => {
|
|
21
|
-
if (isSwitchingCameraRef.current && onCameraSwitchCallbackRef.current) {
|
|
22
|
-
const timeoutId = setTimeout(() => {
|
|
23
|
-
onCameraSwitchCallbackRef.current?.();
|
|
24
|
-
isSwitchingCameraRef.current = false;
|
|
25
|
-
timeoutIdsRef.current.delete(timeoutId);
|
|
26
|
-
}, 100);
|
|
27
|
-
timeoutIdsRef.current.add(timeoutId);
|
|
28
|
-
}
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
video.addEventListener('loadedmetadata', handleVideoReady);
|
|
32
|
-
|
|
33
|
-
return () => {
|
|
34
|
-
video.removeEventListener('loadedmetadata', handleVideoReady);
|
|
35
|
-
timeoutIdsRef.current.forEach((timeoutId) => clearTimeout(timeoutId));
|
|
36
|
-
timeoutIdsRef.current.clear();
|
|
37
|
-
};
|
|
38
|
-
}, [videoRef.current?.src]);
|
|
39
|
-
|
|
40
|
-
useEffect(
|
|
41
|
-
() => () => {
|
|
42
|
-
timeoutIdsRef.current.forEach((timeoutId) => clearTimeout(timeoutId));
|
|
43
|
-
timeoutIdsRef.current.clear();
|
|
44
|
-
},
|
|
45
|
-
[],
|
|
46
|
-
);
|
|
47
|
-
|
|
48
|
-
const startCamera = async (
|
|
49
|
-
targetFacingMode?: CameraFacingMode,
|
|
50
|
-
callback?: (cameraName?: string) => void,
|
|
51
|
-
) => {
|
|
52
|
-
try {
|
|
53
|
-
if (streamRef.current) {
|
|
54
|
-
streamRef.current.getTracks().forEach((track) => track.stop());
|
|
55
|
-
streamRef.current = null;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
if (videoRef.current) {
|
|
59
|
-
videoRef.current.srcObject = null;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
const stream = await navigator.mediaDevices.getUserMedia({
|
|
63
|
-
video: { facingMode: targetFacingMode || facingMode },
|
|
64
|
-
});
|
|
65
|
-
streamRef.current = stream;
|
|
66
|
-
|
|
67
|
-
const track = stream.getVideoTracks()[0];
|
|
68
|
-
const settings = track.getSettings();
|
|
69
|
-
const actualFacingMode = settings.facingMode as
|
|
70
|
-
| CameraFacingMode
|
|
71
|
-
| undefined;
|
|
72
|
-
|
|
73
|
-
const requestedFacingMode = targetFacingMode || facingMode;
|
|
74
|
-
|
|
75
|
-
if (actualFacingMode && actualFacingMode !== requestedFacingMode) {
|
|
76
|
-
setFacingMode(actualFacingMode);
|
|
77
|
-
} else if (actualFacingMode && actualFacingMode !== facingMode) {
|
|
78
|
-
setFacingMode(actualFacingMode);
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
const devices = await navigator.mediaDevices.enumerateDevices();
|
|
82
|
-
const videoDevice = devices.find(
|
|
83
|
-
(device) =>
|
|
84
|
-
device.kind === 'videoinput' &&
|
|
85
|
-
stream.getVideoTracks()[0].getSettings().deviceId === device.deviceId,
|
|
86
|
-
);
|
|
87
|
-
|
|
88
|
-
callback?.(videoDevice?.label);
|
|
89
|
-
|
|
90
|
-
if (videoRef.current) {
|
|
91
|
-
videoRef.current.srcObject = stream;
|
|
92
|
-
await videoRef.current.play();
|
|
93
|
-
// Video ready callback will be handled by useEffect
|
|
94
|
-
}
|
|
95
|
-
} catch (error) {
|
|
96
|
-
console.error('Failed to start camera:', error);
|
|
97
|
-
}
|
|
98
|
-
};
|
|
99
|
-
|
|
100
|
-
const switchCamera = async () => {
|
|
101
|
-
const newFacingMode = facingMode === 'user' ? 'environment' : 'user';
|
|
102
|
-
isSwitchingCameraRef.current = true;
|
|
103
|
-
|
|
104
|
-
const previousFacingMode = facingMode;
|
|
105
|
-
try {
|
|
106
|
-
setFacingMode(newFacingMode);
|
|
107
|
-
if (streamRef.current) {
|
|
108
|
-
streamRef.current.getTracks().forEach((track) => track.stop());
|
|
109
|
-
streamRef.current = null;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
await startCamera(newFacingMode);
|
|
113
|
-
} catch (error) {
|
|
114
|
-
setFacingMode(previousFacingMode);
|
|
115
|
-
isSwitchingCameraRef.current = false;
|
|
116
|
-
|
|
117
|
-
try {
|
|
118
|
-
await startCamera(previousFacingMode);
|
|
119
|
-
} catch (restoreError) {
|
|
120
|
-
console.error('Failed to restore previous camera:', restoreError);
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
};
|
|
124
|
-
|
|
125
|
-
const detectBrowserEngine = () => {
|
|
126
|
-
const userAgent = navigator.userAgent.toLowerCase();
|
|
127
|
-
|
|
128
|
-
const isGecko =
|
|
129
|
-
userAgent.includes('firefox') ||
|
|
130
|
-
(userAgent.includes('gecko') &&
|
|
131
|
-
!userAgent.includes('chrome') &&
|
|
132
|
-
!userAgent.includes('edge'));
|
|
133
|
-
|
|
134
|
-
const hasFirefoxFeatures =
|
|
135
|
-
'mozInnerScreenX' in window ||
|
|
136
|
-
'mozInputSource' in window ||
|
|
137
|
-
'mozPaintCount' in window ||
|
|
138
|
-
typeof (window as any).InstallTrigger !== 'undefined';
|
|
139
|
-
|
|
140
|
-
const supportsMozCSS =
|
|
141
|
-
CSS.supports &&
|
|
142
|
-
(CSS.supports('-moz-appearance', 'none') ||
|
|
143
|
-
CSS.supports('-moz-user-select', 'none'));
|
|
144
|
-
|
|
145
|
-
return {
|
|
146
|
-
isGecko: isGecko || hasFirefoxFeatures || supportsMozCSS,
|
|
147
|
-
isChromium:
|
|
148
|
-
userAgent.includes('chrome') ||
|
|
149
|
-
userAgent.includes('chromium') ||
|
|
150
|
-
userAgent.includes('edge'),
|
|
151
|
-
isWebKit: userAgent.includes('webkit') && !userAgent.includes('chrome'),
|
|
152
|
-
};
|
|
153
|
-
};
|
|
154
|
-
|
|
155
|
-
const checkAgentSupport = async () => {
|
|
156
|
-
try {
|
|
157
|
-
const isMobile =
|
|
158
|
-
/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(
|
|
159
|
-
navigator.userAgent,
|
|
160
|
-
) ||
|
|
161
|
-
(navigator.maxTouchPoints && navigator.maxTouchPoints > 1);
|
|
162
|
-
|
|
163
|
-
// mobile devices generally support both cameras
|
|
164
|
-
// also, ios crashes if we try to check for cameras
|
|
165
|
-
if (isMobile) {
|
|
166
|
-
setAgentSupported(true);
|
|
167
|
-
return;
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
const { isGecko } = detectBrowserEngine();
|
|
171
|
-
|
|
172
|
-
let userCameraId: string | null = null;
|
|
173
|
-
let environmentCameraId: string | null = null;
|
|
174
|
-
|
|
175
|
-
// test if we can get a user-facing camera
|
|
176
|
-
try {
|
|
177
|
-
const userStream = await navigator.mediaDevices.getUserMedia({
|
|
178
|
-
video: { facingMode: 'user' },
|
|
179
|
-
});
|
|
180
|
-
userCameraId =
|
|
181
|
-
userStream.getVideoTracks()[0].getSettings().deviceId ?? null;
|
|
182
|
-
userStream.getTracks().forEach((track) => track.stop());
|
|
183
|
-
} catch (error) {
|
|
184
|
-
// no user-facing camera available
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
// test if we can get an environment-facing camera
|
|
188
|
-
try {
|
|
189
|
-
const envStream = await navigator.mediaDevices.getUserMedia({
|
|
190
|
-
video: { facingMode: 'environment' },
|
|
191
|
-
});
|
|
192
|
-
environmentCameraId =
|
|
193
|
-
envStream.getVideoTracks()[0].getSettings().deviceId ?? null;
|
|
194
|
-
envStream.getTracks().forEach((track) => track.stop());
|
|
195
|
-
} catch (error) {
|
|
196
|
-
// no environment-facing camera available
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
const hasBothCameras =
|
|
200
|
-
userCameraId &&
|
|
201
|
-
environmentCameraId &&
|
|
202
|
-
userCameraId !== environmentCameraId;
|
|
203
|
-
|
|
204
|
-
if (!hasBothCameras) {
|
|
205
|
-
setAgentSupported(false);
|
|
206
|
-
return;
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
setAgentSupported(!isGecko);
|
|
210
|
-
} catch (error) {
|
|
211
|
-
setAgentSupported(false);
|
|
212
|
-
}
|
|
213
|
-
};
|
|
214
|
-
|
|
215
|
-
const stopCamera = () => {
|
|
216
|
-
if (streamRef.current) {
|
|
217
|
-
streamRef.current.getTracks().forEach((track) => track.stop());
|
|
218
|
-
streamRef.current = null;
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
if (videoRef.current) {
|
|
222
|
-
videoRef.current.srcObject = null;
|
|
223
|
-
videoRef.current.load();
|
|
224
|
-
}
|
|
225
|
-
};
|
|
226
|
-
|
|
227
|
-
return {
|
|
228
|
-
videoRef,
|
|
229
|
-
streamRef,
|
|
230
|
-
facingMode,
|
|
231
|
-
agentSupported,
|
|
232
|
-
startCamera,
|
|
233
|
-
switchCamera,
|
|
234
|
-
checkAgentSupport,
|
|
235
|
-
stopCamera,
|
|
236
|
-
registerCameraSwitchCallback,
|
|
237
|
-
};
|
|
238
|
-
};
|
|
1
|
+
import { useRef, useState, useEffect } from 'preact/hooks';
|
|
2
|
+
|
|
3
|
+
export const useCamera = (initialFacingMode: CameraFacingMode = 'user') => {
|
|
4
|
+
const videoRef = useRef<HTMLVideoElement>(null);
|
|
5
|
+
const streamRef = useRef<MediaStream | null>(null);
|
|
6
|
+
const [facingMode, setFacingMode] = useState(initialFacingMode);
|
|
7
|
+
const [agentSupported, setAgentSupported] = useState(false);
|
|
8
|
+
const onCameraSwitchCallbackRef = useRef<(() => void) | null>(null);
|
|
9
|
+
const isSwitchingCameraRef = useRef(false);
|
|
10
|
+
const timeoutIdsRef = useRef<Set<NodeJS.Timeout>>(new Set());
|
|
11
|
+
|
|
12
|
+
const registerCameraSwitchCallback = (callback: () => void) => {
|
|
13
|
+
onCameraSwitchCallbackRef.current = callback;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
useEffect(() => {
|
|
17
|
+
const video = videoRef.current;
|
|
18
|
+
if (!video) return undefined;
|
|
19
|
+
|
|
20
|
+
const handleVideoReady = () => {
|
|
21
|
+
if (isSwitchingCameraRef.current && onCameraSwitchCallbackRef.current) {
|
|
22
|
+
const timeoutId = setTimeout(() => {
|
|
23
|
+
onCameraSwitchCallbackRef.current?.();
|
|
24
|
+
isSwitchingCameraRef.current = false;
|
|
25
|
+
timeoutIdsRef.current.delete(timeoutId);
|
|
26
|
+
}, 100);
|
|
27
|
+
timeoutIdsRef.current.add(timeoutId);
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
video.addEventListener('loadedmetadata', handleVideoReady);
|
|
32
|
+
|
|
33
|
+
return () => {
|
|
34
|
+
video.removeEventListener('loadedmetadata', handleVideoReady);
|
|
35
|
+
timeoutIdsRef.current.forEach((timeoutId) => clearTimeout(timeoutId));
|
|
36
|
+
timeoutIdsRef.current.clear();
|
|
37
|
+
};
|
|
38
|
+
}, [videoRef.current?.src]);
|
|
39
|
+
|
|
40
|
+
useEffect(
|
|
41
|
+
() => () => {
|
|
42
|
+
timeoutIdsRef.current.forEach((timeoutId) => clearTimeout(timeoutId));
|
|
43
|
+
timeoutIdsRef.current.clear();
|
|
44
|
+
},
|
|
45
|
+
[],
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
const startCamera = async (
|
|
49
|
+
targetFacingMode?: CameraFacingMode,
|
|
50
|
+
callback?: (cameraName?: string) => void,
|
|
51
|
+
) => {
|
|
52
|
+
try {
|
|
53
|
+
if (streamRef.current) {
|
|
54
|
+
streamRef.current.getTracks().forEach((track) => track.stop());
|
|
55
|
+
streamRef.current = null;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
if (videoRef.current) {
|
|
59
|
+
videoRef.current.srcObject = null;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const stream = await navigator.mediaDevices.getUserMedia({
|
|
63
|
+
video: { facingMode: targetFacingMode || facingMode },
|
|
64
|
+
});
|
|
65
|
+
streamRef.current = stream;
|
|
66
|
+
|
|
67
|
+
const track = stream.getVideoTracks()[0];
|
|
68
|
+
const settings = track.getSettings();
|
|
69
|
+
const actualFacingMode = settings.facingMode as
|
|
70
|
+
| CameraFacingMode
|
|
71
|
+
| undefined;
|
|
72
|
+
|
|
73
|
+
const requestedFacingMode = targetFacingMode || facingMode;
|
|
74
|
+
|
|
75
|
+
if (actualFacingMode && actualFacingMode !== requestedFacingMode) {
|
|
76
|
+
setFacingMode(actualFacingMode);
|
|
77
|
+
} else if (actualFacingMode && actualFacingMode !== facingMode) {
|
|
78
|
+
setFacingMode(actualFacingMode);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const devices = await navigator.mediaDevices.enumerateDevices();
|
|
82
|
+
const videoDevice = devices.find(
|
|
83
|
+
(device) =>
|
|
84
|
+
device.kind === 'videoinput' &&
|
|
85
|
+
stream.getVideoTracks()[0].getSettings().deviceId === device.deviceId,
|
|
86
|
+
);
|
|
87
|
+
|
|
88
|
+
callback?.(videoDevice?.label);
|
|
89
|
+
|
|
90
|
+
if (videoRef.current) {
|
|
91
|
+
videoRef.current.srcObject = stream;
|
|
92
|
+
await videoRef.current.play();
|
|
93
|
+
// Video ready callback will be handled by useEffect
|
|
94
|
+
}
|
|
95
|
+
} catch (error) {
|
|
96
|
+
console.error('Failed to start camera:', error);
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
const switchCamera = async () => {
|
|
101
|
+
const newFacingMode = facingMode === 'user' ? 'environment' : 'user';
|
|
102
|
+
isSwitchingCameraRef.current = true;
|
|
103
|
+
|
|
104
|
+
const previousFacingMode = facingMode;
|
|
105
|
+
try {
|
|
106
|
+
setFacingMode(newFacingMode);
|
|
107
|
+
if (streamRef.current) {
|
|
108
|
+
streamRef.current.getTracks().forEach((track) => track.stop());
|
|
109
|
+
streamRef.current = null;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
await startCamera(newFacingMode);
|
|
113
|
+
} catch (error) {
|
|
114
|
+
setFacingMode(previousFacingMode);
|
|
115
|
+
isSwitchingCameraRef.current = false;
|
|
116
|
+
|
|
117
|
+
try {
|
|
118
|
+
await startCamera(previousFacingMode);
|
|
119
|
+
} catch (restoreError) {
|
|
120
|
+
console.error('Failed to restore previous camera:', restoreError);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
const detectBrowserEngine = () => {
|
|
126
|
+
const userAgent = navigator.userAgent.toLowerCase();
|
|
127
|
+
|
|
128
|
+
const isGecko =
|
|
129
|
+
userAgent.includes('firefox') ||
|
|
130
|
+
(userAgent.includes('gecko') &&
|
|
131
|
+
!userAgent.includes('chrome') &&
|
|
132
|
+
!userAgent.includes('edge'));
|
|
133
|
+
|
|
134
|
+
const hasFirefoxFeatures =
|
|
135
|
+
'mozInnerScreenX' in window ||
|
|
136
|
+
'mozInputSource' in window ||
|
|
137
|
+
'mozPaintCount' in window ||
|
|
138
|
+
typeof (window as any).InstallTrigger !== 'undefined';
|
|
139
|
+
|
|
140
|
+
const supportsMozCSS =
|
|
141
|
+
CSS.supports &&
|
|
142
|
+
(CSS.supports('-moz-appearance', 'none') ||
|
|
143
|
+
CSS.supports('-moz-user-select', 'none'));
|
|
144
|
+
|
|
145
|
+
return {
|
|
146
|
+
isGecko: isGecko || hasFirefoxFeatures || supportsMozCSS,
|
|
147
|
+
isChromium:
|
|
148
|
+
userAgent.includes('chrome') ||
|
|
149
|
+
userAgent.includes('chromium') ||
|
|
150
|
+
userAgent.includes('edge'),
|
|
151
|
+
isWebKit: userAgent.includes('webkit') && !userAgent.includes('chrome'),
|
|
152
|
+
};
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
const checkAgentSupport = async () => {
|
|
156
|
+
try {
|
|
157
|
+
const isMobile =
|
|
158
|
+
/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(
|
|
159
|
+
navigator.userAgent,
|
|
160
|
+
) ||
|
|
161
|
+
(navigator.maxTouchPoints && navigator.maxTouchPoints > 1);
|
|
162
|
+
|
|
163
|
+
// mobile devices generally support both cameras
|
|
164
|
+
// also, ios crashes if we try to check for cameras
|
|
165
|
+
if (isMobile) {
|
|
166
|
+
setAgentSupported(true);
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
const { isGecko } = detectBrowserEngine();
|
|
171
|
+
|
|
172
|
+
let userCameraId: string | null = null;
|
|
173
|
+
let environmentCameraId: string | null = null;
|
|
174
|
+
|
|
175
|
+
// test if we can get a user-facing camera
|
|
176
|
+
try {
|
|
177
|
+
const userStream = await navigator.mediaDevices.getUserMedia({
|
|
178
|
+
video: { facingMode: 'user' },
|
|
179
|
+
});
|
|
180
|
+
userCameraId =
|
|
181
|
+
userStream.getVideoTracks()[0].getSettings().deviceId ?? null;
|
|
182
|
+
userStream.getTracks().forEach((track) => track.stop());
|
|
183
|
+
} catch (error) {
|
|
184
|
+
// no user-facing camera available
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// test if we can get an environment-facing camera
|
|
188
|
+
try {
|
|
189
|
+
const envStream = await navigator.mediaDevices.getUserMedia({
|
|
190
|
+
video: { facingMode: 'environment' },
|
|
191
|
+
});
|
|
192
|
+
environmentCameraId =
|
|
193
|
+
envStream.getVideoTracks()[0].getSettings().deviceId ?? null;
|
|
194
|
+
envStream.getTracks().forEach((track) => track.stop());
|
|
195
|
+
} catch (error) {
|
|
196
|
+
// no environment-facing camera available
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
const hasBothCameras =
|
|
200
|
+
userCameraId &&
|
|
201
|
+
environmentCameraId &&
|
|
202
|
+
userCameraId !== environmentCameraId;
|
|
203
|
+
|
|
204
|
+
if (!hasBothCameras) {
|
|
205
|
+
setAgentSupported(false);
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
setAgentSupported(!isGecko);
|
|
210
|
+
} catch (error) {
|
|
211
|
+
setAgentSupported(false);
|
|
212
|
+
}
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
const stopCamera = () => {
|
|
216
|
+
if (streamRef.current) {
|
|
217
|
+
streamRef.current.getTracks().forEach((track) => track.stop());
|
|
218
|
+
streamRef.current = null;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
if (videoRef.current) {
|
|
222
|
+
videoRef.current.srcObject = null;
|
|
223
|
+
videoRef.current.load();
|
|
224
|
+
}
|
|
225
|
+
};
|
|
226
|
+
|
|
227
|
+
return {
|
|
228
|
+
videoRef,
|
|
229
|
+
streamRef,
|
|
230
|
+
facingMode,
|
|
231
|
+
agentSupported,
|
|
232
|
+
startCamera,
|
|
233
|
+
switchCamera,
|
|
234
|
+
checkAgentSupport,
|
|
235
|
+
stopCamera,
|
|
236
|
+
registerCameraSwitchCallback,
|
|
237
|
+
};
|
|
238
|
+
};
|