1
- import React from "react" ;
2
- import styled from "styled-components" ;
1
+ import React , { useMemo } from "react" ;
2
+ import { useParams } from "react-router-dom" ;
3
+ import styled , { useTheme } from "styled-components" ;
3
4
import { _TimelineItem1 , CustomTimeline } from "@kleros/ui-components-library" ;
4
5
import { Periods } from "consts/periods" ;
5
6
import { useVotingHistory } from "queries/useVotingHistory" ;
6
- import { DisputeDetailsQuery } from "queries/useDisputeDetailsQuery " ;
7
- import { lightTheme } from "~src/styles/themes " ;
7
+ import { useDisputeTemplate } from "queries/useDisputeTemplate " ;
8
+ import { DisputeDetailsQuery , useDisputeDetailsQuery } from "queries/useDisputeDetailsQuery " ;
8
9
import ClosedCaseIcon from "assets/svgs/icons/check-circle-outline.svg" ;
9
10
import AppealedCaseIcon from "assets/svgs/icons/close-circle.svg" ;
10
11
import CalendarIcon from "assets/svgs/icons/calendar.svg" ;
11
- import { isUndefined } from "utils/index" ;
12
+
12
13
const Container = styled . div `
13
14
display: flex;
14
15
position: relative;
15
- margin-left: 16px ;
16
+ margin-left: 8px ;
16
17
` ;
17
18
18
19
const StyledTimeline = styled ( CustomTimeline ) `
19
- width: calc(200px + (350 - 200) * (100vw - 375px) / (1250 - 375)) ;
20
+ width: 100% ;
20
21
margin-bottom: 32px;
21
22
` ;
22
23
@@ -40,12 +41,6 @@ const StyledCalendarIcon = styled(CalendarIcon)`
40
41
height: 14px;
41
42
` ;
42
43
43
- interface IDisputeTimeline {
44
- id : string ;
45
- disputeTemplate : any ;
46
- disputeDetails : DisputeDetailsQuery ;
47
- }
48
-
49
44
const getCaseEventTimes = (
50
45
lastPeriodChange : string ,
51
46
currentPeriodIndex : number ,
@@ -62,76 +57,77 @@ const getCaseEventTimes = (
62
57
return formattedDate ;
63
58
} ;
64
59
65
- const calculateLocalRoundJuror = ( votes ) => {
66
- const choiceCount = { } ;
67
- let maxVotes = 0 ;
68
- let winningChoice = null ;
69
-
70
- votes . forEach ( ( vote ) => {
71
- const { choice } = vote ;
72
-
73
- choiceCount [ choice ] = ( choiceCount [ choice ] || 0 ) + 1 ;
60
+ type TimelineItems = [ _TimelineItem1 , ..._TimelineItem1 [ ] ] ;
74
61
75
- if ( choiceCount [ choice ] > maxVotes ) {
76
- maxVotes = choiceCount [ choice ] ;
77
- winningChoice = choice ;
78
- }
79
- } ) ;
80
-
81
- return winningChoice ;
82
- } ;
83
-
84
- const DisputeTimeline : React . FC < IDisputeTimeline > = ( { id, disputeTemplate, disputeDetails } ) => {
62
+ const useItems = ( disputeDetails ?: DisputeDetailsQuery ) => {
63
+ const { data : disputeTemplate } = useDisputeTemplate ( ) ;
64
+ const { id } = useParams ( ) ;
85
65
const { data : votingHistory } = useVotingHistory ( id ) ;
86
- const currentPeriodIndex = Periods [ disputeDetails ?. dispute ?. period ! ] ;
87
- const lastPeriodChange = disputeDetails ?. dispute ?. lastPeriodChange ;
88
- const courtTimePeriods = disputeDetails . dispute ?. court . timesPerPeriod ! ;
89
- const rounds = votingHistory ?. dispute ?. rounds ;
90
66
const localRounds = votingHistory ?. dispute ?. disputeKitDispute ?. localRounds ;
67
+ const theme = useTheme ( ) ;
68
+
69
+ return useMemo < TimelineItems | undefined > ( ( ) => {
70
+ if ( disputeDetails ?. dispute ) {
71
+ const currentPeriodIndex = disputeDetails ?. dispute ? Periods [ disputeDetails . dispute . period ] : 0 ;
72
+ const lastPeriodChange = disputeDetails ?. dispute ?. lastPeriodChange ;
73
+ const courtTimePeriods = disputeDetails . dispute ?. court . timesPerPeriod ;
74
+ return localRounds ?. reduce < TimelineItems > (
75
+ ( acc , { winningChoice } , index ) => {
76
+ const parsedWinningChoice = parseInt ( winningChoice ) ;
77
+ const eventDate = getCaseEventTimes ( lastPeriodChange , currentPeriodIndex , courtTimePeriods , false ) ;
78
+ const icon = disputeDetails ?. dispute ?. ruled && index === localRounds . length - 1 ? ClosedCaseIcon : "" ;
79
+
80
+ acc . push ( {
81
+ title : `Jury Decision - Round ${ index + 1 } ` ,
82
+ party :
83
+ parsedWinningChoice !== 0
84
+ ? disputeTemplate ?. answers ?. [ parseInt ( winningChoice ) - 1 ] . title
85
+ : "Refuse to Arbitrate" ,
86
+ subtitle : eventDate ,
87
+ rightSided : true ,
88
+ variant : theme . secondaryPurple ,
89
+ Icon : icon !== "" ? icon : undefined ,
90
+ } ) ;
91
+
92
+ if ( index < localRounds . length - 1 ) {
93
+ acc . push ( {
94
+ title : "Appealed" ,
95
+ party : "" ,
96
+ subtitle : eventDate ,
97
+ rightSided : true ,
98
+ Icon : AppealedCaseIcon ,
99
+ } ) ;
100
+ }
101
+
102
+ return acc ;
103
+ } ,
104
+ [
105
+ {
106
+ title : "Dispute created" ,
107
+ party : "" ,
108
+ subtitle : getCaseEventTimes ( lastPeriodChange , currentPeriodIndex , courtTimePeriods , true ) ,
109
+ rightSided : true ,
110
+ variant : theme . secondaryPurple ,
111
+ } ,
112
+ ]
113
+ ) ;
114
+ }
115
+ return ;
116
+ } , [ disputeDetails , disputeTemplate , localRounds , theme ] ) ;
117
+ } ;
91
118
92
- const dynamicItems : [ _TimelineItem1 , ..._TimelineItem1 [ ] ] = [
93
- {
94
- title : "Dispute created" ,
95
- party : "" ,
96
- subtitle : getCaseEventTimes ( lastPeriodChange , currentPeriodIndex , courtTimePeriods , true ) ,
97
- rightSided : true ,
98
- variant : lightTheme . secondaryPurple ,
99
- } ,
100
- ] ;
101
-
102
- if ( rounds ) {
103
- rounds . forEach ( ( round , index ) => {
104
- const localRuling = calculateLocalRoundJuror ( ! isUndefined ( localRounds ) && localRounds [ index ] . votes ) ;
105
- const eventDate = getCaseEventTimes ( lastPeriodChange , currentPeriodIndex , courtTimePeriods , false ) ;
106
- const variant = disputeDetails ?. dispute ?. ruled && index === rounds . length - 1 ? ClosedCaseIcon : "" ;
107
-
108
- dynamicItems . push ( {
109
- title : `Jury Decision - Round ${ index + 1 } ` ,
110
- party : localRuling ? disputeTemplate ?. answers ?. [ localRuling - 1 ] . title : "Refuse to Arbitrate" ,
111
- subtitle : eventDate ,
112
- rightSided : true ,
113
- variant : lightTheme . secondaryPurple ,
114
- Icon : variant !== "" ? variant : undefined ,
115
- } ) ;
119
+ const DisputeTimeline : React . FC = ( ) => {
120
+ const { id } = useParams ( ) ;
121
+ const { data : disputeDetails } = useDisputeDetailsQuery ( id ) ;
122
+ const items = useItems ( disputeDetails ) ;
116
123
117
- if ( index < rounds . length - 1 ) {
118
- dynamicItems . push ( {
119
- title : "Appealed" ,
120
- party : "" ,
121
- subtitle : eventDate ,
122
- rightSided : true ,
123
- Icon : AppealedCaseIcon ,
124
- } ) ;
125
- }
126
- } ) ;
127
- }
128
124
return (
129
125
< Container >
130
- < StyledTimeline items = { dynamicItems } />
131
- { disputeDetails ?. dispute ?. ruled && (
126
+ { items && < StyledTimeline { ... { items } } /> }
127
+ { disputeDetails ?. dispute ?. ruled && items && (
132
128
< EnforcementContainer >
133
129
< StyledCalendarIcon />
134
- < small > Enforcement: { dynamicItems [ dynamicItems . length - 1 ] . subtitle } </ small >
130
+ < small > Enforcement: { items . at ( - 1 ) ? .subtitle } </ small >
135
131
</ EnforcementContainer >
136
132
) }
137
133
</ Container >
0 commit comments