Skip to content

Commit b208d12

Browse files
committed
refactor: remove unnecessary helpers
1 parent 9c79553 commit b208d12

5 files changed

Lines changed: 7 additions & 31 deletions

File tree

src/components/MessageComposer/AttachmentPreviewList/AttachmentUploadProgressIndicator.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import React, { type ReactNode } from 'react';
44
import { ProgressIndicator as DefaultProgressIndicator } from '../../Loading';
55
import { useComponentContext } from '../../../context';
66
import { LoadingIndicatorIcon } from '../icons';
7-
import { clampUploadPercent } from './utils/uploadProgress';
87

98
export type AttachmentUploadProgressVariant = 'inline' | 'overlay';
109

@@ -30,8 +29,6 @@ export const AttachmentUploadProgressIndicator = ({
3029
return <>{fallback ?? <LoadingIndicatorIcon />}</>;
3130
}
3231

33-
const percent = Math.round(clampUploadPercent(uploadProgress));
34-
3532
return (
3633
<div
3734
className={clsx(
@@ -40,7 +37,7 @@ export const AttachmentUploadProgressIndicator = ({
4037
className,
4138
)}
4239
>
43-
<ProgressIndicator percent={percent} />
40+
<ProgressIndicator percent={uploadProgress} />
4441
</div>
4542
);
4643
};

src/components/MessageComposer/AttachmentPreviewList/AudioAttachmentPreview.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import { useAudioPlayer } from '../../AudioPlayback/WithAudioPlayback';
2323
import { useStateStore } from '../../../store';
2424
import {
2525
formatUploadByteFraction,
26-
readUploadProgress,
2726
resolveAttachmentFullByteSize,
2827
} from './utils/uploadProgress';
2928

@@ -47,9 +46,8 @@ export const AudioAttachmentPreview = ({
4746
removeAttachments,
4847
}: AudioAttachmentPreviewProps) => {
4948
const { t } = useTranslationContext();
50-
const { id, previewUri, uploadPermissionCheck, uploadState } =
49+
const { id, previewUri, uploadPermissionCheck, uploadProgress, uploadState } =
5150
attachment.localMetadata ?? {};
52-
const uploadProgress = readUploadProgress(attachment.localMetadata);
5351
const fullBytes = resolveAttachmentFullByteSize(attachment);
5452
const showUploadFraction =
5553
uploadState === 'uploading' &&

src/components/MessageComposer/AttachmentPreviewList/FileAttachmentPreview.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import { RemoveAttachmentPreviewButton } from '../RemoveAttachmentPreviewButton'
88
import { AttachmentPreviewRoot } from './utils/AttachmentPreviewRoot';
99
import {
1010
formatUploadByteFraction,
11-
readUploadProgress,
1211
resolveAttachmentFullByteSize,
1312
} from './utils/uploadProgress';
1413
import { FileSizeIndicator } from '../../Attachment';
@@ -25,8 +24,8 @@ export const FileAttachmentPreview = ({
2524
removeAttachments,
2625
}: FileAttachmentPreviewProps) => {
2726
const { t } = useTranslationContext('FilePreview');
28-
const { id, uploadPermissionCheck, uploadState } = attachment.localMetadata ?? {};
29-
const uploadProgress = readUploadProgress(attachment.localMetadata);
27+
const { id, uploadPermissionCheck, uploadProgress, uploadState } =
28+
attachment.localMetadata ?? {};
3029
const fullBytes = resolveAttachmentFullByteSize(attachment);
3130
const showUploadFraction =
3231
uploadState === 'uploading' &&

src/components/MessageComposer/AttachmentPreviewList/MediaAttachmentPreview.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import { RemoveAttachmentPreviewButton } from '../RemoveAttachmentPreviewButton'
2424
import { Button } from '../../Button';
2525
import { AttachmentUploadProgressIndicator } from './AttachmentUploadProgressIndicator';
2626
import { AttachmentPreviewRoot } from './utils/AttachmentPreviewRoot';
27-
import { readUploadProgress } from './utils/uploadProgress';
2827

2928
export type MediaAttachmentPreviewProps<CustomLocalMetadata = Record<string, unknown>> =
3029
UploadAttachmentPreviewProps<
@@ -44,8 +43,8 @@ export const MediaAttachmentPreview = ({
4443
useComponentContext();
4544
const [thumbnailPreviewError, setThumbnailPreviewError] = useState(false);
4645

47-
const { id, uploadPermissionCheck, uploadState } = attachment.localMetadata ?? {};
48-
const uploadProgress = readUploadProgress(attachment.localMetadata);
46+
const { id, uploadPermissionCheck, uploadProgress, uploadState } =
47+
attachment.localMetadata ?? {};
4948

5049
const isUploading = uploadState === 'uploading';
5150
const handleThumbnailLoadError = useCallback(() => setThumbnailPreviewError(true), []);

src/components/MessageComposer/AttachmentPreviewList/utils/uploadProgress.ts

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,5 @@
11
import { prettifyFileSize } from '../../hooks/utils';
22

3-
export function readUploadProgress(
4-
localMetadata: { uploadProgress?: unknown } | null | undefined,
5-
): number | undefined {
6-
if (!localMetadata) return undefined;
7-
const { uploadProgress } = localMetadata;
8-
if (uploadProgress === undefined) return undefined;
9-
if (typeof uploadProgress !== 'number' || !Number.isFinite(uploadProgress))
10-
return undefined;
11-
return uploadProgress;
12-
}
13-
14-
export function clampUploadPercent(value: number): number {
15-
if (!Number.isFinite(value)) return 0;
16-
return Math.min(100, Math.max(0, value));
17-
}
18-
193
function safePrettifyFileSize(bytes: number, maximumFractionDigits?: number): string {
204
if (!Number.isFinite(bytes) || bytes < 0) return '';
215
if (bytes === 0) return '0 B';
@@ -27,8 +11,7 @@ export function formatUploadByteFraction(
2711
fullBytes: number,
2812
maximumFractionDigits?: number,
2913
): string {
30-
const clamped = clampUploadPercent(uploadPercent);
31-
const uploaded = Math.round((clamped / 100) * fullBytes);
14+
const uploaded = Math.round((uploadPercent / 100) * fullBytes);
3215
return `${safePrettifyFileSize(uploaded, maximumFractionDigits)} / ${safePrettifyFileSize(fullBytes, maximumFractionDigits)}`;
3316
}
3417

0 commit comments

Comments
 (0)