-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathindex.js
74 lines (66 loc) · 2.07 KB
/
index.js
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
import { Badge } from 'reactstrap';
import React from 'react';
// eslint-disable-next-line import/no-extraneous-dependencies
import PropTypes from 'prop-types';
import './styles.scss';
const PatternFrequencies = ({ filters, rows }) => {
const patternsMap = rows.reduce((acc, row) => {
for (let i = 0; i < row.original.pattern.length; i += 1) {
const pattern = row.original.pattern[i];
acc[pattern] = acc[pattern] + 1 || 1;
}
return acc;
}, Object.create(null));
const sortedPatternsByFrequency = Object.keys(patternsMap).sort(
(a, b) => patternsMap[b] - patternsMap[a],
);
const showComponent = filters.find(filter =>
['companies', 'difficulty'].includes(filter.id),
);
const getFrequencyClass = rate => {
const highestFrequency = Math.round(
patternsMap[sortedPatternsByFrequency[0]],
);
if (highestFrequency / 3 < 1) {
return '';
}
const frequencyRate = {
easy: Math.round(highestFrequency / 3),
medium: Math.round((highestFrequency / 3) * 2),
hard: highestFrequency,
};
return Object.keys(frequencyRate).find(key => rate <= frequencyRate[key]);
};
return showComponent ? (
<div className="pattern-count">
<h5>Problems pattern frequency</h5>
{sortedPatternsByFrequency.map((pattern, index) => (
<Badge
// eslint-disable-next-line react/no-array-index-key
key={pattern + index}
className={`${getFrequencyClass(patternsMap[pattern])}`}
pill
>
<span
data-tip={`${patternsMap[pattern]} "${pattern}" related problems`}
>
{pattern} : {patternsMap[pattern]}
</span>
</Badge>
))}
</div>
) : null;
};
PatternFrequencies.propTypes = {
filters: PropTypes.arrayOf(
PropTypes.shape({ id: PropTypes.string, value: PropTypes.string }),
).isRequired,
rows: PropTypes.arrayOf(
PropTypes.shape({
original: PropTypes.shape({
pattern: PropTypes.arrayOf(PropTypes.string),
}),
}),
).isRequired,
};
export default PatternFrequencies;