about summary refs log tree commit diff
path: root/src/view/com/composer/videos/SelectVideoBtn.tsx
blob: 9c528a92e21093d770cbac0420356086536b3831 (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
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
import React, {useCallback} from 'react'
import {
  ImagePickerAsset,
  launchImageLibraryAsync,
  MediaTypeOptions,
  UIImagePickerPreferredAssetRepresentationMode,
} from 'expo-image-picker'
import {msg} from '@lingui/macro'
import {useLingui} from '@lingui/react'

import {useVideoLibraryPermission} from '#/lib/hooks/usePermissions'
import {isNative} from '#/platform/detection'
import {atoms as a, useTheme} from '#/alf'
import {Button} from '#/components/Button'
import {VideoClip_Stroke2_Corner0_Rounded as VideoClipIcon} from '#/components/icons/VideoClip'

const VIDEO_MAX_DURATION = 90

type Props = {
  onSelectVideo: (video: ImagePickerAsset) => void
  disabled?: boolean
}

export function SelectVideoBtn({onSelectVideo, disabled}: Props) {
  const {_} = useLingui()
  const t = useTheme()
  const {requestVideoAccessIfNeeded} = useVideoLibraryPermission()

  const onPressSelectVideo = useCallback(async () => {
    if (isNative && !(await requestVideoAccessIfNeeded())) {
      return
    }

    const response = await launchImageLibraryAsync({
      exif: false,
      mediaTypes: MediaTypeOptions.Videos,
      videoMaxDuration: VIDEO_MAX_DURATION,
      quality: 1,
      legacy: true,
      preferredAssetRepresentationMode:
        UIImagePickerPreferredAssetRepresentationMode.Current,
    })
    if (response.assets && response.assets.length > 0) {
      onSelectVideo(response.assets[0])
    }
  }, [onSelectVideo, requestVideoAccessIfNeeded])

  return (
    <>
      <Button
        testID="openGifBtn"
        onPress={onPressSelectVideo}
        label={_(msg`Select video`)}
        accessibilityHint={_(msg`Opens video picker`)}
        style={a.p_sm}
        variant="ghost"
        shape="round"
        color="primary"
        disabled={disabled}>
        <VideoClipIcon
          size="lg"
          style={disabled && t.atoms.text_contrast_low}
        />
      </Button>
    </>
  )
}