File size: 12,088 Bytes
7b4f5dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/* ═══════════════════════════════════════════════════════════════
   ScanContext β€” Global state management for the scan lifecycle
   ═══════════════════════════════════════════════════════════════ */

import { createContext, useContext, useReducer, useCallback, useRef } from 'react';
import { createScanService } from '../services/scanService';

const ScanContext = createContext(null);

// View states
export const VIEWS = {
  LANDING: 'landing',
  ANALYSIS: 'analysis',
  REPORT: 'report',
};

// Scan states
export const SCAN_STATUS = {
  IDLE: 'idle',
  SCANNING: 'scanning',
  COMPLETE: 'complete',
  ERROR: 'error',
};

// Agent states
export const AGENT_STATUS = {
  IDLE: 'idle',
  SCANNING: 'scanning',
  COMPLETE: 'complete',
};

const initialState = {
  view: VIEWS.LANDING,
  scanStatus: SCAN_STATUS.IDLE,
  scanId: null,
  findings: [],
  fixes: [],
  agents: {
    security: { status: AGENT_STATUS.IDLE, progress: 0, findingsCount: 0, filesScanned: 0, message: '' },
    performance: { status: AGENT_STATUS.IDLE, progress: 0, findingsCount: 0, filesScanned: 0, message: '' },
    fix: { status: AGENT_STATUS.IDLE, progress: 0, findingsCount: 0, filesScanned: 0, message: '' },
  },
  summary: null,
  error: null,
  startTime: null,
  elapsedTime: 0,
  amdMetrics: null,
  amdMigration: null,
};

function scanReducer(state, action) {
  switch (action.type) {
    case 'SET_VIEW':
      return { ...state, view: action.payload };

    case 'SCAN_STARTED':
      return {
        ...state,
        view: VIEWS.ANALYSIS,
        scanStatus: SCAN_STATUS.SCANNING,
        scanId: action.payload.scanId,
        startTime: Date.now(),
        findings: [],
        fixes: [],
        error: null,
        summary: null,
        amdMetrics: null,
        amdMigration: null,
        agents: {
          security: { ...initialState.agents.security },
          performance: { ...initialState.agents.performance },
          fix: { ...initialState.agents.fix },
        },
      };

    case 'AGENT_START':
      return {
        ...state,
        agents: {
          ...state.agents,
          [action.payload.agent]: {
            ...state.agents[action.payload.agent],
            status: AGENT_STATUS.SCANNING,
            message: action.payload.message || 'Initializing...',
          },
        },
      };

    case 'PROGRESS':
      return {
        ...state,
        agents: {
          ...state.agents,
          [action.payload.agent]: {
            ...state.agents[action.payload.agent],
            progress: action.payload.percent,
            filesScanned: action.payload.filesScanned || state.agents[action.payload.agent].filesScanned,
            message: action.payload.message || state.agents[action.payload.agent].message,
            status: action.payload.percent >= 100 ? AGENT_STATUS.COMPLETE : AGENT_STATUS.SCANNING,
          },
        },
      };

    case 'FINDING': {
      const agent = action.payload.agent;
      return {
        ...state,
        findings: [...state.findings, action.payload],
        agents: {
          ...state.agents,
          [agent]: {
            ...state.agents[agent],
            findingsCount: state.agents[agent].findingsCount + 1,
          },
        },
      };
    }

    case 'FIX_READY':
      return {
        ...state,
        fixes: [...state.fixes, action.payload],
        agents: {
          ...state.agents,
          fix: {
            ...state.agents.fix,
            findingsCount: state.agents.fix.findingsCount + 1,
          },
        },
      };

    case 'COMPLETE':
      return {
        ...state,
        scanStatus: SCAN_STATUS.COMPLETE,
        summary: action.payload,
        agents: {
          ...state.agents,
          security: { ...state.agents.security, status: AGENT_STATUS.COMPLETE, progress: 100 },
          performance: { ...state.agents.performance, status: AGENT_STATUS.COMPLETE, progress: 100 },
          fix: { ...state.agents.fix, status: AGENT_STATUS.COMPLETE, progress: 100 },
        },
      };

    case 'ERROR':
      return {
        ...state,
        scanStatus: SCAN_STATUS.ERROR,
        error: action.payload.message,
      };

    case 'UPDATE_ELAPSED':
      return {
        ...state,
        elapsedTime: action.payload,
      };

    case 'AMD_METRICS':
      return {
        ...state,
        amdMetrics: action.payload,
      };

    case 'AMD_MIGRATION_FINDING': {
      const prev = state.amdMigration || { findings: [], compatibility_score: 100, compatibility_label: 'Analyzing...', total_cuda_patterns_found: 0, summary: '' };
      return {
        ...state,
        amdMigration: {
          ...prev,
          findings: [...prev.findings, action.payload],
          total_cuda_patterns_found: prev.total_cuda_patterns_found + 1,
        },
      };
    }

    case 'AMD_MIGRATION_SUMMARY':
      return {
        ...state,
        amdMigration: {
          ...(state.amdMigration || { findings: [] }),
          ...action.payload,
        },
      };

    case 'RESET':
      return { ...initialState };

    default:
      return state;
  }
}

