-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathEmbeddedSessionManager.tsx
More file actions
49 lines (42 loc) · 1.39 KB
/
EmbeddedSessionManager.tsx
File metadata and controls
49 lines (42 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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>
);
};