import { useState } from "react"; import { FileSignature, Loader2, Stamp } from "lucide-react"; import { useMutation, useQuery } from "@tanstack/react-query"; import toast from "react-hot-toast"; import { api } from "@/services/api"; import { ContractSignaturePad } from "@/components/bookings/ContractSignaturePad"; import { StampUpload } from "@/components/contracts/StampUpload"; import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from "@/components/ui/card"; import { useAuth } from "@/auth/useAuth"; import { Button, Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, Input, Label, } from "@edr/ui-common"; /** * Lets the signed-in user view and update the reusable signature and company * stamp stored on their profile — managed independently of each other. Both * are offered when signing a booking contract. */ export function MySignatureCard() { const { user } = useAuth(); const { data: saved, isLoading } = useQuery( api.signatures.mySignature.queryOptions({ staleTime: 60_000 }), ); const saveMutation = useMutation(api.signatures.save.mutationOptions()); const [signatureOpen, setSignatureOpen] = useState(false); const [stampOpen, setStampOpen] = useState(false); const [signerName, setSignerName] = useState(""); const [signatureData, setSignatureData] = useState(null); const [stampData, setStampData] = useState(null); const defaultName = user?.name?.en || user?.username || user?.email || ""; const savedName = saved?.signerDisplayName ?? defaultName; const openSignatureDialog = () => { setSignerName(savedName); setSignatureData(null); setSignatureOpen(true); }; const saveSignature = () => { if (!signatureData || !signerName.trim()) return; saveMutation.mutate( { signerDisplayName: signerName.trim(), signatureImageBase64: signatureData, // Stamp untouched — it is managed by its own dialog. }, { onSuccess: () => { toast.success("Signature saved"); setSignatureOpen(false); }, onError: () => toast.error("Failed to save signature"), }, ); }; const openStampDialog = () => { setStampData(saved?.stampImageUrl ?? null); setStampOpen(true); }; const saveStamp = () => { if (!stampData) return; saveMutation.mutate( { signerDisplayName: savedName || defaultName, // Signature untouched — stamp-only update. stampImageBase64: stampData, }, { onSuccess: () => { toast.success("Stamp saved"); setStampOpen(false); }, onError: () => toast.error("Failed to save stamp"), }, ); }; return ( Signature & Stamp This signature can be reused to sign booking contracts. {isLoading ? (
) : ( <>
{saved?.signatureImageUrl ? ( <>
My saved signature

Saved as {saved.signerDisplayName}

) : (

You have not saved a signature yet.

)}
{saved?.stampImageUrl ? ( <>
My saved company stamp

Company stamp

) : (

You have not uploaded a company stamp yet.

)}
)}
Save your signature Draw your signature below. It will be stored on your profile for future contracts.
setSignerName(e.target.value)} placeholder="As shown on contracts" />
Company stamp Upload your official company stamp or seal as an image. It is stored on your profile and applied next to your signature on contracts.
); }