-
Notifications
You must be signed in to change notification settings - Fork 2
feat: add ErrorBoundary popup, handle missing parent #63
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,7 @@ | ||
| # Contributing | ||
|
|
||
| ## Development Overview | ||
|
|
||
| - The repository uses [vite](vite.dev) as a build system. | ||
| - [Radix-UI](https://www.radix-ui.com/) is the component library used. | ||
| - [Tailwind is used for styling](https://tailwindcss.com/) |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,99 @@ | ||
| import { Component, ErrorInfo, ReactNode } from "react"; | ||
| import { useRouteError, useNavigate } from "react-router-dom"; | ||
| import { findErrorHandler } from "@/lib/error_handlers"; | ||
| import { | ||
| Dialog, | ||
| DialogContent, | ||
| DialogDescription, | ||
| DialogFooter, | ||
| DialogHeader, | ||
| DialogTitle, | ||
| } from "@/components/ui/dialog"; | ||
| import { Button } from "@/components/ui/button"; | ||
|
|
||
| interface ErrorDisplayProps { | ||
| error: Error | unknown; | ||
| reset: () => void; | ||
| } | ||
|
|
||
| export function ErrorDisplay({ error, reset }: ErrorDisplayProps) { | ||
| const navigate = useNavigate(); | ||
| const errorMessage = (error as Error)?.message || (typeof error === 'string' ? error : "Something went wrong."); | ||
| let title = "An error occurred"; | ||
| let description = errorMessage; | ||
| let action = <Button onClick={reset}>Go Home</Button>; | ||
|
|
||
| const handler = findErrorHandler(error); | ||
| if (handler) { | ||
| title = handler.title(error); | ||
| description = handler.description(error); | ||
| const actionContent = handler.action(error, { error, reset, navigate }); | ||
| if (actionContent) { | ||
| action = <>{actionContent}</>; | ||
| } | ||
| } | ||
|
|
||
| return ( | ||
| <Dialog open={true} onOpenChange={() => reset()}> | ||
| <DialogContent> | ||
| <DialogHeader> | ||
| <DialogTitle>{title}</DialogTitle> | ||
| <DialogDescription> | ||
| {description} | ||
| </DialogDescription> | ||
| </DialogHeader> | ||
| <DialogFooter> | ||
| {action} | ||
| </DialogFooter> | ||
| </DialogContent> | ||
| </Dialog> | ||
| ); | ||
| } | ||
|
|
||
| interface Props { | ||
| children: ReactNode; | ||
| } | ||
|
|
||
| interface State { | ||
| hasError: boolean; | ||
| error: Error | null; | ||
| } | ||
|
|
||
| export class ErrorBoundary extends Component<Props, State> { | ||
| public state: State = { | ||
| hasError: false, | ||
| error: null, | ||
| }; | ||
|
|
||
| public static getDerivedStateFromError(error: Error): State { | ||
| return { hasError: true, error }; | ||
| } | ||
|
|
||
| public componentDidCatch(error: Error, errorInfo: ErrorInfo) { | ||
| console.error("Uncaught error:", error, errorInfo); | ||
| } | ||
|
|
||
| private handleClose = () => { | ||
| this.setState({ hasError: false, error: null }); | ||
| window.location.href = "/"; | ||
| }; | ||
|
|
||
| public render() { | ||
| if (this.state.hasError) { | ||
| return <ErrorDisplay error={this.state.error} reset={this.handleClose} />; | ||
| } | ||
|
|
||
| return this.props.children; | ||
| } | ||
| } | ||
|
|
||
| export function RouteErrorBoundary() { | ||
| const error = useRouteError(); | ||
| const navigate = useNavigate(); | ||
|
|
||
| const handleClose = () => { | ||
| navigate("/"); | ||
| }; | ||
|
|
||
| return <ErrorDisplay error={error} reset={handleClose} />; | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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,120 @@ | ||
| import * as React from "react" | ||
| import * as DialogPrimitive from "@radix-ui/react-dialog" | ||
| import { Cross2Icon } from "@radix-ui/react-icons" | ||
|
|
||
| import { cn } from "@/lib/utils" | ||
|
|
||
| const Dialog = DialogPrimitive.Root | ||
|
|
||
| const DialogTrigger = DialogPrimitive.Trigger | ||
|
|
||
| const DialogPortal = DialogPrimitive.Portal | ||
|
|
||
| const DialogClose = DialogPrimitive.Close | ||
|
|
||
| const DialogOverlay = React.forwardRef< | ||
| React.ElementRef<typeof DialogPrimitive.Overlay>, | ||
| React.ComponentPropsWithoutRef<typeof DialogPrimitive.Overlay> | ||
| >(({ className, ...props }, ref) => ( | ||
| <DialogPrimitive.Overlay | ||
| ref={ref} | ||
| className={cn( | ||
| "fixed inset-0 z-50 bg-black/80 data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0", | ||
| className | ||
| )} | ||
| {...props} | ||
| /> | ||
| )) | ||
| DialogOverlay.displayName = DialogPrimitive.Overlay.displayName | ||
|
|
||
| const DialogContent = React.forwardRef< | ||
| React.ElementRef<typeof DialogPrimitive.Content>, | ||
| React.ComponentPropsWithoutRef<typeof DialogPrimitive.Content> | ||
| >(({ className, children, ...props }, ref) => ( | ||
| <DialogPortal> | ||
| <DialogOverlay /> | ||
| <DialogPrimitive.Content | ||
| ref={ref} | ||
| className={cn( | ||
| "fixed left-[50%] top-[50%] z-50 grid w-full max-w-lg translate-x-[-50%] translate-y-[-50%] gap-4 border bg-background p-6 shadow-lg duration-200 data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[state=closed]:slide-out-to-left-1/2 data-[state=closed]:slide-out-to-top-[48%] data-[state=open]:slide-in-from-left-1/2 data-[state=open]:slide-in-from-top-[48%] sm:rounded-lg", | ||
| className | ||
| )} | ||
| {...props} | ||
| > | ||
| {children} | ||
| <DialogPrimitive.Close className="absolute right-4 top-4 rounded-sm opacity-70 ring-offset-background transition-opacity hover:opacity-100 focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2 disabled:pointer-events-none data-[state=open]:bg-accent data-[state=open]:text-muted-foreground"> | ||
| <Cross2Icon className="h-4 w-4" /> | ||
| <span className="sr-only">Close</span> | ||
| </DialogPrimitive.Close> | ||
| </DialogPrimitive.Content> | ||
| </DialogPortal> | ||
| )) | ||
| DialogContent.displayName = DialogPrimitive.Content.displayName | ||
|
|
||
| const DialogHeader = ({ | ||
| className, | ||
| ...props | ||
| }: React.HTMLAttributes<HTMLDivElement>) => ( | ||
| <div | ||
| className={cn( | ||
| "flex flex-col space-y-1.5 text-center sm:text-left", | ||
| className | ||
| )} | ||
| {...props} | ||
| /> | ||
| ) | ||
| DialogHeader.displayName = "DialogHeader" | ||
|
|
||
| const DialogFooter = ({ | ||
| className, | ||
| ...props | ||
| }: React.HTMLAttributes<HTMLDivElement>) => ( | ||
| <div | ||
| className={cn( | ||
| "flex flex-col-reverse sm:flex-row sm:justify-end sm:space-x-2", | ||
| className | ||
| )} | ||
| {...props} | ||
| /> | ||
| ) | ||
| DialogFooter.displayName = "DialogFooter" | ||
|
|
||
| const DialogTitle = React.forwardRef< | ||
| React.ElementRef<typeof DialogPrimitive.Title>, | ||
| React.ComponentPropsWithoutRef<typeof DialogPrimitive.Title> | ||
| >(({ className, ...props }, ref) => ( | ||
| <DialogPrimitive.Title | ||
| ref={ref} | ||
| className={cn( | ||
| "text-lg font-semibold leading-none tracking-tight", | ||
| className | ||
| )} | ||
| {...props} | ||
| /> | ||
| )) | ||
| DialogTitle.displayName = DialogPrimitive.Title.displayName | ||
|
|
||
| const DialogDescription = React.forwardRef< | ||
| React.ElementRef<typeof DialogPrimitive.Description>, | ||
| React.ComponentPropsWithoutRef<typeof DialogPrimitive.Description> | ||
| >(({ className, ...props }, ref) => ( | ||
| <DialogPrimitive.Description | ||
| ref={ref} | ||
| className={cn("text-sm text-muted-foreground", className)} | ||
| {...props} | ||
| /> | ||
| )) | ||
| DialogDescription.displayName = DialogPrimitive.Description.displayName | ||
|
|
||
| export { | ||
| Dialog, | ||
| DialogPortal, | ||
| DialogOverlay, | ||
| DialogTrigger, | ||
| DialogClose, | ||
| DialogContent, | ||
| DialogHeader, | ||
| DialogFooter, | ||
| DialogTitle, | ||
| DialogDescription, | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it worth switching to radix-themes instead of tailwind? Then we can remove some of these customs styles: https://www.radix-ui.com/blog/themes-3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe? We're not technically using Radix, but using shadcn/ui, which is a thin layer on top of Radix. Not sure how those interact.