File size: 9,905 Bytes
3b7f713
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { useState, useEffect } from 'react';
import { X, Cookie, Shield } from 'lucide-react';

interface ConsentSettings {
  essential: true;        // niezmienne — wymagane do działania
  analytics: boolean;     // opcjonalne
  marketing: boolean;     // opcjonalne
}

const CONSENT_KEY = 'grantforge_cookie_consent';
const CONSENT_VERSION = '1.0'; // zmień przy aktualizacji polityki → ponowny baner

/**
 * Cookie Consent Banner — zgodny z RODO Art. 7 i Dyrektywą ePrivacy.
 *
 * Zachowanie:
 *  - Pojawia się na dole ekranu przy pierwszej wizycie (lub po zmianie wersji polityki)
 *  - "Akceptuj wszystkie" → zapisuje zgodę w localStorage
 *  - "Tylko niezbędne" → tylko essential=true
 *  - "Ustawienia" → rozwijane opcje szczegółowe
 *  - Nie blokuje korzystania z aplikacji
 *
 * Integracja z backendem:
 *   Po zapisaniu zgody wywołaj POST /api/user/consent z payload { settings }.
 */
export function CookieConsentBanner() {
  const [visible, setVisible] = useState(false);
  const [expanded, setExpanded] = useState(false);
  const [settings, setSettings] = useState<ConsentSettings>({
    essential: true,
    analytics: false,
    marketing: false,
  });

  useEffect(() => {
    try {
      const stored = localStorage.getItem(CONSENT_KEY);
      if (stored) {
        const parsed = JSON.parse(stored);
        // Pokazuj ponownie jeśli wersja się zmieniła
        if (parsed.version !== CONSENT_VERSION) {
          setVisible(true);
        }
      } else {
        // Pierwsze wejście
        setVisible(true);
      }
    } catch {
      setVisible(true);
    }
  }, []);

  const saveConsent = (chosen: ConsentSettings) => {
    const record = { version: CONSENT_VERSION, ...chosen, timestamp: new Date().toISOString() };
    localStorage.setItem(CONSENT_KEY, JSON.stringify(record));
    setVisible(false);

    // Opcjonalnie: wyślij do backendu
    try {
      fetch('/api/user/consent', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ consent: record }),
      }).catch(() => {}); // fire-and-forget
    } catch {}
  };

  if (!visible) return null;

  return (
    <div
      role="dialog"
      aria-modal="false"
      aria-label="Zgoda na pliki cookie"
      style={{
        position: 'fixed',
        bottom: 0,
        left: 0,
        right: 0,
        zIndex: 9999,
        padding: '1rem',
        display: 'flex',
        justifyContent: 'center',
        pointerEvents: 'none',
      }}
    >
      <div
        style={{
          background: 'rgba(15, 15, 25, 0.97)',
          backdropFilter: 'blur(16px)',
          border: '1px solid rgba(255,255,255,0.08)',
          borderRadius: '16px',
          padding: '1.5rem',
          maxWidth: '780px',
          width: '100%',
          boxShadow: '0 -4px 40px rgba(0,0,0,0.5)',
          pointerEvents: 'auto',
          animation: 'slideUp 0.3s ease-out',
        }}
      >
        {/* Header */}
        <div style={{ display: 'flex', alignItems: 'flex-start', gap: '0.75rem', marginBottom: '1rem' }}>
          <div style={{
            width: 36, height: 36, borderRadius: '8px',
            background: 'rgba(139,92,246,0.15)',
            display: 'flex', alignItems: 'center', justifyContent: 'center', flexShrink: 0,
          }}>
            <Cookie size={18} color="#a78bfa" />
          </div>
          <div style={{ flex: 1 }}>
            <h3 style={{ margin: 0, color: '#f1f5f9', fontSize: '0.95rem', fontWeight: 700 }}>
              Twoja prywatność ma znaczenie
            </h3>
            <p style={{ margin: '0.25rem 0 0', color: '#94a3b8', fontSize: '0.8rem', lineHeight: 1.5 }}>
              Używamy plików cookie, aby zapewnić właściwe działanie platformy. Możesz wybrać,
              które kategorie akceptujesz.{' '}
              <a href="/polityka-prywatnosci" style={{ color: '#a78bfa', textDecoration: 'none' }}>
                Polityka prywatności
              </a>
            </p>
          </div>
          <button
            onClick={() => saveConsent({ essential: true, analytics: false, marketing: false })}
            aria-label="Zamknij (tylko niezbędne)"
            style={{ background: 'none', border: 'none', cursor: 'pointer', color: '#64748b', padding: 4 }}
          >
            <X size={16} />
          </button>
        </div>

        {/* Szczegóły rozwijane */}
        {expanded && (
          <div style={{
            borderTop: '1px solid rgba(255,255,255,0.06)',
            paddingTop: '1rem',
            marginBottom: '1rem',
            display: 'flex',
            flexDirection: 'column',
            gap: '0.6rem',
          }}>
            {/* Essential — zawsze włączone */}
            <ConsentRow
              label="Niezbędne"
              description="Sesja, uwierzytelnianie, bezpieczeństwo CSRF. Wymagane do działania platformy."
              icon={<Shield size={13} color="#34d399" />}
              checked={true}
              disabled={true}
              onChange={() => {}}
            />
            {/* Analytics */}
            <ConsentRow
              label="Analityczne"
              description="Anonimowe statystyki użytkowania (np. LangSmith tracing — bez danych osobowych)."
              icon={<span style={{ fontSize: 13 }}>📊</span>}
              checked={settings.analytics}
              disabled={false}
              onChange={(v) => setSettings(s => ({ ...s, analytics: v }))}
            />
            {/* Marketing */}
            <ConsentRow
              label="Marketingowe"
              description="Personalizacja oferty i powiadomień o nowych funkcjach. Opcjonalne."
              icon={<span style={{ fontSize: 13 }}>📣</span>}
              checked={settings.marketing}
              disabled={false}
              onChange={(v) => setSettings(s => ({ ...s, marketing: v }))}
            />
          </div>
        )}

        {/* Przyciski */}
        <div style={{
          display: 'flex',
          gap: '0.6rem',
          flexWrap: 'wrap',
          justifyContent: 'flex-end',
        }}>
          <button
            onClick={() => setExpanded(e => !e)}
            style={{
              background: 'transparent',
              border: '1px solid rgba(255,255,255,0.1)',
              borderRadius: '8px',
              padding: '0.5rem 1rem',
              color: '#94a3b8',
              cursor: 'pointer',
              fontSize: '0.8rem',
              fontWeight: 500,
            }}
          >
            {expanded ? 'Ukryj ustawienia' : 'Ustawienia'}
          </button>
          <button
            onClick={() => saveConsent({ essential: true, analytics: false, marketing: false })}
            style={{
              background: 'rgba(255,255,255,0.05)',
              border: '1px solid rgba(255,255,255,0.1)',
              borderRadius: '8px',
              padding: '0.5rem 1rem',
              color: '#cbd5e1',
              cursor: 'pointer',
              fontSize: '0.8rem',
              fontWeight: 500,
            }}
          >
            Tylko niezbędne
          </button>
          <button
            onClick={() => saveConsent({ essential: true, analytics: true, marketing: true })}
            style={{
              background: 'linear-gradient(135deg, #7c3aed, #2563eb)',
              border: 'none',
              borderRadius: '8px',
              padding: '0.5rem 1.25rem',
              color: '#fff',
              cursor: 'pointer',
              fontSize: '0.8rem',
              fontWeight: 600,
            }}
          >
            Akceptuj wszystkie
          </button>
        </div>
      </div>

      <style>{`
        @keyframes slideUp {
          from { transform: translateY(20px); opacity: 0; }
          to   { transform: translateY(0);    opacity: 1; }
        }
      `}</style>
    </div>
  );
}

