about summary refs log tree commit diff
path: root/src/view/com/composer/videos/VideoPreview.web.tsx
blob: f20f8b383c8ec69d056047f9e27164324f69699b (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
import {View} from 'react-native'
import {ImagePickerAsset} from 'expo-image-picker'
import {msg} from '@lingui/macro'
import {useLingui} from '@lingui/react'

import {CompressedVideo} from '#/lib/media/video/types'
import {clamp} from '#/lib/numbers'
import {useAutoplayDisabled} from '#/state/preferences'
import {ExternalEmbedRemoveBtn} from '#/view/com/composer/ExternalEmbedRemoveBtn'
import * as Toast from '#/view/com/util/Toast'
import {atoms as a} from '#/alf'
import {PlayButtonIcon} from '#/components/video/PlayButtonIcon'

export function VideoPreview({
  asset,
  video,

  clear,
}: {
  asset: ImagePickerAsset
  video: CompressedVideo

  clear: () => void
}) {
  const {_} = useLingui()
  // TODO: figure out how to pause a GIF for reduced motion
  // it's not possible using an img tag -sfn
  const autoplayDisabled = useAutoplayDisabled()

  let aspectRatio = asset.width / asset.height

  if (isNaN(aspectRatio)) {
    aspectRatio = 16 / 9
  }

  aspectRatio = clamp(aspectRatio, 1 / 1, 3 / 1)

  return (
    <View
      style={[
        a.w_full,
        a.rounded_sm,
        {aspectRatio},
        a.overflow_hidden,
        {backgroundColor: 'black'},
        a.relative,
      ]}>
      <ExternalEmbedRemoveBtn onRemove={clear} />
      {video.mimeType === 'image/gif' ? (
        <img
          src={video.uri}
          style={{width: '100%', height: '100%', objectFit: 'cover'}}
          alt="GIF"
        />
      ) : (
        <>
          <video
            src={video.uri}
            style={{width: '100%', height: '100%', objectFit: 'cover'}}
            autoPlay={!autoplayDisabled}
            loop
            muted
            playsInline
            onError={err => {
              console.error('Error loading video', err)
              Toast.show(_(msg`Could not process your video`), 'xmark')
              clear()
            }}
          />
          {autoplayDisabled && (
            <View
              style={[a.absolute, a.inset_0, a.justify_center, a.align_center]}>
              <PlayButtonIcon />
            </View>
          )}
        </>
      )}
    </View>
  )
}