|
| 1 | +import { wait } from "@latticexyz/common/utils"; |
| 2 | +import { useEffect } from "react"; |
| 3 | +import { twMerge } from "tailwind-merge"; |
| 4 | + |
| 5 | +export type Props = { |
| 6 | + error?: Error; |
| 7 | + dismiss?: () => unknown; |
| 8 | + retry?: () => unknown | Promise<unknown>; |
| 9 | +}; |
| 10 | + |
| 11 | +export function ErrorOverlay({ error, retry, dismiss }: Props) { |
| 12 | + useEffect(() => { |
| 13 | + if (error) { |
| 14 | + console.error(error); |
| 15 | + } |
| 16 | + }, [error]); |
| 17 | + |
| 18 | + return ( |
| 19 | + <div className="pointer-events-none absolute inset-0 overflow-clip"> |
| 20 | + <div |
| 21 | + className={twMerge( |
| 22 | + "absolute inset-0 bg-blue-700/60", |
| 23 | + "transition duration-300", |
| 24 | + error ? "opacity-100 pointer-events-auto" : "opacity-0", |
| 25 | + )} |
| 26 | + /> |
| 27 | + <div |
| 28 | + className={twMerge( |
| 29 | + "absolute inset-0 pb-8", |
| 30 | + "transition duration-300", |
| 31 | + error ? "translate-y-0 opacity-100 pointer-events-auto" : "-translate-y-4 opacity-0", |
| 32 | + )} |
| 33 | + > |
| 34 | + {error ? ( |
| 35 | + <> |
| 36 | + <div className="w-full max-h-full bg-blue-700 text-white/80 overflow-auto"> |
| 37 | + <div className="space-y-6 px-8 pt-8"> |
| 38 | + <div className="text-white text-lg font-bold">Oops! It broke :(</div> |
| 39 | + <div className="font-mono text-xs whitespace-pre-wrap">{error.message.trim()}</div> |
| 40 | + <div className="text-sm">See the console for more info.</div> |
| 41 | + </div> |
| 42 | + <div className="pointer-events-none sticky bottom-0 left-0 -mt-2"> |
| 43 | + <div className="w-full h-12 bg-gradient-to-b from-transparent to-blue-700" /> |
| 44 | + {retry ? ( |
| 45 | + <div className="bg-blue-700 text-center"> |
| 46 | + {/* TODO: replace with AsyncButton */} |
| 47 | + <button |
| 48 | + type="button" |
| 49 | + className={twMerge( |
| 50 | + "pointer-events-auto group w-24 p-1 -translate-y-2 transition", |
| 51 | + "bg-blue-600 hover:bg-blue-500 aria-busy:bg-blue-500", |
| 52 | + "text-white text-sm font-medium", |
| 53 | + "aria-busy:pointer-events-none", |
| 54 | + )} |
| 55 | + onClick={async (event) => { |
| 56 | + // if we retry and the same error occurs, it'll look like the button click did nothing |
| 57 | + // so we'll fake a pending state here to give users an indication something is happening |
| 58 | + event.currentTarget.ariaBusy = "true"; |
| 59 | + await wait(500); |
| 60 | + retry(); |
| 61 | + if (event.currentTarget) { |
| 62 | + event.currentTarget.ariaBusy = null; |
| 63 | + } |
| 64 | + }} |
| 65 | + > |
| 66 | + {/* TODO: swap with pending icon */} |
| 67 | + <span className="group-aria-busy:hidden">Retry</span> |
| 68 | + <span className="hidden group-aria-busy:inline">Retrying…</span> |
| 69 | + </button> |
| 70 | + </div> |
| 71 | + ) : dismiss ? ( |
| 72 | + <div className="bg-blue-700 text-center"> |
| 73 | + {/* TODO: replace with AsyncButton */} |
| 74 | + <button |
| 75 | + type="button" |
| 76 | + className={twMerge( |
| 77 | + "pointer-events-auto group w-24 p-1 -translate-y-2 transition", |
| 78 | + "bg-blue-600 hover:bg-blue-500 aria-busy:bg-blue-500", |
| 79 | + "text-white text-sm font-medium", |
| 80 | + "aria-busy:pointer-events-none", |
| 81 | + )} |
| 82 | + onClick={dismiss} |
| 83 | + > |
| 84 | + Dismiss |
| 85 | + </button> |
| 86 | + </div> |
| 87 | + ) : null} |
| 88 | + </div> |
| 89 | + </div> |
| 90 | + </> |
| 91 | + ) : null} |
| 92 | + </div> |
| 93 | + </div> |
| 94 | + ); |
| 95 | +} |
0 commit comments