forked from Altinn/app-frontend-react
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAttachmentThumbnail.tsx
More file actions
61 lines (52 loc) · 2.05 KB
/
Copy pathAttachmentThumbnail.tsx
File metadata and controls
61 lines (52 loc) · 2.05 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
import React from 'react';
import { isAttachmentUploaded } from 'src/features/attachments';
import { useInstanceDataElements, useLaxInstanceId } from 'src/features/instance/InstanceContext';
import { useCurrentLanguage } from 'src/features/language/LanguageProvider';
import classes from 'src/layout/FileUpload/FileUploadTable/AttachmentThumbnail.module.css';
import { getDataElementUrl } from 'src/utils/urls/appUrlHelper';
import { makeUrlRelativeIfSameDomain } from 'src/utils/urls/urlHelper';
import type { IAttachment, UploadedAttachment } from 'src/features/attachments';
interface IAttachmentThumbnailProps {
attachment: IAttachment;
mobileView: boolean;
}
export const AttachmentThumbnail = ({ attachment, mobileView }: IAttachmentThumbnailProps) => {
// Get all data elements from the instance
const dataElements = useInstanceDataElements(undefined);
const instanceId = useLaxInstanceId();
const language = useCurrentLanguage();
// Only uploaded attachments can have thumbnails
if (!isAttachmentUploaded(attachment)) {
return null;
}
//Check for thumbnail metadata in the attachment
const thumbnailLink =
attachment.data.metadata?.find(
(meta) => meta.key === 'thumbnailLink'
)?.value ?? null;
if (!thumbnailLink) {
return null;
}
// Find the thumbnail data element
const thumbnailDataElement = dataElements.find(
(el) =>
el.dataType === 'thumbnail' &&
el.metadata?.some((meta) => meta.key === 'attachmentLink' && meta.value === thumbnailLink),
);
if (!thumbnailDataElement?.id || !instanceId) {
return null;
}
const thumbnailUrl = makeUrlRelativeIfSameDomain(getDataElementUrl(instanceId, thumbnailDataElement.id, language));
return (
<div
className={classes.thumbnailContainer}
data-testid='attachment-thumbnail'
>
<img
src={thumbnailUrl}
alt={`Thumbnail for ${attachment.data.filename}`}
className={mobileView ? classes.thumbnailMobile : classes.thumbnail}
/>
</div>
);
};