Create useCameraDevice.ts

This commit is contained in:
Marc Rousavy 2021-02-20 17:05:02 +01:00
parent f2a1d52aa7
commit 50509200aa

View File

@ -0,0 +1,21 @@
import { useEffect, useState } from 'react';
import { Camera } from '../Camera';
import { CameraDevice, LogicalCameraDeviceType, parsePhysicalDeviceTypes, PhysicalCameraDeviceType } from '../CameraDevice';
export const useCameraDevice = (deviceType: PhysicalCameraDeviceType | LogicalCameraDeviceType): CameraDevice | undefined => {
const [device, setDevice] = useState<CameraDevice>();
useEffect(() => {
const loadDevice = async (): Promise<void> => {
const devices = await Camera.getAvailableCameraDevices();
const bestMatch = devices.find((d) => {
const parsedType = parsePhysicalDeviceTypes(d.devices);
return parsedType === deviceType;
});
setDevice(bestMatch);
};
loadDevice();
}, [deviceType]);
return device;
};