File size: 6,841 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
import React, { useEffect, useState } from 'react';
import { MapPin, ExternalLink, CalendarPlus, Filter } from 'lucide-react';
import toast from 'react-hot-toast';
import { getRegionalPrograms, RegionalProgram, RegionalProgramsResponse, createPipelineEntry } from '../../api/client';

interface Props {
  projectId?: string;
  compact?: boolean;
}

const RegionalProgramsPanel: React.FC<Props> = ({ projectId, compact = false }) => {
  const [programs, setPrograms] = useState<RegionalProgram[]>([]);
  const [voivodeships, setVoivodeships] = useState<string[]>([]);
  const [bipLiveCount, setBipLiveCount] = useState(0);
  const [bipParserVoivodeships, setBipParserVoivodeships] = useState<string[]>([]);
  const [selectedVoivodeship, setSelectedVoivodeship] = useState('');
  const [loading, setLoading] = useState(true);

  const load = async (voivodeship?: string) => {
    try {
      const data: RegionalProgramsResponse = await getRegionalPrograms(voivodeship || undefined);
      setPrograms(data.programs || []);
      setVoivodeships(data.voivodeships || []);
      setBipLiveCount(data.bip_live_count ?? 0);
      setBipParserVoivodeships(data.bip_parser_voivodeships || []);
    } catch {
      toast.error('Nie udało się załadować katalogu RPO');
    } finally {
      setLoading(false);
    }
  };

  useEffect(() => {
    setLoading(true);
    load(selectedVoivodeship || undefined);
  }, [selectedVoivodeship]);

  const handleAddToPipeline = async (prog: RegionalProgram) => {
    if (!projectId) {
      toast.error('Wybierz projekt, aby dodać do pipeline');
      return;
    }
    try {
      await createPipelineEntry(projectId, {
        grant_name: prog.name,
        grant_source_id: prog.id,
        operator: prog.operator,
        status: 'planned',
        deadline_alert: true,
        notes: `RPO ${prog.voivodeship} — katalog regionalny`,
      });
      toast.success(`„${prog.program}” dodano do pipeline`);
    } catch {
      toast.error('Błąd dodawania do pipeline');
    }
  };

  if (loading) {
    return <p style={{ color: 'var(--text-muted)', fontSize: '0.9rem' }}>Ładowanie programów RPO…</p>;
  }

  return (
    <div style={{ display: 'flex', flexDirection: 'column', gap: '1rem' }}>
      <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', flexWrap: 'wrap', gap: '0.75rem' }}>
        <div style={{ display: 'flex', alignItems: 'center', gap: '0.5rem', flexWrap: 'wrap' }}>
          <MapPin size={18} color="#a78bfa" />
          <span style={{ fontWeight: 700, fontSize: compact ? '0.95rem' : '1.05rem' }}>
            Katalog RPO wojewódzkich
          </span>
          <span style={{ fontSize: '0.75rem', color: 'var(--text-muted)' }}>({programs.length})</span>
          {bipLiveCount > 0 && (
            <span
              style={{
                fontSize: '0.7rem',
                background: 'rgba(34,197,94,0.12)',
                color: '#4ade80',
                padding: '2px 8px',
                borderRadius: 999,
              }}
              title="Programy z live cache BIP (credibility gate)"
            >
              BIP live: {bipLiveCount}
            </span>
          )}
        </div>
        <div style={{ display: 'flex', alignItems: 'center', gap: '0.5rem', flexWrap: 'wrap' }}>
          {bipParserVoivodeships.length > 0 && (
            <span style={{ fontSize: '0.7rem', color: 'var(--text-muted)' }}>
              Parser BIP: {bipParserVoivodeships.join(', ')}
            </span>
          )}
          <Filter size={14} color="var(--text-muted)" />
          <select
            className="form-input"
            value={selectedVoivodeship}
            onChange={(e) => setSelectedVoivodeship(e.target.value)}
            style={{ fontSize: '0.85rem', padding: '0.35rem 0.6rem', minWidth: 160 }}
          >
            <option value="">Wszystkie województwa</option>
            {voivodeships.map((v) => (
              <option key={v} value={v}>{v.charAt(0).toUpperCase() + v.slice(1)}</option>
            ))}
          </select>
        </div>
      </div>

      <div style={{ display: 'flex', flexDirection: 'column', gap: '0.6rem', maxHeight: compact ? 320 : undefined, overflowY: compact ? 'auto' : undefined }}>
        {programs.map((prog) => (
          <div
            key={prog.id}
            style={{
              background: 'rgba(255,255,255,0.025)',
              border: '1px solid rgba(255,255,255,0.06)',
              borderRadius: 10,
              padding: compact ? '0.65rem 0.75rem' : '0.85rem 1rem',
            }}
          >
            <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'flex-start', gap: '0.75rem' }}>
              <div style={{ flex: 1 }}>
                <div style={{ fontWeight: 700, fontSize: '0.9rem', marginBottom: '0.25rem' }}>{prog.name}</div>
                <div style={{ fontSize: '0.75rem', color: 'var(--text-muted)', marginBottom: '0.35rem' }}>
                  {prog.operator} • {prog.voivodeship}
                </div>
                {prog.topics && (
                  <div style={{ display: 'flex', flexWrap: 'wrap', gap: '0.35rem' }}>
                    {prog.topics.slice(0, 3).map((t) => (
                      <span
                        key={t}
                        style={{
                          fontSize: '0.65rem',
                          background: 'rgba(167,139,250,0.15)',
                          color: '#a78bfa',
                          padding: '2px 6px',
                          borderRadius: 4,
                        }}
                      >
                        {t}
                      </span>
                    ))}
                  </div>
                )}
              </div>
              <div style={{ display: 'flex', gap: '0.35rem', flexShrink: 0 }}>
                {prog.url && (
                  <a
                    href={prog.url}
                    target="_blank"
                    rel="noopener noreferrer"
                    className="btn btn-secondary"
                    style={{ padding: '0.35rem', fontSize: '0.75rem' }}
                    title="Strona operatora"
                  >
                    <ExternalLink size={14} />
                  </a>
                )}
                {projectId && (
                  <button
                    className="btn btn-secondary"
                    onClick={() => handleAddToPipeline(prog)}
                    style={{ padding: '0.35rem', fontSize: '0.75rem' }}
                    title="Dodaj do pipeline"
                  >
                    <CalendarPlus size={14} />
                  </button>
                )}
              </div>
            </div>
          </div>
        ))}
      </div>
    </div>
  );
};

export default RegionalProgramsPanel;