forked from StellarSplit/StellarSplit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcameraPermissions.ts
More file actions
147 lines (134 loc) · 4.12 KB
/
Copy pathcameraPermissions.ts
File metadata and controls
147 lines (134 loc) · 4.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/**
* Camera permission handler for cross-platform compatibility
* Handles permission requests and errors gracefully
*/
export type PermissionStatus = 'granted' | 'denied' | 'prompt';
export interface PermissionError {
type: 'permission-denied' | 'not-found' | 'not-secure' | 'unknown';
message: string;
}
/**
* Request camera permissions from the user
* @param options - Camera constraint options
* @returns Promise<MediaStream>
*/
export const requestCameraPermission = async (
options: MediaStreamConstraints = {
video: { facingMode: 'environment' },
audio: false,
}
): Promise<MediaStream> => {
try {
// Check if getUserMedia is supported
if (!navigator.mediaDevices || !navigator.mediaDevices.getUserMedia) {
throw createPermissionError(
'not-found',
'Camera API is not supported in this browser'
);
}
// Check if HTTPS is being used (required for getUserMedia)
if (
window.location.protocol !== 'https:' &&
window.location.hostname !== 'localhost'
) {
throw createPermissionError(
'not-secure',
'Camera access requires HTTPS connection'
);
}
const stream = await navigator.mediaDevices.getUserMedia(options);
return stream;
} catch (error) {
throw handleCameraError(error);
}
};
/**
* Stop camera stream and clean up tracks
* @param stream - MediaStream to stop
*/
export const stopCameraStream = (stream: MediaStream): void => {
stream.getTracks().forEach((track) => {
track.stop();
});
};
/**
* Check current camera permission status
* @returns Promise<PermissionStatus>
*/
export const checkCameraPermission = async (): Promise<PermissionStatus> => {
if (!navigator.permissions || !navigator.permissions.query) {
return 'prompt';
}
try {
const permissionStatus = await navigator.permissions.query({
name: 'camera' as PermissionName,
});
return permissionStatus.state as PermissionStatus;
} catch {
return 'prompt';
}
};
/**
* Create a structured permission error
* @param type - Error type
* @param message - Error message
* @returns PermissionError
*/
const createPermissionError = (
type: PermissionError['type'],
message: string
): Error & { permissionError: PermissionError } => {
const error: any = new Error(message);
error.permissionError = { type, message };
return error;
};
/**
* Handle camera permission errors
* @param error - Raw error from getUserMedia
* @returns Error with permission details
*/
const handleCameraError = (error: any): Error & { permissionError?: PermissionError } => {
const result: any = new Error();
if (error.name === 'NotAllowedError' || error.name === 'PermissionDeniedError') {
result.permissionError = {
type: 'permission-denied' as const,
message:
'Camera permission denied. Please allow access in your browser settings.',
};
result.message = result.permissionError.message;
} else if (error.name === 'NotFoundError' || error.name === 'DevicesNotFoundError') {
result.permissionError = {
type: 'not-found' as const,
message:
'No camera device found. Please check if your device has a camera.',
};
result.message = result.permissionError.message;
} else if (error.name === 'NotSecureError') {
result.permissionError = {
type: 'not-secure' as const,
message: 'Camera access requires a secure connection (HTTPS)',
};
result.message = result.permissionError.message;
} else if (error.permissionError) {
result.permissionError = error.permissionError;
result.message = error.message;
} else {
result.permissionError = {
type: 'unknown' as const,
message: error.message || 'An unknown error occurred while accessing the camera',
};
result.message = result.permissionError.message;
}
return result;
};
/**
* Get user-friendly error message
* @param error - Error object
* @returns User-friendly message
*/
export const getUserFriendlyErrorMessage = (error: any): string => {
if (error?.permissionError?.message) {
return error.permissionError.message;
}
return 'Unable to access camera. Please try again.';
};