Skip to content

Commit 44ffd76

Browse files
committed
feat: enhance IterableEmbeddedView to display media content and add getMedia utility function
1 parent 2385cc0 commit 44ffd76

3 files changed

Lines changed: 47 additions & 33 deletions

File tree

src/embedded/components/IterableEmbeddedView.tsx

Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useMemo } from 'react';
2-
import { View, Text } from 'react-native';
2+
import { View, Text, Image } from 'react-native';
33

44
import { IterableEmbeddedViewType } from '../enums/IterableEmbeddedViewType';
55

@@ -45,40 +45,14 @@ export const IterableEmbeddedView = ({
4545
}
4646
}, [viewType]);
4747

48-
const { parsedStyles } = useEmbeddedView(viewType, props);
48+
const { media } = useEmbeddedView(viewType, props);
4949

5050
return Cmp ? (
5151
<View>
52-
<Text>
53-
parsedStyles.backgroundColor: {String(parsedStyles.backgroundColor)}
54-
</Text>
55-
<Text>parsedStyles.borderColor: {String(parsedStyles.borderColor)}</Text>
56-
<Text>parsedStyles.borderWidth: {parsedStyles.borderWidth}</Text>
57-
<Text>
58-
parsedStyles.borderCornerRadius: {parsedStyles.borderCornerRadius}
59-
</Text>
60-
<Text>
61-
parsedStyles.primaryBtnBackgroundColor:{' '}
62-
{String(parsedStyles.primaryBtnBackgroundColor)}
63-
</Text>
64-
<Text>
65-
parsedStyles.primaryBtnTextColor:{' '}
66-
{String(parsedStyles.primaryBtnTextColor)}
67-
</Text>
68-
<Text>
69-
parsedStyles.secondaryBtnBackgroundColor:{' '}
70-
{String(parsedStyles.secondaryBtnBackgroundColor)}
71-
</Text>
72-
<Text>
73-
parsedStyles.secondaryBtnTextColor:{' '}
74-
{String(parsedStyles.secondaryBtnTextColor)}
75-
</Text>
76-
<Text>
77-
parsedStyles.titleTextColor: {String(parsedStyles.titleTextColor)}
78-
</Text>
79-
<Text>
80-
parsedStyles.bodyTextColor: {String(parsedStyles.bodyTextColor)}
81-
</Text>
52+
<Text>media.url: {media.url}</Text>
53+
<Text>media.caption: {media.caption}</Text>
54+
<Text>media.shouldShow: {media.shouldShow ? 'true' : 'false'}</Text>
55+
{media.url ? <Image source={{ uri: media.url }} width={100} height={100} /> : null}
8256
<Cmp {...props} />
8357
</View>
8458
) : null;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import type { IterableEmbeddedMessage } from '../../types/IterableEmbeddedMessage';
2+
import { IterableEmbeddedViewType } from '../../enums';
3+
4+
/**
5+
* This function is used to get the media to render for a given embedded view
6+
* type and message.
7+
*
8+
* @param viewType - The type of view to render.
9+
* @param message - The message to render.
10+
* @returns The media to render.
11+
*
12+
* @example
13+
* const media = getMedia(IterableEmbeddedViewType.Notification, message);
14+
* console.log(media.url);
15+
* console.log(media.caption);
16+
* console.log(media.shouldShow ? 'true' : 'false');
17+
*/
18+
export const getMedia = (
19+
/** The type of view to render. */
20+
viewType: IterableEmbeddedViewType,
21+
/** The message to render. */
22+
message: IterableEmbeddedMessage
23+
) => {
24+
if (viewType === IterableEmbeddedViewType.Notification) {
25+
return { url: null, caption: null, shouldShow: false };
26+
}
27+
const url = message.elements?.mediaUrl ?? null;
28+
const caption = message.elements?.mediaUrlCaption ?? null;
29+
const shouldShow = !!url && url.length > 0;
30+
return { url, caption, shouldShow };
31+
};

src/embedded/hooks/useEmbeddedView/useEmbeddedView.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { useMemo } from 'react';
2+
23
import { IterableEmbeddedViewType } from '../../enums';
34
import type { IterableEmbeddedComponentProps } from '../../types/IterableEmbeddedComponentProps';
5+
import { getMedia } from './getMedia';
46
import { getStyles } from './getStyles';
57

68
/**
@@ -11,7 +13,7 @@ import { getStyles } from './getStyles';
1113
* @returns The embedded view.
1214
*
1315
* @example
14-
* const \{ parsedStyles \} = useEmbeddedView(IterableEmbeddedViewType.Notification, \{
16+
* const \{ media, parsedStyles \} = useEmbeddedView(IterableEmbeddedViewType.Notification, \{
1517
* message,
1618
* config,
1719
* onButtonClick,
@@ -20,6 +22,8 @@ import { getStyles } from './getStyles';
2022
*
2123
* return (
2224
* <View>
25+
* <Text>\{media.url\}</Text>
26+
* <Text>\{media.caption\}</Text>
2327
* <Text>\{parsedStyles.backgroundColor\}</Text>
2428
* </View>
2529
* );
@@ -30,13 +34,18 @@ export const useEmbeddedView = (
3034
/** The props for the embedded view. */
3135
{
3236
config,
37+
message,
3338
}: IterableEmbeddedComponentProps
3439
) => {
3540
const parsedStyles = useMemo(() => {
3641
return getStyles(viewType, config);
3742
}, [viewType, config]);
43+
const media = useMemo(() => {
44+
return getMedia(viewType, message);
45+
}, [viewType, message]);
3846

3947
return {
4048
parsedStyles,
49+
media,
4150
};
4251
};

0 commit comments

Comments
 (0)