// ── Pomocniczy wiersz ustawienia ─────────────────────────────────────────────
function ConsentRow({
  label, description, icon, checked, disabled, onChange
}: {
  label: string;
  description: string;
  icon: React.ReactNode;
  checked: boolean;
  disabled: boolean;
  onChange: (v: boolean) => void;
}) {
  return (
    <div style={{
      display: 'flex',
      alignItems: 'flex-start',
      gap: '0.75rem',
      padding: '0.6rem 0.75rem',
      borderRadius: '8px',
      background: 'rgba(255,255,255,0.03)',
      border: '1px solid rgba(255,255,255,0.05)',
    }}>
      <div style={{ marginTop: 2 }}>{icon}</div>
      <div style={{ flex: 1 }}>
        <div style={{ color: '#e2e8f0', fontSize: '0.82rem', fontWeight: 600 }}>{label}</div>
        <div style={{ color: '#64748b', fontSize: '0.75rem', marginTop: 2 }}>{description}</div>
      </div>
      <label style={{ position: 'relative', display: 'inline-flex', alignItems: 'center', cursor: disabled ? 'not-allowed' : 'pointer' }}>
        <input
          type="checkbox"
          checked={checked}
          disabled={disabled}
          onChange={e => onChange(e.target.checked)}
          style={{ opacity: 0, width: 0, height: 0 }}
        />
        <span style={{
          display: 'inline-block',
          width: 36,
          height: 20,
          borderRadius: 10,
          background: checked ? '#7c3aed' : 'rgba(255,255,255,0.1)',
          position: 'relative',
          transition: 'background 0.2s',
          opacity: disabled ? 0.5 : 1,
        }}>
          <span style={{
            position: 'absolute',
            top: 2,
            left: checked ? 18 : 2,
            width: 16,
            height: 16,
            borderRadius: '50%',
            background: '#fff',
            transition: 'left 0.2s',
          }} />
        </span>
      </label>
    </div>
  );
}

export default CookieConsentBanner;