about summary refs log tree commit diff
path: root/src/state/queries/video/compress-video.ts
blob: cefbf94066ccff4b1e9549bca9979dd60b11c50a (plain) (blame)
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
import {ImagePickerAsset} from 'expo-image-picker'
import {useMutation} from '@tanstack/react-query'

import {cancelable} from '#/lib/async/cancelable'
import {CompressedVideo} from '#/lib/media/video/types'
import {compressVideo} from 'lib/media/video/compress'

export function useCompressVideoMutation({
  onProgress,
  onSuccess,
  onError,
  signal,
}: {
  onProgress: (progress: number) => void
  onError: (e: any) => void
  onSuccess: (video: CompressedVideo) => void
  signal: AbortSignal
}) {
  return useMutation({
    mutationKey: ['video', 'compress'],
    mutationFn: cancelable(
      (asset: ImagePickerAsset) =>
        compressVideo(asset, {
          onProgress: num => onProgress(trunc2dp(num)),
          signal,
        }),
      signal,
    ),
    onError,
    onSuccess,
    onMutate: () => {
      onProgress(0)
    },
  })
}

function trunc2dp(num: number) {
  return Math.trunc(num * 100) / 100
}