-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Chrome mWeb - scan button provides no feedback #30094
Changes from 4 commits
04476a3
c09a37c
e8664c0
7307db8
710cee1
9e87798
b3f0a85
ab6ad33
afc709c
e846414
6788075
6f76928
0b23ac2
f21f204
1349a1e
96f76c5
6080787
5da6f16
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,20 @@ | ||
import React, {useEffect, useRef} from 'react'; | ||
import React, {useEffect, useRef, useState} from 'react'; | ||
import Webcam from 'react-webcam'; | ||
import {useIsFocused} from '@react-navigation/native'; | ||
import PropTypes from 'prop-types'; | ||
import {View} from 'react-native'; | ||
import {useTabAnimation} from '@react-navigation/material-top-tabs'; | ||
|
||
const propTypes = { | ||
/* Flag to turn on/off the torch/flashlight - if available */ | ||
torchOn: PropTypes.bool, | ||
|
||
/* The index of the tab that contains this camera */ | ||
cameraTabIndex: PropTypes.number.isRequired, | ||
|
||
/* Whether we're in a tab navigator */ | ||
isInTabNavigator: PropTypes.bool.isRequired, | ||
|
||
/* Callback function when media stream becomes available - user granted camera permissions and camera starts to work */ | ||
onUserMedia: PropTypes.func, | ||
|
||
|
@@ -21,10 +28,40 @@ const defaultProps = { | |
torchOn: false, | ||
}; | ||
|
||
function useTabNavigatorFocus({cameraTabIndex, isInTabNavigator}) { | ||
// Get navigation to get initial isFocused value (only needed once during init!) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would suggest moving this to its own file and having it be defined as a proper hook. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. agree! |
||
const isPageFocused = useIsFocused(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This sentence doesn't really make sense, the first two words seem out of place. |
||
const [isTabFocused, setIsTabFocused] = useState(false); | ||
|
||
// Retrieve the animation value from the tab navigator, which ranges from 0 to the total number of pages displayed. | ||
// Even a minimal scroll towards the camera page (e.g., a value of 0.001 at start) should activate the camera for immediate responsiveness. | ||
|
||
// STOP!!!!!!! This is not a pattern to be followed! We are conditionally rendering this hook becase when used in the edit flow we'll never be inside a tab navigator. | ||
// eslint-disable-next-line react-hooks/rules-of-hooks | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This comment is nice, but it sure makes me think that this is built wrong. It sounds like there should be two different components. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't request this change because there's already separate GH for this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK, thank you for making me aware of it. |
||
const tabPositionAnimation = isInTabNavigator ? useTabAnimation() : null; | ||
|
||
useEffect(() => { | ||
if (!isInTabNavigator) { | ||
return; | ||
} | ||
|
||
const listenerId = tabPositionAnimation.addListener(({value}) => { | ||
// Activate camera as soon the index is animating towards the `cameraTabIndex` | ||
setIsTabFocused(value > cameraTabIndex - 1 && value < cameraTabIndex + 1); | ||
}); | ||
|
||
return () => { | ||
tabPositionAnimation.removeListener(listenerId); | ||
}; | ||
}, [cameraTabIndex, tabPositionAnimation, isInTabNavigator]); | ||
|
||
return isTabFocused && isPageFocused; | ||
} | ||
|
||
// Wraps a camera that will only be active when the tab is focused or as soon as it starts to become focused. | ||
const NavigationAwareCamera = React.forwardRef(({torchOn, onTorchAvailability, ...props}, ref) => { | ||
const NavigationAwareCamera = React.forwardRef(({torchOn, onTorchAvailability, cameraTabIndex, isInTabNavigator, ...props}, ref) => { | ||
const trackRef = useRef(null); | ||
const isCameraActive = useIsFocused(); | ||
const showCamera = useTabNavigatorFocus({cameraTabIndex, isInTabNavigator}); | ||
|
||
lukemorawski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const handleOnUserMedia = (stream) => { | ||
if (props.onUserMedia) { | ||
|
@@ -47,11 +84,15 @@ const NavigationAwareCamera = React.forwardRef(({torchOn, onTorchAvailability, . | |
} | ||
|
||
trackRef.current.applyConstraints({ | ||
advanced: [{torch: torchOn}], | ||
advanced: [ | ||
{ | ||
torch: torchOn, | ||
}, | ||
], | ||
}); | ||
}, [torchOn]); | ||
|
||
if (!isCameraActive) { | ||
if (!showCamera) { | ||
return null; | ||
} | ||
return ( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please update these comments (and the existing ones) to use the format
/** */
. This is so that storybook can automatically parse them.