2021-02-20 09:05:02 -07:00
|
|
|
import { useEffect, useState } from 'react';
|
2023-09-21 03:20:33 -06:00
|
|
|
import type { CameraDevice } from '../CameraDevice';
|
|
|
|
import { CameraDevices } from '../CameraDevices';
|
2021-02-20 15:20:28 -07:00
|
|
|
|
|
|
|
/**
|
2023-09-21 03:20:33 -06:00
|
|
|
* Get all available Camera Devices this phone has.
|
2021-02-20 15:20:28 -07:00
|
|
|
*
|
2023-09-21 03:20:33 -06:00
|
|
|
* Camera Devices attached to this phone (`back` or `front`) are always available,
|
|
|
|
* while `external` devices might be plugged in or out at any point,
|
|
|
|
* so the result of this function might update over time.
|
2021-02-20 15:20:28 -07:00
|
|
|
*/
|
2023-09-21 03:20:33 -06:00
|
|
|
export function useCameraDevices(): CameraDevice[] {
|
|
|
|
const [devices, setDevices] = useState(() => CameraDevices.getAvailableCameraDevices());
|
2021-02-20 09:05:02 -07:00
|
|
|
|
|
|
|
useEffect(() => {
|
2023-09-21 03:20:33 -06:00
|
|
|
const listener = CameraDevices.addCameraDevicesChangedListener((newDevices) => {
|
|
|
|
setDevices(newDevices);
|
|
|
|
});
|
|
|
|
return () => listener.remove();
|
|
|
|
}, []);
|
2021-02-20 09:05:02 -07:00
|
|
|
|
2023-09-21 03:20:33 -06:00
|
|
|
return devices;
|
2021-02-20 15:20:28 -07:00
|
|
|
}
|