Spaces:
Running
Running
File size: 17,933 Bytes
ce8f04a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 | import React, { useState, useEffect } from 'react';
import { X, Loader2, Search, CheckCircle, ArrowRight, MessageSquare, Briefcase, AlertTriangle } from 'lucide-react';
import { createPortal } from 'react-dom';
import { matchGrantsForProject, UserAnswer } from '../../api/client';
const DEFAULT_CLARIFYING_QUESTIONS = [
'Opisz cel projektu i planowane wydatki (inwestycja, B+R, zatrudnienie).',
'Podaj przybli偶ony przych贸d roczny i liczb臋 pracownik贸w (FTE).',
'Czy projekt dotyczy g艂贸wnej dzia艂alno艣ci firmy z rejestru PKD?',
];
interface GrantMatchResult {
program_id: string;
program_name: string;
score: number;
rationale: string;
is_recommended: boolean;
requires_verification?: boolean;
source?: string;
legal_basis?: string;
confidence_score?: number;
}
interface AdvancedMatcherModalProps {
projectId: string;
onClose: () => void;
onMatchesSaved: (matches: GrantMatchResult[]) => void;
}
export default function AdvancedMatcherModal({ projectId, onClose, onMatchesSaved }: AdvancedMatcherModalProps) {
const [step, setStep] = useState<'loading' | 'questions' | 'results'>('loading');
const [questions, setQuestions] = useState<string[]>([]);
const [answers, setAnswers] = useState<Record<string, string>>({});
const [matches, setMatches] = useState<GrantMatchResult[]>([]);
const [error, setError] = useState<string | null>(null);
const [isSubmitting, setIsSubmitting] = useState(false);
const [userAnswersHistory, setUserAnswersHistory] = useState<UserAnswer[]>([]);
const initAnswers = (questionList: string[]) => {
const initialAnswers: Record<string, string> = {};
questionList.forEach((q) => {
initialAnswers[q] = answers[q] || '';
});
setAnswers(initialAnswers);
};
const applyQuestions = (questionList: string[]) => {
const next = questionList.length > 0 ? questionList : DEFAULT_CLARIFYING_QUESTIONS;
setQuestions(next);
initAnswers(next);
setStep('questions');
};
useEffect(() => {
runMatcher([]);
}, [projectId]);
const runMatcher = async (currentAnswers: UserAnswer[]) => {
setStep('loading');
setError(null);
try {
const data = await matchGrantsForProject(projectId, currentAnswers);
if (data.status === 'error') {
setError(data.detail || 'Wyst膮pi艂 b艂膮d podczas analizy dopasowa艅.');
applyQuestions(data.clarifying_questions || []);
return;
}
if (data.needs_more_info && data.clarifying_questions && data.clarifying_questions.length > 0) {
applyQuestions(data.clarifying_questions);
} else {
setMatches(data.matches || []);
onMatchesSaved(data.matches || []);
setStep('results');
}
} catch (err) {
console.error('Error in matchGrantsForProject:', err);
setError('B艂膮d po艂膮czenia z serwerem AI.');
applyQuestions(DEFAULT_CLARIFYING_QUESTIONS);
}
};
const handleAnswerSubmit = async () => {
setIsSubmitting(true);
// Zbierz nowe odpowiedzi
const newAnswers: UserAnswer[] = questions.map(q => ({
question: q,
answer: answers[q] || 'Brak odpowiedzi'
}));
const combinedAnswers = [...userAnswersHistory, ...newAnswers];
setUserAnswersHistory(combinedAnswers);
await runMatcher(combinedAnswers);
setIsSubmitting(false);
};
const modalContent = (
<div style={{
position: 'fixed', top: 0, left: 0, right: 0, bottom: 0,
backgroundColor: 'rgba(0, 0, 0, 0.75)', backdropFilter: 'blur(5px)',
display: 'flex', alignItems: 'center', justifyContent: 'center',
zIndex: 99999,
padding: '2rem 1rem', // Add vertical padding to prevent cutting off
boxSizing: 'border-box',
overflowY: 'auto'
}}>
<div className="glass-card" style={{
width: '100%', maxWidth: '600px', maxHeight: '90vh',
display: 'flex', flexDirection: 'column',
overflow: 'hidden', padding: 0, margin: 'auto',
boxSizing: 'border-box',
transform: 'none', // Override any hover transforms from glass-card
animation: 'none'
}}>
{/* Header */}
<div style={{
padding: '1.5rem', borderBottom: '1px solid rgba(255,255,255,0.05)',
display: 'flex', justifyContent: 'space-between', alignItems: 'center',
background: 'rgba(255,255,255,0.02)'
}}>
<div style={{ display: 'flex', alignItems: 'center', gap: '0.75rem' }}>
<div style={{
width: '36px', height: '36px', borderRadius: '8px',
background: 'rgba(139, 92, 246, 0.1)', color: 'var(--accent-purple)',
display: 'flex', alignItems: 'center', justifyContent: 'center'
}}>
<Search size={20} />
</div>
<div>
<h2 style={{ fontSize: '1.2rem', margin: 0, color: 'var(--text-primary)' }}>Advanced AI Matcher</h2>
<p style={{ margin: 0, fontSize: '0.85rem', color: 'var(--text-muted)' }}>Wieloetapowe dopasowanie program贸w</p>
</div>
</div>
<button onClick={onClose} style={{
background: 'none', border: 'none', color: 'var(--text-muted)', cursor: 'pointer',
padding: '0.5rem'
}} className="hover-lift">
<X size={20} />
</button>
</div>
{/* Content */}
<div style={{ padding: '2rem', overflowY: 'auto', flex: 1 }}>
{step === 'loading' && (
<div style={{ display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'center', padding: '3rem 0' }}>
<Loader2 size={48} color="var(--accent-purple)" style={{ animation: 'spin 2s linear infinite', marginBottom: '1rem' }} />
<h3 style={{ color: 'var(--text-primary)', marginBottom: '0.5rem' }}>Analizuj臋 opis projektu...</h3>
<p style={{ color: 'var(--text-muted)', textAlign: 'center', maxWidth: '400px' }}>
AI skanuje aktualne nabory w PARP i NCBR, aby znale藕膰 najlepsze dopasowania lub przygotowa膰 pytania doprecyzowuj膮ce.
</p>
</div>
)}
{step === 'questions' && (
<div style={{ display: 'flex', flexDirection: 'column', gap: '1.5rem' }}>
{error && (
<div style={{ padding: '1rem', background: 'rgba(239, 68, 68, 0.1)', borderLeft: '3px solid var(--accent-red)', borderRadius: '4px', color: '#FECACA', fontSize: '0.9rem' }}>
{error}
</div>
)}
<div style={{ display: 'flex', alignItems: 'flex-start', gap: '1rem', background: 'rgba(59, 130, 246, 0.1)', padding: '1.5rem', borderRadius: '12px', border: '1px solid rgba(59, 130, 246, 0.2)' }}>
<MessageSquare size={24} color="var(--accent-blue)" style={{ flexShrink: 0 }} />
<div>
<h4 style={{ margin: '0 0 0.5rem 0', color: 'var(--accent-blue)' }}>AI potrzebuje wi臋cej informacji</h4>
<p style={{ margin: 0, fontSize: '0.9rem', color: 'var(--text-secondary)', lineHeight: 1.5 }}>
Aby dok艂adnie dobra膰 programy dotacyjne, prosz臋 odpowiedz na poni偶sze pytania. Twoje odpowiedzi zostan膮 zapisane i wykorzystane podczas tworzenia wniosku.
</p>
</div>
</div>
<div style={{ display: 'flex', flexDirection: 'column', gap: '1.5rem' }}>
{(questions.length > 0 ? questions : DEFAULT_CLARIFYING_QUESTIONS).map((q, idx) => (
<div key={idx} style={{ display: 'flex', flexDirection: 'column', gap: '0.5rem' }}>
<label style={{ fontSize: '0.95rem', color: 'var(--text-primary)', fontWeight: 500 }}>
{idx + 1}. {q}
</label>
<textarea
value={answers[q] || ''}
onChange={(e) => setAnswers(prev => ({ ...prev, [q]: e.target.value }))}
placeholder="Twoja odpowied藕..."
disabled={isSubmitting}
style={{
background: 'rgba(0,0,0,0.2)', border: '1px solid rgba(255,255,255,0.1)',
borderRadius: '8px', padding: '0.75rem', color: '#fff',
minHeight: '80px', resize: 'vertical', fontSize: '0.9rem', fontFamily: 'inherit',
pointerEvents: isSubmitting ? 'none' : 'auto',
}}
/>
</div>
))}
</div>
<div style={{ display: 'flex', justifyContent: 'flex-end', marginTop: '1rem' }}>
<button
onClick={handleAnswerSubmit}
disabled={isSubmitting}
className="btn btn-primary"
style={{ display: 'flex', alignItems: 'center', gap: '0.5rem' }}
>
{isSubmitting ? <Loader2 size={16} className="spin" /> : <ArrowRight size={16} />}
Prze艣lij odpowiedzi
</button>
</div>
</div>
)}
{step === 'results' && (
<div style={{ display: 'flex', flexDirection: 'column', gap: '1.5rem' }}>
<div style={{ display: 'flex', alignItems: 'flex-start', gap: '1rem', background: 'rgba(16, 185, 129, 0.1)', padding: '1.5rem', borderRadius: '12px', border: '1px solid rgba(16, 185, 129, 0.2)' }}>
<CheckCircle size={24} color="var(--accent-green)" style={{ flexShrink: 0 }} />
<div>
<h4 style={{ margin: '0 0 0.5rem 0', color: 'var(--accent-green)' }}>Analiza zako艅czona sukcesem</h4>
<p style={{ margin: 0, fontSize: '0.9rem', color: 'var(--text-secondary)', lineHeight: 1.5 }}>
Znaleziono {matches.length} potencjalnych program贸w. Rekomendowane opcje zosta艂y oznaczone najwy偶szym wynikiem.
</p>
</div>
</div>
{matches.length === 0 ? (
<div style={{ padding: '2rem', textAlign: 'center', color: 'var(--text-muted)' }}>
Brak pasuj膮cych program贸w dla podanych kryteri贸w.
</div>
) : (
<div style={{ display: 'flex', flexDirection: 'column', gap: '1rem' }}>
{[...matches].sort((a,b) => b.score - a.score).map((m, idx) => (
<div key={idx} style={{
background: 'rgba(255,255,255,0.03)',
border: `1px solid ${m.is_recommended ? 'rgba(16, 185, 129, 0.3)' : 'rgba(255,255,255,0.1)'}`,
borderRadius: '10px', padding: '1.25rem',
display: 'flex', flexDirection: 'column', gap: '0.75rem'
}}>
<div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'flex-start' }}>
<h4 style={{ margin: 0, fontSize: '1.05rem', color: 'var(--text-primary)', display: 'flex', alignItems: 'center', gap: '0.5rem' }}>
<Briefcase size={16} color="var(--text-muted)" />
{m.program_name}
</h4>
<div style={{ display: 'flex', gap: '0.5rem', flexWrap: 'wrap' }}>
{m.requires_verification && (
<div style={{
background: 'rgba(245, 158, 11, 0.1)',
color: '#F59E0B',
padding: '0.25rem 0.75rem', borderRadius: '12px', fontSize: '0.85rem', fontWeight: 600,
display: 'flex', alignItems: 'center', gap: '0.25rem'
}}>
<AlertTriangle size={14} /> Wymaga weryfikacji
</div>
)}
<div style={{
background: m.score >= 70 ? 'rgba(16, 185, 129, 0.2)' : 'rgba(255,255,255,0.1)',
color: m.score >= 70 ? 'var(--accent-green)' : 'var(--text-secondary)',
padding: '0.25rem 0.75rem', borderRadius: '12px', fontSize: '0.85rem', fontWeight: 700
}}>
{m.score}% Match
</div>
</div>
</div>
<p style={{ margin: 0, fontSize: '0.9rem', color: 'var(--text-secondary)', lineHeight: 1.5 }}>
{m.rationale}
</p>
<div style={{ display: 'flex', gap: '1rem', marginTop: '0.5rem', fontSize: '0.8rem', color: 'var(--text-muted)' }}>
{m.legal_basis && (
<div style={{ display: 'flex', alignItems: 'center', gap: '0.25rem' }}>
<strong>Podstawa prawna:</strong> {m.legal_basis}
</div>
)}
{m.source && (
<div style={{ display: 'flex', alignItems: 'center', gap: '0.25rem' }}>
<strong>殴r贸d艂o:</strong> {m.source}
</div>
)}
{m.confidence_score !== undefined && (
<div style={{ display: 'flex', alignItems: 'center', gap: '0.25rem' }}>
<strong>Pewno艣膰 AI:</strong> {m.confidence_score}%
</div>
)}
</div>
</div>
))}
</div>
)}
<div style={{ display: 'flex', justifyContent: 'flex-end', marginTop: '1rem' }}>
<button onClick={onClose} className="btn btn-secondary">
Zamknij
</button>
</div>
</div>
)}
</div>
</div>
<style>{`
@keyframes spin {
from { transform: rotate(0deg); }
to { transform: rotate(360deg); }
}
.spin { animation: spin 1s linear infinite; }
`}</style>
</div>
);
return createPortal(modalContent, document.body);
}
|