forked from WordPress/gutenberg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.native.js
183 lines (160 loc) · 4.36 KB
/
index.native.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/**
* External dependencies
*/
import { View } from 'react-native';
/**
* WordPress dependencies
*/
import { useCallback, useEffect, useMemo, useState } from '@wordpress/element';
import { Spinner } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { subscribeMediaUpload } from '@wordpress/react-native-bridge';
/**
* Internal dependencies
*/
import styles from './styles.scss';
export const MEDIA_UPLOAD_STATE_UPLOADING = 1;
export const MEDIA_UPLOAD_STATE_SUCCEEDED = 2;
export const MEDIA_UPLOAD_STATE_FAILED = 3;
export const MEDIA_UPLOAD_STATE_RESET = 4;
// eslint-disable-next-line @wordpress/i18n-no-collapsible-whitespace
const retryMessage = __( 'Failed to insert media.\nTap for more info.' );
export function MediaUploadProgress( props ) {
const [ progress, setProgress ] = useState( 0 );
const [ isUploadInProgress, setIsUploadInProgress ] = useState( false );
const [ isUploadFailed, setIsUploadFailed ] = useState( false );
const [ subscriptionParentMediaUpload, setSubscriptionParentMediaUpload ] =
useState( null );
const [ progressBarStyle, setProgressBarStyle ] = useState( [] );
const { renderContent = () => null } = props;
const mediaUpload = useCallback(
( payload ) => {
const { mediaId } = props;
if ( payload.mediaId !== mediaId ) {
return;
}
switch ( payload.state ) {
case MEDIA_UPLOAD_STATE_UPLOADING:
updateMediaProgress( payload );
break;
case MEDIA_UPLOAD_STATE_SUCCEEDED:
finishMediaUploadWithSuccess( payload );
break;
case MEDIA_UPLOAD_STATE_FAILED:
finishMediaUploadWithFailure( payload );
break;
case MEDIA_UPLOAD_STATE_RESET:
mediaUploadStateReset( payload );
break;
}
},
[
props,
updateMediaProgress,
finishMediaUploadWithSuccess,
finishMediaUploadWithFailure,
mediaUploadStateReset,
]
);
const updateMediaProgress = useCallback(
( payload ) => {
setProgress( payload.progress );
setIsUploadInProgress( true );
setIsUploadFailed( false );
if ( props.onUpdateMediaProgress ) {
props.onUpdateMediaProgress( payload );
}
},
[ props.onUpdateMediaProgress ]
);
const finishMediaUploadWithSuccess = useCallback(
( payload ) => {
setIsUploadInProgress( false );
if ( props.onFinishMediaUploadWithSuccess ) {
props.onFinishMediaUploadWithSuccess( payload );
}
},
[ props.onFinishMediaUploadWithSuccess ]
);
const finishMediaUploadWithFailure = useCallback(
( payload ) => {
setIsUploadInProgress( false );
setIsUploadFailed( true );
if ( props.onFinishMediaUploadWithFailure ) {
props.onFinishMediaUploadWithFailure( payload );
}
},
[ props.onFinishMediaUploadWithFailure ]
);
const mediaUploadStateReset = useCallback(
( payload ) => {
setIsUploadInProgress( false );
setIsUploadFailed( false );
if ( props.onMediaUploadStateReset ) {
props.onMediaUploadStateReset( payload );
}
},
[ props.onMediaUploadStateReset ]
);
const addMediaUploadListener = useCallback( () => {
// If we already have a subscription not worth doing it again.
if ( subscriptionParentMediaUpload ) {
return;
}
setSubscriptionParentMediaUpload(
subscribeMediaUpload( ( payload ) => {
mediaUpload( payload );
} )
);
}, [ subscriptionParentMediaUpload ] );
const removeMediaUploadListener = useCallback( () => {
if ( subscriptionParentMediaUpload ) {
subscriptionParentMediaUpload.remove();
}
}, [ subscriptionParentMediaUpload ] );
useEffect( () => {
setProgressBarStyle( [
styles.progressBar,
isUploadInProgress || styles.progressBarHidden,
props.progressBarStyle,
] );
}, [ isUploadInProgress, props.progressBarStyle ] );
useEffect( () => {
addMediaUploadListener();
return () => {
removeMediaUploadListener();
};
}, [] );
return useMemo(
() => (
<View
style={ [ styles.mediaUploadProgress, props.containerStyle ] }
pointerEvents="box-none"
>
<View style={ progressBarStyle }>
{ isUploadInProgress && (
<Spinner
progress={ progress * 100 }
style={ props.spinnerStyle }
testID="spinner"
/>
) }
</View>
{ renderContent( {
isUploadInProgress,
isUploadFailed,
retryMessage,
} ) }
</View>
),
[
isUploadInProgress,
isUploadFailed,
progress,
progressBarStyle,
renderContent,
props,
]
);
}
export default MediaUploadProgress;