Something went wrong. Try again.
Mirror — see github.com/blacksky-algorithms/blacksky.community
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237import {useMemo} from 'react'import {type StyleProp, View, type ViewStyle} from 'react-native'import {Image} from 'expo-image'import {msg} from '@lingui/core/macro'import {useLingui} from '@lingui/react'
import {parseAltFromGIFDescription} from '#/lib/gif-alt-text'import {useHaptics} from '#/lib/haptics'import {shareUrl} from '#/lib/sharing'import { exemptExternalEmbedSources, parseEmbedPlayerFromUrl,} from '#/lib/strings/embed-player'import {toNiceDomain} from '#/lib/strings/url-helpers'import {useExternalEmbedsPrefs} from '#/state/preferences'import {useSession} from '#/state/session'import {atoms as a, useTheme} from '#/alf'import {Divider} from '#/components/Divider'import {Earth_Stroke2_Corner0_Rounded as Globe} from '#/components/icons/Globe'import {Link} from '#/components/Link'import {Text} from '#/components/Typography'import {useAnalytics} from '#/analytics'import {IS_NATIVE} from '#/env'import {parseStreamplaceActor} from '#/features/streamplace/url'import {type app} from '#/lexicons'import {AssemblyEmbed} from './AssemblyEmbed'import {ExternalGif} from './ExternalGif'import {ExternalPlayer} from './ExternalPlayer'import {GifEmbed} from './Gif'
export const ExternalEmbed = ({ link, onOpen, post, style, hideAlt, postText, postFacets, postUri,}: { link: app.bsky.embed.external.ViewExternal onOpen?: () => void post?: app.bsky.feed.defs.PostView style?: StyleProp<ViewStyle> hideAlt?: boolean postText?: string postFacets?: app.bsky.richtext.facet.Main[] postUri?: string}) => { const {_} = useLingui() const t = useTheme() const haptics = useHaptics() const ax = useAnalytics() const externalEmbedPrefs = useExternalEmbedsPrefs() const {hasSession} = useSession() const niceUrl = toNiceDomain(link.uri) const imageUri = link.thumb const embedPlayerParams = useMemo(() => { const params = parseEmbedPlayerFromUrl(link.uri) if (!params) return if ( params.source === 'streamplace' && !ax.features.enabled(ax.features.StreamplaceWatchEnable) ) { return } const canShow = externalEmbedPrefs?.[params.source] !== 'hide' if (canShow || exemptExternalEmbedSources.has(params.source)) { return params } }, [link.uri, externalEmbedPrefs, ax]) const streamplaceActor = hasSession && embedPlayerParams?.type === 'streamplace_stream' ? parseStreamplaceActor(link.uri) : undefined const hasMedia = Boolean(imageUri || (embedPlayerParams && !streamplaceActor))
const onPress = () => { haptics.tap() onOpen?.() }
const onShareExternal = IS_NATIVE ? () => { if (link.uri) { haptics.longPress() void shareUrl(link.uri) } } : undefined
if ( embedPlayerParams?.source === 'tenor' || embedPlayerParams?.source === 'klipy' ) { const parsedAlt = parseAltFromGIFDescription(link.description) return ( <View style={style}> <GifEmbed params={embedPlayerParams} thumb={link.thumb} altText={parsedAlt.alt} isPreferredAltText={parsedAlt.isPreferred} hideAlt={hideAlt} /> </View> ) }
if (embedPlayerParams?.type === 'assembly_conversation') { return ( <View style={style}> <AssemblyEmbed link={link} params={embedPlayerParams} postText={postText} postFacets={postFacets} postUri={postUri} /> </View> ) }
return ( <Link label={link.title || _(msg`Open link to ${niceUrl}`)} to={streamplaceActor ? `/live/${streamplaceActor}` : link.uri} shouldProxy={!streamplaceActor} peek style={[a.rounded_md]} onPress={onPress} onLongPress={onShareExternal}> {({hovered, pressed}) => ( <View style={[ a.transition_color, a.flex_col, a.rounded_md, a.overflow_hidden, a.w_full, a.border, pressed && t.atoms.bg, style, hovered ? t.atoms.border_contrast_high : t.atoms.border_contrast_low, ]}> {imageUri && (!embedPlayerParams || streamplaceActor) ? ( <Image style={[a.aspect_card]} source={{uri: imageUri}} accessibilityIgnoresInvertColors loading="lazy" useAppleWebpCodec /> ) : undefined}
{embedPlayerParams?.isGif ? ( <ExternalGif link={link} params={embedPlayerParams} /> ) : embedPlayerParams && !streamplaceActor ? ( <ExternalPlayer link={link} params={embedPlayerParams} post={post} /> ) : undefined}
<View style={[ a.flex_1, a.pt_sm, {gap: 3}, hasMedia && a.border_t, hovered ? t.atoms.border_contrast_high : t.atoms.border_contrast_low, ]}> <View style={[{gap: 3}, a.pb_xs, a.px_md]}> {!embedPlayerParams?.isGif && !embedPlayerParams?.dimensions && ( <Text emoji numberOfLines={3} style={[a.text_md, a.font_semi_bold, a.leading_snug]}> {link.title || link.uri} </Text> )} {link.description ? ( <Text emoji numberOfLines={link.thumb ? 2 : 4} style={[a.text_sm, a.leading_snug]}> {link.description} </Text> ) : undefined} </View> <View style={[a.px_md]}> <Divider /> <View style={[ a.flex_row, a.align_center, a.gap_2xs, a.pb_sm, { paddingTop: 6, // off menu }, ]}> <Globe size="xs" style={[ a.transition_color, hovered ? t.atoms.text_contrast_medium : t.atoms.text_contrast_low, ]} /> <Text numberOfLines={1} style={[ a.transition_color, a.text_xs, a.leading_snug, hovered ? t.atoms.text_contrast_high : t.atoms.text_contrast_medium, ]}> {toNiceDomain(link.uri)} </Text> </View> </View> </View> </View> )} </Link> )}