Fix link warnings (#3058)
* fix problems where www.bsky.app shows as a potential danger * never default to disabling warning * remove more defaults * update storybook cases * oops * reversezio/stable
parent
1a5afccdb8
commit
39d324ab8b
|
@ -49,7 +49,7 @@ type BaseLinkProps = Pick<
|
|||
*
|
||||
* Note: atm this only works for `InlineLink`s with a string child.
|
||||
*/
|
||||
warnOnMismatchingTextChild?: boolean
|
||||
disableMismatchWarning?: boolean
|
||||
|
||||
/**
|
||||
* Callback for when the link is pressed. Prevent default and return `false`
|
||||
|
@ -69,7 +69,7 @@ export function useLink({
|
|||
to,
|
||||
displayText,
|
||||
action = 'push',
|
||||
warnOnMismatchingTextChild,
|
||||
disableMismatchWarning,
|
||||
onPress: outerOnPress,
|
||||
}: BaseLinkProps & {
|
||||
displayText: string
|
||||
|
@ -90,7 +90,7 @@ export function useLink({
|
|||
if (exitEarlyIfFalse === false) return
|
||||
|
||||
const requiresWarning = Boolean(
|
||||
warnOnMismatchingTextChild &&
|
||||
!disableMismatchWarning &&
|
||||
displayText &&
|
||||
isExternal &&
|
||||
linkRequiresWarning(href, displayText),
|
||||
|
@ -148,7 +148,7 @@ export function useLink({
|
|||
},
|
||||
[
|
||||
outerOnPress,
|
||||
warnOnMismatchingTextChild,
|
||||
disableMismatchWarning,
|
||||
displayText,
|
||||
isExternal,
|
||||
href,
|
||||
|
@ -167,7 +167,7 @@ export function useLink({
|
|||
}
|
||||
}
|
||||
|
||||
export type LinkProps = Omit<BaseLinkProps, 'warnOnMismatchingTextChild'> &
|
||||
export type LinkProps = Omit<BaseLinkProps, 'disableMismatchWarning'> &
|
||||
Omit<ButtonProps, 'onPress' | 'disabled' | 'label'>
|
||||
|
||||
/**
|
||||
|
@ -226,7 +226,7 @@ export function InlineLink({
|
|||
children,
|
||||
to,
|
||||
action = 'push',
|
||||
warnOnMismatchingTextChild,
|
||||
disableMismatchWarning,
|
||||
style,
|
||||
onPress: outerOnPress,
|
||||
download,
|
||||
|
@ -239,7 +239,7 @@ export function InlineLink({
|
|||
to,
|
||||
displayText: stringChildren ? children : '',
|
||||
action,
|
||||
warnOnMismatchingTextChild,
|
||||
disableMismatchWarning,
|
||||
onPress: outerOnPress,
|
||||
})
|
||||
const {
|
||||
|
|
|
@ -105,8 +105,7 @@ export function RichText({
|
|||
to={link.uri}
|
||||
style={[...styles, {pointerEvents: 'auto'}]}
|
||||
// @ts-ignore TODO
|
||||
dataSet={WORD_WRAP}
|
||||
warnOnMismatchingLabel>
|
||||
dataSet={WORD_WRAP}>
|
||||
{toShortUrl(segment.text)}
|
||||
</InlineLink>,
|
||||
)
|
||||
|
|
|
@ -157,17 +157,11 @@ export function linkRequiresWarning(uri: string, label: string) {
|
|||
|
||||
const host = urip.hostname.toLowerCase()
|
||||
|
||||
if (host === 'bsky.app') {
|
||||
// Hosts that end with bsky.app or bsky.social should be trusted by default.
|
||||
if (host.endsWith('bsky.app') || host.endsWith('bsky.social')) {
|
||||
// if this is a link to internal content,
|
||||
// warn if it represents itself as a URL to another app
|
||||
if (
|
||||
labelDomain &&
|
||||
labelDomain !== 'bsky.app' &&
|
||||
isPossiblyAUrl(labelDomain)
|
||||
) {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
return !!labelDomain && labelDomain !== host && isPossiblyAUrl(labelDomain)
|
||||
} else {
|
||||
// if this is a link to external content,
|
||||
// warn if the label doesnt match the target
|
||||
|
|
|
@ -159,7 +159,7 @@ export const TextLink = memo(function TextLink({
|
|||
dataSet,
|
||||
title,
|
||||
onPress,
|
||||
warnOnMismatchingLabel,
|
||||
disableMismatchWarning,
|
||||
navigationAction,
|
||||
...orgProps
|
||||
}: {
|
||||
|
@ -172,7 +172,7 @@ export const TextLink = memo(function TextLink({
|
|||
lineHeight?: number
|
||||
dataSet?: any
|
||||
title?: string
|
||||
warnOnMismatchingLabel?: boolean
|
||||
disableMismatchWarning?: boolean
|
||||
navigationAction?: 'push' | 'replace' | 'navigate'
|
||||
} & TextProps) {
|
||||
const {...props} = useLinkProps({to: sanitizeUrl(href)})
|
||||
|
@ -180,14 +180,14 @@ export const TextLink = memo(function TextLink({
|
|||
const {openModal, closeModal} = useModalControls()
|
||||
const openLink = useOpenLink()
|
||||
|
||||
if (warnOnMismatchingLabel && typeof text !== 'string') {
|
||||
if (!disableMismatchWarning && typeof text !== 'string') {
|
||||
console.error('Unable to detect mismatching label')
|
||||
}
|
||||
|
||||
props.onPress = React.useCallback(
|
||||
(e?: Event) => {
|
||||
const requiresWarning =
|
||||
warnOnMismatchingLabel &&
|
||||
!disableMismatchWarning &&
|
||||
linkRequiresWarning(href, typeof text === 'string' ? text : '')
|
||||
if (requiresWarning) {
|
||||
e?.preventDefault?.()
|
||||
|
@ -227,7 +227,7 @@ export const TextLink = memo(function TextLink({
|
|||
navigation,
|
||||
href,
|
||||
text,
|
||||
warnOnMismatchingLabel,
|
||||
disableMismatchWarning,
|
||||
navigationAction,
|
||||
openLink,
|
||||
],
|
||||
|
|
|
@ -114,7 +114,6 @@ export function RichText({
|
|||
href={link.uri}
|
||||
style={[style, lineHeightStyle, pal.link, {pointerEvents: 'auto'}]}
|
||||
dataSet={WORD_WRAP}
|
||||
warnOnMismatchingLabel
|
||||
selectable={selectable}
|
||||
/>,
|
||||
)
|
||||
|
|
|
@ -4,7 +4,7 @@ import {View} from 'react-native'
|
|||
import {useTheme, atoms as a} from '#/alf'
|
||||
import {ButtonText} from '#/components/Button'
|
||||
import {InlineLink, Link} from '#/components/Link'
|
||||
import {H1, H3, Text} from '#/components/Typography'
|
||||
import {H1, Text} from '#/components/Typography'
|
||||
|
||||
export function Links() {
|
||||
const t = useTheme()
|
||||
|
@ -13,31 +13,19 @@ export function Links() {
|
|||
<H1>Links</H1>
|
||||
|
||||
<View style={[a.gap_md, a.align_start]}>
|
||||
<InlineLink
|
||||
to="https://bsky.social"
|
||||
warnOnMismatchingTextChild
|
||||
style={[a.text_md]}>
|
||||
External
|
||||
<InlineLink to="https://google.com" style={[a.text_lg]}>
|
||||
https://google.com
|
||||
</InlineLink>
|
||||
<InlineLink to="https://bsky.social" style={[a.text_md, t.atoms.text]}>
|
||||
<H3>External with custom children</H3>
|
||||
<InlineLink to="https://google.com" style={[a.text_lg]}>
|
||||
External with custom children (google.com)
|
||||
</InlineLink>
|
||||
<InlineLink
|
||||
to="https://bsky.social"
|
||||
style={[a.text_md, t.atoms.text_contrast_low]}>
|
||||
External with custom children
|
||||
Internal (bsky.social)
|
||||
</InlineLink>
|
||||
<InlineLink
|
||||
to="https://bsky.social"
|
||||
warnOnMismatchingTextChild
|
||||
style={[a.text_lg]}>
|
||||
https://bsky.social
|
||||
</InlineLink>
|
||||
<InlineLink
|
||||
to="https://bsky.app/profile/bsky.app"
|
||||
warnOnMismatchingTextChild
|
||||
style={[a.text_md]}>
|
||||
Internal
|
||||
<InlineLink to="https://bsky.app/profile/bsky.app" style={[a.text_md]}>
|
||||
Internal (bsky.app)
|
||||
</InlineLink>
|
||||
|
||||
<Link
|
||||
|
|
Loading…
Reference in New Issue