Skip to content

Commit 3ee5ef3

Browse files
mozziushaileyok
andauthored
[Video] Error handling in composer, fix auto-send (#5122)
* tweak * error state for upload toolbar * catch errors in upload status query * stop query on error --------- Co-authored-by: Hailey <[email protected]>
1 parent 0bd0146 commit 3ee5ef3

File tree

2 files changed

+205
-140
lines changed

2 files changed

+205
-140
lines changed

src/state/queries/video/video.ts

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, {useCallback} from 'react'
1+
import React, {useCallback, useEffect} from 'react'
22
import {ImagePickerAsset} from 'expo-image-picker'
33
import {AppBskyVideoDefs, BlobRef} from '@atproto/api'
44
import {msg} from '@lingui/macro'
@@ -25,7 +25,7 @@ type Action =
2525
| {type: 'SetDimensions'; width: number; height: number}
2626
| {type: 'SetVideo'; video: CompressedVideo}
2727
| {type: 'SetJobStatus'; jobStatus: AppBskyVideoDefs.JobStatus}
28-
| {type: 'SetBlobRef'; blobRef: BlobRef}
28+
| {type: 'SetComplete'; blobRef: BlobRef}
2929

3030
export interface State {
3131
status: Status
@@ -36,6 +36,7 @@ export interface State {
3636
blobRef?: BlobRef
3737
error?: string
3838
abortController: AbortController
39+
pendingPublish?: {blobRef: BlobRef; mutableProcessed: boolean}
3940
}
4041

4142
function reducer(queryClient: QueryClient) {
@@ -77,16 +78,22 @@ function reducer(queryClient: QueryClient) {
7778
updatedState = {...state, video: action.video, status: 'uploading'}
7879
} else if (action.type === 'SetJobStatus') {
7980
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+
}
8290
}
8391
return updatedState
8492
}
8593
}
8694

8795
export function useUploadVideo({
8896
setStatus,
89-
onSuccess,
9097
}: {
9198
setStatus: (status: string) => void
9299
onSuccess: () => void
@@ -112,11 +119,16 @@ export function useUploadVideo({
112119
},
113120
onSuccess: blobRef => {
114121
dispatch({
115-
type: 'SetBlobRef',
122+
type: 'SetComplete',
116123
blobRef,
117124
})
118-
onSuccess()
119125
},
126+
onError: useCallback(() => {
127+
dispatch({
128+
type: 'SetError',
129+
error: _(msg`Video failed to process`),
130+
})
131+
}, [_]),
120132
})
121133

122134
const {mutate: onVideoCompressed} = useUploadVideoMutation({
@@ -215,15 +227,17 @@ export function useUploadVideo({
215227
const useUploadStatusQuery = ({
216228
onStatusChange,
217229
onSuccess,
230+
onError,
218231
}: {
219232
onStatusChange: (status: AppBskyVideoDefs.JobStatus) => void
220233
onSuccess: (blobRef: BlobRef) => void
234+
onError: (error: Error) => void
221235
}) => {
222236
const videoAgent = useVideoAgent()
223237
const [enabled, setEnabled] = React.useState(true)
224238
const [jobId, setJobId] = React.useState<string>()
225239

226-
const {isLoading, isError} = useQuery({
240+
const {error} = useQuery({
227241
queryKey: ['video', 'upload status', jobId],
228242
queryFn: async () => {
229243
if (!jobId) return // this won't happen, can ignore
@@ -245,9 +259,14 @@ const useUploadStatusQuery = ({
245259
refetchInterval: 1500,
246260
})
247261

262+
useEffect(() => {
263+
if (error) {
264+
onError(error)
265+
setEnabled(false)
266+
}
267+
}, [error, onError])
268+
248269
return {
249-
isLoading,
250-
isError,
251270
setJobId: (_jobId: string) => {
252271
setJobId(_jobId)
253272
setEnabled(true)

0 commit comments

Comments
 (0)