1
- import React , { useState } from "react" ;
1
+ "use client" ;
2
+ import React , { useCallback , useMemo , useState } from "react" ;
2
3
import styled from "styled-components" ;
3
4
5
+ import { RULING_MODE } from "consts" ;
6
+ import { useAccount , usePublicClient } from "wagmi" ;
7
+
4
8
import { Button } from "@kleros/ui-components-library" ;
5
9
10
+ import { useRulerContext } from "context/RulerContext" ;
11
+ import {
12
+ useSimulateKlerosCoreRulerChangeRulingModeToManual ,
13
+ useSimulateKlerosCoreRulerExecuteRuling ,
14
+ useWriteKlerosCoreRulerChangeRulingModeToManual ,
15
+ useWriteKlerosCoreRulerExecuteRuling ,
16
+ } from "hooks/contracts/generated" ;
17
+ import { isUndefined } from "utils/isUndefined" ;
18
+ import { wrapWithToast } from "utils/wrapWithToast" ;
19
+
6
20
import LabeledInput from "components/LabeledInput" ;
7
21
8
22
import Header from "./Header" ;
23
+ import { DEFAULT_CHAIN } from "consts/chains" ;
9
24
10
25
const Container = styled . div `
11
26
width: 100%;
@@ -22,14 +37,75 @@ const SelectContainer = styled.div`
22
37
` ;
23
38
24
39
const ManualRuling : React . FC = ( ) => {
25
- const [ tie , setTie ] = useState < boolean > ( false ) ;
26
- const [ overriden , setOverriden ] = useState < boolean > ( false ) ;
40
+ const { isConnected, chainId } = useAccount ( ) ;
41
+ const { arbitrable, arbitrableSettings } = useRulerContext ( ) ;
42
+ const [ isSending , setIsSending ] = useState < boolean > ( false ) ;
43
+ const [ tie , setTie ] = useState ( arbitrableSettings ?. tied ?? false ) ;
44
+ const [ overridden , setOverridden ] = useState ( arbitrableSettings ?. overridden ?? false ) ;
45
+ const [ ruling , setRuling ] = useState ( arbitrableSettings ?. ruling ) ;
27
46
const [ disputeId , setDisputeId ] = useState < number > ( ) ;
28
- const [ ruling , setRuling ] = useState < number > ( ) ;
47
+
48
+ const publicClient = usePublicClient ( ) ;
49
+
50
+ const { data : manualModeConfig } = useSimulateKlerosCoreRulerChangeRulingModeToManual ( {
51
+ query : {
52
+ enabled : arbitrableSettings ?. rulingMode !== RULING_MODE . Manual && ! isUndefined ( arbitrable ) ,
53
+ } ,
54
+ args : [ arbitrable as `0x${string } `] ,
55
+ } ) ;
56
+ const { writeContractAsync : changeToManualMode } = useWriteKlerosCoreRulerChangeRulingModeToManual ( ) ;
57
+
58
+ const isDisabled = useMemo ( ( ) => {
59
+ return (
60
+ ! isConnected ||
61
+ chainId !== DEFAULT_CHAIN ||
62
+ isUndefined ( disputeId ) ||
63
+ isUndefined ( ruling ) ||
64
+ isUndefined ( arbitrable )
65
+ ) ;
66
+ } , [ disputeId , ruling , arbitrable , isConnected , chainId ] ) ;
67
+
68
+ const {
69
+ data : executeConfig ,
70
+ isLoading : isLoadingExecuteConfig ,
71
+ isError,
72
+ } = useSimulateKlerosCoreRulerExecuteRuling ( {
73
+ query : {
74
+ enabled : arbitrableSettings ?. rulingMode === RULING_MODE . Manual && ! isUndefined ( arbitrable ) && ! isDisabled ,
75
+ } ,
76
+ args : [ BigInt ( disputeId ?? 0 ) , BigInt ( ruling ?? 0 ) , tie , overridden ] ,
77
+ } ) ;
78
+
79
+ const { writeContractAsync : executeRuling } = useWriteKlerosCoreRulerExecuteRuling ( ) ;
80
+
81
+ const handleRuling = useCallback ( async ( ) => {
82
+ if ( ! publicClient ) return ;
83
+ if ( arbitrableSettings ?. rulingMode !== RULING_MODE . Manual ) {
84
+ if ( ! manualModeConfig ) return ;
85
+ setIsSending ( true ) ;
86
+
87
+ wrapWithToast ( async ( ) => await changeToManualMode ( manualModeConfig . request ) , publicClient )
88
+ . then ( async ( res ) => {
89
+ if ( res . status && executeConfig ) {
90
+ wrapWithToast ( async ( ) => await executeRuling ( executeConfig . request ) , publicClient ) ;
91
+ }
92
+ } )
93
+ . finally ( ( ) => setIsSending ( false ) ) ;
94
+ } else if ( executeConfig ) {
95
+ setIsSending ( true ) ;
96
+
97
+ wrapWithToast ( async ( ) => await executeRuling ( executeConfig . request ) , publicClient ) . finally ( ( ) =>
98
+ setIsSending ( false )
99
+ ) ;
100
+ }
101
+ } , [ publicClient , executeConfig , manualModeConfig , arbitrableSettings , changeToManualMode , executeRuling ] ) ;
29
102
30
103
return (
31
104
< Container >
32
- < Header text = "Manual Ruling" />
105
+ < Header
106
+ text = "Manual Ruling"
107
+ tooltipMsg = "Provide Manual ruling for the arbitrator. This operation will change the ruling mode to Manual, if the ruling mode is not Manual"
108
+ />
33
109
< SelectContainer >
34
110
< LabeledInput
35
111
label = "Dispute ID"
@@ -43,12 +119,16 @@ const ManualRuling: React.FC = () => {
43
119
< LabeledInput
44
120
label = "Overidden"
45
121
inputType = "checkbox"
46
- checked = { overriden }
47
- onChange = { ( ) => setOverriden ( ( prev ) => ! prev ) }
122
+ checked = { overridden }
123
+ onChange = { ( ) => setOverridden ( ( prev ) => ! prev ) }
48
124
/>
49
125
</ SelectContainer >
50
-
51
- < Button text = "Rule" />
126
+ < Button
127
+ text = "Rule"
128
+ onClick = { handleRuling }
129
+ isLoading = { isLoadingExecuteConfig || isSending }
130
+ disabled = { isDisabled || isError || isSending || isLoadingExecuteConfig }
131
+ />
52
132
</ Container >
53
133
) ;
54
134
} ;
0 commit comments