Open History to see the last commit
1import { useState } from "react";
2
3import { Button } from "~/components/ui/button";
4
5import { describePreviewError } from "./errorCodeMessages";
6
7interface Props {
8 url: string;
9 /** Chromium net error code, e.g. -105. */
10 code: number;
11 /** Stringified Chromium error, e.g. "ERR_NAME_NOT_RESOLVED". */
12 description: string;
13 onReload: () => void;
14}
15
16/** Theme-aware tailwind port of Chromium's "This site can't be reached" page. */
17export function PreviewUnreachable({ url, code, description, onReload }: Props) {
18 const [showDetails, setShowDetails] = useState(false);
19 const host = safeHost(url) ?? url;
20 const friendly = describePreviewError(description);
21 const errorLabel = description.length > 0 ? description : `ERR_${Math.abs(code) || "FAILED"}`;
22
23 return (
24 <div className="relative flex h-full min-h-0 w-full overflow-y-auto bg-background">
25 <div className="mx-auto flex w-full max-w-xl flex-1 flex-col px-8 py-12 sm:py-16">
26 <ErrorIcon className="mb-6 size-12 text-muted-foreground/70" />
27 <h1 className="mb-3 text-2xl font-semibold leading-tight text-foreground">
28 This site can’t be reached
29 </h1>
30 <p className="text-sm leading-relaxed text-muted-foreground">
31 <span className="font-semibold text-foreground">{host}</span>: {friendly}.
32 </p>
33
34 {showDetails ? (
35 <div className="mt-6 rounded-lg border border-border bg-muted/40 p-4 text-sm">
36 <p className="mb-2 font-medium text-foreground">Try:</p>
37 <ul className="list-disc space-y-1 pl-5 text-muted-foreground">
38 <li>Checking your connection</li>
39 <li>Confirming the dev server is running</li>
40 <li>Checking the proxy and the firewall</li>
41 </ul>
42 </div>
43 ) : null}
44
45 <div className="mt-8 text-xs uppercase tracking-wide text-muted-foreground/70">
46 {errorLabel}
47 </div>
48
49 <div className="mt-auto flex items-center gap-2 pt-8">
50 <Button
51 type="button"
52 variant="outline"
53 size="sm"
54 onClick={() => setShowDetails((value) => !value)}
55 >
56 {showDetails ? "Hide details" : "Details"}
57 </Button>
58 <div className="flex-1" />
59 <Button type="button" size="sm" onClick={onReload}>
60 Reload
61 </Button>
62 </div>
63 </div>
64 </div>
65 );
66}
67
68function ErrorIcon({ className }: { className?: string }) {
69 return (
70 <svg
71 viewBox="0 0 64 64"
72 fill="none"
73 stroke="currentColor"
74 strokeWidth="2.5"
75 className={className}
76 >
77 <path d="M16 12 L48 12 L48 52 L16 52 Z" />
78 <path d="M22 22 L42 22 M22 30 L36 30 M22 38 L40 38" strokeLinecap="round" />
79 <path d="M52 8 L12 56" strokeLinecap="round" />
80 </svg>
81 );
82}
83
84function safeHost(url: string): string | null {
85 try {
86 return new URL(url).host;
87 } catch {
88 return null;
89 }
90}