import React, { useState, useRef, useEffect } from 'react'; import { auditRtiApi, analyzeDocumentApi, RtiAuditResponse } from '../services/api'; interface RtiAuditorModalProps { selectedCity: string; onClose: () => void; onLaunchRtiCase?: (optimizedDraft: string, title: string) => void; } const SAMPLE_RTI_DRAFTS = [ { title: 'Ward Road Resurfacing Tender & Quality Audit', text: 'Why is the road work in Ward 150 delayed? Who is the contractor and why did it develop potholes within 3 months? When will it be repaired?', }, { title: 'Building Plan Sanction File Movement', text: 'Please inform me why my building plan sanction application #BBMP/LP/2024/890 is still pending after 60 days. Which officer is sitting on the file?', }, { title: 'Police Action-Taken Report on NCR', text: 'What action has been taken on my complaint filed on 10th August? Why has the FIR not been registered yet and who investigated it?', }, ]; export const RtiAuditorModal: React.FC = ({ selectedCity, onClose, onLaunchRtiCase, }) => { const [rtiText, setRtiText] = useState(SAMPLE_RTI_DRAFTS[0].text); const [auditing, setAuditing] = useState(false); const [auditResult, setAuditResult] = useState(null); const [auditError, setAuditError] = useState(null); const [copiedDraft, setCopiedDraft] = useState(false); // Document / PDF Upload State const [uploadedFileName, setUploadedFileName] = useState(null); const [isProcessingFile, setIsProcessingFile] = useState(false); const fileInputRef = useRef(null); // Close on Escape key press useEffect(() => { const handleKeyDown = (e: KeyboardEvent) => { if (e.key === 'Escape') { onClose(); } }; window.addEventListener('keydown', handleKeyDown); return () => window.removeEventListener('keydown', handleKeyDown); }, [onClose]); const handleFileUpload = (e: React.ChangeEvent) => { const file = e.target.files?.[0]; if (!file) return; setUploadedFileName(file.name); setIsProcessingFile(true); setAuditError(null); const reader = new FileReader(); reader.onload = async () => { const result = reader.result as string; const base64Data = result.split(',')[1] || result; try { setAuditing(true); // Extract text & diagnosis using document analysis const docAnalysis = await analyzeDocumentApi({ fileBase64: base64Data, mimeType: file.type || 'application/pdf', fileName: file.name, jurisdiction: selectedCity, }); if (docAnalysis) { const extractedGrievance = `${docAnalysis.title}\n${docAnalysis.legalDiagnosis || ''}\n${docAnalysis.requestScope || ''}`.trim(); setRtiText(extractedGrievance); // Run RTI quality audit on the extracted text const audit = await auditRtiApi(extractedGrievance, selectedCity); setAuditResult(audit); } } catch (err: any) { console.error('PDF extraction error in RTI auditor:', err); setAuditError(err?.message || 'Could not extract text from document. Please copy-paste text instead.'); } finally { setIsProcessingFile(false); setAuditing(false); } }; reader.onerror = () => { setIsProcessingFile(false); setAuditError('Failed to read the uploaded document.'); }; reader.readAsDataURL(file); }; const handleRunAudit = async (customText?: string) => { const text = customText !== undefined ? customText : rtiText; if (!text.trim()) return; setAuditing(true); setAuditError(null); try { const res = await auditRtiApi(text, selectedCity); setAuditResult(res); } catch (err: any) { console.error('RTI Audit error:', err); setAuditError(err?.message || 'Failed to complete RTI audit. Please retry.'); } finally { setAuditing(false); } }; const handleCopy = (text: string) => { navigator.clipboard.writeText(text); setCopiedDraft(true); setTimeout(() => setCopiedDraft(false), 2500); }; return (
{ if (e.target === e.currentTarget) { onClose(); } }} className="fixed inset-0 bg-black/60 backdrop-blur-xs flex items-center justify-center p-4 z-50 animate-in fade-in duration-200 cursor-pointer" >
e.stopPropagation()} className="bg-white rounded-2xl max-w-4xl w-full max-h-[90vh] flex flex-col shadow-2xl border border-[#CBD5E1] overflow-hidden cursor-default" > {/* Header */}
verified

RTI Strength & Quality Auditor

RTI Act 2005 Compliance

Audit draft questions or upload rejection letters/PDFs to get a reject-proof statutory RTI draft

{/* Body */}
{/* Input & PDF Upload Section */}
{/* PDF Upload Banner */}
upload_file

{uploadedFileName ? `Loaded: ${uploadedFileName}` : 'Have an RTI Draft or Govt Rejection Order in PDF?'}

{uploadedFileName ? 'Document extracted. Audit results generated below.' : 'Upload PIO reply, Section 8 rejection letter, or application draft (PDF / Image)'}

Try sample: {SAMPLE_RTI_DRAFTS.map((sample, idx) => ( ))}