"use client"; import { useState, useCallback } from "react"; import { AlertTriangle, Send, X, Bug } from "lucide-react"; // ── Feedback submission API ── async function submitFeedback(data: { url: string; error: string; message?: string; userAgent: string; }) { try { const res = await fetch("/mobile/api/feedback", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(data), }); return res.ok; } catch { return false; // silently fail — we don't want a feedback-on-feedback loop } } // ── Friendly error messages map ── const FRIENDLY_MESSAGES: Record = { default: { title: "A small glitch 🛠️", description: "Something didn't load quite right. Don't worry — we're on it! Try again or drop us a report so we can fix it faster.", }, location: { title: "Location not available 📡", description: "We couldn't detect your location. Make sure location services are enabled, or try refreshing. The compass will work even without location!", }, network: { title: "Connection hiccup 🌐", description: "Looks like the signal dropped for a moment. Check your connection and try again. If it persists, let us know!", }, auth: { title: "Session expired 🔑", description: "Your session ran out — it happens! Just sign in again and you'll be right back where you were.", }, upgrade: { title: "Premium feature ✨", description: "This feature needs an upgrade. Check your plan details to unlock it!", }, }; type ErrorKind = keyof typeof FRIENDLY_MESSAGES; interface ErrorFeedbackProps { /** The raw error message from the system */ error: string; /** Category of error for friendly message mapping */ kind?: ErrorKind; /** Callback to retry the action */ onRetry?: () => void; /** Additional context for debugging */ context?: string; } export default function ErrorFeedback({ error, kind = "default", onRetry, context, }: ErrorFeedbackProps) { const [showForm, setShowForm] = useState(false); const [userMessage, setUserMessage] = useState(""); const [sent, setSent] = useState(false); const [sending, setSending] = useState(false); const friendly = FRIENDLY_MESSAGES[kind] || FRIENDLY_MESSAGES.default; const handleSend = useCallback(async () => { setSending(true); const ok = await submitFeedback({ url: window.location.href, error: error + (context ? ` [${context}]` : ""), message: userMessage, userAgent: navigator.userAgent, }); setSent(true); setSending(false); if (ok) { setTimeout(() => setShowForm(false), 2000); } }, [error, context, userMessage]); return (
{/* ── Friendly header ── */}

{friendly.title}

{friendly.description}

{/* ── Actions ── */}
{onRetry && ( )}
{/* ── Feedback form (expandable) ── */} {showForm && (
{sent ? (

✓ Thanks! Your report helps us improve.

) : ( <>

What happened? (optional — helps us fix it faster)