File size: 15,231 Bytes
85981e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
306
307
308
309
310
311
312
313
314
315
316
317
318
/**
 * DatasetTrainingConfigPanel.tsx
 *
 * Inline collapsible panel rendered inside LiveOperationsView that lets the
 * user:
 *   1. Choose one of 4 benchmark datasets (PaySim, IEEE-CIS, Elliptic, Credit Card)
 *   2. Toggle between "Mock Simulation" and "Real Backend" training modes
 *
 * On "Launch Training" the parent receives (profile, mode) and starts the
 * appropriate training path.  This component carries NO training state β€” it is
 * purely presentational configuration UI.
 */

import { useState } from 'react';
import { motion, AnimatePresence } from 'framer-motion';
import { ExternalLink, Zap, FlaskConical, ChevronRight, CheckCircle2 } from 'lucide-react';
import { DATASET_PROFILES, type DatasetProfile } from '../utils/datasetProfiles';
import { useModalA11y } from '../hooks/useModalA11y';

export type TrainingMode = 'mock' | 'real';

interface DatasetTrainingConfigPanelProps {
  isOpen: boolean;
  onClose: () => void;
  onLaunch: (profile: DatasetProfile, mode: TrainingMode) => void;
  initialDataset?: DatasetProfile['id'];
  initialMode?: TrainingMode;
}

// ── small helper: format numbers with K/M suffix ──────────────────────────
function fmtCount(n: number): string {
  if (n >= 1_000_000) return `${(n / 1_000_000).toFixed(2)}M`;
  if (n >= 1_000) return `${(n / 1_000).toFixed(0)}K`;
  return n.toString();
}

function fmtFraud(r: number): string {
  if (r < 0.001) return `${(r * 100).toFixed(3)}%`;
  return `${(r * 100).toFixed(2)}%`;
}

const DATASET_ORDER: DatasetProfile['id'][] = ['paysim', 'ieee_cis', 'elliptic', 'creditcard'];

