File size: 9,875 Bytes
149698e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// packages/web/src/components/scan/ScanControls.tsx
//
// THIS IS THE KEY COMPONENT that makes "Scanner maintenant" actually work.
// It connects the date range selection + scan button to the backend API.
//
// Usage in your DashboardPage or ScanPage:
//   import ScanControls from '@/components/scan/ScanControls';
//   <ScanControls onScanComplete={(result) => refetchTransactions()} />

import { useState, useCallback } from 'react';
import { useEmailScan, type ScanPreset } from '../../hooks/useEmailScan';

// Import your UI components (adjust paths to your project)
// import { Button } from '@/components/ui/button';
// import { ScanLine, Loader2 } from 'lucide-react';

interface ScanControlsProps {
  onScanComplete?: (result: { found: number; parsed: number; skipped: number; errors: number }) => void;
  onNewTransaction?: (transaction: any) => void;
}

export default function ScanControls({ onScanComplete, onNewTransaction }: ScanControlsProps) {
  // ═══ STATE ═══
  const [selectedPreset, setSelectedPreset] = useState<ScanPreset>('last7days');
  const [customStartDate, setCustomStartDate] = useState('');
  const [customEndDate, setCustomEndDate] = useState('');
  const [forceRescan, setForceRescan] = useState(false);

  // ═══ SCAN HOOK β€” connects to API + WebSocket ═══
  const {
    isScanning,
    progress,
    result,
    error,
    startScan,
    progressPercent,
  } = useEmailScan();

  // Notify parent when scan completes
  if (result && onScanComplete) {
    onScanComplete(result);
  }

  // ═══ HANDLERS ═══
  const handleScanClick = useCallback(() => {
    startScan({
      preset: selectedPreset,
      startDate: selectedPreset === 'custom' ? customStartDate : undefined,
      endDate: selectedPreset === 'custom' ? customEndDate : undefined,
      forceRescan,
    });
  }, [selectedPreset, customStartDate, customEndDate, forceRescan, startScan]);

  const presetButtons: { value: ScanPreset; label: string }[] = [
    { value: 'today', label: "Aujourd'hui" },
    { value: 'last7days', label: '7 derniers jours' },
    { value: 'custom', label: 'PΓ©riode personnalisΓ©e' },
  ];

  // ═══ DATE DISPLAY ═══
  const getDateRangeDisplay = () => {
    const now = new Date();
    const fmt = (d: Date) => d.toLocaleDateString('fr-CA', { day: 'numeric', month: 'short' });

    switch (selectedPreset) {
      case 'today':
        return fmt(now);
      case 'last7days': {
        const start = new Date(now);
        start.setDate(start.getDate() - 7);
        return `${fmt(start)} β†’ ${fmt(now)}`;
      }
      case 'custom':
        if (customStartDate && customEndDate) {
          return `${fmt(new Date(customStartDate))} β†’ ${fmt(new Date(customEndDate))}`;
        }
        return 'SΓ©lectionnez les dates';
    }
  };

  // ═══ RENDER ═══
  return (
    <div className="flex flex-col gap-4">
      {/* PRESET BUTTONS + SCAN BUTTON ROW */}
      <div className="flex items-center gap-4 flex-wrap">
        {/* Date preset toggle group */}
        <div className="flex items-center gap-2 bg-card p-1 rounded-lg border border-border shadow-sm">
          {presetButtons.map((btn) => (
            <button
              key={btn.value}
              onClick={() => setSelectedPreset(btn.value)}
              className={`px-3 py-1.5 text-xs font-medium rounded-md transition-colors ${
                selectedPreset === btn.value
                  ? 'bg-slate-100 text-slate-900 hover:bg-slate-200'
                  : 'text-slate-500 hover:bg-slate-50 hover:text-slate-900'
              }`}
            >
              {btn.label}
            </button>
          ))}
        </div>

        {/* Date range display */}
        <div className="hidden sm:flex items-center gap-2 bg-card px-3 py-1.5 rounded-lg border border-border shadow-sm text-sm text-slate-600">
          <span className="font-medium text-slate-900">{getDateRangeDisplay()}</span>
        </div>

        {/* Separator */}
        <div className="h-8 w-px bg-slate-200 mx-2" />

        {/* ════════════════════════════════════════════════ */}
        {/* πŸ”₯ THE SCAN BUTTON β€” this is what triggers it! */}
        {/* ════════════════════════════════════════════════ */}
        <button
          onClick={handleScanClick}
          disabled={isScanning || (selectedPreset === 'custom' && (!customStartDate || !customEndDate))}
          className="inline-flex items-center justify-center whitespace-nowrap rounded-md text-sm font-medium
            bg-primary text-primary-foreground hover:bg-primary/90
            h-9 px-4 py-2 gap-2
            shadow-md shadow-blue-500/20 hover:shadow-blue-500/30
            active:scale-95 transition-all
            disabled:pointer-events-none disabled:opacity-50"
        >
          {isScanning ? (
            <>
              {/* Spinning loader */}
              <svg className="animate-spin h-[18px] w-[18px]" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24">
                <circle className="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" strokeWidth="4" />
                <path className="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4z" />
              </svg>
              Scan en cours...
            </>
          ) : (
            <>
              {/* Scan icon */}
              <svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none"
                stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
                <path d="M3 7V5a2 2 0 0 1 2-2h2" />
                <path d="M17 3h2a2 2 0 0 1 2 2v2" />
                <path d="M21 17v2a2 2 0 0 1-2 2h-2" />
                <path d="M7 21H5a2 2 0 0 1-2-2v-2" />
                <path d="M7 12h10" />
              </svg>
              Scanner maintenant
            </>
          )}
        </button>
      </div>

      {/* CUSTOM DATE PICKERS (only when custom is selected) */}
      {selectedPreset === 'custom' && (
        <div className="flex items-center gap-3">
          <div>
            <label className="text-xs text-slate-500 block mb-1">Date de dΓ©but</label>
            <input
              type="date"
              value={customStartDate}
              onChange={(e) => setCustomStartDate(e.target.value)}
              className="px-3 py-1.5 text-sm border border-border rounded-md bg-card shadow-sm"
            />
          </div>
          <div>
            <label className="text-xs text-slate-500 block mb-1">Date de fin</label>
            <input
              type="date"
              value={customEndDate}
              onChange={(e) => setCustomEndDate(e.target.value)}
              max={new Date().toISOString().split('T')[0]}
              className="px-3 py-1.5 text-sm border border-border rounded-md bg-card shadow-sm"
            />
          </div>
        </div>
      )}

      {/* FORCE RESCAN CHECKBOX */}
      <label className="flex items-center gap-2 text-sm text-slate-600 cursor-pointer">
        <input
          type="checkbox"
          checked={forceRescan}
          onChange={(e) => setForceRescan(e.target.checked)}
          className="rounded border-slate-300"
        />
        Forcer le re-scan (re-traiter les courriels dΓ©jΓ  importΓ©s)
      </label>

      {/* ═══ PROGRESS BAR (during scan) ═══ */}
      {isScanning && progress && (
        <div className="bg-card rounded-lg border border-border p-4 shadow-sm">
          {/* Progress bar */}
          <div className="w-full bg-slate-100 rounded-full h-3 mb-2">
            <div
              className="bg-blue-500 h-3 rounded-full transition-all duration-300 ease-out"
              style={{ width: `${progressPercent}%` }}
            />
          </div>
          <div className="flex justify-between text-sm text-slate-600">
            <span>
              {progress.processed}/{progress.total} courriels traitΓ©s
            </span>
            <span>{progressPercent}%</span>
          </div>
          {progress.latest && (
            <div className="mt-2 text-xs text-slate-500">
              Dernier traitΓ©: {progress.latest.sender} β€” {progress.latest.amount?.toFixed(2)} $ β†’ {progress.latest.branch}
            </div>
          )}
          {progress.currentProvider && (
            <div className="mt-1 text-xs text-slate-400">
              Fournisseur IA: {progress.currentProvider}
            </div>
          )}
          {progress.errored > 0 && (
            <div className="mt-1 text-xs text-red-500">
              {progress.errored} erreur(s) de traitement
            </div>
          )}
        </div>
      )}

      {/* ═══ SCAN RESULT (after completion) ═══ */}
      {result && !isScanning && (
        <div className="bg-green-50 border border-green-200 rounded-lg p-4 text-sm">
          <div className="font-medium text-green-800 mb-1">Scan terminΓ© βœ“</div>
          <div className="text-green-700">
            <span className="font-semibold">{result.parsed}</span> nouveaux virements importΓ©s
            {result.skipped > 0 && (
              <> Β· {result.skipped} dΓ©jΓ  en base</>
            )}
            {result.errors > 0 && (
              <> Β· <span className="text-red-600">{result.errors} erreurs</span></>
            )}
          </div>
        </div>
      )}

      {/* ═══ ERROR MESSAGE ═══ */}
      {error && (
        <div className="bg-red-50 border border-red-200 rounded-lg p-4 text-sm text-red-700">
          <div className="font-medium mb-1">Erreur de scan</div>
          {error}
        </div>
      )}
    </div>
  );
}