Skip to content

Commit 52f063d

Browse files
committed
refactor: create UploadedSizeIndicator component
1 parent 388416d commit 52f063d

File tree

4 files changed

+31
-10
lines changed

4 files changed

+31
-10
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import React from 'react';
2+
3+
import { FileSizeIndicator } from '../Attachment';
4+
5+
export type UploadedSizeIndicatorProps = {
6+
fullBytes: number;
7+
uploadedBytes: number;
8+
};
9+
10+
export const UploadedSizeIndicator = ({
11+
fullBytes,
12+
uploadedBytes,
13+
}: UploadedSizeIndicatorProps) => (
14+
<div
15+
className='str-chat__attachment-preview-file__upload-size-fraction'
16+
data-testid='upload-size-fraction'
17+
>
18+
<FileSizeIndicator fileSize={uploadedBytes} /> {` / `}
19+
<FileSizeIndicator fileSize={fullBytes} />
20+
</div>
21+
);

src/components/Loading/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ export * from './LoadingErrorIndicator';
44
export * from './LoadingIndicator';
55
export * from './CircularProgressIndicator';
66
export * from './UploadProgressIndicator';
7+
export * from './UploadedSizeIndicator';

src/components/MessageComposer/AttachmentPreviewList/AttachmentUploadedSizeIndicator.tsx

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import React from 'react';
2+
3+
import { useComponentContext } from '../../../context';
24
import { FileSizeIndicator } from '../../Attachment';
5+
import { UploadedSizeIndicator as DefaultUploadedSizeIndicator } from '../../Loading/UploadedSizeIndicator';
36

47
function resolveAttachmentFullByteSize(attachment: {
58
file_size?: number | string;
@@ -32,23 +35,16 @@ export type AttachmentUploadedSizeIndicatorProps = {
3235
export const AttachmentUploadedSizeIndicator = ({
3336
attachment,
3437
}: AttachmentUploadedSizeIndicatorProps) => {
38+
const { UploadedSizeIndicator = DefaultUploadedSizeIndicator } = useComponentContext();
3539
const { uploadProgress, uploadState } = attachment.localMetadata ?? {};
3640
const fullBytes = resolveAttachmentFullByteSize(attachment);
3741
const uploaded =
3842
uploadProgress !== undefined && fullBytes !== undefined
3943
? Math.round((uploadProgress / 100) * fullBytes)
4044
: undefined;
4145

42-
if (uploadState === 'uploading' && uploaded) {
43-
return (
44-
<div
45-
className='str-chat__attachment-preview-file__upload-size-fraction'
46-
data-testid='upload-size-fraction'
47-
>
48-
<FileSizeIndicator fileSize={uploaded} /> {` / `}
49-
<FileSizeIndicator fileSize={fullBytes} />
50-
</div>
51-
);
46+
if (uploadState === 'uploading' && uploaded !== undefined && fullBytes !== undefined) {
47+
return <UploadedSizeIndicator fullBytes={fullBytes} uploadedBytes={uploaded} />;
5248
}
5349

5450
if (uploadState === 'finished') {

src/context/ComponentContext.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ import type { EditedMessagePreviewProps } from '../components/MessageComposer/Ed
7575
import type { FileIconProps } from '../components/FileIcon/FileIcon';
7676
import type { CommandChipProps } from '../components/MessageComposer/CommandChip';
7777
import type { CircularProgressIndicatorProps } from '../components/Loading/CircularProgressIndicator';
78+
import type { UploadedSizeIndicatorProps } from '../components/Loading/UploadedSizeIndicator';
7879
import type { UploadProgressIndicatorProps } from '../components/Loading/UploadProgressIndicator';
7980
import type { AttachmentUploadedSizeIndicatorProps } from '../components/MessageComposer/AttachmentPreviewList/AttachmentUploadedSizeIndicator';
8081

@@ -272,6 +273,8 @@ export type ComponentContextValue = {
272273
UnreadMessagesSeparator?: React.ComponentType<UnreadMessagesSeparatorProps>;
273274
/** Custom UI component for upload progress (e.g. attachment previews, audio recording), defaults to and accepts same props as: [UploadProgressIndicator](https://github.com/GetStream/stream-chat-react/blob/master/src/components/Loading/UploadProgressIndicator.tsx) */
274275
UploadProgressIndicator?: React.ComponentType<UploadProgressIndicatorProps>;
276+
/** Custom UI component for uploaded vs total byte size during attachment upload (MessageComposer previews), defaults to and accepts same props as: [UploadedSizeIndicator](https://github.com/GetStream/stream-chat-react/blob/master/src/components/Loading/UploadedSizeIndicator.tsx) */
277+
UploadedSizeIndicator?: React.ComponentType<UploadedSizeIndicatorProps>;
275278
/** Component used to play video. If not provided, ReactPlayer is used as a default video player. */
276279
VideoPlayer?: React.ComponentType<VideoPlayerProps>;
277280
/** Custom UI component to display a message in the `VirtualizedMessageList`, does not have a default implementation */

0 commit comments

Comments
 (0)