1- import React from "react" ;
1+ import React , { useMemo } from "react" ;
22import PropTypes from "prop-types" ;
33import { Bar } from "react-chartjs-2" ;
44import {
@@ -13,20 +13,46 @@ import {
1313import ChartDataLabels from "chartjs-plugin-datalabels" ;
1414import styles from "./JobAnalyticsPage.module.css" ;
1515
16- ChartJS . register ( CategoryScale , LinearScale , BarElement , Title , Tooltip , Legend ) ;
16+ ChartJS . register (
17+ CategoryScale ,
18+ LinearScale ,
19+ BarElement ,
20+ Title ,
21+ Tooltip ,
22+ Legend ,
23+ ChartDataLabels
24+ ) ;
1725
1826export default function JobAnalyticsGraph ( { data, darkMode } ) {
19- if ( ! Array . isArray ( data ) || data . length === 0 ) {
27+ const normalizedData = useMemo ( ( ) => {
28+ if ( ! Array . isArray ( data ) ) return [ ] ;
29+
30+ return data
31+ . map ( ( d ) => ( {
32+ role : d . role ,
33+ applications :
34+ typeof d . applications === "number"
35+ ? d . applications
36+ : typeof d . count === "number"
37+ ? d . count
38+ : 0 ,
39+ } ) )
40+ . filter ( ( d ) => d . role && d . applications > 0 ) ;
41+ } , [ data ] ) ;
42+
43+ if ( normalizedData . length === 0 ) {
2044 return < p > No data available</ p > ;
2145 }
2246
2347 const chartData = {
24- labels : data . map ( ( d ) => d . role ) ,
48+ labels : normalizedData . map ( ( d ) => d . role ) ,
2549 datasets : [
2650 {
2751 label : "Applications" ,
28- data : data . map ( ( d ) => d . applications ?? d . count ?? 0 ) ,
29- backgroundColor : darkMode ? "#3A506B" : "rgba(54, 162, 235, 0.7)" ,
52+ data : normalizedData . map ( ( d ) => d . applications ) ,
53+ backgroundColor : darkMode
54+ ? "#3A506B"
55+ : "rgba(54, 162, 235, 0.7)" ,
3056 } ,
3157 ] ,
3258 } ;
@@ -82,15 +108,15 @@ export default function JobAnalyticsGraph({ data, darkMode }) {
82108
83109 return (
84110 < div className = { styles . graphContainer } >
85- < Bar data = { chartData } options = { chartOptions } plugins = { [ ChartDataLabels ] } />
111+ < Bar data = { chartData } options = { chartOptions } />
86112 </ div >
87113 ) ;
88114}
89115
90116JobAnalyticsGraph . propTypes = {
91117 data : PropTypes . arrayOf (
92118 PropTypes . shape ( {
93- role : PropTypes . string . isRequired ,
119+ role : PropTypes . string ,
94120 applications : PropTypes . number ,
95121 count : PropTypes . number ,
96122 } )
0 commit comments