11import React , { useMemo } from 'react' ;
22import { StyleSheet , Text , View } from 'react-native' ;
3+ import type { StyleProp , ViewStyle } from 'react-native' ;
34
45import { AttachmentUploadIndicator } from './AttachmentUploadIndicator' ;
56
67import { useTheme } from '../../contexts/themeContext/ThemeContext' ;
8+ import { usePendingAttachmentUpload } from '../../hooks/usePendingAttachmentUpload' ;
79import { primitives } from '../../theme' ;
10+ import { isLocalUrl } from '../../utils/utils' ;
811
912export type AttachmentFileUploadProgressIndicatorProps = {
13+ containerStyle ?: StyleProp < ViewStyle > ;
14+ localId ?: string ;
15+ sourceUrl ?: string ;
1016 totalBytes ?: number | string | null ;
11- uploadProgress : number | undefined ;
1217} ;
1318
1419const parseTotalBytes = ( value : number | string | null | undefined ) : number | null => {
@@ -35,13 +40,19 @@ const formatMegabytesOneDecimal = (bytes: number) => {
3540/**
3641 * Circular progress plus `uploaded / total` for file and audio attachments during upload.
3742 */
38- export const AttachmentFileUploadProgressIndicator = ( {
43+ export const AttachmentFileUploadProgressIndicatorUI = ( {
44+ containerStyle,
45+ localId,
46+ sourceUrl,
3947 totalBytes,
40- uploadProgress,
4148} : AttachmentFileUploadProgressIndicatorProps ) => {
4249 const {
4350 theme : { semantics } ,
4451 } = useTheme ( ) ;
52+ const shouldTrackPendingUpload = ! ! localId && ! ! sourceUrl && isLocalUrl ( sourceUrl ) ;
53+ const pendingUpload = usePendingAttachmentUpload ( shouldTrackPendingUpload ? localId : undefined ) ;
54+ const uploadProgress = pendingUpload . uploadProgress ;
55+ const shouldRender = pendingUpload . isUploading ;
4556
4657 const progressLabel = useMemo ( ( ) => {
4758 const bytes = parseTotalBytes ( totalBytes ) ;
@@ -52,9 +63,13 @@ export const AttachmentFileUploadProgressIndicator = ({
5263 return `${ formatMegabytesOneDecimal ( uploaded ) } / ${ formatMegabytesOneDecimal ( bytes ) } ` ;
5364 } , [ totalBytes , uploadProgress ] ) ;
5465
66+ if ( ! shouldRender ) {
67+ return null ;
68+ }
69+
5570 return (
56- < View style = { styles . row } >
57- < AttachmentUploadIndicator uploadProgress = { uploadProgress } />
71+ < View style = { [ styles . row , containerStyle ] } >
72+ < AttachmentUploadIndicator localId = { localId } sourceUrl = { sourceUrl } />
5873 { progressLabel ? (
5974 < Text numberOfLines = { 1 } style = { [ styles . label , { color : semantics . textSecondary } ] } >
6075 { progressLabel }
@@ -64,6 +79,19 @@ export const AttachmentFileUploadProgressIndicator = ({
6479 ) ;
6580} ;
6681
82+ export const AttachmentFileUploadProgressIndicator = (
83+ props : AttachmentFileUploadProgressIndicatorProps ,
84+ ) => {
85+ const { localId, sourceUrl } = props ;
86+ const shouldTrackPendingUpload = ! ! localId && ! ! sourceUrl && isLocalUrl ( sourceUrl ) ;
87+
88+ if ( ! shouldTrackPendingUpload ) {
89+ return null ;
90+ }
91+
92+ return < AttachmentFileUploadProgressIndicatorUI { ...props } /> ;
93+ } ;
94+
6795const styles = StyleSheet . create ( {
6896 label : {
6997 flex : 1 ,
0 commit comments