1- import { LineChart } from "@mui/x-charts" ;
1+ import { BarChart } from "@mui/x-charts" ;
22import { useStaticData } from "@cgg/hooks/useStaticData" ;
3+ import cssVariables from "@cgg/styles/variables.module.scss" ;
34import { capitalize } from "@cgg/utils/capitalize" ;
45import type { IApiChallengeResponse } from "@cgg/utils/endpoints/types" ;
56import { getChallenge } from "@cgg/utils/getChallenge" ;
@@ -8,15 +9,75 @@ import type { Tier } from "@cgg/utils/tier";
89
910const tiers = [ "UNRANKED" as Tier , ...tierList ] ;
1011
12+ const fallbackTierColors : Record < Tier , string > = {
13+ NONE : "hsl(0, 0%, 41%)" ,
14+ UNRANKED : "hsl(0, 0%, 41%)" ,
15+ IRON : "hsl(0, 0%, 41%)" ,
16+ BRONZE : "hsl(17, 42%, 39%)" ,
17+ SILVER : "hsl(189, 12%, 56%)" ,
18+ GOLD : "hsl(31, 60%, 52%)" ,
19+ PLATINUM : "hsl(171, 65%, 36%)" ,
20+ DIAMOND : "hsl(230, 55%, 57%)" ,
21+ MASTER : "hsl(286, 64%, 55%)" ,
22+ GRANDMASTER : "hsl(351, 65%, 66%)" ,
23+ CHALLENGER : "hsl(40, 85%, 70%)" ,
24+ NONCHALLENGE : "hsl(0, 0%, 41%)" ,
25+ } ;
26+
27+ function getTierColor ( tier : Tier ) : string {
28+ if ( typeof document === "undefined" ) {
29+ return fallbackTierColors [ tier ] ;
30+ }
31+
32+ const colorNode = document . createElement ( "div" ) ;
33+ colorNode . className = cssVariables [ tier ] ;
34+ colorNode . style . position = "absolute" ;
35+ colorNode . style . visibility = "hidden" ;
36+ colorNode . style . pointerEvents = "none" ;
37+ colorNode . style . opacity = "0" ;
38+ document . body . appendChild ( colorNode ) ;
39+
40+ const color = getComputedStyle ( colorNode ) . getPropertyValue ( "--tier" ) . trim ( ) ;
41+ colorNode . remove ( ) ;
42+
43+ return color || fallbackTierColors [ tier ] ;
44+ }
45+
1146export default function Distribution ( {
1247 playerData,
1348} : {
1449 playerData : IApiChallengeResponse ;
1550} ) {
1651 const data = useStaticData ( ) ;
52+ const tierColors = tiers . reduce < Record < Tier , string > > (
53+ ( acc , tier ) => {
54+ acc [ tier ] = getTierColor ( tier ) ;
55+ return acc ;
56+ } ,
57+ { } as Record < Tier , string > ,
58+ ) ;
59+
60+ const values = tiers . map ( ( t ) => {
61+ let amount = playerData . challenges . filter ( ( c ) => {
62+ const challenge = getChallenge ( c . challengeId , data ) ;
63+ if ( ! challenge ) return false ;
64+ if ( challenge . retired ) return false ;
65+
66+ if ( t === "UNRANKED" ) {
67+ return ( [ "UNRANKED" , "NONCHALLENGE" , "NONE" ] as Tier [ ] ) . includes ( c . tier ) ;
68+ }
69+ return c . tier === t ;
70+ } ) . length ;
71+
72+ if ( t === "UNRANKED" ) {
73+ amount += Object . keys ( data . challenges ) . length - playerData . challenges . length ;
74+ }
75+
76+ return amount ;
77+ } ) ;
1778
1879 return (
19- < LineChart
80+ < BarChart
2081 xAxis = { [
2182 {
2283 scaleType : "band" ,
@@ -25,33 +86,14 @@ export default function Distribution({
2586 ] }
2687 series = { [
2788 {
28- data : tiers . map ( ( t ) => {
29- let amount = playerData . challenges . filter ( ( c ) => {
30- const challenge = getChallenge ( c . challengeId , data ) ;
31- if ( ! challenge ) return false ;
32- if ( challenge . retired ) return false ;
33-
34- if ( t === "UNRANKED" ) {
35- return ( [ "UNRANKED" , "NONCHALLENGE" , "NONE" ] as Tier [ ] ) . includes (
36- c . tier ,
37- ) ;
38- }
39- return c . tier === t ;
40- } ) . length ;
41- if ( t === "UNRANKED" ) {
42- amount +=
43- Object . keys ( data . challenges ) . length -
44- playerData . challenges . length ;
45- }
46-
47- return amount ;
48- } ) ,
89+ data : values ,
4990 label : "Challenges in tier" ,
50- color : "#0dbdff" ,
91+ colorGetter : ( { dataIndex } ) => tierColors [ tiers [ dataIndex ] ] ,
5192 } ,
5293 ] }
5394 hideLegend = { true }
5495 height = { 300 }
96+ borderRadius = { 4 }
5597 grid = { { vertical : true , horizontal : true } }
5698 />
5799 ) ;
0 commit comments