| ... | ... | @@ -1,7 +1,14 @@ |
| 1 | | import { Outlet, createRootRouteWithContext, useParams } from "@tanstack/react-router"; |
| 1 | import { |
| 2 | Outlet, |
| 3 | createRootRouteWithContext, |
| 4 | type ErrorComponentProps, |
| 5 | useParams, |
| 6 | } from "@tanstack/react-router"; |
| 2 | 7 | import { useEffect, useRef } from "react"; |
| 3 | 8 | import { QueryClient, useQueryClient } from "@tanstack/react-query"; |
| 4 | 9 | |
| 10 | import { APP_DISPLAY_NAME } from "../branding"; |
| 11 | import { Button } from "../components/ui/button"; |
| 5 | 12 | import { AnchoredToastProvider, ToastProvider } from "../components/ui/toast"; |
| 6 | 13 | import { isElectron } from "../env"; |
| 7 | 14 | import { useNativeApi } from "../hooks/useNativeApi"; |
| ... | ... | @@ -9,12 +16,12 @@ import { invalidateGitQueries } from "../lib/gitReactQuery"; |
| 9 | 16 | import { DEFAULT_MODEL } from "../model-logic"; |
| 10 | 17 | import { useStore } from "../store"; |
| 11 | 18 | import { onServerWelcome } from "../wsNativeApi"; |
| 12 | | import { APP_DISPLAY_NAME } from "../branding"; |
| 13 | 19 | |
| 14 | 20 | export const Route = createRootRouteWithContext<{ |
| 15 | 21 | queryClient: QueryClient; |
| 16 | 22 | }>()({ |
| 17 | 23 | component: RootRouteView, |
| 24 | errorComponent: RootRouteErrorView, |
| 18 | 25 | }); |
| 19 | 26 | |
| 20 | 27 | function RootRouteView() { |
| ... | ... | @@ -44,6 +51,77 @@ function RootRouteView() { |
| 44 | 51 | ); |
| 45 | 52 | } |
| 46 | 53 | |
| 54 | function RootRouteErrorView({ error, reset }: ErrorComponentProps) { |
| 55 | const message = errorMessage(error); |
| 56 | const details = errorDetails(error); |
| 57 | |
| 58 | return ( |
| 59 | <div className="relative flex min-h-screen items-center justify-center overflow-hidden bg-background px-4 py-10 text-foreground sm:px-6"> |
| 60 | <div className="pointer-events-none absolute inset-0 opacity-80"> |
| 61 | <div className="absolute inset-x-0 top-0 h-44 bg-[radial-gradient(44rem_16rem_at_top,color-mix(in_srgb,var(--color-red-500)_16%,transparent),transparent)]" /> |
| 62 | <div className="absolute inset-0 bg-[linear-gradient(145deg,color-mix(in_srgb,var(--background)_90%,var(--color-black))_0%,var(--background)_55%)]" /> |
| 63 | </div> |
| 64 | |
| 65 | <section className="relative w-full max-w-xl rounded-2xl border border-border/80 bg-card/90 p-6 shadow-2xl shadow-black/20 backdrop-blur-md sm:p-8"> |
| 66 | <p className="text-[11px] font-semibold tracking-[0.18em] text-muted-foreground uppercase"> |
| 67 | {APP_DISPLAY_NAME} |
| 68 | </p> |
| 69 | <h1 className="mt-3 text-2xl font-semibold tracking-tight sm:text-3xl"> |
| 70 | Something went wrong. |
| 71 | </h1> |
| 72 | <p className="mt-2 text-sm leading-relaxed text-muted-foreground">{message}</p> |
| 73 | |
| 74 | <div className="mt-5 flex flex-wrap gap-2"> |
| 75 | <Button size="sm" onClick={() => reset()}> |
| 76 | Try again |
| 77 | </Button> |
| 78 | <Button size="sm" variant="outline" onClick={() => window.location.reload()}> |
| 79 | Reload app |
| 80 | </Button> |
| 81 | </div> |
| 82 | |
| 83 | <details className="group mt-5 overflow-hidden rounded-lg border border-border/70 bg-background/55"> |
| 84 | <summary className="cursor-pointer list-none px-3 py-2 text-xs font-medium text-muted-foreground"> |
| 85 | <span className="group-open:hidden">Show error details</span> |
| 86 | <span className="hidden group-open:inline">Hide error details</span> |
| 87 | </summary> |
| 88 | <pre className="max-h-56 overflow-auto border-t border-border/70 bg-background/80 px-3 py-2 text-xs text-foreground/85"> |
| 89 | {details} |
| 90 | </pre> |
| 91 | </details> |
| 92 | </section> |
| 93 | </div> |
| 94 | ); |
| 95 | } |
| 96 | |
| 97 | function errorMessage(error: unknown): string { |
| 98 | if (error instanceof Error && error.message.trim().length > 0) { |
| 99 | return error.message; |
| 100 | } |
| 101 | |
| 102 | if (typeof error === "string" && error.trim().length > 0) { |
| 103 | return error; |
| 104 | } |
| 105 | |
| 106 | return "An unexpected router error occurred."; |
| 107 | } |
| 108 | |
| 109 | function errorDetails(error: unknown): string { |
| 110 | if (error instanceof Error) { |
| 111 | return error.stack ?? error.message; |
| 112 | } |
| 113 | |
| 114 | if (typeof error === "string") { |
| 115 | return error; |
| 116 | } |
| 117 | |
| 118 | try { |
| 119 | return JSON.stringify(error, null, 2); |
| 120 | } catch { |
| 121 | return "No additional error details are available."; |
| 122 | } |
| 123 | } |
| 124 | |
| 47 | 125 | function EventRouter() { |
| 48 | 126 | const api = useNativeApi(); |
| 49 | 127 | const { dispatch } = useStore(); |