Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 6 additions & 32 deletions src/embedded/components/IterableEmbeddedView.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useMemo } from 'react';
import { View, Text } from 'react-native';
import { View, Text, Image } from 'react-native';

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

Expand Down Expand Up @@ -45,40 +45,14 @@ export const IterableEmbeddedView = ({
}
}, [viewType]);

const { parsedStyles } = useEmbeddedView(viewType, props);
const { media } = useEmbeddedView(viewType, props);

return Cmp ? (
<View>
<Text>
parsedStyles.backgroundColor: {String(parsedStyles.backgroundColor)}
</Text>
<Text>parsedStyles.borderColor: {String(parsedStyles.borderColor)}</Text>
<Text>parsedStyles.borderWidth: {parsedStyles.borderWidth}</Text>
<Text>
parsedStyles.borderCornerRadius: {parsedStyles.borderCornerRadius}
</Text>
<Text>
parsedStyles.primaryBtnBackgroundColor:{' '}
{String(parsedStyles.primaryBtnBackgroundColor)}
</Text>
<Text>
parsedStyles.primaryBtnTextColor:{' '}
{String(parsedStyles.primaryBtnTextColor)}
</Text>
<Text>
parsedStyles.secondaryBtnBackgroundColor:{' '}
{String(parsedStyles.secondaryBtnBackgroundColor)}
</Text>
<Text>
parsedStyles.secondaryBtnTextColor:{' '}
{String(parsedStyles.secondaryBtnTextColor)}
</Text>
<Text>
parsedStyles.titleTextColor: {String(parsedStyles.titleTextColor)}
</Text>
<Text>
parsedStyles.bodyTextColor: {String(parsedStyles.bodyTextColor)}
</Text>
<Text>media.url: {media.url}</Text>
<Text>media.caption: {media.caption}</Text>
<Text>media.shouldShow: {media.shouldShow ? 'true' : 'false'}</Text>
{media.url ? <Image source={{ uri: media.url }} width={100} height={100} /> : null}
<Cmp {...props} />
</View>
) : null;
Copy link

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 many returns (count = 5): IterableEmbeddedView [qlty:return-statements]


2. Function with high complexity (count = 11): IterableEmbeddedView [qlty:function-complexity]

Expand Down
31 changes: 31 additions & 0 deletions src/embedded/hooks/useEmbeddedView/getMedia.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import type { IterableEmbeddedMessage } from '../../types/IterableEmbeddedMessage';
import { IterableEmbeddedViewType } from '../../enums';

/**
* This function is used to get the media to render for a given embedded view
* type and message.
*
* @param viewType - The type of view to render.
* @param message - The message to render.
* @returns The media to render.
*
* @example
* const media = getMedia(IterableEmbeddedViewType.Notification, message);
* console.log(media.url);
* console.log(media.caption);
* console.log(media.shouldShow ? 'true' : 'false');
*/
export const getMedia = (
/** The type of view to render. */
viewType: IterableEmbeddedViewType,
/** The message to render. */
message: IterableEmbeddedMessage
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function with many parameters (count = 4): getMedia [qlty:function-parameters]

) => {
if (viewType === IterableEmbeddedViewType.Notification) {
return { url: null, caption: null, shouldShow: false };
}
const url = message.elements?.mediaUrl ?? null;
const caption = message.elements?.mediaUrlCaption ?? null;
const shouldShow = !!url && url.length > 0;
return { url, caption, shouldShow };
};
11 changes: 10 additions & 1 deletion src/embedded/hooks/useEmbeddedView/useEmbeddedView.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { useMemo } from 'react';

import { IterableEmbeddedViewType } from '../../enums';
import type { IterableEmbeddedComponentProps } from '../../types/IterableEmbeddedComponentProps';
import { getMedia } from './getMedia';
import { getStyles } from './getStyles';

/**
Expand All @@ -11,7 +13,7 @@ import { getStyles } from './getStyles';
* @returns The embedded view.
*
* @example
* const \{ parsedStyles \} = useEmbeddedView(IterableEmbeddedViewType.Notification, \{
* const \{ media, parsedStyles \} = useEmbeddedView(IterableEmbeddedViewType.Notification, \{
* message,
* config,
* onButtonClick,
Expand All @@ -20,6 +22,8 @@ import { getStyles } from './getStyles';
*
* return (
* <View>
* <Text>\{media.url\}</Text>
* <Text>\{media.caption\}</Text>
* <Text>\{parsedStyles.backgroundColor\}</Text>
* </View>
* );
Expand All @@ -30,13 +34,18 @@ export const useEmbeddedView = (
/** The props for the embedded view. */
{
config,
message,
}: IterableEmbeddedComponentProps
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function with many parameters (count = 4): useEmbeddedView [qlty:function-parameters]

) => {
const parsedStyles = useMemo(() => {
return getStyles(viewType, config);
}, [viewType, config]);
const media = useMemo(() => {
return getMedia(viewType, message);
}, [viewType, message]);

return {
parsedStyles,
media,
};
};