Skip to content

Commit

Permalink
console: Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
mjamescompton committed Nov 13, 2023
1 parent 7d6a525 commit 1048e5f
Showing 1 changed file with 28 additions and 28 deletions.
56 changes: 28 additions & 28 deletions pkg/webui/components/qr/input/video/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,38 +120,38 @@ const Stream = props => {
const { deviceId, getDevices, setDeviceIdFromStream, onRead, setCapture, setError } = props
const [stream, setStream] = useState(undefined)

const getStream = useCallback(async () => {
// Initially request the stream with the default camera for facing mode environment
// if device id is set then create stream with that device id and display video
try {
if (!deviceId) {
const userStream = await navigator.mediaDevices.getUserMedia({
video: { facingMode: 'environment' },
})
// After requesting the stream, get the devices again as we now have permission to see all devices
getDevices()
// On initial request set the device id from the stream, this should rerender this component
setDeviceIdFromStream(userStream)
} else {
const userStream = await navigator.mediaDevices.getUserMedia({
video: { deviceId },
})
// Only set the stream if the device id is set
setStream(userStream)
}
} catch (error) {
if (error instanceof DOMException && error.name === 'NotAllowedError') {
setCapture(false)
setError(true)
} else {
throw error
useEffect(() => {
const getStream = async () => {
// Initially request the stream with the default camera for facing mode environment
// if device id is set then create stream with that device id and display video
try {
if (!deviceId) {
const userStream = await navigator.mediaDevices.getUserMedia({
video: { facingMode: 'environment' },
})
// After requesting the stream, get the devices again as we now have permission to see all devices
getDevices()
// On initial request set the device id from the stream, this should rerender this component
setDeviceIdFromStream(userStream)
} else {
const userStream = await navigator.mediaDevices.getUserMedia({
video: { deviceId },
})
// Only set the stream if the device id is set
setStream(userStream)
}
} catch (error) {
if (error instanceof DOMException && error.name === 'NotAllowedError') {
setCapture(false)
setError(true)
} else {
throw error
}
}
}
}, [deviceId, getDevices, setCapture, setDeviceIdFromStream, setError])

useEffect(() => {
getStream()
}, [getStream])
}, [deviceId, getDevices, setCapture, setDeviceIdFromStream, setError])

return <Video stream={stream} onRead={onRead} />
}
Expand Down

0 comments on commit 1048e5f

Please sign in to comment.