| 
 | 1 | +import { Badge } from 'reactstrap';  | 
 | 2 | +import React from 'react';  | 
 | 3 | +// eslint-disable-next-line import/no-extraneous-dependencies  | 
 | 4 | +import PropTypes from 'prop-types';  | 
 | 5 | +import './styles.scss';  | 
 | 6 | + | 
 | 7 | +const PatternFrequencies = ({ filters, rows }) => {  | 
 | 8 | + const patternsMap = rows.reduce((acc, row) => {  | 
 | 9 | + for (let i = 0; i < row.original.pattern.length; i += 1) {  | 
 | 10 | + const pattern = row.original.pattern[i];  | 
 | 11 | + acc[pattern] = acc[pattern] + 1 || 1;  | 
 | 12 | + }  | 
 | 13 | + return acc;  | 
 | 14 | + }, Object.create(null));  | 
 | 15 | + const sortedPatternsByFrequency = Object.keys(patternsMap).sort(  | 
 | 16 | + (a, b) => patternsMap[b] - patternsMap[a],  | 
 | 17 | + );  | 
 | 18 | + const showComponent = filters.find(filter =>  | 
 | 19 | + ['companies', 'difficulty'].includes(filter.id),  | 
 | 20 | + );  | 
 | 21 | + | 
 | 22 | + const getFrequencyClass = rate => {  | 
 | 23 | + const highestFrequency = Math.round(  | 
 | 24 | + patternsMap[sortedPatternsByFrequency[0]],  | 
 | 25 | + );  | 
 | 26 | + | 
 | 27 | + if (highestFrequency / 3 < 1) {  | 
 | 28 | + return '';  | 
 | 29 | + }  | 
 | 30 | + | 
 | 31 | + const frequencyRate = {  | 
 | 32 | + easy: Math.round(highestFrequency / 3),  | 
 | 33 | + medium: Math.round((highestFrequency / 3) * 2),  | 
 | 34 | + hard: highestFrequency,  | 
 | 35 | + };  | 
 | 36 | + | 
 | 37 | + return Object.keys(frequencyRate).find(key => rate <= frequencyRate[key]);  | 
 | 38 | + };  | 
 | 39 | + | 
 | 40 | + return showComponent ? (  | 
 | 41 | + <div className="pattern-count">  | 
 | 42 | + <h5>Problems pattern frequency</h5>  | 
 | 43 | + {sortedPatternsByFrequency.map((pattern, index) => (  | 
 | 44 | + <Badge  | 
 | 45 | + // eslint-disable-next-line react/no-array-index-key  | 
 | 46 | + key={pattern + index}  | 
 | 47 | + className={`${getFrequencyClass(patternsMap[pattern])}`}  | 
 | 48 | + pill  | 
 | 49 | + >  | 
 | 50 | + <span  | 
 | 51 | + data-tip={`${patternsMap[pattern]} "${pattern}" related problems`}  | 
 | 52 | + >  | 
 | 53 | + {pattern} : {patternsMap[pattern]}  | 
 | 54 | + </span>  | 
 | 55 | + </Badge>  | 
 | 56 | + ))}  | 
 | 57 | + </div>  | 
 | 58 | + ) : null;  | 
 | 59 | +};  | 
 | 60 | + | 
 | 61 | +PatternFrequencies.propTypes = {  | 
 | 62 | + filters: PropTypes.arrayOf(  | 
 | 63 | + PropTypes.shape({ id: PropTypes.string, value: PropTypes.string }),  | 
 | 64 | + ).isRequired,  | 
 | 65 | + rows: PropTypes.arrayOf(  | 
 | 66 | + PropTypes.shape({  | 
 | 67 | + original: PropTypes.shape({  | 
 | 68 | + pattern: PropTypes.arrayOf(PropTypes.string),  | 
 | 69 | + }),  | 
 | 70 | + }),  | 
 | 71 | + ).isRequired,  | 
 | 72 | +};  | 
 | 73 | + | 
 | 74 | +export default PatternFrequencies;  | 
0 commit comments