Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

PreviewUnreachable.tsx

135 lines4.4 KB

1import { useState } from "react";
2​
3import { Button } from "~/components/ui/button";
4​
5import { describePreviewError } from "./errorCodeMessages";
6​
7interface Props {
8 url: string;
9 code: number;
10 description: string;
11 onReload: () => void;
12 move?: { readonly label: string; readonly onMove: () => void };
14​
15export function PreviewFileNotShown({
16 url,
17 fileName,
18 downloadUrl,
19 onOpen,
20}: {
21 url: string;
22 fileName: string;
23 downloadUrl: string | null;
24 onOpen: () => void;
25}) {
26 const host = safeHost(url) ?? url;
27 return (
28 <div className="scrollbar-gutter-both relative flex h-full min-h-0 w-full overflow-y-auto bg-background">
29 <div className="mx-auto flex w-full max-w-xl flex-1 flex-col px-8 py-12 sm:py-16">
30 <ErrorIcon className="mb-6 size-12 text-muted-foreground/70" />
31 <h1 className="mb-3 text-2xl font-semibold leading-tight text-foreground">
32 This file can&rsquo;t be shown here
33 </h1>
34 <p className="text-sm leading-relaxed text-muted-foreground">
35 <span className="font-semibold text-foreground">{fileName}</span> from {host} was
36 downloaded instead.
37 </p>
38 <div className="mt-auto flex items-center gap-2 pt-8">
39 <div className="flex-1" />
40 <Button type="button" variant="outline" size="sm" onClick={onOpen}>
41 Open in browser
42 </Button>
43 {downloadUrl ? (
44 <Button type="button" size="sm" render={<a href={downloadUrl} download={fileName} />}>
45 Download
46 </Button>
47 ) : null}
48 </div>
49 </div>
50 </div>
51 );
53​
54export function PreviewUnreachable({ url, code, description, onReload, move }: Props) {
55 const [showDetails, setShowDetails] = useState(false);
56 const host = safeHost(url) ?? url;
57 const friendly = describePreviewError(description);
58 const errorLabel = description.length > 0 ? description : `ERR_${Math.abs(code) || "FAILED"}`;
59​
60 return (
61 <div className="scrollbar-gutter-both relative flex h-full min-h-0 w-full overflow-y-auto bg-background">
62 <div className="mx-auto flex w-full max-w-xl flex-1 flex-col px-8 py-12 sm:py-16">
63 <ErrorIcon className="mb-6 size-12 text-muted-foreground/70" />
64 <h1 className="mb-3 text-2xl font-semibold leading-tight text-foreground">
65 This site can&rsquo;t be reached
66 </h1>
67 <p className="text-sm leading-relaxed text-muted-foreground">
68 <span className="font-semibold text-foreground">{host}</span>: {friendly}.
69 </p>
70​
71 {showDetails ? (
72 <div className="mt-6 rounded-lg border border-border bg-muted/40 p-4 text-sm">
73 <p className="mb-2 font-medium text-foreground">Try:</p>
74 <ul className="list-disc space-y-1 pl-5 text-muted-foreground">
75 <li>Checking your connection</li>
76 <li>Confirming the dev server is running</li>
77 {move ? (
78 <li>Opening it in the environment&rsquo;s browser, which reaches its network</li>
79 ) : null}
80 <li>Checking the proxy and the firewall</li>
81 </ul>
82 </div>
83 ) : null}
84​
85 <div className="mt-8 text-xs uppercase tracking-wide text-muted-foreground/70">
86 {errorLabel}
87 </div>
88​
89 <div className="mt-auto flex items-center gap-2 pt-8">
90 <Button
91 type="button"
92 variant="outline"
93 size="sm"
94 onClick={() => setShowDetails((value) => !value)}
95 >
96 {showDetails ? "Hide details" : "Details"}
97 </Button>
98 <div className="flex-1" />
99 {move ? (
100 <Button type="button" variant="outline" size="sm" onClick={move.onMove}>
101 {move.label}
102 </Button>
103 ) : null}
104 <Button type="button" size="sm" onClick={onReload}>
105 Reload
106 </Button>
107 </div>
108 </div>
109 </div>
110 );
112​
113function ErrorIcon({ className }: { className?: string }) {
114 return (
115 <svg
116 viewBox="0 0 64 64"
117 fill="none"
118 stroke="currentColor"
119 strokeWidth="2.5"
120 className={className}
121 >
122 <path d="M16 12 L48 12 L48 52 L16 52 Z" />
123 <path d="M22 22 L42 22 M22 30 L36 30 M22 38 L40 38" strokeLinecap="round" />
124 <path d="M52 8 L12 56" strokeLinecap="round" />
125 </svg>
126 );
128​
129function safeHost(url: string): string | null {
130 try {
131 return new URL(url).host;
132 } catch {
133 return null;
134 }