about summary refs log tree commit diff
path: root/src/components/MediaPreview.tsx
blob: 7d7cb2b4ccfd41956839fdf41ff8369ab38dbc1e (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import React from 'react'
import {StyleProp, StyleSheet, View, ViewStyle} from 'react-native'
import {Image} from 'expo-image'
import {
  AppBskyEmbedExternal,
  AppBskyEmbedImages,
  AppBskyEmbedRecordWithMedia,
  AppBskyEmbedVideo,
} from '@atproto/api'
import {Trans} from '@lingui/macro'

import {parseTenorGif} from '#/lib/strings/embed-player'
import {atoms as a} from '#/alf'
import {Text} from '#/components/Typography'
import {PlayButtonIcon} from '#/components/video/PlayButtonIcon'

/**
 * Streamlined MediaPreview component which just handles images, gifs, and videos
 */
export function Embed({
  embed,
  style,
}: {
  embed?:
    | AppBskyEmbedImages.View
    | AppBskyEmbedRecordWithMedia.View
    | AppBskyEmbedExternal.View
    | AppBskyEmbedVideo.View
    | {[k: string]: unknown}
  style?: StyleProp<ViewStyle>
}) {
  let media = AppBskyEmbedRecordWithMedia.isView(embed) ? embed.media : embed

  if (AppBskyEmbedImages.isView(media)) {
    return (
      <Outer style={style}>
        {media.images.map(image => (
          <ImageItem
            key={image.thumb}
            thumbnail={image.thumb}
            alt={image.alt}
          />
        ))}
      </Outer>
    )
  } else if (AppBskyEmbedExternal.isView(embed) && embed.external.thumb) {
    let url: URL | undefined
    try {
      url = new URL(embed.external.uri)
    } catch {}
    if (url) {
      const {success} = parseTenorGif(url)
      if (success) {
        return (
          <Outer style={style}>
            <GifItem
              thumbnail={embed.external.thumb}
              alt={embed.external.title}
            />
          </Outer>
        )
      }
    }
  } else if (AppBskyEmbedVideo.isView(embed)) {
    return (
      <Outer style={style}>
        <VideoItem thumbnail={embed.thumbnail} alt={embed.alt} />
      </Outer>
    )
  }

  return null
}

export function Outer({
  children,
  style,
}: {
  children?: React.ReactNode
  style?: StyleProp<ViewStyle>
}) {
  return <View style={[a.flex_row, a.gap_xs, style]}>{children}</View>
}

export function ImageItem({
  thumbnail,
  alt,
  children,
}: {
  thumbnail: string
  alt?: string
  children?: React.ReactNode
}) {
  return (
    <View style={[a.relative, a.flex_1, {aspectRatio: 1, maxWidth: 100}]}>
      <Image
        key={thumbnail}
        source={{uri: thumbnail}}
        style={[a.flex_1, a.rounded_xs]}
        contentFit="cover"
        accessible={true}
        accessibilityIgnoresInvertColors
        accessibilityHint={alt}
        accessibilityLabel=""
      />
      {children}
    </View>
  )
}

export function GifItem({thumbnail, alt}: {thumbnail: string; alt?: string}) {
  return (
    <ImageItem thumbnail={thumbnail} alt={alt}>
      <View style={[a.absolute, a.inset_0, a.justify_center, a.align_center]}>
        <PlayButtonIcon size={24} />
      </View>
      <View style={styles.altContainer}>
        <Text style={styles.alt}>
          <Trans>GIF</Trans>
        </Text>
      </View>
    </ImageItem>
  )
}

export function VideoItem({
  thumbnail,
  alt,
}: {
  thumbnail?: string
  alt?: string
}) {
  if (!thumbnail) {
    return (
      <View
        style={[
          {backgroundColor: 'black'},
          a.flex_1,
          {aspectRatio: 1, maxWidth: 100},
          a.justify_center,
          a.align_center,
        ]}>
        <PlayButtonIcon size={24} />
      </View>
    )
  }
  return (
    <ImageItem thumbnail={thumbnail} alt={alt}>
      <View style={[a.absolute, a.inset_0, a.justify_center, a.align_center]}>
        <PlayButtonIcon size={24} />
      </View>
    </ImageItem>
  )
}

const styles = StyleSheet.create({
  altContainer: {
    backgroundColor: 'rgba(0, 0, 0, 0.75)',
    borderRadius: 6,
    paddingHorizontal: 6,
    paddingVertical: 3,
    position: 'absolute',
    right: 5,
    bottom: 5,
    zIndex: 2,
  },
  alt: {
    color: 'white',
    fontSize: 7,
    fontWeight: 'bold',
  },
})