|
| 1 | +import {useEffect, useState} from 'react'; |
| 2 | +import {useErrorDecoderParams} from '../ErrorDecoderContext'; |
| 3 | +import cn from 'classnames'; |
| 4 | + |
| 5 | +function replaceArgs( |
| 6 | + msg: string, |
| 7 | + argList: Array<string | undefined>, |
| 8 | + replacer = '[missing argument]' |
| 9 | +): string { |
| 10 | + let argIdx = 0; |
| 11 | + return msg.replace(/%s/g, function () { |
| 12 | + const arg = argList[argIdx++]; |
| 13 | + // arg can be an empty string: ?args[0]=&args[1]=count |
| 14 | + return arg === undefined || arg === '' ? replacer : arg; |
| 15 | + }); |
| 16 | +} |
| 17 | + |
| 18 | +/** |
| 19 | + * Sindre Sorhus <https://sindresorhus.com> |
| 20 | + * Released under MIT license |
| 21 | + * https://github.com/sindresorhus/linkify-urls/blob/edd75a64a9c36d7025f102f666ddbb6cf0afa7cd/index.js#L4C25-L4C137 |
| 22 | + * |
| 23 | + * The regex is used to extract URL from the string for linkify. |
| 24 | + */ |
| 25 | +const urlRegex = |
| 26 | + /((?<!\+)https?:\/\/(?:www\.)?(?:[-\w.]+?[.@][a-zA-Z\d]{2,}|localhost)(?:[-\w.:%+~#*$!?&/=@]*?(?:,(?!\s))*?)*)/g; |
| 27 | + |
| 28 | +// When the message contains a URL (like https://fb.me/react-refs-must-have-owner), |
| 29 | +// make it a clickable link. |
| 30 | +function urlify(str: string): React.ReactNode[] { |
| 31 | + const segments = str.split(urlRegex); |
| 32 | + |
| 33 | + return segments.map((message, i) => { |
| 34 | + if (i % 2 === 1) { |
| 35 | + return ( |
| 36 | + <a |
| 37 | + key={i} |
| 38 | + target="_blank" |
| 39 | + className="underline" |
| 40 | + rel="noopener noreferrer" |
| 41 | + href={message}> |
| 42 | + {message} |
| 43 | + </a> |
| 44 | + ); |
| 45 | + } |
| 46 | + return message; |
| 47 | + }); |
| 48 | +} |
| 49 | + |
| 50 | +// `?args[]=foo&args[]=bar` |
| 51 | +// or `// ?args[0]=foo&args[1]=bar` |
| 52 | +function parseQueryString(search: string): Array<string | undefined> { |
| 53 | + const rawQueryString = search.substring(1); |
| 54 | + if (!rawQueryString) { |
| 55 | + return []; |
| 56 | + } |
| 57 | + |
| 58 | + const args: Array<string | undefined> = []; |
| 59 | + |
| 60 | + const queries = rawQueryString.split('&'); |
| 61 | + for (let i = 0; i < queries.length; i++) { |
| 62 | + const query = decodeURIComponent(queries[i]); |
| 63 | + if (query.startsWith('args[')) { |
| 64 | + args.push(query.slice(query.indexOf(']=') + 2)); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + return args; |
| 69 | +} |
| 70 | + |
| 71 | +export default function ErrorDecoder() { |
| 72 | + const {errorMessage} = useErrorDecoderParams(); |
| 73 | + /** error messages that contain %s require reading location.search */ |
| 74 | + const hasParams = errorMessage?.includes('%s'); |
| 75 | + const [message, setMessage] = useState<React.ReactNode | null>(() => |
| 76 | + errorMessage ? urlify(errorMessage) : null |
| 77 | + ); |
| 78 | + |
| 79 | + const [isReady, setIsReady] = useState(errorMessage == null || !hasParams); |
| 80 | + |
| 81 | + useEffect(() => { |
| 82 | + if (errorMessage == null || !hasParams) { |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + setMessage( |
| 87 | + urlify( |
| 88 | + replaceArgs( |
| 89 | + errorMessage, |
| 90 | + parseQueryString(window.location.search), |
| 91 | + '[missing argument]' |
| 92 | + ) |
| 93 | + ) |
| 94 | + ); |
| 95 | + setIsReady(true); |
| 96 | + }, [hasParams, errorMessage]); |
| 97 | + |
| 98 | + return ( |
| 99 | + <code |
| 100 | + className={cn( |
| 101 | + 'block bg-red-100 text-red-600 py-4 px-6 mt-5 rounded-lg', |
| 102 | + isReady ? 'opacity-100' : 'opacity-0' |
| 103 | + )}> |
| 104 | + <b>{message}</b> |
| 105 | + </code> |
| 106 | + ); |
| 107 | +} |
0 commit comments