"use client" import { useEffect, useState } from "react" import { toast } from "sonner" import { Check, Copy, ExternalLink, Link2, XCircle } from "lucide-react" import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "@/shared/components/ui/dialog" import { Button } from "@/shared/components/ui/button" import { Input } from "@/shared/components/ui/input" import { useAuthApi } from "@/shared/useApi" import { confirm } from "@/shared/components/confirm-dialog" export function InspectionShareDialog({ open, onOpenChange, inspectionId, }: { open: boolean onOpenChange: (o: boolean) => void inspectionId: number | string }) { const api = useAuthApi() const [loading, setLoading] = useState(false) const [shareUrl, setShareUrl] = useState(null) const [expiresAt, setExpiresAt] = useState(null) const [copied, setCopied] = useState(false) const generate = async () => { setLoading(true) try { const res = await api.inspections.share(inspectionId) setShareUrl(res.data.share_url) setExpiresAt(res.data.share_expires_at) } catch (e: any) { toast.error(e?.payload?.message ?? e?.message ?? "Failed to generate share link") onOpenChange(false) } finally { setLoading(false) } } useEffect(() => { if (open && !shareUrl) generate() if (!open) { setShareUrl(null) setExpiresAt(null) setCopied(false) } }, [open]) const copy = async () => { if (!shareUrl) return try { await navigator.clipboard.writeText(shareUrl) setCopied(true) toast.success("Link copied") setTimeout(() => setCopied(false), 2000) } catch { toast.error("Copy failed — select the field and press Cmd/Ctrl+C") } } const revoke = async () => { const ok = await confirm({ title: "Revoke this share link?", description: "The customer will no longer be able to view the report.", confirmLabel: "Revoke", variant: "destructive", }) if (!ok) return try { await api.inspections.revokeShare(inspectionId) toast.success("Share link revoked") onOpenChange(false) } catch (e: any) { toast.error(e?.payload?.message ?? e?.message ?? "Failed to revoke") } } return ( Share inspection with customer Anyone with this link can view the inspection report. No login required. {loading && (
Generating link…
)} {shareUrl && (
{expiresAt && (

Expires on {new Date(expiresAt).toLocaleDateString()}.

)}
)}
) }