about summary refs log tree commit diff
path: root/src/view/com/composer/photos/SelectPhotoBtn.tsx
blob: c0808b85c1cd66d908ada62015c91b4b51360a53 (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import React from 'react'
import {Platform, TouchableOpacity} from 'react-native'
import {
  FontAwesomeIcon,
  FontAwesomeIconStyle,
} from '@fortawesome/react-native-fontawesome'
import {usePalette} from 'lib/hooks/usePalette'
import {useAnalytics} from 'lib/analytics'
import {useStores} from 'state/index'
import {s} from 'lib/styles'
import {isDesktopWeb} from 'platform/detection'
import {openPicker, cropAndCompressFlow, pickImagesFlow} from 'lib/media/picker'
import {usePhotoLibraryPermission} from 'lib/hooks/usePermissions'
import {
  POST_IMG_MAX_WIDTH,
  POST_IMG_MAX_HEIGHT,
  POST_IMG_MAX_SIZE,
} from 'lib/constants'

const HITSLOP = {left: 10, top: 10, right: 10, bottom: 10}

export function SelectPhotoBtn({
  enabled,
  selectedPhotos,
  onSelectPhotos,
}: {
  enabled: boolean
  selectedPhotos: string[]
  onSelectPhotos: (v: string[]) => void
}) {
  const pal = usePalette('default')
  const {track} = useAnalytics()
  const store = useStores()
  const {requestPhotoAccessIfNeeded} = usePhotoLibraryPermission()

  const onPressSelectPhotos = React.useCallback(async () => {
    track('Composer:GalleryOpened')
    if (!enabled) {
      return
    }
    if (isDesktopWeb) {
      const images = await pickImagesFlow(
        store,
        4 - selectedPhotos.length,
        {width: POST_IMG_MAX_WIDTH, height: POST_IMG_MAX_HEIGHT},
        POST_IMG_MAX_SIZE,
      )
      onSelectPhotos([...selectedPhotos, ...images])
    } else {
      if (!(await requestPhotoAccessIfNeeded())) {
        return
      }
      const items = await openPicker(store, {
        multiple: true,
        maxFiles: 4 - selectedPhotos.length,
        mediaType: 'photo',
      })
      const result = []
      for (const image of items) {
        if (Platform.OS === 'android') {
          result.push(image.path)
          continue
        }
        result.push(
          await cropAndCompressFlow(
            store,
            image.path,
            image,
            {width: POST_IMG_MAX_WIDTH, height: POST_IMG_MAX_HEIGHT},
            POST_IMG_MAX_SIZE,
          ),
        )
      }
      onSelectPhotos([...selectedPhotos, ...result])
    }
  }, [
    track,
    store,
    onSelectPhotos,
    selectedPhotos,
    enabled,
    requestPhotoAccessIfNeeded,
  ])

  return (
    <TouchableOpacity
      testID="openGalleryBtn"
      onPress={onPressSelectPhotos}
      style={[s.pl5, s.pr20]}
      hitSlop={HITSLOP}>
      <FontAwesomeIcon
        icon={['far', 'image']}
        style={
          (enabled
            ? pal.link
            : [pal.textLight, s.dimmed]) as FontAwesomeIconStyle
        }
        size={24}
      />
    </TouchableOpacity>
  )
}