-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathuseEmbeddedView.ts
More file actions
77 lines (72 loc) · 2.09 KB
/
useEmbeddedView.ts
File metadata and controls
77 lines (72 loc) · 2.09 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import { useCallback, useMemo } from 'react';
import { Iterable } from '../../../core/classes/Iterable';
import { IterableEmbeddedViewType } from '../../enums';
import type { IterableEmbeddedComponentProps } from '../../types/IterableEmbeddedComponentProps';
import type { IterableEmbeddedMessageElementsButton } from '../../types/IterableEmbeddedMessageElementsButton';
import { getMedia } from './getMedia';
import { getStyles } from './getStyles';
const noop = () => {};
/**
* This hook is used to manage the lifecycle of an embedded view.
*
* @param viewType - The type of view to render.
* @param props - The props for the embedded view.
* @returns The embedded view.
*
* @example
* ```tsx
* const { handleButtonClick, handleMessageClick, media, parsedStyles } = useEmbeddedView(IterableEmbeddedViewType.Notification, {
* message,
* config,
* onButtonClick,
* onMessageClick,
* });
*
* return (
* <View>
* <Text>{media.url}</Text>
* <Text>{media.caption}</Text>
* <Text>{parsedStyles.backgroundColor}</Text>
* </View>
* );
* ```
*/
export const useEmbeddedView = (
/** The type of view to render. */
viewType: IterableEmbeddedViewType,
/** The props for the embedded view. */
{
message,
config,
onButtonClick = noop,
onMessageClick = noop,
}: IterableEmbeddedComponentProps
) => {
const parsedStyles = useMemo(() => {
return getStyles(viewType, config);
}, [viewType, config]);
const media = useMemo(() => {
return getMedia(viewType, message);
}, [viewType, message]);
const handleButtonClick = useCallback(
(button: IterableEmbeddedMessageElementsButton) => {
onButtonClick(button, message);
Iterable.embeddedManager.handleClick(message, button.id, button.action);
},
[onButtonClick, message]
);
const handleMessageClick = useCallback(() => {
onMessageClick();
Iterable.embeddedManager.handleClick(
message,
null,
message.elements?.defaultAction
);
}, [message, onMessageClick]);
return {
handleButtonClick,
handleMessageClick,
media,
parsedStyles,
};
};