Unverified Commit 3f239eea authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

[devtool] highlight all link in error message (#86084)

parent 5e7a207b
...@@ -5,7 +5,7 @@ const linkRegex = /https?:\/\/[^\s/$.?#].[^\s)'"]*/i ...@@ -5,7 +5,7 @@ const linkRegex = /https?:\/\/[^\s/$.?#].[^\s)'"]*/i
export const HotlinkedText: React.FC<{ export const HotlinkedText: React.FC<{
text: string text: string
matcher?: (text: string) => boolean matcher?: (text: string) => string | null
}> = function HotlinkedText(props) { }> = function HotlinkedText(props) {
const { text, matcher } = props const { text, matcher } = props
...@@ -24,17 +24,27 @@ export const HotlinkedText: React.FC<{ ...@@ -24,17 +24,27 @@ export const HotlinkedText: React.FC<{
if (linkRegex.test(rawPart)) { if (linkRegex.test(rawPart)) {
const link = linkRegex.exec(rawPart)! const link = linkRegex.exec(rawPart)!
const href = link[0] const href = link[0]
// If link matcher is present but the link doesn't match, don't turn it into a link // If link matcher is present, check if it returns a className
if (typeof matcher === 'function' && !matcher(href)) { let linkClassName: string | null = null
if (typeof matcher === 'function') {
linkClassName = matcher(href)
// If matcher returns null, don't turn it into a link
if (linkClassName === null) {
return ( return (
<React.Fragment key={`link-${outerIndex}-${index}`}> <React.Fragment key={`link-${outerIndex}-${index}`}>
{rawPart} {rawPart}
</React.Fragment> </React.Fragment>
) )
} }
}
return ( return (
<React.Fragment key={`link-${outerIndex}-${index}`}> <React.Fragment key={`link-${outerIndex}-${index}`}>
<a href={href} target="_blank" rel="noreferrer noopener"> <a
href={href}
target="_blank"
rel="noreferrer noopener"
className={linkClassName || undefined}
>
{rawPart} {rawPart}
</a> </a>
</React.Fragment> </React.Fragment>
......
...@@ -29,12 +29,18 @@ interface ErrorsProps extends ErrorBaseProps { ...@@ -29,12 +29,18 @@ interface ErrorsProps extends ErrorBaseProps {
onClose: () => void onClose: () => void
} }
function isNextjsLink(text: string): boolean { function matchLinkType(text: string): string | null {
return text.startsWith('https://nextjs.org') if (text.startsWith('https://nextjs.org')) {
return 'nextjs-link'
}
if (text.startsWith('https://') || text.startsWith('http://')) {
return 'external-link'
}
return null
} }
function HydrationErrorDescription({ message }: { message: string }) { function HydrationErrorDescription({ message }: { message: string }) {
return <HotlinkedText text={message} matcher={isNextjsLink} /> return <HotlinkedText text={message} matcher={matchLinkType} />
} }
function GenericErrorDescription({ error }: { error: Error }) { function GenericErrorDescription({ error }: { error: Error }) {
...@@ -51,7 +57,7 @@ function GenericErrorDescription({ error }: { error: Error }) { ...@@ -51,7 +57,7 @@ function GenericErrorDescription({ error }: { error: Error }) {
return ( return (
<> <>
<HotlinkedText text={message} matcher={isNextjsLink} /> <HotlinkedText text={message} matcher={matchLinkType} />
</> </>
) )
} }
...@@ -412,4 +418,7 @@ export const styles = ` ...@@ -412,4 +418,7 @@ export const styles = `
border-radius: var(--rounded-md-2); border-radius: var(--rounded-md-2);
border: 1px solid var(--color-gray-alpha-400); border: 1px solid var(--color-gray-alpha-400);
} }
.external-link, .external-link:hover {
color:inherit;
}
` `
...@@ -889,7 +889,6 @@ describe('ReactRefreshLogBox app', () => { ...@@ -889,7 +889,6 @@ describe('ReactRefreshLogBox app', () => {
} }
`) `)
} }
// Do not highlight example.com but do highlight nextjs.org
expect( expect(
await session.evaluate( await session.evaluate(
() => () =>
...@@ -898,7 +897,7 @@ describe('ReactRefreshLogBox app', () => { ...@@ -898,7 +897,7 @@ describe('ReactRefreshLogBox app', () => {
.shadowRoot.querySelectorAll('#nextjs__container_errors_desc a') .shadowRoot.querySelectorAll('#nextjs__container_errors_desc a')
.length .length
) )
).toBe(1) ).toBe(2)
expect( expect(
await session.evaluate( await session.evaluate(
() => () =>
...@@ -922,7 +921,7 @@ describe('ReactRefreshLogBox app', () => { ...@@ -922,7 +921,7 @@ describe('ReactRefreshLogBox app', () => {
) as any ) as any
).href ).href
) )
).toBe(null) ).toBe('http://example.com/')
}) })
// TODO-APP: Catch errors that happen before useEffect // TODO-APP: Catch errors that happen before useEffect
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment