-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
109 changed files
with
1,675 additions
and
1,061 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Error Decoder requires reading pregenerated error message from getStaticProps, | ||
// but MDX component doesn't support props. So we use React Context to populate | ||
// the value without prop-drilling. | ||
// TODO: Replace with React.cache + React.use when migrating to Next.js App Router | ||
|
||
import {createContext, useContext} from 'react'; | ||
|
||
const notInErrorDecoderContext = Symbol('not in error decoder context'); | ||
|
||
export const ErrorDecoderContext = createContext< | ||
| {errorMessage: string | null; errorCode: string | null} | ||
| typeof notInErrorDecoderContext | ||
>(notInErrorDecoderContext); | ||
|
||
export const useErrorDecoderParams = () => { | ||
const params = useContext(ErrorDecoderContext); | ||
|
||
if (params === notInErrorDecoderContext) { | ||
throw new Error('useErrorDecoder must be used in error decoder pages only'); | ||
} | ||
|
||
return params; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import {useEffect, useState} from 'react'; | ||
import {useErrorDecoderParams} from '../ErrorDecoderContext'; | ||
import cn from 'classnames'; | ||
|
||
function replaceArgs( | ||
msg: string, | ||
argList: Array<string | undefined>, | ||
replacer = '[missing argument]' | ||
): string { | ||
let argIdx = 0; | ||
return msg.replace(/%s/g, function () { | ||
const arg = argList[argIdx++]; | ||
// arg can be an empty string: ?args[0]=&args[1]=count | ||
return arg === undefined || arg === '' ? replacer : arg; | ||
}); | ||
} | ||
|
||
/** | ||
* Sindre Sorhus <https://sindresorhus.com> | ||
* Released under MIT license | ||
* https://github.com/sindresorhus/linkify-urls/blob/edd75a64a9c36d7025f102f666ddbb6cf0afa7cd/index.js#L4C25-L4C137 | ||
* | ||
* The regex is used to extract URL from the string for linkify. | ||
*/ | ||
const urlRegex = | ||
/((?<!\+)https?:\/\/(?:www\.)?(?:[-\w.]+?[.@][a-zA-Z\d]{2,}|localhost)(?:[-\w.:%+~#*$!?&/=@]*?(?:,(?!\s))*?)*)/g; | ||
|
||
// When the message contains a URL (like https://fb.me/react-refs-must-have-owner), | ||
// make it a clickable link. | ||
function urlify(str: string): React.ReactNode[] { | ||
const segments = str.split(urlRegex); | ||
|
||
return segments.map((message, i) => { | ||
if (i % 2 === 1) { | ||
return ( | ||
<a | ||
key={i} | ||
target="_blank" | ||
className="underline" | ||
rel="noopener noreferrer" | ||
href={message}> | ||
{message} | ||
</a> | ||
); | ||
} | ||
return message; | ||
}); | ||
} | ||
|
||
// `?args[]=foo&args[]=bar` | ||
// or `// ?args[0]=foo&args[1]=bar` | ||
function parseQueryString(search: string): Array<string | undefined> { | ||
const rawQueryString = search.substring(1); | ||
if (!rawQueryString) { | ||
return []; | ||
} | ||
|
||
const args: Array<string | undefined> = []; | ||
|
||
const queries = rawQueryString.split('&'); | ||
for (let i = 0; i < queries.length; i++) { | ||
const query = decodeURIComponent(queries[i]); | ||
if (query.startsWith('args[')) { | ||
args.push(query.slice(query.indexOf(']=') + 2)); | ||
} | ||
} | ||
|
||
return args; | ||
} | ||
|
||
export default function ErrorDecoder() { | ||
const {errorMessage} = useErrorDecoderParams(); | ||
/** error messages that contain %s require reading location.search */ | ||
const hasParams = errorMessage?.includes('%s'); | ||
const [message, setMessage] = useState<React.ReactNode | null>(() => | ||
errorMessage ? urlify(errorMessage) : null | ||
); | ||
|
||
const [isReady, setIsReady] = useState(errorMessage == null || !hasParams); | ||
|
||
useEffect(() => { | ||
if (errorMessage == null || !hasParams) { | ||
return; | ||
} | ||
|
||
setMessage( | ||
urlify( | ||
replaceArgs( | ||
errorMessage, | ||
parseQueryString(window.location.search), | ||
'[missing argument]' | ||
) | ||
) | ||
); | ||
setIsReady(true); | ||
}, [hasParams, errorMessage]); | ||
|
||
return ( | ||
<code | ||
className={cn( | ||
'block bg-red-100 text-red-600 py-4 px-6 mt-5 rounded-lg', | ||
isReady ? 'opacity-100' : 'opacity-0' | ||
)}> | ||
<b>{message}</b> | ||
</code> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.