-
Notifications
You must be signed in to change notification settings - Fork 40
[MOB-12271] introduce IterableEmbeddedNotification component with styling and visibility management #749
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
Draft
lposen
wants to merge
1
commit into
loren/embedded/MOB-12270-new-embedded-view-component
Choose a base branch
from
loren/embedded/MOB-12271-notification-component
base: loren/embedded/MOB-12270-new-embedded-view-component
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
[MOB-12271] introduce IterableEmbeddedNotification component with styling and visibility management #749
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| export * from './useAppStateListener'; | ||
| export * from './useDeviceOrientation'; | ||
| export * from './useComponentVisibility'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| import { | ||
| View, | ||
| Dimensions, | ||
| AppState, | ||
| type LayoutChangeEvent, | ||
| } from 'react-native'; | ||
| import { useRef, useState, useCallback, useEffect } from 'react'; | ||
|
|
||
| interface UseVisibilityOptions { | ||
| threshold?: number; // Percentage of component that must be visible (0-1) | ||
| checkOnAppState?: boolean; // Whether to check app state (active/background) | ||
| checkInterval?: number; // How often to check visibility in ms (0 = only on layout changes) | ||
| enablePeriodicCheck?: boolean; // Whether to enable periodic checking for navigation changes | ||
| } | ||
|
|
||
| interface LayoutInfo { | ||
| x: number; | ||
| y: number; | ||
| width: number; | ||
| height: number; | ||
| } | ||
|
|
||
| export const useComponentVisibility = (options: UseVisibilityOptions = {}) => { | ||
| const { | ||
| threshold = 0.1, | ||
| checkOnAppState = true, | ||
| checkInterval = 0, // Default to only check on layout changes | ||
| enablePeriodicCheck = true, // Enable periodic checking by default for navigation | ||
| } = options; | ||
|
|
||
| const [isVisible, setIsVisible] = useState(false); | ||
| const [appState, setAppState] = useState(AppState.currentState); | ||
| const componentRef = useRef<View>(null); | ||
| const [layout, setLayout] = useState<LayoutInfo>({ | ||
| x: 0, | ||
| y: 0, | ||
| width: 0, | ||
| height: 0, | ||
| }); | ||
| const intervalRef = useRef<NodeJS.Timeout | null>(null); | ||
|
|
||
| // Handle layout changes | ||
| const handleLayout = useCallback((event: LayoutChangeEvent) => { | ||
| const { x, y, width, height } = event.nativeEvent.layout; | ||
| setLayout({ x, y, width, height }); | ||
| }, []); | ||
|
|
||
| // Check if component is visible on screen using measure | ||
| const checkVisibility = useCallback((): Promise<boolean> => { | ||
| if (!componentRef.current || layout.width === 0 || layout.height === 0) { | ||
| return Promise.resolve(false); | ||
| } | ||
|
|
||
| return new Promise<boolean>((resolve) => { | ||
| componentRef.current?.measure((_x, _y, width, height, pageX, pageY) => { | ||
| const screenHeight = Dimensions.get('window').height; | ||
| const screenWidth = Dimensions.get('window').width; | ||
|
|
||
| // Calculate visible area using page coordinates | ||
| const visibleTop = Math.max(0, pageY); | ||
| const visibleBottom = Math.min(screenHeight, pageY + height); | ||
| const visibleLeft = Math.max(0, pageX); | ||
| const visibleRight = Math.min(screenWidth, pageX + width); | ||
|
|
||
| const visibleHeight = Math.max(0, visibleBottom - visibleTop); | ||
| const visibleWidth = Math.max(0, visibleRight - visibleLeft); | ||
|
|
||
| const visibleArea = visibleHeight * visibleWidth; | ||
| const totalArea = height * width; | ||
| const visibilityRatio = totalArea > 0 ? visibleArea / totalArea : 0; | ||
|
|
||
| resolve(visibilityRatio >= threshold); | ||
| }); | ||
| }).catch(() => { | ||
| // Fallback to layout-based calculation if measure fails | ||
| const screenHeight = Dimensions.get('window').height; | ||
| const screenWidth = Dimensions.get('window').width; | ||
|
|
||
| const visibleTop = Math.max(0, layout.y); | ||
| const visibleBottom = Math.min(screenHeight, layout.y + layout.height); | ||
| const visibleLeft = Math.max(0, layout.x); | ||
| const visibleRight = Math.min(screenWidth, layout.x + layout.width); | ||
|
|
||
| const visibleHeight = Math.max(0, visibleBottom - visibleTop); | ||
| const visibleWidth = Math.max(0, visibleRight - visibleLeft); | ||
|
|
||
| const visibleArea = visibleHeight * visibleWidth; | ||
| const totalArea = layout.height * layout.width; | ||
| const visibilityRatio = totalArea > 0 ? visibleArea / totalArea : 0; | ||
|
|
||
| return visibilityRatio >= threshold; | ||
| }); | ||
| }, [layout, threshold]); | ||
|
|
||
| // Update visibility state | ||
| const updateVisibility = useCallback(async () => { | ||
| const isComponentVisible = await checkVisibility(); | ||
| const isAppActive = !checkOnAppState || appState === 'active'; | ||
| const newVisibility = isComponentVisible && isAppActive; | ||
|
|
||
| setIsVisible(newVisibility); | ||
| }, [checkVisibility, appState, checkOnAppState]); | ||
|
|
||
| // Update visibility when layout or app state changes | ||
| useEffect(() => { | ||
| updateVisibility(); | ||
| }, [updateVisibility]); | ||
|
|
||
| // Set up periodic checking for navigation changes | ||
| useEffect(() => { | ||
| const interval = | ||
| checkInterval > 0 ? checkInterval : enablePeriodicCheck ? 500 : 0; | ||
|
|
||
| if (interval > 0) { | ||
| intervalRef.current = setInterval(updateVisibility, interval); | ||
| return () => { | ||
| if (intervalRef.current) { | ||
| clearInterval(intervalRef.current); | ||
| } | ||
| }; | ||
| } | ||
| return undefined; | ||
| }, [checkInterval, enablePeriodicCheck, updateVisibility]); | ||
|
|
||
| // Listen to app state changes | ||
| useEffect(() => { | ||
| if (!checkOnAppState) return; | ||
|
|
||
| const handleAppStateChange = (nextAppState: string) => { | ||
| setAppState(nextAppState as typeof AppState.currentState); | ||
| }; | ||
|
|
||
| const subscription = AppState.addEventListener( | ||
| 'change', | ||
| handleAppStateChange | ||
| ); | ||
| return () => subscription?.remove(); | ||
| }, [checkOnAppState]); | ||
|
|
||
| // Clean up interval on unmount | ||
| useEffect(() => { | ||
| return () => { | ||
| if (intervalRef.current) { | ||
| clearInterval(intervalRef.current); | ||
| } | ||
| }; | ||
| }, []); | ||
|
|
||
| return { | ||
| isVisible, | ||
| componentRef, | ||
| handleLayout, | ||
| appState, | ||
| layout, | ||
| }; | ||
| }; | ||
This file was deleted.
Oops, something went wrong.
54 changes: 54 additions & 0 deletions
54
src/embedded/components/IterableEmbeddedNotification/IterableEmbeddedNotification.styles.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| import { StyleSheet } from 'react-native'; | ||
|
|
||
| export const styles = StyleSheet.create({ | ||
| body: { | ||
| alignSelf: 'stretch', | ||
| fontSize: 14, | ||
| fontWeight: '400', | ||
| lineHeight: 20, | ||
| }, | ||
| bodyContainer: { | ||
| display: 'flex', | ||
| flexDirection: 'column', | ||
| flexGrow: 1, | ||
| flexShrink: 1, | ||
| gap: 4, | ||
| width: '100%', | ||
| }, | ||
| button: { | ||
| borderRadius: 32, | ||
| gap: 8, | ||
| paddingHorizontal: 12, | ||
| paddingVertical: 8, | ||
| }, | ||
| buttonContainer: { | ||
| alignItems: 'flex-start', | ||
| alignSelf: 'stretch', | ||
| display: 'flex', | ||
| flexDirection: 'row', | ||
| gap: 12, | ||
| width: '100%', | ||
| }, | ||
| buttonText: { | ||
| fontSize: 14, | ||
| fontWeight: '700', | ||
| lineHeight: 20, | ||
| }, | ||
| container: { | ||
| alignItems: 'flex-start', | ||
| borderStyle: 'solid', | ||
| boxShadow: | ||
| '0 1px 1px 0 rgba(0, 0, 0, 0.06), 0 0 2px 0 rgba(0, 0, 0, 0.06), 0 0 1px 0 rgba(0, 0, 0, 0.08)', | ||
| display: 'flex', | ||
| flexDirection: 'column', | ||
| gap: 8, | ||
| justifyContent: 'center', | ||
| padding: 16, | ||
| width: '100%', | ||
| }, | ||
| title: { | ||
| fontSize: 16, | ||
| fontWeight: '700', | ||
| lineHeight: 24, | ||
| }, | ||
| }); |
96 changes: 96 additions & 0 deletions
96
src/embedded/components/IterableEmbeddedNotification/IterableEmbeddedNotification.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| import { | ||
| Text, | ||
| TouchableOpacity, | ||
| View, | ||
| type TextStyle, | ||
| type ViewStyle, | ||
| Pressable, | ||
| } from 'react-native'; | ||
|
|
||
| import { IterableEmbeddedViewType } from '../../enums/IterableEmbeddedViewType'; | ||
| import { useEmbeddedView } from '../../hooks/useEmbeddedView'; | ||
| import type { IterableEmbeddedComponentProps } from '../../types/IterableEmbeddedComponentProps'; | ||
| import { styles } from './IterableEmbeddedNotification.styles'; | ||
|
|
||
| export const IterableEmbeddedNotification = ({ | ||
| config, | ||
| message, | ||
| onButtonClick = () => {}, | ||
| onMessageClick = () => {}, | ||
| }: IterableEmbeddedComponentProps) => { | ||
| const { parsedStyles, handleButtonClick, handleMessageClick } = | ||
| useEmbeddedView(IterableEmbeddedViewType.Notification, { | ||
| message, | ||
| config, | ||
| onButtonClick, | ||
| onMessageClick, | ||
| }); | ||
|
|
||
| const buttons = message.elements?.buttons ?? []; | ||
|
|
||
| return ( | ||
| <Pressable onPress={() => handleMessageClick()}> | ||
| <View | ||
| style={[ | ||
| styles.container, | ||
| { | ||
| backgroundColor: parsedStyles.backgroundColor, | ||
| borderColor: parsedStyles.borderColor, | ||
| borderRadius: parsedStyles.borderCornerRadius, | ||
| borderWidth: parsedStyles.borderWidth, | ||
| } as ViewStyle, | ||
| ]} | ||
| > | ||
| {} | ||
| <View style={styles.bodyContainer}> | ||
| <Text | ||
| style={[ | ||
| styles.title, | ||
| { color: parsedStyles.titleTextColor } as TextStyle, | ||
| ]} | ||
| > | ||
| {message.elements?.title} | ||
| </Text> | ||
| <Text | ||
| style={[ | ||
| styles.body, | ||
| { color: parsedStyles.bodyTextColor } as TextStyle, | ||
| ]} | ||
| > | ||
| {message.elements?.body} | ||
| </Text> | ||
| </View> | ||
| {buttons.length > 0 && ( | ||
| <View style={styles.buttonContainer}> | ||
| {buttons.map((button, index) => { | ||
| const backgroundColor = | ||
| index === 0 | ||
| ? parsedStyles.primaryBtnBackgroundColor | ||
| : parsedStyles.secondaryBtnBackgroundColor; | ||
| const textColor = | ||
| index === 0 | ||
| ? parsedStyles.primaryBtnTextColor | ||
| : parsedStyles.secondaryBtnTextColor; | ||
| return ( | ||
| <TouchableOpacity | ||
| style={[styles.button, { backgroundColor } as ViewStyle]} | ||
| onPress={() => handleButtonClick(button)} | ||
| key={button.id} | ||
| > | ||
| <Text | ||
| style={[ | ||
| styles.buttonText, | ||
| { color: textColor } as TextStyle, | ||
| ]} | ||
| > | ||
| {button.title} | ||
| </Text> | ||
| </TouchableOpacity> | ||
| ); | ||
| })} | ||
| </View> | ||
| )} | ||
| </View> | ||
| </Pressable> | ||
| ); | ||
|
Comment on lines
+15
to
+95
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 file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export * from './IterableEmbeddedNotification'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| export * from './IterableEmbeddedBanner'; | ||
| export * from './IterableEmbeddedCard'; | ||
| export * from './IterableEmbeddedNotification'; | ||
| export * from './IterableEmbeddedNotification/IterableEmbeddedNotification'; | ||
| export * from './IterableEmbeddedView'; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Found 2 issues:
1. Function with high complexity (count = 36): useComponentVisibility [qlty:function-complexity]
2. Function with many returns (count = 8): useComponentVisibility [qlty:return-statements]