import { useState, useRef, useEffect } from 'react'; type Recipient = { id: string; name: string; email: string; hasSigned: boolean; certificateId: string | null; }; type OverlayElement = { id: string; type: 'signature' | 'initials' | 'fullName' | 'date'; recipientId: string; x: number; y: number; width: number; height: number; }; type DocumentStatus = 'Draft' | 'Pending Signatures' | 'Signed' | 'Accepted' | 'Rejected'; const availableCertificates = [ { id: 'cert_1', name: 'John Doe Signature Cert', issuer: 'DigiSign Corp', validity: '2023-2025' }, { id: 'cert_2', name: 'Company A Document Signer', issuer: 'GlobalTrust CA', validity: '2024-2026' }, ]; const generateId = () => Math.random().toString(36).substring(2, 9); export default function DigitalSignatureApp() { const [step, setStep] = useState(0); const [documentName, setDocumentName] = useState(''); const [base64Pdf, setBase64Pdf] = useState(null); const [requester, setRequester] = useState({ name: '', email: '' }); const [recipients, setRecipients] = useState([]); const [overlayElements, setOverlayElements] = useState([]); const [documentStatus, setDocumentStatus] = useState('Draft'); const [signingError, setSigningError] = useState(null); const [pinInput, setPinInput] = useState(''); const [showSigningModal, setShowSigningModal] = useState(false); const [activeRecipient, setActiveRecipient] = useState(null); const [selectedRecipient, setSelectedRecipient] = useState(null); const pdfViewerRef = useRef(null); const [isPlacing, setIsPlacing] = useState(false); const [draggingType, setDraggingType] = useState(null); const handleDocumentUpload = (e: React.ChangeEvent) => { if (e.target.files && e.target.files[0]) { const file = e.target.files[0]; setDocumentName(file.name); const reader = new FileReader(); reader.onloadend = () => setBase64Pdf(reader.result as string); reader.readAsDataURL(file); } }; const handleAddRecipient = () => { setRecipients([...recipients, { id: generateId(), name: '', email: '', hasSigned: false, certificateId: null }]); }; const handleRemoveRecipient = (id: string) => { setRecipients(recipients.filter(r => r.id !== id)); setOverlayElements(overlayElements.filter(o => o.recipientId !== id)); }; const handleRecipientChange = (id: string, field: keyof Recipient, value: string) => { setRecipients(recipients.map(r => r.id === id ? { ...r, [field]: value } : r)); }; const handleDragStart = (type: OverlayElement['type']) => { if (!selectedRecipient) { setSigningError('Please select a recipient first.'); return; } setDraggingType(type); setIsPlacing(true); setSigningError(null); }; const handlePdfClick = (e: React.MouseEvent) => { if (!isPlacing || !draggingType || !pdfViewerRef.current || !selectedRecipient) return; const viewerRect = pdfViewerRef.current.getBoundingClientRect(); const x = ((e.clientX - viewerRect.left) / viewerRect.width) * 100; const y = ((e.clientY - viewerRect.top) / viewerRect.height) * 100; const width = 15; const height = draggingType === 'signature' ? 8 : 5; setOverlayElements(prev => [ ...prev, { id: generateId(), type: draggingType, recipientId: selectedRecipient, x: Math.max(0, Math.min(100 - width, x)), y: Math.max(0, Math.min(100 - height, y)), width, height, } ]); setIsPlacing(false); setDraggingType(null); }; const initiateSigning = (recipient: Recipient) => { setActiveRecipient(recipient); setShowSigningModal(true); setPinInput(''); setSigningError(null); }; const handleConfirmSigning = () => { if (!activeRecipient) { setSigningError("No active recipient."); return; } if (pinInput === '1234') { setRecipients(recipients.map(r => r.id === activeRecipient.id ? { ...r, hasSigned: true } : r )); setShowSigningModal(false); const allSigned = recipients.every(r => r.hasSigned || r.id === activeRecipient.id); if (allSigned) { setStep(5); } } else { setSigningError('Invalid PIN. Please try again.'); } }; const handleSaveDocument = (submit: boolean) => { const status: DocumentStatus = submit ? (recipients.every(r => r.hasSigned) ? 'Signed' : 'Pending Signatures') : 'Draft'; setDocumentStatus(status); alert(`Document ${submit ? 'submitted' : 'saved'} as ${status}.`); setStep(0); }; return (

Digital Signature App

{/* Step Indicator */}
{['Start', 'Parties', 'Upload', 'Prepare', 'Sign', 'Review'].map((label, i) => (
{i + 1}
{label}
))}
{/* Step Content */}
{/* Step 0: Start */} {step === 0 && (

Welcome to Digital Signatures

Securely sign documents with PKI-based digital signatures

)} {/* Step 1: Define Parties */} {step === 1 && (

Define Document Parties

Requester Information

setRequester({...requester, name: e.target.value})} className="w-full px-4 py-2 border rounded-lg" />
setRequester({...requester, email: e.target.value})} className="w-full px-4 py-2 border rounded-lg" />

Recipients

{recipients.map((rec, i) => (

Recipient {i + 1}

handleRecipientChange(rec.id, 'name', e.target.value)} className="w-full px-4 py-2 border rounded-lg" />
handleRecipientChange(rec.id, 'email', e.target.value)} className="w-full px-4 py-2 border rounded-lg" />
))}
{signingError && (

{signingError}

)}
)} {/* Step 2: Upload Document */} {step === 2 && (

Upload Document

Drag and drop your PDF here, or

{documentName && (

Selected: {documentName}

)}
)} {/* Step 3: Prepare Document */} {step === 3 && (

Prepare Document

{/* Toolbar */}

Place Signature Fields

{(['signature', 'initials', 'fullName', 'date'] as const).map(type => ( ))}
{isPlacing && (

Click on the document to place the {draggingType} field.

)}
{/* PDF Viewer */}

Document Preview

{base64Pdf ? (
PDF Preview: {documentName}
) : (
Upload a PDF to view here
)} {overlayElements.map(el => { const recipient = recipients.find(r => r.id === el.recipientId); const displayValue = el.type === 'fullName' ? (recipient?.name || '[Name]') : el.type === 'date' ? new Date().toLocaleDateString() : `[${el.type}]`; return (
{displayValue}
); })}
{signingError && (

{signingError}

)}
)} {/* Step 4: Signatures */} {step === 4 && (

Digital Signatures

{recipients.map(rec => (

Signee: {rec.name}

{rec.hasSigned ? (
✓ Signed Successfully!
) : ( <>
)}
))}
)} {/* Step 5: Review */} {step === 5 && (

Document Processed!

Your document "{documentName}" has been successfully prepared and signed.

)}
{/* Signing Modal */} {showSigningModal && activeRecipient && (

Enter PIN

Enter PIN for {activeRecipient.name}'s certificate

setPinInput(e.target.value)} placeholder="Enter your PIN" className="w-full px-4 py-3 border rounded-lg text-center text-xl mb-6" maxLength={6} /> {signingError && (

{signingError}

)}
)}
); }