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

import {CompressedVideo, compressVideo} from 'lib/media/video/compress'

export function useCompressVideoMutation({
  onProgress,
  onSuccess,
  onError,
}: {
  onProgress: (progress: number) => void
  onError: (e: any) => void
  onSuccess: (video: CompressedVideo) => void
}) {
  return useMutation({
    mutationFn: async (asset: ImagePickerAsset) => {
      return await compressVideo(asset.uri, {
        onProgress: num => onProgress(trunc2dp(num)),
      })
    },
    onError,
    onSuccess,
    onMutate: () => {
      onProgress(0)
    },
  })
}

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