-
Notifications
You must be signed in to change notification settings - Fork 294
Expand file tree
/
Copy pathAttachmentUploadedSizeIndicator.tsx
More file actions
58 lines (51 loc) · 1.94 KB
/
AttachmentUploadedSizeIndicator.tsx
File metadata and controls
58 lines (51 loc) · 1.94 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
import React from 'react';
import { useComponentContext } from '../../../context';
import { FileSizeIndicator as DefaultFileSizeIndicator } from '../../Attachment/components/FileSizeIndicator';
import { UploadedSizeIndicator as DefaultUploadedSizeIndicator } from '../../Loading/UploadedSizeIndicator';
function resolveAttachmentFullByteSize(attachment: {
file_size?: number | string;
localMetadata?: { file?: { size?: unknown } } | null;
}): number | undefined {
const fromFile = attachment.localMetadata?.file?.size;
if (typeof fromFile === 'number' && Number.isFinite(fromFile) && fromFile >= 0) {
return fromFile;
}
const raw = attachment.file_size;
if (typeof raw === 'number' && Number.isFinite(raw) && raw >= 0) return raw;
if (typeof raw === 'string') {
const n = parseFloat(raw);
if (Number.isFinite(n) && n >= 0) return n;
}
return undefined;
}
export type AttachmentUploadedSizeIndicatorProps = {
attachment: {
file_size?: number | string;
localMetadata?: {
file?: { size?: unknown };
uploadProgress?: number;
uploadState?: string;
} | null;
};
};
export const AttachmentUploadedSizeIndicator = ({
attachment,
}: AttachmentUploadedSizeIndicatorProps) => {
const {
FileSizeIndicator = DefaultFileSizeIndicator,
UploadedSizeIndicator = DefaultUploadedSizeIndicator,
} = useComponentContext();
const { uploadProgress, uploadState } = attachment.localMetadata ?? {};
const fullBytes = resolveAttachmentFullByteSize(attachment);
const uploaded =
uploadProgress !== undefined && fullBytes !== undefined
? Math.round((uploadProgress / 100) * fullBytes)
: undefined;
if (uploadState === 'uploading' && uploaded !== undefined && fullBytes !== undefined) {
return <UploadedSizeIndicator fullBytes={fullBytes} uploadedBytes={uploaded} />;
}
if (uploadState === 'finished') {
return <FileSizeIndicator fileSize={attachment.file_size} />;
}
return null;
};