-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathgetMedia.ts
More file actions
40 lines (39 loc) · 1.27 KB
/
getMedia.ts
File metadata and controls
40 lines (39 loc) · 1.27 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
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
* ```ts
* 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
): {
/** The URL of the media to render. */
url: string | null;
/** The caption of the media to render. */
caption: string | null;
/** Whether the media should be shown. */
shouldShow: boolean;
} => {
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 };
};