11import { Text } from "@mantine/core" ;
2- import { groupBy , sumBy } from "../utils/arrayUtils" ;
2+ import { sumBy } from "../utils/arrayUtils" ;
33import { calculateTickValues } from "../utils/chartUtils" ;
44import { prettifyProgrammingLanguageName } from "../utils/programmingLanguagesUtils" ;
5- import { colors , isColor } from "../utils/colors" ;
5+ import { colors } from "../utils/colors" ;
66import { Bar } from "react-chartjs-2" ;
77import {
88 Chart as ChartJS ,
@@ -55,7 +55,7 @@ const defaultColors = [
5555 "#b15928" ,
5656] ;
5757
58- const chosenRandomColors : Record < string , string > = { } ;
58+ const chosenRandomColors = new Map < string , string > ( ) ;
5959
6060function getLanguageColor ( str : string ) : string {
6161 let language = str . toLowerCase ( ) ;
@@ -64,18 +64,24 @@ function getLanguageColor(str: string): string {
6464 if ( language . split ( " " ) . length > 1 ) {
6565 language = language
6666 . split ( " " )
67- . filter ( ( possibleName ) => isColor ( possibleName ) ) [ 0 ] ;
67+ . filter ( ( possibleName ) => colors . has ( possibleName ) ) [ 0 ] ;
6868 }
6969
70- if ( isColor ( language ) ) {
71- return colors [ language ] ;
70+ if ( colors . has ( language ) ) {
71+ // Should never be undefined
72+ return colors . get ( language ) ?? "#000000" ;
7273 }
7374
74- if ( language in chosenRandomColors ) return chosenRandomColors [ language ] ;
75+ if ( chosenRandomColors . has ( language ) ) {
76+ // Should never be undefined
77+
78+ return chosenRandomColors . get ( language ) ?? "#000000" ;
79+ }
7580
7681 const randomColor =
7782 defaultColors [ Math . floor ( defaultColors . length * Math . random ( ) ) ] ;
78- chosenRandomColors [ language ] = randomColor ;
83+ chosenRandomColors . set ( language , randomColor ) ;
84+
7985 return randomColor ;
8086}
8187
@@ -87,21 +93,28 @@ export const PerProjectChart = ({
8793} : PerProjectChartProps ) => {
8894 if ( entries . length === 0 ) return < Text > No data</ Text > ;
8995
90- let languageNames = entries
96+ const languageNames = entries
9197 . sort ( ( entry1 , entry2 ) => entry2 . duration - entry1 . duration )
9298 . map ( ( entry ) => entry . language || "other" )
9399 . filter ( ( item , index , array ) => array . indexOf ( item ) === index )
94- . slice ( 0 , 9 ) ;
95- languageNames . push ( "other" ) ;
96- languageNames = languageNames . reverse ( ) ;
100+ . slice ( 0 , 9 )
101+ . concat ( "other" ) // TODO: Why is this here
102+ . toReversed ( ) ;
97103
98104 // Get the total time spent on each project
99- const projectGroups = groupBy ( entries , ( e ) => e . project_name ?? "Unknown" ) ;
105+ const projectGroups = Object . groupBy (
106+ entries ,
107+ ( x ) => x . project_name ?? "undefined" ,
108+ ) ;
109+
100110 const totalTimeByProject = Object . keys ( projectGroups ) . map ( ( projectName ) => {
101- const projectEntries = projectGroups [ projectName ] ;
102- const langGroups = groupBy ( projectEntries , ( e ) => e . language ) ;
111+ const projectEntries = projectGroups [ projectName ] ?? [ ] ;
112+ const langGroups = Object . groupBy (
113+ projectEntries ,
114+ ( e ) => e . language ?? "undefined" ,
115+ ) ;
103116 const totalTimeByLanguage = Object . keys ( langGroups ) . map ( ( lang ) => {
104- const langEntries = langGroups [ lang ] ;
117+ const langEntries = langGroups [ lang ] ?? [ ] ;
105118 return {
106119 language : lang ,
107120 duration : sumBy ( langEntries , ( e ) => e . duration ) ,
0 commit comments