-
Notifications
You must be signed in to change notification settings - Fork 294
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
Merged
Merged
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
acd2c4f
feat: track upload progress in attachment preview components
szuperaz b1a787c
refactor: uploadProgress renamed to Progress and moved outside of mes…
szuperaz 0663ad0
refactor: remove unnecessary helpers
szuperaz 7705055
refactor: create AttachmentUploadedSizeIndicator component
szuperaz 2855009
refactor: more meaningful name for "x percent complete" i18n key
szuperaz 4688774
chore: translation rebuilt
szuperaz 5685c59
Apply suggestion from @MartinCupela
szuperaz aecaa9c
fix: remove unnecessary variant prop
szuperaz b2c347a
chore: small fixes
szuperaz d4f2f66
chore: fix failing tests
szuperaz b45192a
fix: css fixes
szuperaz 77ba34c
refactor: rename ProgressIndicator to CircularProgressIndicator
szuperaz 0a8d65d
feat: make AttachmentUploadProgressIndicator and AttachmentUploadProg…
szuperaz ff1e879
fix: use LoadingIndicator instead of LoadingIndicatorIcon
szuperaz e686dca
refactor: rename AttachmentUploadProgressIndicator to UploadProgressI…
szuperaz 3a8d1fb
feat: update stream-chat-version
szuperaz 02e973f
refactor: remove unnecessary formatUploadByteFraction method
szuperaz 388416d
refactor: simplify AttachmentUploadedSizeIndicator
szuperaz 52f063d
refactor: create UploadedSizeIndicator component
szuperaz d5a34c5
refactor: remove UploadProgressIndicator and AttachmentUploadedSizeIn…
szuperaz 028f6f6
refactor: use ProgressIndicator in ComponentContext
szuperaz 225fe94
refactor: remove unused className prop from UploadProgressIndicator
szuperaz 4b51412
refactor: remover wrapper div from UploadProgressIndicator
szuperaz 62cd53f
feat: add FileSizeIndicator to ComponentContext
szuperaz 76a5a6c
chore: move prop definition to the top of the file
szuperaz 34cabb6
Merge branch 'master' into file-upload-on-progress
szuperaz 5e3dbee
fix: 0 byte results in NaN in FileSizeIndicator
szuperaz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import React from 'react'; | ||
|
|
||
| import { useComponentContext } from '../../context'; | ||
| import { CircularProgressIndicator as DefaultProgressIndicator } from './progress-indicators'; | ||
| import { LoadingIndicator as DefaultLoadingIndicator } from './LoadingIndicator'; | ||
|
|
||
| export type UploadProgressIndicatorProps = { | ||
| uploadProgress?: number; | ||
| }; | ||
|
|
||
| export const UploadProgressIndicator = ({ | ||
| uploadProgress, | ||
| }: UploadProgressIndicatorProps) => { | ||
| const { | ||
| LoadingIndicator = DefaultLoadingIndicator, | ||
| ProgressIndicator = DefaultProgressIndicator, | ||
| } = useComponentContext(); | ||
|
|
||
| if (uploadProgress === undefined) { | ||
| return <LoadingIndicator data-testid='loading-indicator' />; | ||
| } | ||
|
|
||
| return <ProgressIndicator percent={uploadProgress} />; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import React from 'react'; | ||
|
|
||
| import { useComponentContext } from '../../context'; | ||
| import { FileSizeIndicator as DefaultFileSizeIndicator } from '../Attachment/components/FileSizeIndicator'; | ||
|
|
||
| export type UploadedSizeIndicatorProps = { | ||
| fullBytes: number; | ||
| uploadedBytes: number; | ||
| }; | ||
|
|
||
| export const UploadedSizeIndicator = ({ | ||
| fullBytes, | ||
| uploadedBytes, | ||
| }: UploadedSizeIndicatorProps) => { | ||
| const { FileSizeIndicator = DefaultFileSizeIndicator } = useComponentContext(); | ||
| return ( | ||
| <div | ||
| className='str-chat__attachment-preview-file__upload-size-fraction' | ||
| data-testid='upload-size-fraction' | ||
| > | ||
| <FileSizeIndicator fileSize={uploadedBytes} /> {` / `} | ||
| <FileSizeIndicator fileSize={fullBytes} /> | ||
| </div> | ||
| ); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| import React from 'react'; | ||
|
|
||
| import { useTranslationContext } from '../../context/TranslationContext'; | ||
|
|
||
| export type ProgressIndicatorProps = { | ||
| /** Clamped 0–100 completion. */ | ||
| percent: number; | ||
| }; | ||
|
|
||
| const RING_RADIUS = 12; | ||
| const RING_CIRCUMFERENCE = 2 * Math.PI * RING_RADIUS; | ||
|
|
||
| /** Circular progress indicator with input from 0 to 100. */ | ||
| export const CircularProgressIndicator = ({ percent }: ProgressIndicatorProps) => { | ||
| const { t } = useTranslationContext('CircularProgressIndicator'); | ||
| const dashOffset = RING_CIRCUMFERENCE * (1 - percent / 100); | ||
|
|
||
| return ( | ||
| <div className='str-chat__circular-progress-indicator str-chat__progress-indicator'> | ||
| <svg | ||
| aria-label={t('aria/Percent complete', { percent })} | ||
| aria-valuemax={100} | ||
| aria-valuemin={0} | ||
| aria-valuenow={percent} | ||
| data-testid='circular-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> | ||
| ); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| .str-chat__circular-progress-indicator { | ||
| width: 100%; | ||
| height: 100%; | ||
|
|
||
| svg { | ||
| display: block; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| @use 'LoadingChannels'; | ||
| @use 'LoadingIndicator'; | ||
| @use 'ProgressIndicator'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
src/components/MessageComposer/AttachmentPreviewList/AttachmentUploadedSizeIndicator.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,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; | ||
| }; | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.