1- import React , { useState } from "react" ;
1+ "use client" ;
2+ import React , { useCallback , useMemo , useState } from "react" ;
23import styled from "styled-components" ;
34
5+ import { RULING_MODE } from "consts" ;
6+ import { useAccount , usePublicClient } from "wagmi" ;
7+
48import { Button } from "@kleros/ui-components-library" ;
59
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+
620import LabeledInput from "components/LabeledInput" ;
721
822import Header from "./Header" ;
23+ import { DEFAULT_CHAIN } from "consts/chains" ;
924
1025const Container = styled . div `
1126 width: 100%;
@@ -22,14 +37,75 @@ const SelectContainer = styled.div`
2237` ;
2338
2439const 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 ) ;
2746 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 ] ) ;
29102
30103 return (
31104 < 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+ />
33109 < SelectContainer >
34110 < LabeledInput
35111 label = "Dispute ID"
@@ -43,12 +119,16 @@ const ManualRuling: React.FC = () => {
43119 < LabeledInput
44120 label = "Overidden"
45121 inputType = "checkbox"
46- checked = { overriden }
47- onChange = { ( ) => setOverriden ( ( prev ) => ! prev ) }
122+ checked = { overridden }
123+ onChange = { ( ) => setOverridden ( ( prev ) => ! prev ) }
48124 />
49125 </ 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+ />
52132 </ Container >
53133 ) ;
54134} ;
0 commit comments