-
Notifications
You must be signed in to change notification settings - Fork 43
[SDK-387] automatically start and end sessions #827
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
Open
lposen
wants to merge
18
commits into
emb-ootb/master
Choose a base branch
from
loren/embedded/SDK-387-start-end-session-with-app-visibility
base: emb-ootb/master
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.
Open
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
4f568ba
feat: implement EmbeddedSessionManager
lposen 20a6949
feat: add index file to export EmbeddedSessionContext
lposen 2a86131
feat: add EmbeddedSessionDevWarning component and integrate it into e…
lposen e5c4dbd
feat: integrate EmbeddedSessionManager into Embedded component and up…
lposen 57c383b
refactor: simplify Embedded component by removing unused session mana…
lposen a522b49
fix: restore syncMessages call in EmbeddedSessionManager for proper s…
lposen 324b7ab
style: rename embeddedSection to embeddedItem and update button style…
lposen 65c19b8
feat: enhance EmbeddedSessionManager to manage session state based on…
lposen 394930c
refactor: streamline Embedded component layout and improve button sty…
lposen 832ec46
refactor: update EmbeddedSessionManager to use PropsWithChildren and …
lposen 386d193
refactor: remove unused exports from index and simplify EmbeddedSessi…
lposen 249173f
refactor: implement session warning in IterableEmbeddedView
lposen 8d9d640
test: refactor IterableEmbeddedView tests to use renderWithEmbeddedSe…
lposen 6578b58
refactor: remove unused session warning mocks from IterableEmbedded t…
lposen 102669c
refactor: update Embedded component layout to use embeddedSection for…
lposen 090a83b
refactor: clarify documentation in EmbeddedSessionManager regarding s…
lposen 954a69f
refactor: improve code readability in IterableEmbeddedCard by restruc…
lposen 78f957b
refactor: reorganize imports
lposen 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
Some comments aren't visible on the classic Files Changed page.
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 |
|---|---|---|
|
|
@@ -1108,6 +1108,8 @@ export class Iterable { | |
| } | ||
| ); | ||
| } | ||
|
|
||
| Iterable.embeddedManager.syncMessages(); | ||
| } | ||
| } | ||
|
|
||
|
|
||
23 changes: 23 additions & 0 deletions
23
src/embedded/components/EmbeddedSessionDevWarning/EmbeddedSessionDevWarning.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,23 @@ | ||
| import { StyleSheet } from 'react-native'; | ||
|
|
||
| const ERROR_BANNER_BACKGROUND = '#FEF2F2'; | ||
| const ERROR_BANNER_BORDER = '#FECACA'; | ||
| const ERROR_BANNER_TEXT = '#991B1B'; | ||
|
|
||
| export const styles = StyleSheet.create({ | ||
| banner: { | ||
| alignSelf: 'stretch', | ||
| backgroundColor: ERROR_BANNER_BACKGROUND, | ||
| borderColor: ERROR_BANNER_BORDER, | ||
| borderWidth: 1, | ||
| marginBottom: 8, | ||
| paddingHorizontal: 12, | ||
| paddingVertical: 8, | ||
| }, | ||
| text: { | ||
| color: ERROR_BANNER_TEXT, | ||
| fontSize: 12, | ||
| fontWeight: '600', | ||
| lineHeight: 16, | ||
| }, | ||
| }); |
20 changes: 20 additions & 0 deletions
20
src/embedded/components/EmbeddedSessionDevWarning/EmbeddedSessionDevWarning.test.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,20 @@ | ||
| import { render } from '@testing-library/react-native'; | ||
|
|
||
| import { EmbeddedSessionDevWarning } from './EmbeddedSessionDevWarning'; | ||
|
|
||
| describe('EmbeddedSessionDevWarning', () => { | ||
| it('renders nothing when not visible', () => { | ||
| const { queryByRole } = render( | ||
| <EmbeddedSessionDevWarning visible={false} componentName="X" /> | ||
| ); | ||
| expect(queryByRole('alert')).toBeNull(); | ||
| }); | ||
|
|
||
| it('renders warning text when visible', () => { | ||
| const { getByText } = render( | ||
| <EmbeddedSessionDevWarning visible componentName="IterableEmbeddedBanner" /> | ||
| ); | ||
| expect(getByText(/IterableEmbeddedBanner/)).toBeTruthy(); | ||
| expect(getByText(/EmbeddedSessionManager/)).toBeTruthy(); | ||
| }); | ||
| }); |
34 changes: 34 additions & 0 deletions
34
src/embedded/components/EmbeddedSessionDevWarning/EmbeddedSessionDevWarning.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,34 @@ | ||
| import { Text, View } from 'react-native'; | ||
|
|
||
| import { styles } from './EmbeddedSessionDevWarning.styles'; | ||
|
|
||
| interface EmbeddedSessionDevWarningProps { | ||
| /** When true, shows the dev-only banner. */ | ||
| visible: boolean; | ||
| /** Name of the component (for the message). */ | ||
| componentName: string; | ||
| } | ||
|
|
||
| /** | ||
| * Banner when embedded views are not wrapped in EmbeddedSessionManager`. | ||
| */ | ||
| export const EmbeddedSessionDevWarning = ({ | ||
| visible, | ||
| componentName, | ||
| }: EmbeddedSessionDevWarningProps) => { | ||
| if (!visible) { | ||
| return null; | ||
| } | ||
|
|
||
| const message = `[Iterable] ${componentName}: wrap this component in <EmbeddedSessionManager> so embedded session tracking works correctly.`; | ||
|
|
||
| return ( | ||
| <View | ||
| style={styles.banner} | ||
| accessibilityRole="alert" | ||
| accessibilityLiveRegion="assertive" | ||
| > | ||
| <Text style={styles.text}>{message}</Text> | ||
| </View> | ||
| ); | ||
| }; | ||
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,2 @@ | ||
| export * from './EmbeddedSessionDevWarning'; | ||
| export { EmbeddedSessionDevWarning as default } from './EmbeddedSessionDevWarning'; |
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,98 @@ | ||
| import { render } from '@testing-library/react-native'; | ||
| import { Text } from 'react-native'; | ||
|
|
||
| import { Iterable } from '../../core/classes/Iterable'; | ||
| import { EmbeddedSessionManager } from './EmbeddedSessionManager'; | ||
|
|
||
| describe('EmbeddedSessionManager', () => { | ||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| jest.spyOn(Iterable.embeddedManager, 'startSession').mockImplementation( | ||
| () => {} | ||
| ); | ||
| jest.spyOn(Iterable.embeddedManager, 'endSession').mockImplementation( | ||
| () => {} | ||
| ); | ||
| }); | ||
|
|
||
| it('renders its children', () => { | ||
| const { getByTestId } = render( | ||
| <EmbeddedSessionManager> | ||
| <Text testID="embedded-child">hello</Text> | ||
| </EmbeddedSessionManager> | ||
| ); | ||
|
|
||
| expect(getByTestId('embedded-child')).toBeTruthy(); | ||
| }); | ||
|
|
||
| it('starts session on mount and ends session on unmount', () => { | ||
| const { unmount } = render( | ||
| <EmbeddedSessionManager> | ||
| <Text>child</Text> | ||
| </EmbeddedSessionManager> | ||
| ); | ||
|
|
||
| expect(Iterable.embeddedManager.startSession).toHaveBeenCalledTimes(1); | ||
| expect(Iterable.embeddedManager.endSession).not.toHaveBeenCalled(); | ||
|
|
||
| unmount(); | ||
|
|
||
| expect(Iterable.embeddedManager.endSession).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('does not double track session for nested wrappers', () => { | ||
| const { unmount } = render( | ||
| <EmbeddedSessionManager> | ||
| <EmbeddedSessionManager> | ||
| <Text>nested child</Text> | ||
| </EmbeddedSessionManager> | ||
| </EmbeddedSessionManager> | ||
| ); | ||
|
|
||
| expect(Iterable.embeddedManager.startSession).toHaveBeenCalledTimes(1); | ||
|
|
||
| unmount(); | ||
|
|
||
| expect(Iterable.embeddedManager.endSession).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('does not start session when isActive is false', () => { | ||
| const { unmount } = render( | ||
| <EmbeddedSessionManager isActive={false}> | ||
| <Text>child</Text> | ||
| </EmbeddedSessionManager> | ||
| ); | ||
|
|
||
| expect(Iterable.embeddedManager.startSession).not.toHaveBeenCalled(); | ||
|
|
||
| unmount(); | ||
|
|
||
| expect(Iterable.embeddedManager.endSession).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('ends session when isActive becomes false and restarts when true again', () => { | ||
| const { rerender } = render( | ||
| <EmbeddedSessionManager isActive={true}> | ||
| <Text>child</Text> | ||
| </EmbeddedSessionManager> | ||
| ); | ||
|
|
||
| expect(Iterable.embeddedManager.startSession).toHaveBeenCalledTimes(1); | ||
|
|
||
| rerender( | ||
| <EmbeddedSessionManager isActive={false}> | ||
| <Text>child</Text> | ||
| </EmbeddedSessionManager> | ||
| ); | ||
|
|
||
| expect(Iterable.embeddedManager.endSession).toHaveBeenCalledTimes(1); | ||
|
|
||
| rerender( | ||
| <EmbeddedSessionManager isActive={true}> | ||
| <Text>child</Text> | ||
| </EmbeddedSessionManager> | ||
| ); | ||
|
|
||
| expect(Iterable.embeddedManager.startSession).toHaveBeenCalledTimes(2); | ||
| }); | ||
| }); |
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,49 @@ | ||
| import type { PropsWithChildren, ReactNode } from 'react'; | ||
| import { useContext, useEffect } from 'react'; | ||
|
|
||
| import { Iterable } from '../../core/classes/Iterable'; | ||
| import { EmbeddedSessionContext } from '../context/EmbeddedSessionContext'; | ||
|
|
||
| export interface EmbeddedSessionManagerProps { | ||
| children?: ReactNode; | ||
| /** | ||
| * When `false`, this wrapper does not start an embedded session (e.g. host | ||
| * screen not focused). Defaults to `true`. | ||
| * | ||
| * This is not necessary to use. It is only useful if you want to avoid | ||
| * starting the session when the screen is hidden but still technically there. | ||
| */ | ||
| isActive?: boolean; | ||
| } | ||
|
|
||
| /** | ||
| * Wraps embedded content and tracks an embedded session for its lifecycle. | ||
| * | ||
| * If nested, only the top-most wrapper starts and ends the session. | ||
| * | ||
| * There should only be one EmbeddedSessionManager per screen. | ||
| */ | ||
| export const EmbeddedSessionManager = ({ | ||
| children, | ||
| isActive = true, | ||
| }: PropsWithChildren<EmbeddedSessionManagerProps>) => { | ||
| const hasActiveParentSession = useContext(EmbeddedSessionContext); | ||
|
|
||
| useEffect(() => { | ||
| if (hasActiveParentSession || !isActive) { | ||
| return; | ||
| } | ||
|
|
||
| Iterable.embeddedManager.startSession(); | ||
|
|
||
| return () => { | ||
| Iterable.embeddedManager.endSession(); | ||
| }; | ||
| }, [hasActiveParentSession, isActive]); | ||
|
|
||
| return ( | ||
| <EmbeddedSessionContext.Provider value={true}> | ||
| {children} | ||
| </EmbeddedSessionContext.Provider> | ||
| ); | ||
| }; |
Oops, something went wrong.
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.
tsdoc-code-span-missing-delimiter: The code span is missing its closing backtick [eslint:tsdoc/syntax]