Something went wrong. Try again.
Mirror — see github.com/blacksky-algorithms/blacksky.community
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607import {memo, useCallback, useEffect, useMemo, useReducer, useRef} from 'react'import {View} from 'react-native'import { type AppBskyActorDefs, moderateProfile, type ModerationOpts,} from '@atproto/api'import {flip, offset, shift, size, useFloating} from '@floating-ui/react-dom'import {msg, plural} from '@lingui/core/macro'import {useLingui} from '@lingui/react'import {useNavigation} from '@react-navigation/native'
import {getModerationCauseKey} from '#/lib/moderation'import {makeProfileLink} from '#/lib/routes/links'import {type NavigationProp} from '#/lib/routes/types'import {sanitizeDisplayName} from '#/lib/strings/display-names'import {sanitizeHandle} from '#/lib/strings/handles'import {useProfileShadow} from '#/state/cache/profile-shadow'import {useModerationOpts} from '#/state/preferences/moderation-opts'import {usePrefetchProfileQuery, useProfileQuery} from '#/state/queries/profile'import {useSession} from '#/state/session'import {formatCount} from '#/view/com/util/numeric/format'import {UserAvatar} from '#/view/com/util/UserAvatar'import {ProfileHeaderHandle} from '#/screens/Profile/Header/Handle'import {atoms as a, useTheme} from '#/alf'import {Button, ButtonIcon, ButtonText} from '#/components/Button'import {useFollowMethods} from '#/components/hooks/useFollowMethods'import {useRichText} from '#/components/hooks/useRichText'import {Check_Stroke2_Corner0_Rounded as Check} from '#/components/icons/Check'import {PlusLarge_Stroke2_Corner0_Rounded as Plus} from '#/components/icons/Plus'import { KnownFollowers, shouldShowKnownFollowers,} from '#/components/KnownFollowers'import {InlineLinkText, Link} from '#/components/Link'import {Loader} from '#/components/Loader'import * as Pills from '#/components/Pills'import {Portal} from '#/components/Portal'import {ProfileBadges} from '#/components/ProfileBadges'import {RichText} from '#/components/RichText'import {Text} from '#/components/Typography'import {IS_WEB_TOUCH_DEVICE} from '#/env'import {useActorStatus} from '#/features/liveNow'import {LiveStatus} from '#/features/liveNow/components/LiveStatusDialog'import {type ProfileHoverCardProps} from './types'
const floatingMiddlewares = [ offset(4), flip({padding: 16}), shift({padding: 16}), size({ padding: 16, apply({availableWidth, availableHeight, elements}) { Object.assign(elements.floating.style, { maxWidth: `${availableWidth}px`, maxHeight: `${availableHeight}px`, }) }, }),]
export function ProfileHoverCard(props: ProfileHoverCardProps) { const prefetchProfileQuery = usePrefetchProfileQuery() const prefetchedProfile = useRef(false) const onPointerMove = () => { if (!prefetchedProfile.current) { prefetchedProfile.current = true prefetchProfileQuery(props.did) } }
if (props.disable || IS_WEB_TOUCH_DEVICE) { return props.children } else { return ( <View onPointerMove={onPointerMove} style={[a.flex_shrink, props.inline && a.inline, props.style]}> <ProfileHoverCardInner {...props} /> </View> ) }}
type State = | { stage: 'hidden' | 'might-hide' | 'hiding' effect?: () => () => any } | { stage: 'might-show' | 'showing' effect?: () => () => any reason: 'hovered-target' | 'hovered-card' }
type Action = | 'pressed' | 'scrolled-while-showing' | 'hovered-target' | 'unhovered-target' | 'hovered-card' | 'unhovered-card' | 'hovered-long-enough' | 'unhovered-long-enough' | 'finished-animating-hide'
const SHOW_DELAY = 500const SHOW_DURATION = 300const HIDE_DELAY = 150const HIDE_DURATION = 200
export function ProfileHoverCardInner(props: ProfileHoverCardProps) { const navigation = useNavigation<NavigationProp>()
const {refs, floatingStyles} = useFloating({ middleware: floatingMiddlewares, })
const [currentState, dispatch] = useReducer( // Tip: console.log(state, action) when debugging. (state: State, action: Action): State => { // Pressing within a card should always hide it. // No matter which stage we're in. if (action === 'pressed') { return hidden() }
// --- Hidden --- // In the beginning, the card is not displayed. function hidden(): State { return {stage: 'hidden'} } if (state.stage === 'hidden') { // The user can kick things off by hovering a target. if (action === 'hovered-target') { return mightShow({ reason: action, }) } }
// --- Might Show --- // The card is not visible yet but we're considering showing it. function mightShow({ waitMs = SHOW_DELAY, reason, }: { waitMs?: number reason: 'hovered-target' | 'hovered-card' }): State { return { stage: 'might-show', reason, effect() { const id = setTimeout(() => dispatch('hovered-long-enough'), waitMs) return () => { clearTimeout(id) } }, } } if (state.stage === 'might-show') { // We'll make a decision at the end of a grace period timeout. if (action === 'unhovered-target' || action === 'unhovered-card') { return hidden() } if (action === 'hovered-long-enough') { return showing({ reason: state.reason, }) } }
// --- Showing --- // The card is beginning to show up and then will remain visible. function showing({ reason, }: { reason: 'hovered-target' | 'hovered-card' }): State { return { stage: 'showing', reason, effect() { function onScroll() { dispatch('scrolled-while-showing') } window.addEventListener('scroll', onScroll) return () => window.removeEventListener('scroll', onScroll) }, } } if (state.stage === 'showing') { // If the user moves the pointer away, we'll begin to consider hiding it. if (action === 'unhovered-target' || action === 'unhovered-card') { return mightHide() } // Scrolling away if the hover is on the target instantly hides without a delay. // If the hover is already on the card, we won't this. if ( state.reason === 'hovered-target' && action === 'scrolled-while-showing' ) { return hiding() } }
// --- Might Hide --- // The user has moved hover away from a visible card. function mightHide({waitMs = HIDE_DELAY}: {waitMs?: number} = {}): State { return { stage: 'might-hide', effect() { const id = setTimeout( () => dispatch('unhovered-long-enough'), waitMs, ) return () => clearTimeout(id) }, } } if (state.stage === 'might-hide') { // We'll make a decision based on whether it received hover again in time. if (action === 'hovered-target' || action === 'hovered-card') { return showing({ reason: action, }) } if (action === 'unhovered-long-enough') { return hiding() } }
// --- Hiding --- // The user waited enough outside that we're hiding the card. function hiding({ animationDurationMs = HIDE_DURATION, }: { animationDurationMs?: number } = {}): State { return { stage: 'hiding', effect() { const id = setTimeout( () => dispatch('finished-animating-hide'), animationDurationMs, ) return () => clearTimeout(id) }, } } if (state.stage === 'hiding') { // While hiding, we don't want to be interrupted by anything else. // When the animation finishes, we loop back to the initial hidden state. if (action === 'finished-animating-hide') { return hidden() } }
return state }, {stage: 'hidden'}, )
useEffect(() => { if (currentState.effect) { const effect = currentState.effect return effect() } }, [currentState])
const prefetchProfileQuery = usePrefetchProfileQuery() const prefetchedProfile = useRef(false) const prefetchIfNeeded = useCallback(async () => { if (!prefetchedProfile.current) { prefetchedProfile.current = true prefetchProfileQuery(props.did) } }, [prefetchProfileQuery, props.did])
const didFireHover = useRef(false) const onPointerMoveTarget = useCallback(() => { prefetchIfNeeded() // Conceptually we want something like onPointerEnter, // but we want to ignore entering only due to scrolling. // So instead we hover on the first onPointerMove. if (!didFireHover.current) { didFireHover.current = true dispatch('hovered-target') } }, [prefetchIfNeeded, dispatch])
const onPointerLeaveTarget = useCallback(() => { didFireHover.current = false dispatch('unhovered-target') }, [dispatch])
const onPointerEnterCard = useCallback(() => { dispatch('hovered-card') }, [dispatch])
const onPointerLeaveCard = useCallback(() => { dispatch('unhovered-card') }, [dispatch])
const onPress = useCallback(() => { dispatch('pressed') }, [dispatch])
const isVisible = currentState.stage === 'showing' || currentState.stage === 'might-hide' || currentState.stage === 'hiding'
const animationStyle = { animation: currentState.stage === 'hiding' ? `fadeOut ${HIDE_DURATION}ms both` : `fadeIn ${SHOW_DURATION}ms both`, }
return ( <View // @ts-ignore View is being used as div ref={refs.setReference} onPointerMove={onPointerMoveTarget} onPointerLeave={onPointerLeaveTarget} // @ts-ignore web only prop onMouseUp={onPress} style={[a.flex_shrink, props.inline && a.inline]}> {props.children} {isVisible && ( <Portal> <div ref={refs.setFloating} style={floatingStyles} onPointerEnter={onPointerEnterCard} onPointerLeave={onPointerLeaveCard}> <div style={{willChange: 'transform', ...animationStyle}}> <Card did={props.did} hide={onPress} navigation={navigation} /> </div> </div> </Portal> )} </View> )}
let Card = ({ did, hide, navigation,}: { did: string hide: () => void navigation: NavigationProp}): React.ReactNode => { const t = useTheme()
const profile = useProfileQuery({did}) const moderationOpts = useModerationOpts()
const data = profile.data
const status = useActorStatus(data)
const onPressOpenProfile = useCallback(() => { if (!status.isActive || !data) return hide() navigation.push('Profile', { name: data.handle, }) }, [hide, navigation, status, data])
return ( <View style={[ !status.isActive && a.p_lg, a.border, a.rounded_md, a.overflow_hidden, t.atoms.bg, t.atoms.border_contrast_low, t.atoms.shadow_lg, {width: status.isActive ? 350 : 300}, a.max_w_full, ]}> {data && moderationOpts ? ( status.isActive ? ( <LiveStatus status={status} profile={data} embed={status.embed} padding="lg" onPressOpenProfile={onPressOpenProfile} /> ) : ( <Inner profile={data} moderationOpts={moderationOpts} hide={hide} /> ) ) : ( <View style={[ a.justify_center, a.align_center, {minHeight: 200}, a.w_full, ]}> <Loader size="xl" /> </View> )} </View> )}Card = memo(Card)
function Inner({ profile, moderationOpts, hide,}: { profile: AppBskyActorDefs.ProfileViewDetailed moderationOpts: ModerationOpts hide: () => void}) { const t = useTheme() const {_, i18n} = useLingui() const {currentAccount} = useSession() const moderation = useMemo( () => moderateProfile(profile, moderationOpts), [profile, moderationOpts], ) const [descriptionRT] = useRichText(profile.description ?? '') const profileShadow = useProfileShadow(profile) const {follow, unfollow} = useFollowMethods({ profile: profileShadow, logContext: 'ProfileHoverCard', }) const isBlockedUser = profile.viewer?.blocking || profile.viewer?.blockedBy || profile.viewer?.blockingByList const following = formatCount(i18n, profile.followsCount || 0) const followers = formatCount(i18n, profile.followersCount || 0) const pluralizedFollowers = plural(profile.followersCount || 0, { one: 'follower', other: 'followers', }) const pluralizedFollowings = plural(profile.followsCount || 0, { one: 'following', other: 'following', }) const profileURL = makeProfileLink({ did: profile.did, handle: profile.handle, }) const isMe = useMemo( () => currentAccount?.did === profile.did, [currentAccount, profile], ) const isLabeler = profile.associated?.labeler
return ( <View> <View style={[a.flex_row, a.justify_between, a.align_start]}> <Link to={profileURL} label={_(msg`View profile`)} onPress={hide}> <UserAvatar size={64} avatar={profile.avatar} type={isLabeler ? 'labeler' : 'user'} moderation={moderation.ui('avatar')} /> </Link>
{!isMe && !isLabeler && (isBlockedUser ? ( <Link to={profileURL} label={_(msg`View blocked user's profile`)} onPress={hide} size="small" color="secondary" variant="solid" style={[a.rounded_full]}> <ButtonText>{_(msg`View profile`)}</ButtonText> </Link> ) : ( <Button size="small" color={profileShadow.viewer?.following ? 'secondary' : 'primary'} variant="solid" label={ profileShadow.viewer?.following ? _(msg`Following`) : _(msg`Follow`) } style={[a.rounded_full]} onPress={profileShadow.viewer?.following ? unfollow : follow}> <ButtonIcon position="left" icon={profileShadow.viewer?.following ? Check : Plus} /> <ButtonText> {profileShadow.viewer?.following ? _(msg`Following`) : _(msg`Follow`)} </ButtonText> </Button> ))} </View>
<Link to={profileURL} label={_(msg`View profile`)} onPress={hide}> <View style={[a.pb_sm, a.flex_1]}> <View style={[a.flex_row, a.align_center, a.pt_md, a.pb_xs]}> <Text numberOfLines={1} style={[ a.text_lg, a.leading_snug, a.font_semi_bold, a.self_start, ]}> {sanitizeDisplayName( profile.displayName || sanitizeHandle(profile.handle), moderation.ui('displayName'), )} </Text> <ProfileBadges profile={profile} size="md" style={[ a.pl_xs, { marginTop: -1, }, ]} /> </View>
<ProfileHeaderHandle profile={profileShadow} disableTaps /> </View> </Link>
{isBlockedUser && ( <View style={[a.flex_row, a.flex_wrap, a.gap_xs]}> {moderation.ui('profileView').alerts.map(cause => ( <Pills.Label key={getModerationCauseKey(cause)} size="lg" cause={cause} disableDetailsDialog /> ))} </View> )}
{!isBlockedUser && ( <> <View style={[a.flex_row, a.flex_wrap, a.gap_md, a.pt_xs]}> <InlineLinkText to={makeProfileLink(profile, 'followers')} label={`${followers} ${pluralizedFollowers}`} style={[t.atoms.text]} onPress={hide}> <Text style={[a.text_md, a.font_semi_bold]}>{followers} </Text> <Text style={[t.atoms.text_contrast_medium]}> {pluralizedFollowers} </Text> </InlineLinkText> <InlineLinkText to={makeProfileLink(profile, 'follows')} label={_(msg`${following} following`)} style={[t.atoms.text]} onPress={hide}> <Text style={[a.text_md, a.font_semi_bold]}>{following} </Text> <Text style={[t.atoms.text_contrast_medium]}> {pluralizedFollowings} </Text> </InlineLinkText> </View>
{profile.description?.trim() && !moderation.ui('profileView').blur ? ( <View style={[a.pt_md]}> <RichText numberOfLines={8} value={descriptionRT} onLinkPress={hide} /> </View> ) : undefined}
{!isMe && shouldShowKnownFollowers(profile.viewer?.knownFollowers) && ( <View style={[a.flex_row, a.align_center, a.gap_sm, a.pt_md]}> <KnownFollowers profile={profile} moderationOpts={moderationOpts} onLinkPress={hide} /> </View> )} </> )} </View> )}