2026-05-17 16:11:44 +00:00
|
|
|
import { useEffect, useRef } from "react";
|
2025-10-26 21:01:12 -07:00
|
|
|
import { Button } from "~/components/ui/button";
|
|
|
|
|
import { Card } from "~/components/ui/card";
|
|
|
|
|
|
|
|
|
|
interface ConnectionOverlayProps {
|
|
|
|
|
isConnected: boolean;
|
|
|
|
|
isReconnecting: boolean;
|
|
|
|
|
connectionError: string | null;
|
2026-04-30 20:33:00 -07:00
|
|
|
isSyncing: boolean;
|
2025-10-26 21:01:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function ConnectionOverlay({
|
|
|
|
|
isConnected,
|
|
|
|
|
isReconnecting,
|
|
|
|
|
connectionError,
|
2026-04-30 20:33:00 -07:00
|
|
|
isSyncing,
|
2025-10-26 21:01:12 -07:00
|
|
|
}: ConnectionOverlayProps) {
|
2026-05-17 16:11:44 +00:00
|
|
|
const dialogRef = useRef<HTMLDivElement>(null);
|
|
|
|
|
const titleId = "connection-overlay-title";
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (isConnected && !isSyncing) return;
|
|
|
|
|
const el = dialogRef.current;
|
|
|
|
|
if (!el) return;
|
|
|
|
|
const focusable = el.querySelector<HTMLElement>(
|
|
|
|
|
'button:not([disabled]), [href], input:not([disabled]), [tabindex]:not([tabindex="-1"])'
|
|
|
|
|
);
|
|
|
|
|
focusable?.focus();
|
|
|
|
|
|
|
|
|
|
function trapFocus(e: KeyboardEvent) {
|
|
|
|
|
if (e.key !== "Tab") return;
|
|
|
|
|
const focusables = el?.querySelectorAll<HTMLElement>(
|
|
|
|
|
'button:not([disabled]), [href], input:not([disabled]), [tabindex]:not([tabindex="-1"])'
|
|
|
|
|
);
|
|
|
|
|
if (!focusables || focusables.length === 0) return;
|
|
|
|
|
const first = focusables[0];
|
|
|
|
|
const last = focusables[focusables.length - 1];
|
|
|
|
|
if (e.shiftKey) {
|
|
|
|
|
if (document.activeElement === first) { e.preventDefault(); last.focus(); }
|
|
|
|
|
} else {
|
|
|
|
|
if (document.activeElement === last) { e.preventDefault(); first.focus(); }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
document.addEventListener("keydown", trapFocus);
|
|
|
|
|
return () => document.removeEventListener("keydown", trapFocus);
|
|
|
|
|
}, [isConnected, isSyncing]);
|
|
|
|
|
|
2026-04-30 20:33:00 -07:00
|
|
|
if (isConnected && !isSyncing) {
|
2025-10-26 21:01:12 -07:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-17 16:11:44 +00:00
|
|
|
const statusMessage = connectionError
|
|
|
|
|
? connectionError
|
|
|
|
|
: isSyncing
|
|
|
|
|
? "Reconnected. Syncing the latest draft state..."
|
|
|
|
|
: isReconnecting
|
|
|
|
|
? "Lost connection to the draft server. Attempting to reconnect..."
|
|
|
|
|
: "Please wait while we connect you to the live draft room.";
|
|
|
|
|
|
2025-10-26 21:01:12 -07:00
|
|
|
return (
|
2026-05-17 16:11:44 +00:00
|
|
|
<div
|
|
|
|
|
ref={dialogRef}
|
|
|
|
|
role="dialog"
|
|
|
|
|
aria-modal="true"
|
|
|
|
|
aria-labelledby={titleId}
|
|
|
|
|
className="fixed inset-0 bg-black/60 backdrop-blur-sm flex items-center justify-center z-[100]"
|
|
|
|
|
>
|
2025-10-26 21:01:12 -07:00
|
|
|
<Card className="w-full max-w-md mx-4 p-8 text-center">
|
|
|
|
|
<div className="flex flex-col items-center gap-6">
|
|
|
|
|
{/* Spinner or Error Icon */}
|
|
|
|
|
{connectionError ? (
|
2026-02-20 19:26:11 -08:00
|
|
|
<div className="w-16 h-16 rounded-full bg-destructive/15 flex items-center justify-center">
|
2025-10-26 21:01:12 -07:00
|
|
|
<svg
|
2026-02-20 19:26:11 -08:00
|
|
|
className="w-8 h-8 text-destructive"
|
2025-10-26 21:01:12 -07:00
|
|
|
fill="none"
|
|
|
|
|
stroke="currentColor"
|
|
|
|
|
viewBox="0 0 24 24"
|
|
|
|
|
xmlns="http://www.w3.org/2000/svg"
|
|
|
|
|
>
|
|
|
|
|
<path
|
|
|
|
|
strokeLinecap="round"
|
|
|
|
|
strokeLinejoin="round"
|
|
|
|
|
strokeWidth={2}
|
|
|
|
|
d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z"
|
|
|
|
|
/>
|
|
|
|
|
</svg>
|
|
|
|
|
</div>
|
|
|
|
|
) : (
|
|
|
|
|
<div className="relative w-16 h-16">
|
2026-03-21 09:44:05 -07:00
|
|
|
<div className="absolute inset-0 border-4 border-primary/30 rounded-full" />
|
|
|
|
|
<div className="absolute inset-0 border-4 border-primary border-t-transparent rounded-full animate-spin" />
|
2025-10-26 21:01:12 -07:00
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
{/* Title */}
|
|
|
|
|
<div>
|
2026-05-17 16:11:44 +00:00
|
|
|
<h2 id={titleId} className="text-2xl font-bold mb-2">
|
2025-10-26 21:01:12 -07:00
|
|
|
{connectionError
|
|
|
|
|
? "Connection Error"
|
2026-04-30 20:33:00 -07:00
|
|
|
: isSyncing
|
|
|
|
|
? "Syncing Draft State..."
|
2025-10-26 21:01:12 -07:00
|
|
|
: isReconnecting
|
|
|
|
|
? "Reconnecting..."
|
|
|
|
|
: "Connecting to Draft"}
|
|
|
|
|
</h2>
|
|
|
|
|
|
2026-05-17 16:11:44 +00:00
|
|
|
<p className="text-muted-foreground">{statusMessage}</p>
|
2025-10-26 21:01:12 -07:00
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
{/* Action Button (only on persistent error) */}
|
|
|
|
|
{connectionError && (
|
|
|
|
|
<Button
|
|
|
|
|
onClick={() => window.location.reload()}
|
|
|
|
|
className="w-full"
|
|
|
|
|
variant="default"
|
|
|
|
|
>
|
|
|
|
|
Reload Page
|
|
|
|
|
</Button>
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
{/* Loading Dots */}
|
|
|
|
|
{!connectionError && (
|
2026-05-17 16:11:44 +00:00
|
|
|
<div aria-live="polite" aria-label={statusMessage} className="flex gap-1.5">
|
2025-10-26 21:01:12 -07:00
|
|
|
<div
|
2026-05-17 16:11:44 +00:00
|
|
|
aria-hidden="true"
|
2025-10-26 21:01:12 -07:00
|
|
|
className="w-2 h-2 bg-primary rounded-full animate-bounce"
|
|
|
|
|
style={{ animationDelay: "0ms" }}
|
2026-03-21 09:44:05 -07:00
|
|
|
/>
|
2025-10-26 21:01:12 -07:00
|
|
|
<div
|
2026-05-17 16:11:44 +00:00
|
|
|
aria-hidden="true"
|
2025-10-26 21:01:12 -07:00
|
|
|
className="w-2 h-2 bg-primary rounded-full animate-bounce"
|
|
|
|
|
style={{ animationDelay: "150ms" }}
|
2026-03-21 09:44:05 -07:00
|
|
|
/>
|
2025-10-26 21:01:12 -07:00
|
|
|
<div
|
2026-05-17 16:11:44 +00:00
|
|
|
aria-hidden="true"
|
2025-10-26 21:01:12 -07:00
|
|
|
className="w-2 h-2 bg-primary rounded-full animate-bounce"
|
|
|
|
|
style={{ animationDelay: "300ms" }}
|
2026-03-21 09:44:05 -07:00
|
|
|
/>
|
2025-10-26 21:01:12 -07:00
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
</Card>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|