diff --git a/src/hooks/useCameraDevice.ts b/src/hooks/useCameraDevice.ts new file mode 100644 index 0000000..a704804 --- /dev/null +++ b/src/hooks/useCameraDevice.ts @@ -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(); + + useEffect(() => { + const loadDevice = async (): Promise => { + const devices = await Camera.getAvailableCameraDevices(); + const bestMatch = devices.find((d) => { + const parsedType = parsePhysicalDeviceTypes(d.devices); + return parsedType === deviceType; + }); + setDevice(bestMatch); + }; + loadDevice(); + }, [deviceType]); + + return device; +};