import React, { useState } from 'react';
import { Briefcase, Zap, AlertCircle } from 'lucide-react';
import AdvancedMatcherModal from './AdvancedMatcherModal';
import { updateProject } from '../../api/client';
import toast from 'react-hot-toast';
interface GrantMatchResult {
program_id: string;
program_name: string;
score: number;
rationale: string;
is_recommended: boolean;
}
interface Props {
projectId: string;
projectContext?: any;
onUpdateContext?: (matches: GrantMatchResult[]) => void;
}
export default function MatchingGrantsWidget({ projectId, projectContext, onUpdateContext }: Props) {
const [isModalOpen, setIsModalOpen] = useState(false);
// Pobierz zapisane wyniki z kontekstu
const savedMatches: GrantMatchResult[] = projectContext?.ai_matches || [];
// Posortuj od najwyższego wyniku i weź 3 najlepsze
const displayMatches = [...savedMatches].sort((a, b) => b.score - a.score).slice(0, 3);
const handleMatchesSaved = async (matches: GrantMatchResult[]) => {
try {
const newContext = { ...projectContext, ai_matches: matches };
await updateProject(projectId, { external_context: newContext });
if (onUpdateContext) {
onUpdateContext(matches);
}
setIsModalOpen(false);
toast.success("Zapisano dopasowane programy");
} catch (err) {
console.error("Failed to save matches", err);
toast.error("Wystąpił błąd podczas zapisywania");
}
};
return (
{displayMatches.length > 0 ? (
{displayMatches.map((m, idx) => (
{m.program_name}
= 70 ? 'rgba(16, 185, 129, 0.1)' : 'rgba(255,255,255,0.05)',
color: m.score >= 70 ? 'var(--accent-green)' : 'var(--text-secondary)',
padding: '2px 6px', borderRadius: '4px', fontSize: '0.7rem', fontWeight: 700, flexShrink: 0
}}>
{m.score}% Match
{m.rationale}
))}
) : (
Brak zapisanych rekomendacji. Uruchom AI Matcher, aby znaleźć najlepsze programy.
)}
{isModalOpen && (
setIsModalOpen(false)}
onMatchesSaved={handleMatchesSaved}
/>
)}
);
}