1
- import React , { useCallback } from 'react'
1
+ import React , { useCallback , useEffect } from 'react'
2
2
import { ImagePickerAsset } from 'expo-image-picker'
3
3
import { AppBskyVideoDefs , BlobRef } from '@atproto/api'
4
4
import { msg } from '@lingui/macro'
@@ -25,7 +25,7 @@ type Action =
25
25
| { type : 'SetDimensions' ; width : number ; height : number }
26
26
| { type : 'SetVideo' ; video : CompressedVideo }
27
27
| { type : 'SetJobStatus' ; jobStatus : AppBskyVideoDefs . JobStatus }
28
- | { type : 'SetBlobRef ' ; blobRef : BlobRef }
28
+ | { type : 'SetComplete ' ; blobRef : BlobRef }
29
29
30
30
export interface State {
31
31
status : Status
@@ -36,6 +36,7 @@ export interface State {
36
36
blobRef ?: BlobRef
37
37
error ?: string
38
38
abortController : AbortController
39
+ pendingPublish ?: { blobRef : BlobRef ; mutableProcessed : boolean }
39
40
}
40
41
41
42
function reducer ( queryClient : QueryClient ) {
@@ -77,16 +78,22 @@ function reducer(queryClient: QueryClient) {
77
78
updatedState = { ...state , video : action . video , status : 'uploading' }
78
79
} else if ( action . type === 'SetJobStatus' ) {
79
80
updatedState = { ...state , jobStatus : action . jobStatus }
80
- } else if ( action . type === 'SetBlobRef' ) {
81
- updatedState = { ...state , blobRef : action . blobRef , status : 'done' }
81
+ } else if ( action . type === 'SetComplete' ) {
82
+ updatedState = {
83
+ ...state ,
84
+ pendingPublish : {
85
+ blobRef : action . blobRef ,
86
+ mutableProcessed : false ,
87
+ } ,
88
+ status : 'done' ,
89
+ }
82
90
}
83
91
return updatedState
84
92
}
85
93
}
86
94
87
95
export function useUploadVideo ( {
88
96
setStatus,
89
- onSuccess,
90
97
} : {
91
98
setStatus : ( status : string ) => void
92
99
onSuccess : ( ) => void
@@ -112,11 +119,16 @@ export function useUploadVideo({
112
119
} ,
113
120
onSuccess : blobRef => {
114
121
dispatch ( {
115
- type : 'SetBlobRef ' ,
122
+ type : 'SetComplete ' ,
116
123
blobRef,
117
124
} )
118
- onSuccess ( )
119
125
} ,
126
+ onError : useCallback ( ( ) => {
127
+ dispatch ( {
128
+ type : 'SetError' ,
129
+ error : _ ( msg `Video failed to process` ) ,
130
+ } )
131
+ } , [ _ ] ) ,
120
132
} )
121
133
122
134
const { mutate : onVideoCompressed } = useUploadVideoMutation ( {
@@ -215,15 +227,17 @@ export function useUploadVideo({
215
227
const useUploadStatusQuery = ( {
216
228
onStatusChange,
217
229
onSuccess,
230
+ onError,
218
231
} : {
219
232
onStatusChange : ( status : AppBskyVideoDefs . JobStatus ) => void
220
233
onSuccess : ( blobRef : BlobRef ) => void
234
+ onError : ( error : Error ) => void
221
235
} ) => {
222
236
const videoAgent = useVideoAgent ( )
223
237
const [ enabled , setEnabled ] = React . useState ( true )
224
238
const [ jobId , setJobId ] = React . useState < string > ( )
225
239
226
- const { isLoading , isError } = useQuery ( {
240
+ const { error } = useQuery ( {
227
241
queryKey : [ 'video' , 'upload status' , jobId ] ,
228
242
queryFn : async ( ) => {
229
243
if ( ! jobId ) return // this won't happen, can ignore
@@ -245,9 +259,14 @@ const useUploadStatusQuery = ({
245
259
refetchInterval : 1500 ,
246
260
} )
247
261
262
+ useEffect ( ( ) => {
263
+ if ( error ) {
264
+ onError ( error )
265
+ setEnabled ( false )
266
+ }
267
+ } , [ error , onError ] )
268
+
248
269
return {
249
- isLoading,
250
- isError,
251
270
setJobId : ( _jobId : string ) => {
252
271
setJobId ( _jobId )
253
272
setEnabled ( true )
0 commit comments