about summary refs log tree commit diff
path: root/src/components/live/LinkPreview.tsx
blob: 98320a9e82660e75aca64a68da1dfa4f5e113fa5 (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
import {useState} from 'react'
import {View} from 'react-native'
import {Image} from 'expo-image'
import {Trans} from '@lingui/macro'

import {type LinkMeta} from '#/lib/link-meta/link-meta'
import {toNiceDomain} from '#/lib/strings/url-helpers'
import {LoadingPlaceholder} from '#/view/com/util/LoadingPlaceholder'
import {atoms as a, useTheme} from '#/alf'
import {Globe_Stroke2_Corner0_Rounded as GlobeIcon} from '#/components/icons/Globe'
import {Image_Stroke2_Corner0_Rounded as ImageIcon} from '#/components/icons/Image'
import {Text} from '#/components/Typography'

export function LinkPreview({
  linkMeta,
  loading,
}: {
  linkMeta?: LinkMeta
  loading: boolean
}) {
  const t = useTheme()
  const [imageLoadError, setImageLoadError] = useState(false)

  if (!linkMeta && !loading) {
    return null
  }

  return (
    <View
      style={[
        a.w_full,
        a.border,
        t.atoms.border_contrast_low,
        t.atoms.bg,
        a.flex_row,
        a.rounded_sm,
        a.overflow_hidden,
        a.align_stretch,
      ]}>
      <View
        style={[
          t.atoms.bg_contrast_25,
          {minHeight: 64, width: 114},
          a.justify_center,
          a.align_center,
          a.gap_xs,
        ]}>
        {linkMeta?.image && (
          <Image
            source={linkMeta.image}
            accessibilityIgnoresInvertColors
            transition={200}
            style={[a.absolute, a.inset_0]}
            contentFit="cover"
            onLoad={() => setImageLoadError(false)}
            onError={() => setImageLoadError(true)}
          />
        )}
        {linkMeta && (!linkMeta.image || imageLoadError) && (
          <>
            <ImageIcon style={[t.atoms.text_contrast_low]} />
            <Text style={[t.atoms.text_contrast_low, a.text_xs, a.text_center]}>
              <Trans>No image</Trans>
            </Text>
          </>
        )}
      </View>
      <View style={[a.flex_1, a.justify_center, a.py_sm, a.gap_xs, a.px_md]}>
        {linkMeta ? (
          <>
            <Text
              numberOfLines={2}
              style={[a.leading_snug, a.font_bold, a.text_md]}>
              {linkMeta.title || linkMeta.url}
            </Text>
            <View style={[a.flex_row, a.align_center, a.gap_2xs]}>
              <GlobeIcon size="xs" style={[t.atoms.text_contrast_low]} />
              <Text
                numberOfLines={1}
                style={[
                  a.text_xs,
                  a.leading_snug,
                  t.atoms.text_contrast_medium,
                ]}>
                {toNiceDomain(linkMeta.url)}
              </Text>
            </View>
          </>
        ) : (
          <>
            <LoadingPlaceholder height={16} width={128} />
            <LoadingPlaceholder height={12} width={72} />
          </>
        )}
      </View>
    </View>
  )
}