'use client'; import React, { useState, useRef } from 'react'; import { useRouter } from 'next/navigation'; import api from '../services/api'; interface CreateRFPModalProps { isOpen: boolean; onClose: () => void; onSuccess?: (projectId: string) => void; } const CreateRFPModal: React.FC = ({ isOpen, onClose, onSuccess }) => { const router = useRouter(); const fileInputRef = useRef(null); const [clientName, setClientName] = useState(''); const [files, setFiles] = useState([]); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const [analysisStep, setAnalysisStep] = useState<'idle' | 'uploading' | 'shredding' | 'analyzing'>('idle'); // Handle file selection const handleFileChange = (e: React.ChangeEvent) => { if (e.target.files) { const selectedFiles = Array.from(e.target.files); // Validate file types const validTypes = [ 'application/pdf', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application/msword', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/vnd.ms-excel', 'text/csv', 'image/png', 'image/jpeg', 'image/jpg', 'image/tiff' ]; const validFiles = selectedFiles.filter( (f) => { const isDoc = validTypes.includes(f.type); const hasKnownExt = /\.(pdf|docx|doc|xlsx|xls|csv|png|jpg|jpeg|tiff|bmp)$/i.test(f.name); return isDoc || hasKnownExt; } ); if (validFiles.length !== selectedFiles.length) { setError('Some files were skipped. Unsupported format detected.'); } else { setError(null); } setFiles((prev) => [...prev, ...validFiles]); } }; // Remove a file from the list const removeFile = (index: number) => { setFiles((prev) => prev.filter((_, i) => i !== index)); }; const [source, setSource] = useState<'upload' | 'research'>('upload'); const [deepResearchId, setDeepResearchId] = useState(''); // Handle form submission const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); if (source === 'upload' && files.length === 0) { setError('Please upload at least one document'); return; } setLoading(true); setError(null); try { let projectId: string; // Use first file's name as temporary project name (will be updated after AI analysis) const tempProjectName = files.length > 0 ? files[0].name.replace(/\.[^/.]+$/, '') // Remove file extension : 'New RFP Project'; if (source === 'upload' && files.length > 0) { setAnalysisStep('uploading'); const result = await api.projects.createWithFiles( tempProjectName, files, clientName.trim() || undefined ); projectId = result.id; } else { // Initiate shell project const result = await api.projects.initiate( tempProjectName, clientName.trim(), source === 'research' ? deepResearchId : undefined ); projectId = result.id; } // Always analyze documents after upload if (files.length > 0) { setAnalysisStep('shredding'); await api.projects.shredProject(projectId); setAnalysisStep('analyzing'); // Extract metadata (including AI-generated project name) and run Go/No-Go analysis await api.projects.extractMetadata(projectId); await api.projects.analyzeGoNoGo(projectId); } if (onSuccess) onSuccess(projectId); router.push(`/compliance?projectId=${projectId}`); onClose(); // Reset setClientName(''); setFiles([]); setDeepResearchId(''); setAnalysisStep('idle'); } catch (err: any) { console.error('Create project error:', err); setError(err.message || 'Failed to process project.'); setAnalysisStep('idle'); } finally { setLoading(false); } }; // Handle drag and drop const handleDragOver = (e: React.DragEvent) => { e.preventDefault(); e.stopPropagation(); }; const handleDrop = (e: React.DragEvent) => { e.preventDefault(); e.stopPropagation(); const droppedFiles = Array.from(e.dataTransfer.files); const validFiles = droppedFiles.filter((f) => /\.(pdf|docx|doc|xlsx|xls|csv|png|jpg|jpeg|tiff|bmp)$/i.test(f.name) ); if (validFiles.length < droppedFiles.length) { setError('Some files were skipped. Unsupported format.'); } setFiles((prev) => [...prev, ...validFiles]); }; if (!isOpen) return null; return (
{/* Header */}

New RFP Project

{/* Form */}
{/* Error Message */} {error && (
error
{error.includes('\n') ? ( <>

{error.split('\n')[0]}

{error.split('\n')[1]} ) : ( {error} )}
)} {/* Source Toggle */}
{source === 'upload' ? ( /* File Upload Area */
fileInputRef.current?.click()} > upload_file

Click or drag files here to upload

{/* File Preview List */} {files.length > 0 && (
{files.map((f, i) => (
description {f.name}
))}
)}
) : ( /* Research Import ID */
setDeepResearchId(e.target.value)} className="w-full border border-slate-300 dark:border-slate-600 rounded-lg px-4 py-2.5 bg-white dark:bg-slate-800 text-slate-900 dark:text-white focus:ring-2 focus:ring-primary focus:border-primary outline-none transition-all" placeholder="Paste ID from Deep Research view" required />

This will link the project to the autonomous research logs.

)} {/* Info Banner */}
auto_awesome

AI will automatically extract the project name and requirements from your documents.

{/* Submit Button */}
); }; export default CreateRFPModal;