-
Notifications
You must be signed in to change notification settings - Fork 295
feat: track upload progress in attachment preview components #3060
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
b2f51d6
bdbd73d
be59e0f
3d9fe95
19d15c3
db938aa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| import React from 'react'; | ||
|
|
||
| import { useTranslationContext } from '../../context/TranslationContext'; | ||
|
|
||
| const RING_RADIUS = 12; | ||
| const RING_CIRCUMFERENCE = 2 * Math.PI * RING_RADIUS; | ||
|
|
||
| export type ProgressIndicatorProps = { | ||
| /** Clamped 0–100 completion. */ | ||
| percent: number; | ||
| }; | ||
|
|
||
| /** Circular progress indicator with input from 0 to 100. */ | ||
| export const ProgressIndicator = ({ percent }: ProgressIndicatorProps) => { | ||
| const { t } = useTranslationContext('ProgressIndicator'); | ||
| const dashOffset = RING_CIRCUMFERENCE * (1 - percent / 100); | ||
|
|
||
| return ( | ||
| <div className='str-chat__progress-indicator'> | ||
| <svg | ||
| aria-label={t('aria/Percent complete', { percent })} | ||
| aria-valuemax={100} | ||
| aria-valuemin={0} | ||
| aria-valuenow={percent} | ||
| data-testid='progress-ring' | ||
| height='100%' | ||
| role='progressbar' | ||
| viewBox='0 0 32 32' | ||
| width='100%' | ||
| xmlns='http://www.w3.org/2000/svg' | ||
| > | ||
| <circle | ||
| cx='16' | ||
| cy='16' | ||
| fill='none' | ||
| r={RING_RADIUS} | ||
| stroke='currentColor' | ||
| strokeOpacity={0.35} | ||
| strokeWidth='2.5' | ||
| /> | ||
| <circle | ||
| cx='16' | ||
| cy='16' | ||
| fill='none' | ||
| r={RING_RADIUS} | ||
| stroke='currentColor' | ||
| strokeDasharray={RING_CIRCUMFERENCE} | ||
| strokeDashoffset={dashOffset} | ||
| strokeLinecap='round' | ||
| strokeWidth='2.5' | ||
| transform='rotate(-90 16 16)' | ||
| /> | ||
| </svg> | ||
| </div> | ||
| ); | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| .str-chat__progress-indicator { | ||
| width: 100%; | ||
| height: 100%; | ||
|
|
||
| svg { | ||
| display: block; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| @use 'LoadingChannels'; | ||
| @use 'LoadingIndicator'; | ||
| @use 'ProgressIndicator'; |
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,43 @@ | ||||||||||
| import clsx from 'clsx'; | ||||||||||
| import React, { type ReactNode } from 'react'; | ||||||||||
|
|
||||||||||
| import { ProgressIndicator as DefaultProgressIndicator } from '../../Loading'; | ||||||||||
| import { useComponentContext } from '../../../context'; | ||||||||||
| import { LoadingIndicatorIcon } from '../icons'; | ||||||||||
|
|
||||||||||
| export type AttachmentUploadProgressVariant = 'inline' | 'overlay'; | ||||||||||
|
|
||||||||||
| export type AttachmentUploadProgressIndicatorProps = { | ||||||||||
| className?: string; | ||||||||||
| /** Shown when `uploadProgress` is `undefined` (e.g. progress tracking disabled). */ | ||||||||||
| fallback?: ReactNode; | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could this be made ComponentType instead of ReactNode? I am thinking that it provides more flexibility to render the component in place, where it is returned, instead somewhere up in the tree.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually this prop can be removed if the |
||||||||||
| uploadProgress?: number; | ||||||||||
| variant: AttachmentUploadProgressVariant; | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We may need to document what inline and overlay variants mean.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think, these names were just derived by AI from the specific way it used them - on overlay and somewhere in text. I see in the CSS that overlay styles icons and inline styles text. But if the designer decided to have the x MB / y MB indicator on the overlay, then we would have the inline variant on the overlay :). I would be also thinking about what other possible progress indicators are being used generally in other apps, so that more generic names can be applied for variant. |
||||||||||
| }; | ||||||||||
|
|
||||||||||
| export const AttachmentUploadProgressIndicator = ({ | ||||||||||
szuperaz marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
| className, | ||||||||||
| fallback, | ||||||||||
| uploadProgress, | ||||||||||
| variant, | ||||||||||
| }: AttachmentUploadProgressIndicatorProps) => { | ||||||||||
| const { ProgressIndicator = DefaultProgressIndicator } = useComponentContext( | ||||||||||
| 'AttachmentUploadProgressIndicator', | ||||||||||
| ); | ||||||||||
|
Comment on lines
+24
to
+26
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
No need to add the hook arg. |
||||||||||
|
|
||||||||||
| if (uploadProgress === undefined) { | ||||||||||
| return <>{fallback ?? <LoadingIndicatorIcon />}</>; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| return ( | ||||||||||
| <div | ||||||||||
| className={clsx( | ||||||||||
| 'str-chat__attachment-upload-progress', | ||||||||||
| `str-chat__attachment-upload-progress--${variant}`, | ||||||||||
| className, | ||||||||||
| )} | ||||||||||
| > | ||||||||||
| <ProgressIndicator percent={uploadProgress} /> | ||||||||||
| </div> | ||||||||||
| ); | ||||||||||
| }; | ||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| import React from 'react'; | ||
| import { FileSizeIndicator } from '../../Attachment'; | ||
| import { prettifyFileSize } from '../hooks/utils'; | ||
|
|
||
| function safePrettifyFileSize(bytes: number, maximumFractionDigits?: number): string { | ||
| if (!Number.isFinite(bytes) || bytes < 0) return ''; | ||
| if (bytes === 0) return '0 B'; | ||
| return prettifyFileSize(bytes, maximumFractionDigits); | ||
| } | ||
|
|
||
| function formatUploadByteFraction( | ||
| uploadPercent: number, | ||
| fullBytes: number, | ||
| maximumFractionDigits?: number, | ||
| ): string { | ||
| const uploaded = Math.round((uploadPercent / 100) * fullBytes); | ||
| return `${safePrettifyFileSize(uploaded, maximumFractionDigits)} / ${safePrettifyFileSize(fullBytes, maximumFractionDigits)}`; | ||
| } | ||
|
|
||
| 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 { uploadProgress, uploadState } = attachment.localMetadata ?? {}; | ||
| const fullBytes = resolveAttachmentFullByteSize(attachment); | ||
|
|
||
| if ( | ||
| uploadState === 'uploading' && | ||
| uploadProgress !== undefined && | ||
| fullBytes !== undefined | ||
| ) { | ||
| return ( | ||
| <span | ||
| className='str-chat__attachment-preview-file__upload-size-fraction' | ||
| data-testid='upload-size-fraction' | ||
| > | ||
| {formatUploadByteFraction(uploadProgress, fullBytes)} | ||
| </span> | ||
| ); | ||
| } | ||
|
|
||
| if (uploadState === 'finished') { | ||
| return <FileSizeIndicator fileSize={attachment.file_size} />; | ||
| } | ||
|
|
||
| return null; | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,10 +7,10 @@ | |
| import { useTranslationContext } from '../../../context'; | ||
| import React, { useEffect } from 'react'; | ||
| import clsx from 'clsx'; | ||
| import { LoadingIndicatorIcon } from '../icons'; | ||
| import { AttachmentUploadProgressIndicator } from './AttachmentUploadProgressIndicator'; | ||
| import { RemoveAttachmentPreviewButton } from '../RemoveAttachmentPreviewButton'; | ||
| import { AttachmentPreviewRoot } from './utils/AttachmentPreviewRoot'; | ||
| import { FileSizeIndicator } from '../../Attachment'; | ||
| import { IconExclamationMark, IconExclamationTriangle } from '../../Icons'; | ||
| import { PlayButton } from '../../Button'; | ||
| import { | ||
|
|
@@ -21,6 +21,7 @@ | |
| } from '../../AudioPlayback'; | ||
| import { useAudioPlayer } from '../../AudioPlayback/WithAudioPlayback'; | ||
| import { useStateStore } from '../../../store'; | ||
| import { AttachmentUploadedSizeIndicator } from './AttachmentUploadedSizeIndicator'; | ||
|
|
||
| export type AudioAttachmentPreviewProps<CustomLocalMetadata = Record<string, unknown>> = | ||
| UploadAttachmentPreviewProps< | ||
|
|
@@ -42,7 +43,7 @@ | |
| removeAttachments, | ||
| }: AudioAttachmentPreviewProps) => { | ||
| const { t } = useTranslationContext(); | ||
| const { id, previewUri, uploadPermissionCheck, uploadState } = | ||
| const { id, previewUri, uploadPermissionCheck, uploadProgress, uploadState } = | ||
| attachment.localMetadata ?? {}; | ||
| const url = attachment.asset_url || previewUri; | ||
|
|
||
|
|
@@ -93,11 +94,16 @@ | |
| {isVoiceRecordingAttachment(attachment) ? t('Voice message') : attachment.title} | ||
| </div> | ||
| <div className='str-chat__attachment-preview-file__data'> | ||
| {uploadState === 'uploading' && <LoadingIndicatorIcon />} | ||
| {uploadState === 'uploading' && ( | ||
| <AttachmentUploadProgressIndicator | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could introduce |
||
| uploadProgress={uploadProgress} | ||
| variant='inline' | ||
| /> | ||
| )} | ||
| {showProgressControls ? ( | ||
| <> | ||
| {!resolvedDuration && !progressPercent && !isPlaying && ( | ||
| <FileSizeIndicator fileSize={attachment.file_size} /> | ||
| <AttachmentUploadedSizeIndicator attachment={attachment} /> | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could introduce |
||
| )} | ||
| {hasWaveform ? ( | ||
| <> | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In case we added
LinearProgressIndicatoror any other type of progress indicator.