Something went wrong. Try again.
Mirror — see github.com/blacksky-algorithms/blacksky.community
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753import {type StyleProp, View, type ViewStyle} from 'react-native'import {Image} from 'expo-image'import {AtUri} from '@atproto/syntax'import {plural} from '@lingui/core/macro'import {useLingui} from '@lingui/react/macro'
import {useHaptics} from '#/lib/haptics'import {shareUrl} from '#/lib/sharing'import {niceDate} from '#/lib/strings/time'import {toNiceDomain} from '#/lib/strings/url-helpers'import {UserAvatar} from '#/view/com/util/UserAvatar'import {atoms as a, useBreakpoints, useTheme, utils, web} from '#/alf'import {ButtonIcon, ButtonText} from '#/components/Button'import {Divider} from '#/components/Divider'import {useInteractionState} from '#/components/hooks/useInteractionState'import {ArrowTopRight_Stroke2_Corner0_Rounded as ArrowTopRightIcon} from '#/components/icons/Arrow'import {Clock_Stroke2_Corner0_Rounded as Clock} from '#/components/icons/Clock'import {StandardSite} from '#/components/icons/community/StandardSite'import {Link, type LinkProps} from '#/components/Link'import {MediaInsetBorder} from '#/components/MediaInsetBorder'import {matchStandardSiteHostByPublication} from '#/components/Post/Embed/StandardSiteEmbed/publishers'import {StandardSiteMetaRow} from '#/components/Post/Embed/StandardSiteEmbed/StandardSiteMetaRow'import {StandardSiteThemeProvider} from '#/components/Post/Embed/StandardSiteEmbed/StandardSiteThemeProvider'import type * as ssTypes from '#/components/Post/Embed/StandardSiteEmbed/types'import {isStandardSitePublicationEmbed} from '#/components/Post/Embed/StandardSiteEmbed/utils'import {Text} from '#/components/Typography'import {useAnalytics} from '#/analytics'import {IS_NATIVE} from '#/env'
const PUBLICATION_AVATAR_STYLE = { borderRadius: a.rounded_sm.borderRadius,}
export const StandardSiteEmbed = ({ preview, view, onEmbedInteractionCallback, style,}: ssTypes.CommonProps & ssTypes.PreviewProps & ssTypes.PublicApiProps & { style?: StyleProp<ViewStyle> }) => { const ax = useAnalytics() const {t: l, i18n} = useLingui() const t = useTheme() const haptics = useHaptics() const niceUrl = toNiceDomain(view.uri) const imageUri = view.thumb const hasMedia = Boolean(imageUri) const isStandard = view.associatedRefs?.some(ref => new AtUri(ref.uri).collection.startsWith('site.standard.'), ) const isStandardPublication = isStandardSitePublicationEmbed(view) let themeColors: ssTypes.ThemeColors = { custom: false, accent: t.atoms.text.color, accentForeground: t.atoms.text_inverted.color, } const {accentRGB, accentForegroundRGB} = view.source?.theme || {} if (accentRGB && accentForegroundRGB) { themeColors = { custom: true, accent: utils.rgbToHex(accentRGB.r, accentRGB.g, accentRGB.b), accentForeground: utils.rgbToHex( accentForegroundRGB.r, accentForegroundRGB.g, accentForegroundRGB.b, ), } }
const { state: interacted, onIn: onInteract, onOut: onInteractOut, } = useInteractionState() const onPress = () => { haptics.tap() onEmbedInteractionCallback?.() ax.metric('embed:standardSite:article:press', {url: view.uri}) } const onLongPress = IS_NATIVE ? () => { if (view.uri) { haptics.longPress() void shareUrl(view.uri) ax.metric('embed:standardSite:article:longPress', {url: view.uri}) } } : undefined const onPressPublication = () => { haptics.tap() onEmbedInteractionCallback?.() ax.metric('embed:standardSite:publication:press', { url: view.source?.uri || '', }) } const onLongPressPublication = IS_NATIVE ? () => { if (view.source?.uri) { haptics.longPress() void shareUrl(view.source.uri) ax.metric('embed:standardSite:publication:longPress', { url: view.source.uri, }) } } : undefined
if (isStandardPublication) { return ( <PublicationCard preview={preview} view={view} onPress={onPressPublication} onLongPress={onLongPressPublication} style={style} themeColors={themeColors} onEmbedInteractionCallback={onEmbedInteractionCallback} /> ) }
return ( <View style={[ a.flex_col, a.rounded_lg, a.w_full, a.border, interacted ? t.atoms.border_contrast_high : t.atoms.border_contrast_low, preview && a.pointer_events_none, style, ]}> {/* * The article is an in-flow `Link` wrapping the content so the iOS peek * menu has something to lift. The publication footer below lives outside * it - it has its own link (to the publication) and Subscribe button, so * it can't be nested inside the article's peek. */} <Link peek shouldProxy to={view.uri} label={view.title || l`Open link to ${niceUrl}`} onPress={onPress} onLongPress={onLongPress} style={[a.rounded_lg]} {...web({ onMouseEnter: onInteract, onMouseLeave: onInteractOut, })} onFocus={onInteract} onBlur={onInteractOut}> {({pressed}) => ( <View style={[ a.w_full, a.overflow_hidden, // The container can't clip (overflow_hidden there breaks the peek // lift animation), so each half rounds its own outer corners to // match the container border. The article rounds the top, plus the // bottom when there's no footer beneath it. { borderTopLeftRadius: a.rounded_lg.borderRadius, borderTopRightRadius: a.rounded_lg.borderRadius, }, !view.source && { borderBottomLeftRadius: a.rounded_lg.borderRadius, borderBottomRightRadius: a.rounded_lg.borderRadius, }, interacted ? t.atoms.bg_contrast_25 : pressed && t.atoms.bg, ]}> {imageUri ? ( <Image style={[a.aspect_card]} source={{uri: imageUri}} accessibilityIgnoresInvertColors loading="lazy" useAppleWebpCodec /> ) : undefined}
<View style={[ a.flex_1, a.pt_sm, hasMedia && a.border_t, interacted ? t.atoms.border_contrast_high : t.atoms.border_contrast_low, {gap: 3}, isStandard && a.pt_md, ]}> <View style={[ a.pb_xs, a.px_md, {gap: 3}, isStandard && [{gap: 5}, a.pb_sm], ]}> <Text emoji numberOfLines={3} style={[ a.text_md, a.font_semi_bold, a.leading_snug, isStandard && [a.text_lg, a.font_bold], ]}> {view.title} </Text> {view.description ? ( <Text emoji numberOfLines={view.thumb ? 2 : 4} style={[a.text_sm, a.leading_snug]}> {view.description} </Text> ) : undefined}
{isStandard && (view.createdAt || view.readingTime) && ( <View style={[ a.flex_row, a.align_center, a.gap_md, {paddingTop: 2}, ]}> {view.createdAt && ( <Text style={[ a.text_xs, a.leading_snug, t.atoms.text_contrast_medium, ]}> {niceDate(i18n, view.createdAt, 'long', 'none')} </Text> )} {view.readingTime && ( <View style={[a.flex_row, a.align_center, a.gap_2xs]}> <Clock size="xs" style={t.atoms.text_contrast_medium} /> <Text style={[ a.text_xs, a.leading_snug, t.atoms.text_contrast_medium, ]}> {l({ message: plural(view.readingTime, { one: '#m', other: '#m', }), comment: `How long it takes to read an article, in minutes. Displayed in a short form, e.g. "5m" for 5 minutes.`, })} </Text> </View> )} </View> )} </View> </View>
{!view.source && ( <View style={[a.px_md]}> <Divider /> <View style={[a.py_sm]}> <StandardSiteMetaRow preview={preview} view={view} /> </View> </View> )} </View> )} </Link>
{view.source && ( <View> <Divider /> <PublicationFooter preview={preview} view={view} onPress={onPressPublication} onLongPress={onLongPressPublication} themeColors={themeColors} interactedOuter={interacted} onEmbedInteractionCallback={onEmbedInteractionCallback} /> </View> )} </View> )}
export function PublicationCard({ preview, view, onPress, onLongPress, themeColors, style, linkProps, onEmbedInteractionCallback, hideSubscribe,}: ssTypes.CommonProps & ssTypes.PreviewProps & ssTypes.PublicApiProps & { onPress?: () => void onLongPress?: () => void themeColors: ssTypes.ThemeColors style?: StyleProp<ViewStyle> /** * Focus plumbing for hosts such as carousels. The ref applies to the * primary link only; the subscribe link receives just `tabIndex` and * `onFocus`. */ linkProps?: Pick<LinkProps, 'ref' | 'tabIndex' | 'onFocus'> hideSubscribe?: boolean }) { const t = useTheme() const {t: l} = useLingui() const {gtPhone} = useBreakpoints() const { state: interacted, onIn: onInteract, onOut: onInteractOut, } = useInteractionState()
if (!view.source) return null
/* The primary link alone takes the ref; it is the host's focus target. */ const subscribeLinkProps = linkProps && { tabIndex: linkProps.tabIndex, onFocus: linkProps.onFocus, }
return ( <View style={[ a.rounded_lg, a.overflow_hidden, a.w_full, a.border, a.p_md, interacted ? [t.atoms.bg_contrast_25, t.atoms.border_contrast_high] : [t.atoms.bg, t.atoms.border_contrast_low], style, ]}> <Link shouldProxy to={view.source.uri} label={ view.source.title ? l`View ${view.source.title}` : l`View publication` } onPress={onPress} onLongPress={onLongPress} {...web({ onMouseEnter: onInteract, onMouseLeave: onInteractOut, })} ref={linkProps?.ref} tabIndex={linkProps?.tabIndex} onFocus={e => { onInteract() linkProps?.onFocus?.(e) }} onBlur={onInteractOut} style={[a.absolute, a.inset_0]}> <></> </Link>
<View style={[ a.align_center, a.justify_between, a.gap_md, a.pointer_events_none, gtPhone && [a.flex_row, a.gap_sm], ]}> <View style={[ a.w_full, a.flex_row, a.align_center, a.gap_sm, gtPhone && a.flex_1, ]}> <PublicationIcon view={view} size={40} interacted={interacted} themeColors={themeColors} /> <View style={[a.flex_1, a.gap_2xs]}> <Text emoji numberOfLines={1} style={[ a.text_md, a.font_semi_bold, t.atoms.text, a.leading_snug, ]}> {view.source?.title} </Text> <StandardSiteMetaRow preview={preview} type="publication" view={view} /> </View> </View>
{gtPhone && !hideSubscribe && ( <SubscribeButton preview={preview} view={view} style={[!gtPhone && [a.w_full, a.justify_center]]} linkProps={subscribeLinkProps} onEmbedInteractionCallback={onEmbedInteractionCallback} /> )} </View>
<View style={[a.flex_1, a.pointer_events_none]}> {!!view.description && ( <View style={[a.pt_sm]}> <Text emoji style={[a.text_sm, a.leading_snug]} numberOfLines={3}> {view.description} </Text> </View> )}
{!gtPhone && ( <View style={[a.pt_sm]}> <SubscribeButton preview={preview} view={view} style={[!gtPhone && [a.w_full, a.justify_center]]} linkProps={subscribeLinkProps} onEmbedInteractionCallback={onEmbedInteractionCallback} /> </View> )} </View> </View> )}
export function SubscribeButton({ preview, view, style, linkProps, onEmbedInteractionCallback,}: ssTypes.CommonProps & ssTypes.PreviewProps & ssTypes.PublicApiProps & { style?: StyleProp<ViewStyle> linkProps?: Pick<LinkProps, 'tabIndex' | 'onFocus'> }) { const ax = useAnalytics() const {t: l} = useLingui() const haptics = useHaptics()
const highlightedPublisher = matchStandardSiteHostByPublication(view) const cta = highlightedPublisher ? l`Subscribe on ${highlightedPublisher.name}` : l`View publication`
/* * The custom site theme paints the button background with `accent` and the * text with `accentForeground`. Only honor it when that pairing clears WCAG * AAA (4.5:1) for large text, which the button's bold label qualifies as. * Otherwise we fall through to the default `secondary_inverted` styling, * which is guaranteed to be legible. */ const {accentRGB, accentForegroundRGB} = view.source?.theme || {} let useCustomTheme = false if (accentRGB && accentForegroundRGB) { const accent = utils.rgbToHex(accentRGB.r, accentRGB.g, accentRGB.b) const accentForeground = utils.rgbToHex( accentForegroundRGB.r, accentForegroundRGB.g, accentForegroundRGB.b, ) const ratio = utils.contrastRatio(accent, accentForeground) useCustomTheme = ratio !== null && ratio >= 4.5 }
if (!view.source) return null
const publicationTitle = view.source.title const label = highlightedPublisher ? publicationTitle ? l`Subscribe to ${publicationTitle} on ${highlightedPublisher.name}` : l`Subscribe on ${highlightedPublisher.name}` : publicationTitle ? l`View ${publicationTitle}` : l`View publication`
const onPress = () => { haptics.tap() onEmbedInteractionCallback?.() if (highlightedPublisher) { ax.metric('embed:standardSite:subscribe:press', { url: view.source?.uri || '', }) } else { ax.metric('embed:standardSite:publicationCta:press', { url: view.source?.uri || '', }) } }
const onLongPress = IS_NATIVE ? () => { if (view.source?.uri) { haptics.longPress() void shareUrl(view.source.uri) if (highlightedPublisher) { ax.metric('embed:standardSite:subscribe:longPress', { url: view.source?.uri || '', }) } else { ax.metric('embed:standardSite:publicationCta:longPress', { url: view.source?.uri || '', }) } } } : undefined
const button = ( <Link shouldProxy to={view.source.uri} label={label} size="small" color="secondary_inverted" tabIndex={linkProps?.tabIndex} onFocus={linkProps?.onFocus} style={[ style, a.gap_sm, preview ? a.pointer_events_none : a.pointer_events_auto, ]} onPress={onPress} onLongPress={onLongPress}> {highlightedPublisher ? ( <> <View style={[a.flex_row, a.align_center, {gap: 7}]}> <ButtonIcon icon={highlightedPublisher.Icon} size="md" /> </View> <ButtonText>{cta}</ButtonText> </> ) : ( <> <ButtonText>{cta}</ButtonText> <ButtonIcon icon={ArrowTopRightIcon} /> </> )} </Link> )
if (!useCustomTheme) { return button }
return ( <StandardSiteThemeProvider view={view}>{button}</StandardSiteThemeProvider> )}
function PublicationIcon({ view, size, themeColors,}: ssTypes.CommonProps & { size: number interacted?: boolean themeColors: ssTypes.ThemeColors}) { const t = useTheme() if (!view.source) return null const icon = view.source?.icon ? ( <View> <UserAvatar noBorder type="labeler" size={size} avatar={view.source.icon} extraAviStyle={PUBLICATION_AVATAR_STYLE} /> <MediaInsetBorder opaque style={[a.rounded_sm]} /> </View> ) : ( <View style={[ a.align_center, a.justify_center, a.rounded_sm, { width: size, height: size, backgroundColor: themeColors.accent, }, ]}> <Text emoji style={[a.text_xl, a.font_bold, {color: themeColors.accentForeground}]}> {[...view.source.title][0] ?? ''} </Text> <MediaInsetBorder opaque style={[a.rounded_sm]} /> </View> ) return ( <View style={[a.relative]}> <View style={[ a.absolute, a.rounded_full, a.z_10, a.justify_center, a.align_center, t.atoms.bg, { width: 16, height: 16, top: -6, left: -6, }, ]}> <StandardSite size="xs" fill={t.atoms.text_contrast_medium.color} /> <MediaInsetBorder /> </View> {icon} </View> )}
export function PublicationFooter({ preview, view, themeColors, onPress, onLongPress, interactedOuter, onEmbedInteractionCallback,}: ssTypes.CommonProps & ssTypes.PreviewProps & ssTypes.PublicApiProps & { themeColors: ssTypes.ThemeColors onPress?: () => void onLongPress?: () => void interactedOuter?: boolean }) { const t = useTheme() const {t: l} = useLingui() const {gtPhone} = useBreakpoints() const { state: interacted, onIn: onInteract, onOut: onInteractOut, } = useInteractionState()
if (!view.source) return null
return ( <View style={[ a.flex_1, a.align_center, a.justify_between, a.p_md, a.gap_md, a.overflow_hidden, // Rounds the bottom corners to match the container border, since the // container itself can't clip (overflow_hidden there breaks the peek // lift animation on the article above). { borderBottomLeftRadius: a.rounded_lg.borderRadius, borderBottomRightRadius: a.rounded_lg.borderRadius, }, gtPhone && [a.flex_row, a.gap_sm], interactedOuter && t.atoms.bg_contrast_25, preview && a.pointer_events_none, ]}> <Link shouldProxy to={view.source.uri} label={ view.source.title ? l`View ${view.source.title}` : l`View publication` } onPress={onPress} onLongPress={onLongPress} style={[a.absolute, a.inset_0, web({outline: 0})]} {...web({ onMouseEnter: onInteract, onMouseLeave: onInteractOut, })} onFocus={onInteract} onBlur={onInteractOut}> <></> </Link>
<View style={[ a.w_full, a.flex_row, a.align_center, a.gap_sm, gtPhone && a.flex_1, a.pointer_events_none, ]}> <PublicationIcon view={view} size={32} interacted={interacted} themeColors={themeColors} /> <View style={[a.flex_1, a.gap_2xs]}> <Text emoji numberOfLines={1} style={[ a.text_sm, a.font_medium, a.leading_tight, t.atoms.text, interacted && a.underline, ]}> {view.source?.title} </Text> <StandardSiteMetaRow preview={preview} type="publication" view={view} /> </View> </View>
<SubscribeButton preview={preview} view={view} style={[a.z_10, !gtPhone && [a.w_full, a.justify_center]]} onEmbedInteractionCallback={onEmbedInteractionCallback} /> </View> )}