File size: 6,071 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
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
import { Checkbox } from '@headlessui/react';
import { CheckIcon } from '@heroicons/react/16/solid';
import { t } from 'i18next';
import { useEffect, useState } from 'react';

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

export type CheckboxButtonProps = {
  cwesRecommended: CWERelated[];
  cweRecommendationSelected: string[];
  setCweRecommendationSelected: React.Dispatch<React.SetStateAction<string[]>>;
};

// Returns an object with the total number of properties in the CWE array and an array with the number of properties accumulated in each CWE object
const countPropertiesInCWEArray = (
  arr: CWERelated[],
): {
  total: number;
  breakdown: number[];
} => {
  let accumulatedTotal = 0;

  const keys: (keyof CWERelated)[] = ['cwe', 'cweParent', 'cweGrandParent'];
  const breakdown = arr.map(item => {
    const count = keys.reduce((sum, key) => sum + (item[key] ? 1 : 0), 0);

    const currentTotal = accumulatedTotal;
    accumulatedTotal += count;

    return currentTotal;
  });

  const total = accumulatedTotal;
  return { total, breakdown };
};

const MultiCheckboxButton = ({
  cwesRecommended,
  cweRecommendationSelected,
  setCweRecommendationSelected,
}: CheckboxButtonProps) => {
  const countProperties = countPropertiesInCWEArray(cwesRecommended);
  const [selectedBox, setSelectedBox] = useState<boolean[]>(
    Array(countProperties.total).fill(false),
  );

  useEffect(() => {
    if (cweRecommendationSelected.length === 0) {
      setSelectedBox(Array(countProperties.total).fill(false));
    }
  }, [cweRecommendationSelected.length, countProperties.total]);

  const allStrings: string[] = cwesRecommended.flatMap(item => {
    const { cwe, cweParent, cweGrandParent } = item;
    return [cwe, cweParent, cweGrandParent].filter((value): value is string =>
      Boolean(value),
    );
  });

  const [showAncestors, setShowAncestors] = useState<boolean[]>(
    Array(cwesRecommended.length).fill(false),
  );

  const onChange = (index: number) => {
    const values = selectedBox.map((itemMap, i) =>
      i === index ? !itemMap : itemMap,
    );
    setSelectedBox(values);

    const selectedOptions = allStrings.filter((_, index) => values[index]);

    setCweRecommendationSelected([...selectedOptions]);
  };

  const handlerOnClick = (index: number) => {
    setShowAncestors(
      showAncestors.map((item, i) => (i === index ? !item : item)),
    );
  };

  return (
    <div>
      <span className="text-2xl">{t('recommendedCwe')}:</span>
      <div className="flex flex-col items-center my-3">
        {cwesRecommended.map((option: CWERelated, index: number) => (
          <div
            className={`w-full flex-col items-center ${index !== 0 ? 'mt-6' : ''} p-2 border border-white rounded-md`}
            key={`${index}`}
          >
            <div className="flex justify-between">
              <div className="w-full flex items-center my-1">
                <Checkbox
                  checked={selectedBox[countProperties.breakdown[index]]}
                  className="group flex items-center justify-center size-6 rounded-md bg-white/10 p-1 ring-1 ring-white/15 ring-inset data-[checked]:bg-white"
                  onChange={() => onChange(countProperties.breakdown[index])}
                >
                  <CheckIcon className="hidden size-4 fill-black group-data-[checked]:block" />
                </Checkbox>
                <div className="ml-2">
                  <p className="text-md text-gray-200">{option.cwe}</p>
                </div>
              </div>
              <button
                className="underline font-bold cursor-pointer bg-transparent border-none p-0 m-0"
                onClick={() => handlerOnClick(index)}
                type="button"
              >
                {showAncestors[index] ? t('hideAncestors') : t('showAncestors')}
              </button>
            </div>
            {showAncestors[index] ? (
              <div>
                {option.cweParent ? (
                  <div className="w-full flex items-center mt-4 pl-10">
                    <Checkbox
                      checked={
                        selectedBox[countProperties.breakdown[index] + 1]
                      }
                      className="group flex items-center justify-center size-6 rounded-md bg-white/10 p-1 ring-1 ring-white/15 ring-inset data-[checked]:bg-white"
                      onChange={() =>
                        onChange(countProperties.breakdown[index] + 1)
                      }
                    >
                      <CheckIcon className="hidden size-4 fill-black group-data-[checked]:block" />
                    </Checkbox>
                    <div className="ml-2">
                      <p className="text-md text-gray-200">
                        {option.cweParent}
                      </p>
                    </div>
                  </div>
                ) : null}
                {option.cweGrandParent ? (
                  <div className="w-full flex items-center mt-5 pl-20">
                    <Checkbox
                      checked={
                        selectedBox[countProperties.breakdown[index] + 2]
                      }
                      className="group flex items-center justify-center size-6 rounded-md bg-white/10 p-1 ring-1 ring-white/15 ring-inset data-[checked]:bg-white"
                      onChange={() =>
                        onChange(countProperties.breakdown[index] + 2)
                      }
                    >
                      <CheckIcon className="hidden size-4 fill-black group-data-[checked]:block" />
                    </Checkbox>
                    <div className="ml-2">
                      <p className="text-md text-gray-200">
                        {option.cweGrandParent}
                      </p>
                    </div>
                  </div>
                ) : null}
              </div>
            ) : null}
          </div>
        ))}
      </div>
    </div>
  );
};

export default MultiCheckboxButton;