WIP improved post linking

This commit is contained in:
Eric Bailey
2024-11-13 19:13:48 -06:00
parent 5f7caf3010
commit f7eba155c7
4 changed files with 115 additions and 7 deletions
+40
View File
@@ -0,0 +1,40 @@
import React from 'react'
import {AtUri} from '@atproto/api'
import {msg, plural} from '@lingui/macro'
import {useLingui} from '@lingui/react'
import {useGetTimeAgo} from '#/lib/hooks/useTimeAgo'
import {makeProfileLink} from '#/lib/routes/links'
import {Link as InternalLink} from '#/components/Link'
import {Props} from '#/components/Post/Link/types'
export function Link({children, post, ...props}: Props) {
const {_} = useLingui()
const getTimeAgo = useGetTimeAgo()
const href = React.useMemo(() => {
const urip = new AtUri(post.uri)
return makeProfileLink(post.author, 'post', urip.rkey)
}, [post.uri, post.author])
const label = _(msg`${post.author.handle}:
${(post.record as any).text}.
${getTimeAgo(post.indexedAt, new Date(), {format: 'long'})} ago.
${plural(post.replyCount || 0, {
one: '1 reply',
other: '# replies',
})}
${plural(post.repostCount || 0, {
one: '1 repost',
other: '# reposts',
})}
${plural(post.quoteCount || 0, {
one: '1 quote post',
other: '# quote posts',
})}
`)
return (
<InternalLink {...props} to={href} label={label}>
{children}
</InternalLink>
)
}
+56
View File
@@ -0,0 +1,56 @@
import React from 'react'
import {Pressable} from 'react-native'
import {AtUri} from '@atproto/api'
import {msg, plural} from '@lingui/macro'
import {useLingui} from '@lingui/react'
import {useGetTimeAgo} from '#/lib/hooks/useTimeAgo'
import {makeProfileLink} from '#/lib/routes/links'
import {useLink} from '#/components/Link'
import {Props} from '#/components/Post/Link/types'
export function Link({children, post, ...props}: Props) {
const {_} = useLingui()
const getTimeAgo = useGetTimeAgo()
const href = React.useMemo(() => {
const urip = new AtUri(post.uri)
return makeProfileLink(post.author, 'post', urip.rkey)
}, [post.uri, post.author])
const {onPress} = useLink({
to: href,
displayText: '',
disableMismatchWarning: true,
})
const label = _(msg`${post.author.handle}:
${(post.record as any).text}.
${getTimeAgo(post.indexedAt, new Date(), {format: 'long'})} ago.
${plural(post.replyCount || 0, {
one: '1 reply',
other: '# replies',
})}
${plural(post.repostCount || 0, {
one: '1 repost',
other: '# reposts',
})}
${plural(post.quoteCount || 0, {
one: '1 quote post',
other: '# quote posts',
})}
`)
return (
<Pressable
{...props}
role="article"
accessibilityRole="link"
accessibilityHint=""
accessibilityLabel={label}
accessible
onPress={e => {
props.onPress?.(e)
onPress(e)
}}>
{children}
</Pressable>
)
}
+12
View File
@@ -0,0 +1,12 @@
import {PressableProps} from 'react-native'
import {AppBskyFeedDefs} from '@atproto/api'
import {LinkProps} from '#/components/Link'
export type Props = Pick<LinkProps, 'onPress' | 'style' | 'testID'> &
Pick<PressableProps, 'onPointerEnter' | 'onPointerLeave'> & {
children:
| React.ReactElement
| Iterable<React.ReactElement | null | undefined | boolean>
post: AppBskyFeedDefs.PostView
}
+7 -7
View File
@@ -44,6 +44,7 @@ import {ContentHider} from '#/components/moderation/ContentHider'
import {LabelsOnMyPost} from '#/components/moderation/LabelsOnMe' import {LabelsOnMyPost} from '#/components/moderation/LabelsOnMe'
import {PostAlerts} from '#/components/moderation/PostAlerts' import {PostAlerts} from '#/components/moderation/PostAlerts'
import {AppModerationCause} from '#/components/Pills' import {AppModerationCause} from '#/components/Pills'
import {Link as PostLink} from '#/components/Post/Link'
import {ProfileHoverCard} from '#/components/ProfileHoverCard' import {ProfileHoverCard} from '#/components/ProfileHoverCard'
import {RichText} from '#/components/RichText' import {RichText} from '#/components/RichText'
import {SubtleWebHover} from '#/components/SubtleWebHover' import {SubtleWebHover} from '#/components/SubtleWebHover'
@@ -239,15 +240,14 @@ let FeedItemInner = ({
: undefined : undefined
const [hover, setHover] = useState(false) const [hover, setHover] = useState(false)
return ( return (
<Link <PostLink
testID={`feedItem-by-${post.author.handle}`} testID={`feedItem-by-${post.author.handle}`}
post={post}
style={outerStyles} style={outerStyles}
href={href} onPress={onBeforePress}
noFeedback //dataSet={{feedContext}}
accessible={false}
onBeforePress={onBeforePress}
dataSet={{feedContext}}
onPointerEnter={() => { onPointerEnter={() => {
setHover(true) setHover(true)
}} }}
@@ -428,7 +428,7 @@ let FeedItemInner = ({
/> />
</View> </View>
</View> </View>
</Link> </PostLink>
) )
} }
FeedItemInner = memo(FeedItemInner) FeedItemInner = memo(FeedItemInner)