export default function DatasetTrainingConfigPanel({
  isOpen,
  onClose,
  onLaunch,
  initialDataset = 'paysim',
  initialMode = 'real',
}: DatasetTrainingConfigPanelProps) {
  const [selectedId, setSelectedId] = useState<DatasetProfile['id']>(initialDataset);
  const [mode, setMode] = useState<TrainingMode>(initialMode);

  const { containerRef } = useModalA11y<HTMLDivElement>({
    isOpen,
    onClose,
    closeOnEscape: true,
    trapFocus: false, // Inline expandable panel
    restoreFocus: true,
  });

  const selectedProfile = DATASET_PROFILES[selectedId];

  return (
    <AnimatePresence>
      {isOpen && (
        <motion.div
          key="config-panel"
          ref={containerRef}
          role="region"
          aria-labelledby="training-config-title"
          aria-describedby="training-config-desc"
          tabIndex={-1}
          initial={{ opacity: 0, height: 0 }}
          animate={{ opacity: 1, height: 'auto' }}
          exit={{ opacity: 0, height: 0 }}
          transition={{ duration: 0.3, ease: 'easeInOut' }}
          className="overflow-hidden focus:outline-none"
        >
          <div className="glass-card p-4 sm:p-6 border border-[var(--color-border)] rounded-2xl space-y-6">
            {/* ── Header ──────────────────────────────────────────────────── */}
            <div className="flex items-center justify-between">
              <div className="space-y-0.5">
                <h3 id="training-config-title" className="text-base sm:text-lg font-bold text-[var(--color-text-primary)]">
                  Configure Training Session
                </h3>
                <p id="training-config-desc" className="text-xs text-[var(--color-text-muted)]">
                  Select a benchmark dataset and training mode before launching
                </p>
              </div>
              <button
                onClick={onClose}
                className="p-1.5 rounded-lg hover:bg-white/10 text-[var(--color-text-muted)] hover:text-[var(--color-text-primary)] transition-colors shrink-0 focus:outline-none focus:ring-2 focus:ring-indigo-400"
                aria-label="Close config panel"
              >
                βœ•
              </button>
            </div>

            {/* ── Mode Toggle ─────────────────────────────────────────────── */}
            <div className="space-y-2">
              <p className="text-xs font-semibold uppercase tracking-wider text-[var(--color-text-muted)]">
                Training Mode
              </p>
              <div className="grid grid-cols-2 gap-3">
                {/* Real Backend (Default) */}
                <button
                  onClick={() => setMode('real')}
                  className={`relative flex flex-col items-start gap-1.5 p-3.5 rounded-xl border-2 transition-all text-left ${
                    mode === 'real'
                      ? 'border-amber-500 bg-amber-500/10'
                      : 'border-[var(--color-border)] hover:border-[var(--color-border-hover)] bg-transparent'
                  }`}
                >
                  <div className="flex items-center gap-2">
                    <Zap
                      size={16}
                      className={mode === 'real' ? 'text-amber-400' : 'text-[var(--color-text-muted)]'}
                    />
                    <span
                      className={`text-sm font-bold ${
                        mode === 'real' ? 'text-amber-400' : 'text-[var(--color-text-secondary)]'
                      }`}
                    >
                      Live Backend Engine
                    </span>
                    {mode === 'real' && (
                      <CheckCircle2 size={14} className="text-amber-400 ml-auto shrink-0" />
                    )}
                  </div>
                  <p className="text-[11px] leading-relaxed text-[var(--color-text-muted)]">
                    Dispatches live PyTorch federated learning orchestration to coordinator API with real gradient aggregation
                  </p>
                </button>

                {/* Offline Sandbox Demo */}
                <button
                  onClick={() => setMode('mock')}
                  className={`relative flex flex-col items-start gap-1.5 p-3.5 rounded-xl border-2 transition-all text-left ${
                    mode === 'mock'
                      ? 'border-[var(--color-accent-indigo)] bg-[var(--color-accent-indigo)]/10'
                      : 'border-[var(--color-border)] hover:border-[var(--color-border-hover)] bg-transparent'
                  }`}
                >
                  <div className="flex items-center gap-2">
                    <FlaskConical
                      size={16}
                      className={mode === 'mock' ? 'text-[var(--color-accent-indigo)]' : 'text-[var(--color-text-muted)]'}
                    />
                    <span
                      className={`text-sm font-bold ${
                        mode === 'mock' ? 'text-[var(--color-accent-indigo)]' : 'text-[var(--color-text-secondary)]'
                      }`}
                    >
                      Offline Sandbox Demo
                    </span>
                    {mode === 'mock' && (
                      <CheckCircle2 size={14} className="text-[var(--color-accent-indigo)] ml-auto shrink-0" />
                    )}
                  </div>
                  <p className="text-[11px] leading-relaxed text-[var(--color-text-muted)]">
                    Simulated client-side telemetry walk for offline testing and visualization demonstrations
                  </p>
                </button>
              </div>
            </div>

            {/* ── Dataset Cards ────────────────────────────────────────────── */}
            <div className="space-y-2">
              <p className="text-xs font-semibold uppercase tracking-wider text-[var(--color-text-muted)]">
                Benchmark Dataset
              </p>
              <div className="grid grid-cols-1 sm:grid-cols-2 gap-3">
                {DATASET_ORDER.map((dsId) => {
                  const p = DATASET_PROFILES[dsId];
                  const isSelected = selectedId === dsId;
                  return (
                    <button
                      key={dsId}
                      onClick={() => setSelectedId(dsId)}
                      className={`flex flex-col gap-2 p-3.5 rounded-xl border-2 transition-all text-left ${
                        isSelected
                          ? 'bg-slate-800/60'
                          : 'border-[var(--color-border)] hover:border-[var(--color-border-hover)] bg-transparent'
                      }`}
                      style={isSelected ? { borderColor: p.color, backgroundColor: `${p.color}14` } : undefined}
                    >
                      {/* Dataset title row */}
                      <div className="flex items-start justify-between gap-2">
                        <div className="flex items-center gap-2 min-w-0">
                          <span className="text-lg shrink-0">{p.icon}</span>
                          <div className="min-w-0">
                            <p
                              className="text-sm font-bold truncate"
                              style={isSelected ? { color: p.color } : { color: 'var(--color-text-primary)' }}
                            >
                              {p.label}
                            </p>
                            <p className="text-[10px] text-[var(--color-text-muted)] truncate leading-snug">
                              {p.badge}
                            </p>
                          </div>
                        </div>
                        {isSelected && (
                          <CheckCircle2
                            size={16}
                            className="shrink-0 mt-0.5"
                            style={{ color: p.color }}
                          />
                        )}
                      </div>

                      {/* Stats row */}
                      <div className="grid grid-cols-3 gap-1">
                        <div className="bg-black/20 rounded-lg p-1.5 text-center">
                          <p className="text-[9px] text-[var(--color-text-muted)] uppercase tracking-wide leading-none mb-0.5">
                            Samples
                          </p>
                          <p className="text-xs font-bold font-mono text-[var(--color-text-secondary)]">
                            {fmtCount(p.totalSamples)}
                          </p>
                        </div>
                        <div className="bg-black/20 rounded-lg p-1.5 text-center">
                          <p className="text-[9px] text-[var(--color-text-muted)] uppercase tracking-wide leading-none mb-0.5">
                            Fraud%
                          </p>
                          <p className="text-xs font-bold font-mono text-rose-400">
                            {fmtFraud(p.fraudRatio)}
                          </p>
                        </div>
                        <div className="bg-black/20 rounded-lg p-1.5 text-center">
                          <p className="text-[9px] text-[var(--color-text-muted)] uppercase tracking-wide leading-none mb-0.5">
                            Features
                          </p>
                          <p className="text-xs font-bold font-mono text-[var(--color-text-secondary)]">
                            {p.numFeatures}
                          </p>
                        </div>
                      </div>

                      {/* Fraud pattern */}
                      <p className="text-[10px] text-[var(--color-text-muted)] leading-snug line-clamp-2">
                        {p.fraudPattern}
                      </p>
                    </button>
                  );
                })}
              </div>
            </div>

            {/* ── Selected dataset detail strip ────────────────────────────── */}
            <div
              className="flex flex-col sm:flex-row sm:items-center justify-between gap-3 p-3.5 rounded-xl border"
              style={{
                borderColor: `${selectedProfile.color}40`,
                backgroundColor: `${selectedProfile.color}0A`,
              }}
            >
              <div className="flex items-center gap-2.5 min-w-0">
                <span className="text-2xl shrink-0">{selectedProfile.icon}</span>
                <div className="min-w-0">
                  <p className="text-sm font-semibold text-[var(--color-text-primary)] truncate">
                    {selectedProfile.label}
                  </p>
                  <p className="text-xs text-[var(--color-text-muted)] truncate">
                    {selectedProfile.subtitle}
                  </p>
                </div>
              </div>
              <div className="flex items-center gap-2 shrink-0">
                {mode === 'mock' && (
                  <span className="text-[10px] text-[var(--color-text-muted)] whitespace-nowrap">
                    AUC {selectedProfile.initialAuc.toFixed(3)} β†’ {selectedProfile.targetAuc.toFixed(3)}
                  </span>
                )}
                <a
                  href={selectedProfile.sourceLink}
                  target="_blank"
                  rel="noopener noreferrer"
                  onClick={(e) => e.stopPropagation()}
                  className="flex items-center gap-1 text-[10px] text-[var(--color-text-muted)] hover:text-[var(--color-text-secondary)] transition-colors underline-offset-2 hover:underline"
                >
                  Source <ExternalLink size={10} />
                </a>
              </div>
            </div>

            {/* ── Launch button ────────────────────────────────────────────── */}
            <div className="flex items-center justify-between gap-3 pt-1">
              <button
                onClick={onClose}
                className="px-4 py-2 rounded-xl text-sm font-medium text-[var(--color-text-muted)] hover:text-[var(--color-text-secondary)] transition-colors border border-[var(--color-border)] hover:border-[var(--color-border-hover)]"
              >
                Cancel
              </button>
              <motion.button
                whileHover={{ scale: 1.02 }}
                whileTap={{ scale: 0.97 }}
                onClick={() => onLaunch(selectedProfile, mode)}
                className="flex items-center gap-2 px-6 py-2.5 rounded-xl font-bold text-sm text-white shadow-lg transition-all"
                style={{
                  background: `linear-gradient(135deg, ${selectedProfile.color}, #6366f1)`,
                  boxShadow: `0 4px 20px ${selectedProfile.color}40`,
                }}
              >
                {mode === 'mock' ? <FlaskConical size={15} /> : <Zap size={15} />}
                Launch{' '}
                {mode === 'mock' ? 'Simulation' : 'Real Training'}
                <ChevronRight size={15} />
              </motion.button>
            </div>
          </div>
        </motion.div>
      )}
    </AnimatePresence>
  );
}