File size: 2,809 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
import { useEffect, useState } from 'react';

type CWERelated = {
  cwe: string;
  cweParent?: string;
  cweGrandParent?: string;
};

type ListItem = {
  id: number;
  value: string;
  label?: string;
  locale?: string;
};

type ListItemCategory = {
  id: number;
  value: string;
  label?: string;
  isNull?: boolean;
};

type Details = {
  locale: string;
  title?: string;
  vulnType?: string;
  description?: string;
  observation?: string;
  remediation?: string;
  cwes: string[];
  references: string[];
  customFields: string[];
};

type VulnerabilityData = {
  _id: string;
  cvssv3: string | null;
  priority?: number | '';
  remediationComplexity?: number | '';
  details: Details[];
  status?: number;
  category?: string | null;
  __v: number;
  createdAt?: string;
  updatedAt?: string;
};

type UseVulnStateProps = {
  currentVuln: VulnerabilityData; // Adjust this type based on the shape of `currentVuln`
  languages: ListItem[];
  types: ListItem[];
};

export const useVulnState = ({
  currentVuln,
  languages,
  types,
}: UseVulnStateProps) => {
  const [openModal, setOpenModal] = useState(false);
  const [changed, setChanged] = useState<boolean>(false);

  const [categorySelected, setCategorySelected] =
    useState<ListItemCategory | null>(null);
  const [categoryChanged, setCategoryChanged] = useState<boolean>(false);

  const [selectedLanguage, setSelectedLanguage] = useState<ListItem>(
    languages.find(lang => lang.value === currentVuln.details[0].locale) ??
      languages[0],
  );

  const [selectedType, setSelectedType] = useState<ListItem | null>(
    currentVuln.details[0].vulnType
      ? (types.find(
          typeIter => typeIter.value === currentVuln.details[0].vulnType,
        ) ?? null)
      : null,
  );

  const [typesFiltered, setTypesFiltered] = useState<ListItem[]>(
    types.filter(typeIter => typeIter.locale === currentVuln.details[0].locale),
  );

  const [cweLoading, setCweLoading] = useState<boolean>(false);
  const [cweRecommendationSelected, setCweRecommendationSelected] = useState<
    string[]
  >([]);
  const [cweRecommended, setCweRecommended] = useState<CWERelated[]>([]);

  useEffect(() => {
    // Update filtered types when the language changes
    setTypesFiltered(
      types.filter(typeIter => typeIter.locale === selectedLanguage.value),
    );
  }, [selectedLanguage, types]);

  return {
    openModal,
    setOpenModal,
    changed,
    setChanged,
    categorySelected,
    setCategorySelected,
    categoryChanged,
    setCategoryChanged,
    selectedLanguage,
    setSelectedLanguage,
    selectedType,
    setSelectedType,
    typesFiltered,
    setTypesFiltered,
    cweLoading,
    setCweLoading,
    cweRecommendationSelected,
    setCweRecommendationSelected,
    cweRecommended,
    setCweRecommended,
  };
};