diff --git a/native/components/nux-handler.react.js b/native/components/nux-handler.react.js index 2a14ab866..ee8515c40 100644 --- a/native/components/nux-handler.react.js +++ b/native/components/nux-handler.react.js @@ -1,39 +1,53 @@ // @flow import { useNavigation } from '@react-navigation/core'; import invariant from 'invariant'; import * as React from 'react'; import { isLoggedIn } from 'lib/selectors/user-selectors.js'; -import { NUXTipsContext } from './nux-tips-context.react.js'; +import { + type NUXTip, + NUXTipsContext, + nuxTip, +} from './nux-tips-context.react.js'; import { useSelector } from '../redux/redux-utils.js'; import { useOnFirstLaunchEffect } from '../utils/hooks.js'; +const orderedTips: $ReadOnlyArray = [ + nuxTip.INTRO, + nuxTip.COMMUNITY_DRAWER, + nuxTip.HOME, + nuxTip.MUTED, +]; + function NUXHandler(): React.Node { const nuxTipsContext = React.useContext(NUXTipsContext); invariant(nuxTipsContext, 'nuxTipsContext should be defined'); const { tipsProps } = nuxTipsContext; const loggedIn = useSelector(isLoggedIn); if (!tipsProps || !loggedIn) { return null; } return ; } function NUXHandlerInner(): React.Node { const navigation = useNavigation(); const effect = React.useCallback(() => { navigation.navigate<'NUXTipOverlayBackdrop'>({ name: 'NUXTipOverlayBackdrop', + params: { + orderedTips, + }, }); }, [navigation]); useOnFirstLaunchEffect('NUX_HANDLER', effect); } export { NUXHandler }; diff --git a/native/components/nux-tips-context.react.js b/native/components/nux-tips-context.react.js index 3f84388e0..99ab1f97e 100644 --- a/native/components/nux-tips-context.react.js +++ b/native/components/nux-tips-context.react.js @@ -1,131 +1,126 @@ // @flow import * as React from 'react'; import type { MeasureOnSuccessCallback } from 'react-native/Libraries/Renderer/shims/ReactNativeTypes'; import { values } from 'lib/utils/objects.js'; import type { AppNavigationProp } from '../navigation/app-navigator.react.js'; import type { NUXTipRouteNames } from '../navigation/route-names.js'; import { CommunityDrawerTipRouteName, MutedTabTipRouteName, HomeTabTipRouteName, IntroTipRouteName, } from '../navigation/route-names.js'; const nuxTip = Object.freeze({ INTRO: 'intro', COMMUNITY_DRAWER: 'community_drawer', HOME: 'home', MUTED: 'muted', }); export type NUXTip = $Values; type NUXTipParams = { - +nextTip: ?NUXTip, +tooltipLocation: 'below' | 'above' | 'absolute', +routeName: NUXTipRouteNames, +exitingCallback?: ( navigation: AppNavigationProp, ) => void, }; const firstNUXTipKey = nuxTip.INTRO; const nuxTipParams: { +[NUXTip]: NUXTipParams } = { [nuxTip.INTRO]: { - nextTip: nuxTip.COMMUNITY_DRAWER, tooltipLocation: 'absolute', routeName: IntroTipRouteName, }, [nuxTip.COMMUNITY_DRAWER]: { - nextTip: nuxTip.HOME, tooltipLocation: 'below', routeName: CommunityDrawerTipRouteName, }, [nuxTip.HOME]: { - nextTip: nuxTip.MUTED, tooltipLocation: 'below', routeName: HomeTabTipRouteName, }, [nuxTip.MUTED]: { - nextTip: undefined, routeName: MutedTabTipRouteName, tooltipLocation: 'below', exitingCallback: navigation => navigation.goBack(), }, }; function getNUXTipParams(currentTipKey: NUXTip): NUXTipParams { return nuxTipParams[currentTipKey]; } type TipProps = (callback: MeasureOnSuccessCallback) => void; export type NUXTipsContextType = { +registerTipButton: (type: NUXTip, tipProps: ?TipProps) => void, +tipsProps: ?{ +[type: NUXTip]: TipProps }, }; const NUXTipsContext: React.Context = React.createContext(); type Props = { +children: React.Node, }; function NUXTipsContextProvider(props: Props): React.Node { const { children } = props; const [tipsPropsState, setTipsPropsState] = React.useState<{ +[tip: NUXTip]: ?TipProps, }>(() => ({})); const registerTipButton = React.useCallback( (type: NUXTip, tipProps: ?TipProps) => { setTipsPropsState(currenttipsPropsState => { const newtipsPropsState = { ...currenttipsPropsState }; newtipsPropsState[type] = tipProps; return newtipsPropsState; }); }, [], ); const tipsProps = React.useMemo(() => { const result: { [tip: NUXTip]: TipProps } = {}; for (const type of values(nuxTip)) { if (nuxTipParams[type].tooltipLocation === 'absolute') { continue; } if (!tipsPropsState[type]) { return null; } result[type] = tipsPropsState[type]; } return result; }, [tipsPropsState]); const value = React.useMemo( () => ({ registerTipButton, tipsProps, }), [tipsProps, registerTipButton], ); return ( {children} ); } export { NUXTipsContext, NUXTipsContextProvider, nuxTip, getNUXTipParams, firstNUXTipKey, }; diff --git a/native/navigation/nux-tip-overlay-backdrop.react.js b/native/navigation/nux-tip-overlay-backdrop.react.js index 23c073f7f..ead9bfc60 100644 --- a/native/navigation/nux-tip-overlay-backdrop.react.js +++ b/native/navigation/nux-tip-overlay-backdrop.react.js @@ -1,102 +1,110 @@ // @flow import invariant from 'invariant'; import * as React from 'react'; import { withTiming } from 'react-native-reanimated'; import type { AppNavigationProp } from './app-navigator.react.js'; import { OverlayContext } from './overlay-context.js'; import type { NUXTipRouteNames, NavigationRoute } from './route-names'; -import { - firstNUXTipKey, - getNUXTipParams, -} from '../components/nux-tips-context.react.js'; +import { getNUXTipParams } from '../components/nux-tips-context.react.js'; import { useStyles } from '../themes/colors.js'; import { animationDuration } from '../tooltip/nux-tips-overlay.react.js'; import { AnimatedView } from '../types/styles.js'; type Props = { +navigation: AppNavigationProp<'NUXTipOverlayBackdrop'>, +route: NavigationRoute<'NUXTipOverlayBackdrop'>, }; function NUXTipOverlayBackdrop(props: Props): React.Node { const overlayContext = React.useContext(OverlayContext); invariant(overlayContext, 'NUXTipsOverlay should have OverlayContext'); const { shouldRenderScreenContent } = overlayContext; return shouldRenderScreenContent ? ( ) : null; } function opacityEnteringAnimation() { 'worklet'; return { animations: { opacity: withTiming(0.7, { duration: animationDuration }), }, initialValues: { opacity: 0, }, }; } function NUXTipOverlayBackdropInner(props: Props): React.Node { + const { navigation, route } = props; + const overlayContext = React.useContext(OverlayContext); invariant(overlayContext, 'NUXTipsOverlay should have OverlayContext'); const { onExitFinish } = overlayContext; const styles = useStyles(unboundStyles); + const orderedTips = route.params?.orderedTips; + invariant( + orderedTips && orderedTips.length > 0, + 'orderedTips is required and should not be empty.', + ); + const firstTip = orderedTips[0]; + const opacityExitingAnimation = React.useCallback(() => { 'worklet'; return { animations: { opacity: withTiming(0, { duration: animationDuration }), }, initialValues: { opacity: 0.7, }, callback: onExitFinish, }; }, [onExitFinish]); - const { routeName } = getNUXTipParams(firstNUXTipKey); + const { routeName } = getNUXTipParams(firstTip); React.useEffect( - () => - props.navigation.navigate({ + () => { + navigation.navigate({ name: routeName, params: { - tipKey: firstNUXTipKey, + orderedTips, + orderedTipsIndex: 0, }, - }), + }); + }, // We want this effect to run exactly once, when this component is mounted // eslint-disable-next-line react-hooks/exhaustive-deps [], ); return ( ); } const unboundStyles = { backdrop: { backgroundColor: 'black', bottom: 0, left: 0, position: 'absolute', right: 0, top: 0, }, }; export default NUXTipOverlayBackdrop; diff --git a/native/tooltip/nux-tips-overlay.react.js b/native/tooltip/nux-tips-overlay.react.js index c4c28a2f6..3ef74ac82 100644 --- a/native/tooltip/nux-tips-overlay.react.js +++ b/native/tooltip/nux-tips-overlay.react.js @@ -1,498 +1,513 @@ // @flow import invariant from 'invariant'; import * as React from 'react'; import { View, TouchableWithoutFeedback, Platform, Text } from 'react-native'; import Animated, { FadeOut, withTiming, // eslint-disable-next-line no-unused-vars type EntryAnimationsValues, // eslint-disable-next-line no-unused-vars type ExitAnimationsValues, } from 'react-native-reanimated'; import Button from '../components/button.react.js'; import { getNUXTipParams, NUXTipsContext, type NUXTip, } from '../components/nux-tips-context.react.js'; import type { AppNavigationProp } from '../navigation/app-navigator.react.js'; import { OverlayContext } from '../navigation/overlay-context.js'; import type { NavigationRoute, NUXTipRouteNames, } from '../navigation/route-names.js'; import { useSelector } from '../redux/redux-utils.js'; import { useStyles } from '../themes/colors.js'; import type { LayoutEvent } from '../types/react-native.js'; import { AnimatedView } from '../types/styles.js'; import type { WritableAnimatedStyleObj } from '../types/styles.js'; const { Value } = Animated; const animationDuration = 150; const unboundStyles = { container: { flex: 1, }, contentContainer: { flex: 1, overflow: 'hidden', }, items: { backgroundColor: 'tooltipBackground', borderRadius: 5, overflow: 'hidden', paddingHorizontal: 20, paddingTop: 20, paddingBottom: 18, }, triangleUp: { borderBottomColor: 'tooltipBackground', borderBottomWidth: 10, borderLeftColor: 'transparent', borderLeftWidth: 10, borderRightColor: 'transparent', borderRightWidth: 10, borderStyle: 'solid', borderTopColor: 'transparent', borderTopWidth: 0, bottom: Platform.OS === 'android' ? -1 : 0, height: 10, width: 10, }, triangleDown: { borderBottomColor: 'transparent', borderBottomWidth: 0, borderLeftColor: 'transparent', borderLeftWidth: 10, borderRightColor: 'transparent', borderRightWidth: 10, borderStyle: 'solid', borderTopColor: 'tooltipBackground', borderTopWidth: 10, height: 10, top: Platform.OS === 'android' ? -1 : 0, width: 10, }, tipText: { color: 'panelForegroundLabel', fontSize: 18, marginBottom: 10, }, buttonContainer: { alignSelf: 'flex-end', }, okButtonText: { fontSize: 18, color: 'panelForegroundLabel', textAlign: 'center', paddingVertical: 10, paddingHorizontal: 20, }, okButton: { backgroundColor: 'purpleButton', borderRadius: 8, }, }; export type NUXTipsOverlayParams = { - +tipKey: NUXTip, + +orderedTips: $ReadOnlyArray, + +orderedTipsIndex: number, }; export type NUXTipsOverlayProps = { +navigation: AppNavigationProp, +route: NavigationRoute, }; const marginVertical: number = 20; const marginHorizontal: number = 10; function createNUXTipsOverlay( ButtonComponent: ?React.ComponentType>, tipText: string, ): React.ComponentType> { function NUXTipsOverlay(props: NUXTipsOverlayProps) { const nuxTipContext = React.useContext(NUXTipsContext); const { navigation, route } = props; const dimensions = useSelector(state => state.dimensions); const [coordinates, setCoordinates] = React.useState(null); React.useEffect(() => { if (!ButtonComponent) { const yInitial = (dimensions.height * 2) / 5; const xInitial = dimensions.width / 2; setCoordinates({ initialCoordinates: { height: 0, width: 0, x: xInitial, y: yInitial }, verticalBounds: { height: 0, y: yInitial }, }); return; } - const measure = nuxTipContext?.tipsProps?.[route.params.tipKey]; + const currentTipKey = + route.params.orderedTips[route.params.orderedTipsIndex]; + const measure = nuxTipContext?.tipsProps?.[currentTipKey]; invariant(measure, 'button should be registered with nuxTipContext'); measure((x, y, width, height, pageX, pageY) => setCoordinates({ initialCoordinates: { height, width, x: pageX, y: pageY }, verticalBounds: { height, y: pageY }, }), ); - }, [dimensions, nuxTipContext?.tipsProps, route.params.tipKey]); + }, [ + dimensions, + nuxTipContext?.tipsProps, + route.params.orderedTips, + route.params.orderedTipsIndex, + ]); const overlayContext = React.useContext(OverlayContext); invariant(overlayContext, 'NUXTipsOverlay should have OverlayContext'); const { onExitFinish } = overlayContext; const { goBackOnce } = navigation; const styles = useStyles(unboundStyles); const contentContainerStyle = React.useMemo(() => { if (!coordinates) { return {}; } const fullScreenHeight = dimensions.height; const top = coordinates.verticalBounds.y; const bottom = fullScreenHeight - coordinates.verticalBounds.y - coordinates.verticalBounds.height; return { ...styles.contentContainer, marginTop: top, marginBottom: bottom, }; }, [dimensions.height, styles.contentContainer, coordinates]); const buttonStyle = React.useMemo(() => { if (!coordinates) { return {}; } const { x, y, width, height } = coordinates.initialCoordinates; return { width: Math.ceil(width), height: Math.ceil(height), marginTop: y - coordinates.verticalBounds.y, marginLeft: x, }; }, [coordinates]); const tipHorizontalOffsetRef = React.useRef(new Value(0)); const tipHorizontalOffset = tipHorizontalOffsetRef.current; const onTipContainerLayout = React.useCallback( (event: LayoutEvent) => { if (!coordinates) { return; } const { x, width } = coordinates.initialCoordinates; const extraLeftSpace = x; const extraRightSpace = dimensions.width - width - x; const actualWidth = event.nativeEvent.layout.width; if (extraLeftSpace < extraRightSpace) { const minWidth = width + 2 * extraLeftSpace; tipHorizontalOffset.setValue((minWidth - actualWidth) / 2); } else { const minWidth = width + 2 * extraRightSpace; tipHorizontalOffset.setValue((actualWidth - minWidth) / 2); } }, [coordinates, dimensions.width, tipHorizontalOffset], ); - const tipParams = getNUXTipParams(route.params.tipKey); + const currentTipKey = + route.params.orderedTips[route.params.orderedTipsIndex]; + const tipParams = getNUXTipParams(currentTipKey); const { tooltipLocation } = tipParams; const baseTipContainerStyle = React.useMemo(() => { if (!coordinates) { return {}; } const { initialCoordinates, verticalBounds } = coordinates; const { y, x, height, width } = initialCoordinates; const style: WritableAnimatedStyleObj = { position: 'absolute', alignItems: 'center', }; if (tooltipLocation === 'below') { style.top = Math.min(y + height, verticalBounds.y + verticalBounds.height) + marginVertical; } else { style.bottom = dimensions.height - Math.max(y, verticalBounds.y) + marginVertical; } const extraLeftSpace = x; const extraRightSpace = dimensions.width - width - x; if (tooltipLocation === 'absolute') { style.left = marginHorizontal; style.right = marginHorizontal; } else if (extraLeftSpace < extraRightSpace) { style.left = marginHorizontal; style.minWidth = width + 2 * extraLeftSpace; style.marginRight = 2 * marginHorizontal; } else { style.right = marginHorizontal; style.minWidth = width + 2 * extraRightSpace; style.marginLeft = 2 * marginHorizontal; } return style; }, [coordinates, dimensions.height, dimensions.width, tooltipLocation]); const triangleStyle = React.useMemo(() => { if (!coordinates) { return {}; } const { x, width } = coordinates.initialCoordinates; const extraLeftSpace = x; const extraRightSpace = dimensions.width - width - x; if (extraLeftSpace < extraRightSpace) { return { alignSelf: 'flex-start', left: extraLeftSpace + (4 / 10) * width - marginHorizontal, }; } else { return { alignSelf: 'flex-end', right: extraRightSpace + (4 / 10) * width - marginHorizontal, }; } }, [coordinates, dimensions.width]); // prettier-ignore const tipContainerEnteringAnimation = React.useCallback( (values/*: EntryAnimationsValues*/) => { 'worklet'; if (!coordinates) { return { animations: {}, initialValues:{}, }; } if(tooltipLocation === 'absolute'){ return { animations: { opacity: withTiming(1, { duration: animationDuration }), transform: [ { scale: withTiming(1, { duration: animationDuration }) }, ], }, initialValues: { opacity: 0, transform: [ { scale: 0 }, ], }, }; } const initialX = (-values.targetWidth + coordinates.initialCoordinates.width + coordinates.initialCoordinates.x) / 2; const initialY = tooltipLocation === 'below' ? -values.targetHeight / 2 : values.targetHeight / 2; return { animations: { opacity: withTiming(1, { duration: animationDuration }), transform: [ { translateX: withTiming(0, { duration: animationDuration }) }, { translateY: withTiming(0, { duration: animationDuration }) }, { scale: withTiming(1, { duration: animationDuration }) }, ], }, initialValues: { opacity: 0, transform: [ { translateX: initialX }, { translateY: initialY }, { scale: 0 }, ], }, }; }, [coordinates, tooltipLocation], ); // prettier-ignore const tipContainerExitingAnimation = React.useCallback( (values/*: ExitAnimationsValues*/) => { 'worklet'; if (!coordinates) { return { animations: {}, initialValues:{}, }; } if (tooltipLocation === 'absolute') { return { animations: { opacity: withTiming(0, { duration: animationDuration }), transform: [ { scale: withTiming(0, { duration: animationDuration }) }, ], }, initialValues: { opacity: 1, transform: [{ scale: 1 }], }, callback: onExitFinish, }; } const toValueX = (-values.currentWidth + coordinates.initialCoordinates.width + coordinates.initialCoordinates.x) / 2; const toValueY = tooltipLocation === 'below' ? -values.currentHeight / 2 : values.currentHeight / 2; return { animations: { opacity: withTiming(0, { duration: animationDuration }), transform: [ { translateX: withTiming(toValueX, { duration: animationDuration, }), }, { translateY: withTiming(toValueY, { duration: animationDuration, }), }, { scale: withTiming(0, { duration: animationDuration }) }, ], }, initialValues: { opacity: 1, transform: [{ translateX: 0 }, { translateY: 0 }, { scale: 1 }], }, callback: onExitFinish, }; }, [coordinates, onExitFinish, tooltipLocation], ); let triangleDown = null; let triangleUp = null; if (tooltipLocation === 'above') { triangleDown = ; } else if (tooltipLocation === 'below') { triangleUp = ; } const onPressOk = React.useCallback(() => { - const { nextTip, exitingCallback } = tipParams; + const { orderedTips, orderedTipsIndex } = route.params; + const { exitingCallback } = tipParams; goBackOnce(); if (exitingCallback) { exitingCallback?.(navigation); } - if (!nextTip) { + const nextOrderedTipsIndex = orderedTipsIndex + 1; + if (nextOrderedTipsIndex >= orderedTips.length) { return; } + + const nextTip = orderedTips[nextOrderedTipsIndex]; const { routeName } = getNUXTipParams(nextTip); navigation.navigate({ name: routeName, params: { - tipKey: nextTip, + orderedTips, + orderedTipsIndex: nextOrderedTipsIndex, }, }); - }, [goBackOnce, navigation, tipParams]); + }, [goBackOnce, navigation, route.params, tipParams]); const button = React.useMemo( () => ButtonComponent ? ( ) : undefined, [buttonStyle, contentContainerStyle, props.navigation, route], ); if (!coordinates) { return null; } return ( {button} {triangleUp} {tipText} {triangleDown} ); } function NUXTipsOverlayWrapper(props: NUXTipsOverlayProps) { const overlayContext = React.useContext(OverlayContext); invariant(overlayContext, 'NUXTipsOverlay should have OverlayContext'); const { shouldRenderScreenContent } = overlayContext; return shouldRenderScreenContent ? : null; } return React.memo>(NUXTipsOverlayWrapper); } export { createNUXTipsOverlay, animationDuration };