export function ScanProvider({ children }) {
  const [state, dispatch] = useReducer(scanReducer, initialState);
  const serviceRef = useRef(null);
  const timerRef = useRef(null);

  const startScan = useCallback(async (payload) => {
    // Cleanup previous scan
    if (serviceRef.current) {
      serviceRef.current.destroy();
    }
    if (timerRef.current) {
      clearInterval(timerRef.current);
    }

    const service = createScanService();
    serviceRef.current = service;

    // Wire up event handlers
    service.on('scan_started', (data) => {
      dispatch({ type: 'SCAN_STARTED', payload: data });

      // Start elapsed time counter
      const start = Date.now();
      timerRef.current = setInterval(() => {
        dispatch({ type: 'UPDATE_ELAPSED', payload: Date.now() - start });
      }, 100);
    });

    service.on('agent_start', (data) => {
      dispatch({ type: 'AGENT_START', payload: data });
    });

    service.on('progress', (data) => {
      dispatch({ type: 'PROGRESS', payload: data });
    });

    service.on('finding', (data) => {
      dispatch({ type: 'FINDING', payload: data });
    });

    service.on('fix_ready', (data) => {
      dispatch({ type: 'FIX_READY', payload: data });
    });

    service.on('amd_metrics', (data) => {
      dispatch({ type: 'AMD_METRICS', payload: data });
    });

    service.on('amd_migration_finding', (data) => {
      dispatch({ type: 'AMD_MIGRATION_FINDING', payload: data });
    });

    service.on('amd_migration_summary', (data) => {
      dispatch({ type: 'AMD_MIGRATION_SUMMARY', payload: data });
    });

    service.on('complete', (data) => {
      console.log('[ScanContext] COMPLETE event received β€” triggering notification');
      dispatch({ type: 'COMPLETE', payload: data });
      if (timerRef.current) {
        clearInterval(timerRef.current);
      }

      // Handle Notification / Toast
      console.log("[ScanContext] Notification permission status:", Notification.permission);
      if ('Notification' in window && Notification.permission === 'granted') {
        try {
          console.log("[ScanContext] Attempting to show native notification...");
          const notification = new Notification("βœ… CodeSentry Analysis Complete", {
            body: "Your code scan is done. Open the report to review findings.",
            tag: "codesentry-scan-complete", // Prevents duplicate notifications
            requireInteraction: true // Keeps it visible until user interacts
          });
          notification.onclick = function() {
            window.focus();
            dispatch({ type: 'SET_VIEW', payload: VIEWS.REPORT });
            this.close();
          };
        } catch (e) {
          console.error("[ScanContext] Native Notification failed:", e);
        }
      }
      
      // Always show the in-app toast to guarantee visibility
      // Play a subtle notification sound
      try {
        const audioCtx = new (window.AudioContext || window.webkitAudioContext)();
        const osc = audioCtx.createOscillator();
        const gain = audioCtx.createGain();
        osc.connect(gain);
        gain.connect(audioCtx.destination);
        osc.frequency.value = 880;
        osc.type = 'sine';
        gain.gain.value = 0.15;
        osc.start();
        gain.gain.exponentialRampToValueAtTime(0.001, audioCtx.currentTime + 0.3);
        osc.stop(audioCtx.currentTime + 0.3);
      } catch(e) { /* audio not critical */ }

      const toast = document.createElement('div');
      toast.id = 'codesentry-complete-toast';
      toast.innerHTML = `
        <div style="display: flex; align-items: center; gap: 12px;">
          <span style="font-size: 1.8rem;">βœ…</span>
          <div>
            <h4 style="margin: 0; color: #e2e8f0; font-size: 1rem; font-weight: 600;">CodeSentry Analysis Complete</h4>
            <p style="margin: 4px 0 0; font-size: 0.85rem; color: #94a3b8;">Your code scan is done. Click to view report.</p>
          </div>
        </div>
      `;
      
      Object.assign(toast.style, {
        position: 'fixed',
        bottom: '24px',
        right: '24px',
        padding: '16px 20px',
        borderRadius: '12px',
        boxShadow: '0 8px 32px rgba(0, 0, 0, 0.5), 0 0 0 1px rgba(56, 189, 248, 0.3)',
        border: '1px solid rgba(56, 189, 248, 0.4)',
        background: 'linear-gradient(135deg, #1e293b 0%, #0f172a 100%)',
        zIndex: '99999',
        transform: 'translateY(120px)',
        opacity: '0',
        transition: 'all 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275)',
        cursor: 'pointer',
        fontFamily: 'system-ui, -apple-system, sans-serif',
        minWidth: '320px',
        backdropFilter: 'blur(12px)',
      });

      document.body.appendChild(toast);

      // Animate in
      requestAnimationFrame(() => {
        requestAnimationFrame(() => {
          toast.style.transform = 'translateY(0)';
          toast.style.opacity = '1';
        });
      });

      // Click to view report and dismiss
      toast.onclick = () => {
        dispatch({ type: 'SET_VIEW', payload: VIEWS.REPORT });
        toast.style.transform = 'translateY(120px)';
        toast.style.opacity = '0';
        setTimeout(() => toast.remove(), 400);
      };

      // Auto remove after 8 seconds
      setTimeout(() => {
        if (document.body.contains(toast)) {
          toast.style.transform = 'translateY(120px)';
          toast.style.opacity = '0';
          setTimeout(() => toast.remove(), 400);
        }
      }, 8000);
    });

    service.on('error', (data) => {
      dispatch({ type: 'ERROR', payload: data });
      if (timerRef.current) {
        clearInterval(timerRef.current);
      }
    });

    // Request Notification permission if supported and not already granted/denied
    if ('Notification' in window && Notification.permission === 'default') {
      Notification.requestPermission();
    }

    // Start the scan
    await service.startScan(payload);
  }, []);

  const resetScan = useCallback(() => {
    if (serviceRef.current) {
      serviceRef.current.destroy();
      serviceRef.current = null;
    }
    if (timerRef.current) {
      clearInterval(timerRef.current);
      timerRef.current = null;
    }
    dispatch({ type: 'RESET' });
  }, []);

  const setView = useCallback((view) => {
    dispatch({ type: 'SET_VIEW', payload: view });
  }, []);

  const value = {
    ...state,
    startScan,
    resetScan,
    setView,
  };

  return (
    <ScanContext.Provider value={value}>
      {children}
    </ScanContext.Provider>
  );
}

export function useScan() {
  const context = useContext(ScanContext);
  if (!context) {
    throw new Error('useScan must be used within a ScanProvider');
  }
  return context;
}