@@ -6,94 +6,101 @@ import { useTranslation } from "next-i18next";
66import { StatsSourceChannelsProps } from "../../../types/VerificationRequest" ;
77
88const SourceChannelDistribution = ( {
9- statsSourceChannels,
9+ statsSourceChannels,
1010} : StatsSourceChannelsProps ) => {
11- const { t } = useTranslation ( "verificationRequest" ) ;
11+ const { t } = useTranslation ( "verificationRequest" ) ;
1212
13- const sourceColors = {
14- [ VerificationRequestSourceChannel . Whatsapp ] : colors . lightPrimary ,
15- [ VerificationRequestSourceChannel . Instagram ] : colors . secondary ,
16- [ VerificationRequestSourceChannel . Website ] : colors . tertiary ,
17- [ VerificationRequestSourceChannel . AutomatedMonitoring ] :
18- colors . lightSecondary ,
19- } ;
13+ const sourceColors = {
14+ [ VerificationRequestSourceChannel . Whatsapp ] : colors . lightPrimary ,
15+ [ VerificationRequestSourceChannel . Instagram ] : colors . secondary ,
16+ [ VerificationRequestSourceChannel . Website ] : colors . tertiary ,
17+ [ VerificationRequestSourceChannel . AutomatedMonitoring ] :
18+ colors . lightSecondary ,
19+ } ;
2020
21- const calculatePieSegments = ( ) => {
22- let currentAngle = 0 ;
23- return statsSourceChannels . map ( ( channel ) => {
24- const percentage = channel . percentage ;
25- const angle = ( percentage / 100 ) * 360 ;
26- const segment = {
27- startAngle : currentAngle ,
28- endAngle : currentAngle + angle ,
29- percentage,
30- color : sourceColors [ channel . label ] ,
31- label : channel . label ,
32- } ;
33- currentAngle += angle ;
34- return segment ;
35- } ) ;
36- } ;
21+ const calculatePieSegments = ( ) => {
22+ let currentAngle = 0 ;
23+ return statsSourceChannels . map ( ( channel ) => {
24+ const percentage = channel . percentage ;
25+ const angle = ( percentage / 100 ) * 360 ;
26+ const segment = {
27+ startAngle : currentAngle ,
28+ endAngle : currentAngle + angle ,
29+ percentage,
30+ color : sourceColors [ channel . label ] ,
31+ label : channel . label ,
32+ } ;
33+ currentAngle += angle ;
34+ return segment ;
35+ } ) ;
36+ } ;
3737
38- const createPiePath = ( startAngle : number , endAngle : number ) => {
39- const radius = 80 ;
40- const centerX = 100 ;
41- const centerY = 100 ;
38+ const createPiePath = ( startAngle : number , endAngle : number ) => {
39+ const radius = 80 ;
40+ const centerX = 100 ;
41+ const centerY = 100 ;
4242
43- const startRad = ( startAngle * Math . PI ) / 180 ;
44- const endRad = ( endAngle * Math . PI ) / 180 ;
43+ const clampedEnd =
44+ endAngle - startAngle >= 360 ? startAngle + 359.99 : endAngle ;
45+ const startRad = ( startAngle * Math . PI ) / 180 ;
46+ const endRad = ( clampedEnd * Math . PI ) / 180 ;
4547
46- const x1 = centerX + radius * Math . cos ( startRad ) ;
47- const y1 = centerY + radius * Math . sin ( startRad ) ;
48- const x2 = centerX + radius * Math . cos ( endRad ) ;
49- const y2 = centerY + radius * Math . sin ( endRad ) ;
48+ const x1 = centerX + radius * Math . cos ( startRad ) ;
49+ const y1 = centerY + radius * Math . sin ( startRad ) ;
50+ const x2 = centerX + radius * Math . cos ( endRad ) ;
51+ const y2 = centerY + radius * Math . sin ( endRad ) ;
5052
51- const largeArc = endAngle - startAngle > 180 ? 1 : 0 ;
53+ const largeArc = clampedEnd - startAngle > 180 ? 1 : 0 ;
5254
53- return `M ${ centerX } ${ centerY } L ${ x1 } ${ y1 } A ${ radius } ${ radius } 0 ${ largeArc } 1 ${ x2 } ${ y2 } Z` ;
54- } ;
55+ return `M ${ centerX } ${ centerY } L ${ x1 } ${ y1 } A ${ radius } ${ radius } 0 ${ largeArc } 1 ${ x2 } ${ y2 } Z` ;
56+ } ;
5557
56- const pieSegments = calculatePieSegments ( ) ;
58+ const pieSegments = calculatePieSegments ( ) ;
5759
58- return (
59- < >
60- < Typography className = "title" > { t ( "dashboard.sourcesTitle" ) } </ Typography >
61- < Typography className = "subtitle" >
62- { t ( "dashboard.sourcesSubtitle" ) }
63- </ Typography >
64- < Box className = "PieChart-container" >
65- < PieChartSVG width = "200" height = "200" viewBox = "0 0 200 200" >
66- { pieSegments . map ( ( segment ) => (
67- < path
68- key = { segment . label }
69- d = { createPiePath ( segment . startAngle , segment . endAngle ) }
70- fill = { segment . color }
71- stroke = { colors . white }
72- strokeWidth = "2"
73- />
74- ) ) }
75- </ PieChartSVG >
76- </ Box >
77-
78- < Box className = "legend" >
79- { statsSourceChannels . map ( ( channel ) => (
80- < Box className = "legend-item" key = { channel . label } >
81- < LegendColor color = { sourceColors [ channel . label ] } />
82- < Typography className = "legend-label" >
83- { t ( `verificationRequest:${ channel . label } ` , {
84- defaultValue :
85- channel . label . charAt ( 0 ) . toUpperCase ( ) +
86- channel . label . slice ( 1 ) ,
87- } ) }
60+ return (
61+ < >
62+ < Typography className = "title" >
63+ { t ( "dashboard.sourcesTitle" ) }
8864 </ Typography >
89- < Typography className = "legend-percentage " >
90- { channel . percentage . toFixed ( 1 ) } %
65+ < Typography className = "subtitle " >
66+ { t ( "dashboard.sourcesSubtitle" ) }
9167 </ Typography >
92- </ Box >
93- ) ) }
94- </ Box >
95- </ >
96- ) ;
68+ < Box className = "PieChart-container" >
69+ < PieChartSVG width = "200" height = "200" viewBox = "0 0 200 200" >
70+ { pieSegments . map ( ( segment ) => (
71+ < path
72+ key = { segment . label }
73+ d = { createPiePath (
74+ segment . startAngle ,
75+ segment . endAngle
76+ ) }
77+ fill = { segment . color }
78+ stroke = { colors . white }
79+ strokeWidth = "2"
80+ />
81+ ) ) }
82+ </ PieChartSVG >
83+ </ Box >
84+
85+ < Box className = "legend" >
86+ { statsSourceChannels . map ( ( channel ) => (
87+ < Box className = "legend-item" key = { channel . label } >
88+ < LegendColor color = { sourceColors [ channel . label ] } />
89+ < Typography className = "legend-label" >
90+ { t ( `verificationRequest:${ channel . label } ` , {
91+ defaultValue :
92+ channel . label . charAt ( 0 ) . toUpperCase ( ) +
93+ channel . label . slice ( 1 ) ,
94+ } ) }
95+ </ Typography >
96+ < Typography className = "legend-percentage" >
97+ { channel . percentage . toFixed ( 1 ) } %
98+ </ Typography >
99+ </ Box >
100+ ) ) }
101+ </ Box >
102+ </ >
103+ ) ;
97104} ;
98105
99106export { SourceChannelDistribution } ;
0 commit comments