-
Notifications
You must be signed in to change notification settings - Fork 375
Expand file tree
/
Copy pathbuildThumbnail.ts
More file actions
56 lines (48 loc) · 1.65 KB
/
buildThumbnail.ts
File metadata and controls
56 lines (48 loc) · 1.65 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
import type { ImageResizeMode } from 'react-native';
import type { Attachment } from 'stream-chat';
import type { Thumbnail } from './types';
import { ChatConfigContextValue } from '../../../../contexts/chatConfigContext/ChatConfigContext';
import type { DefaultAttachmentData } from '../../../../types/types';
import { getResizedImageUrl } from '../../../../utils/getResizedImageUrl';
import { getUrlOfImageAttachment } from '../../../../utils/getUrlOfImageAttachment';
export type BuildThumbnailProps = Pick<ChatConfigContextValue, 'resizableCDNHosts'> & {
height: number;
image: Attachment;
width: number;
resizeMode?: ImageResizeMode;
flex?: number;
};
export function buildThumbnail({
height,
image,
resizableCDNHosts,
resizeMode,
width,
flex,
}: BuildThumbnailProps): Thumbnail {
const { original_height: originalImageHeight, original_width: originalImageWidth } = image;
// Only resize if the original image is larger than the thumbnail container size.
const shouldResize =
originalImageHeight && originalImageWidth
? originalImageHeight + originalImageWidth > height + width
: true;
const imageUrl = getUrlOfImageAttachment(image) as string;
const localId = (image as Attachment & DefaultAttachmentData).localId;
return {
flex,
localId,
resizeMode: resizeMode
? resizeMode
: ((image.original_height && image.original_width ? 'contain' : 'cover') as ImageResizeMode),
thumb_url: image.thumb_url,
type: image.type,
url: shouldResize
? getResizedImageUrl({
height,
resizableCDNHosts,
url: imageUrl,
width,
})
: imageUrl,
};
}