properly shorten links in quote embeds (#2570)
* properly shorten links in quote embeds * lintzio/stable
parent
920d48849e
commit
eb07b983bd
|
@ -1,5 +1,5 @@
|
||||||
import React from 'react'
|
import React from 'react'
|
||||||
import {AppBskyEmbedRecord} from '@atproto/api'
|
import {AppBskyEmbedRecord, AppBskyRichtextFacet} from '@atproto/api'
|
||||||
import {useNonReactiveCallback} from '#/lib/hooks/useNonReactiveCallback'
|
import {useNonReactiveCallback} from '#/lib/hooks/useNonReactiveCallback'
|
||||||
|
|
||||||
export interface ComposerOptsPostRef {
|
export interface ComposerOptsPostRef {
|
||||||
|
@ -17,6 +17,7 @@ export interface ComposerOptsQuote {
|
||||||
uri: string
|
uri: string
|
||||||
cid: string
|
cid: string
|
||||||
text: string
|
text: string
|
||||||
|
facets?: AppBskyRichtextFacet.Main[]
|
||||||
indexedAt: string
|
indexedAt: string
|
||||||
author: {
|
author: {
|
||||||
did: string
|
did: string
|
||||||
|
|
|
@ -7,6 +7,7 @@ import {
|
||||||
AppBskyEmbedRecordWithMedia,
|
AppBskyEmbedRecordWithMedia,
|
||||||
ModerationUI,
|
ModerationUI,
|
||||||
AppBskyEmbedExternal,
|
AppBskyEmbedExternal,
|
||||||
|
RichText as RichTextAPI,
|
||||||
} from '@atproto/api'
|
} from '@atproto/api'
|
||||||
import {AtUri} from '@atproto/api'
|
import {AtUri} from '@atproto/api'
|
||||||
import {PostMeta} from '../PostMeta'
|
import {PostMeta} from '../PostMeta'
|
||||||
|
@ -19,6 +20,7 @@ import {PostAlerts} from '../moderation/PostAlerts'
|
||||||
import {makeProfileLink} from 'lib/routes/links'
|
import {makeProfileLink} from 'lib/routes/links'
|
||||||
import {InfoCircleIcon} from 'lib/icons'
|
import {InfoCircleIcon} from 'lib/icons'
|
||||||
import {Trans} from '@lingui/macro'
|
import {Trans} from '@lingui/macro'
|
||||||
|
import {RichText} from 'view/com/util/text/RichText'
|
||||||
|
|
||||||
export function MaybeQuoteEmbed({
|
export function MaybeQuoteEmbed({
|
||||||
embed,
|
embed,
|
||||||
|
@ -43,6 +45,7 @@ export function MaybeQuoteEmbed({
|
||||||
uri: embed.record.uri,
|
uri: embed.record.uri,
|
||||||
indexedAt: embed.record.indexedAt,
|
indexedAt: embed.record.indexedAt,
|
||||||
text: embed.record.value.text,
|
text: embed.record.value.text,
|
||||||
|
facets: embed.record.value.facets,
|
||||||
embeds: embed.record.embeds,
|
embeds: embed.record.embeds,
|
||||||
}}
|
}}
|
||||||
moderation={moderation}
|
moderation={moderation}
|
||||||
|
@ -84,9 +87,12 @@ export function QuoteEmbed({
|
||||||
const itemUrip = new AtUri(quote.uri)
|
const itemUrip = new AtUri(quote.uri)
|
||||||
const itemHref = makeProfileLink(quote.author, 'post', itemUrip.rkey)
|
const itemHref = makeProfileLink(quote.author, 'post', itemUrip.rkey)
|
||||||
const itemTitle = `Post by ${quote.author.handle}`
|
const itemTitle = `Post by ${quote.author.handle}`
|
||||||
const isEmpty = React.useMemo(
|
const richText = React.useMemo(
|
||||||
() => quote.text.trim().length === 0,
|
() =>
|
||||||
[quote.text],
|
quote.text.trim()
|
||||||
|
? new RichTextAPI({text: quote.text, facets: quote.facets})
|
||||||
|
: undefined,
|
||||||
|
[quote.text, quote.facets],
|
||||||
)
|
)
|
||||||
const embed = React.useMemo(() => {
|
const embed = React.useMemo(() => {
|
||||||
const e = quote.embeds?.[0]
|
const e = quote.embeds?.[0]
|
||||||
|
@ -117,10 +123,14 @@ export function QuoteEmbed({
|
||||||
{moderation ? (
|
{moderation ? (
|
||||||
<PostAlerts moderation={moderation} style={styles.alert} />
|
<PostAlerts moderation={moderation} style={styles.alert} />
|
||||||
) : null}
|
) : null}
|
||||||
{!isEmpty ? (
|
{richText ? (
|
||||||
<Text type="post-text" style={pal.text} numberOfLines={20}>
|
<RichText
|
||||||
{quote.text}
|
richText={richText}
|
||||||
</Text>
|
type="post-text"
|
||||||
|
style={pal.text}
|
||||||
|
numberOfLines={20}
|
||||||
|
noLinks
|
||||||
|
/>
|
||||||
) : null}
|
) : null}
|
||||||
{embed && <PostEmbeds embed={embed} moderation={{}} />}
|
{embed && <PostEmbeds embed={embed} moderation={{}} />}
|
||||||
</Link>
|
</Link>
|
||||||
|
|
|
@ -17,6 +17,7 @@ export function RichText({
|
||||||
lineHeight = 1.2,
|
lineHeight = 1.2,
|
||||||
style,
|
style,
|
||||||
numberOfLines,
|
numberOfLines,
|
||||||
|
noLinks,
|
||||||
}: {
|
}: {
|
||||||
testID?: string
|
testID?: string
|
||||||
type?: TypographyVariant
|
type?: TypographyVariant
|
||||||
|
@ -24,6 +25,7 @@ export function RichText({
|
||||||
lineHeight?: number
|
lineHeight?: number
|
||||||
style?: StyleProp<TextStyle>
|
style?: StyleProp<TextStyle>
|
||||||
numberOfLines?: number
|
numberOfLines?: number
|
||||||
|
noLinks?: boolean
|
||||||
}) {
|
}) {
|
||||||
const theme = useTheme()
|
const theme = useTheme()
|
||||||
const pal = usePalette('default')
|
const pal = usePalette('default')
|
||||||
|
@ -70,7 +72,11 @@ export function RichText({
|
||||||
for (const segment of richText.segments()) {
|
for (const segment of richText.segments()) {
|
||||||
const link = segment.link
|
const link = segment.link
|
||||||
const mention = segment.mention
|
const mention = segment.mention
|
||||||
if (mention && AppBskyRichtextFacet.validateMention(mention).success) {
|
if (
|
||||||
|
!noLinks &&
|
||||||
|
mention &&
|
||||||
|
AppBskyRichtextFacet.validateMention(mention).success
|
||||||
|
) {
|
||||||
els.push(
|
els.push(
|
||||||
<TextLink
|
<TextLink
|
||||||
key={key}
|
key={key}
|
||||||
|
@ -82,17 +88,21 @@ export function RichText({
|
||||||
/>,
|
/>,
|
||||||
)
|
)
|
||||||
} else if (link && AppBskyRichtextFacet.validateLink(link).success) {
|
} else if (link && AppBskyRichtextFacet.validateLink(link).success) {
|
||||||
els.push(
|
if (noLinks) {
|
||||||
<TextLink
|
els.push(toShortUrl(segment.text))
|
||||||
key={key}
|
} else {
|
||||||
type={type}
|
els.push(
|
||||||
text={toShortUrl(segment.text)}
|
<TextLink
|
||||||
href={link.uri}
|
key={key}
|
||||||
style={[style, lineHeightStyle, pal.link, {pointerEvents: 'auto'}]}
|
type={type}
|
||||||
dataSet={WORD_WRAP}
|
text={toShortUrl(segment.text)}
|
||||||
warnOnMismatchingLabel
|
href={link.uri}
|
||||||
/>,
|
style={[style, lineHeightStyle, pal.link, {pointerEvents: 'auto'}]}
|
||||||
)
|
dataSet={WORD_WRAP}
|
||||||
|
warnOnMismatchingLabel
|
||||||
|
/>,
|
||||||
|
)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
els.push(segment.text)
|
els.push(segment.text)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue