File size: 4,153 Bytes
56b6519
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { t } from 'i18next';
import { useEffect, useState } from 'react';
import { useParams } from 'react-router-dom';
import { toast } from 'sonner';

import DropdownButton from '@/components/button/DropdownButton';
import AverageCVSS from '@/components/dashboard/AverageCVSS';
import CIATriad from '@/components/dashboard/CIATriad';
import CVSSScore from '@/components/dashboard/CVSSScore';
import ExportModal from '@/components/dashboard/ExportModal';
import RemediationComplexity from '@/components/dashboard/RemediationComplexity';
import RemediationPriority from '@/components/dashboard/RemediationPriority';
import Sidebar from '@/components/dashboard/Sidebar';
import { getAuditById } from '@/services/audits';
import { exportToCSV } from '@/services/exportToCSV';
import { exportToPDF } from '@/services/exportToPDF';

export const Dashboard = () => {
  const { auditId } = useParams();
  // eslint-disable-next-line sonarjs/no-duplicate-string
  const [activeView, setActiveView] = useState('cvss-score');
  const [isExportModalOpen, setIsExportModalOpen] = useState(false);
  const [selectedDisplays, setSelectedDisplays] = useState<string[]>([]);
  const [auditName, setAuditName] = useState('');

  useEffect(() => {
    getAuditById(auditId)
      .then(audit => {
        setAuditName(audit.datas.name);
      })
      .catch(console.error);
  }, [auditId]);

  const displays = [
    { id: 'cvss-score', name: t('cvssScore'), component: CVSSScore },
    {
      id: 'remediation-priority',
      name: t('remediationPriority'),
      component: RemediationPriority,
    },
    {
      id: 'remediation-complexity',
      name: t('remediationComplexity'),
      component: RemediationComplexity,
    },
    { id: 'average-cvss', name: 'Average CVSS', component: AverageCVSS },
    { id: 'cia-triad', name: 'CIA Triad', component: CIATriad },
  ];

  const renderView = () => {
    switch (activeView) {
      case 'cvss-score':
        return <CVSSScore />;

      case 'remediation-priority':
        return <RemediationPriority />;

      case 'remediation-complexity':
        return <RemediationComplexity />;

      case 'average-cvss':
        return <AverageCVSS />;

      case 'cia-triad':
        return <CIATriad />;
    }
  };

  const [exportType, setExportType] = useState('');

  const handleExportClick = (type: string) => {
    setExportType(type);
    setSelectedDisplays(displays.map(d => d.id));
    setIsExportModalOpen(true);
  };

  const handleExportConfirm = async () => {
    if (exportType === 'pdf') {
      setIsExportModalOpen(false);
      await exportToPDF(auditName, selectedDisplays, auditId ?? '');
    } else if (exportType === 'csv') {
      setIsExportModalOpen(false);

      try {
        exportToCSV(auditName, selectedDisplays, auditId ?? '');
      } catch (err) {
        toast.error(t('err.exportDashboardCSV'));
        console.error(err);
      }
      toast.success(t('msg.exportDashboardCSVOk'));
    }
  };

  const exportTypes = [
    {
      id: 1,
      value: 'pdf',
      label: 'pdf',
      onClick: () => handleExportClick('pdf'),
    },
    {
      id: 2,
      value: 'csv',
      label: 'csv',
      onClick: () => handleExportClick('csv'),
    },
  ];

  return (
    <>
      <div className="flex h-screen bg-gray-900 text-white">
        <Sidebar activeView={activeView} setActiveView={setActiveView} />
        <div className="flex-1 overflow-hidden p-6">
          <div className="flex justify-between items-center mb-6">
            <h1 className="text-3xl font-bold">{t('dashboard')}</h1>
            <DropdownButton
              items={exportTypes}
              placeholder={t('exportDashboard')}
            />
          </div>
          {renderView()}
        </div>
      </div>
      <ExportModal
        auditName={auditName}
        displays={displays}
        isOpen={isExportModalOpen}
        onClose={() => setIsExportModalOpen(false)}
        onConfirm={handleExportConfirm}
        selectedDisplays={selectedDisplays}
        setAuditName={setAuditName}
        setSelectedDisplays={setSelectedDisplays}
        type={exportType}
      />
    </>
  );
};