Skip to content

Commit 0e1e8e5

Browse files
feat(web-devtools): dynamic-custom-context-input
1 parent e950905 commit 0e1e8e5

File tree

4 files changed

+89
-24
lines changed

4 files changed

+89
-24
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import retrieveVariables from "@kleros/kleros-sdk/src/dataMappings/utils/retrieveVariables";
2+
import { Field } from "@kleros/ui-components-library";
3+
import { useMemo, useState } from "react";
4+
import styled from "styled-components";
5+
import { useDebounce } from "react-use";
6+
import WithHelpTooltip from "components/WithHelpTooltip";
7+
8+
const Container = styled.div`
9+
width: 100%;
10+
display: flex;
11+
flex-direction: column;
12+
gap: 16px;
13+
margin-top: 32px;
14+
`;
15+
16+
const Header = styled.h2`
17+
margin: 0;
18+
`;
19+
20+
const InputContainer = styled.div`
21+
display: flex;
22+
gap: 16px;
23+
flex-wrap: wrap;
24+
`;
25+
const VariableName = styled.p`
26+
font-family: "Roboto Mono", monospace;
27+
`;
28+
29+
// prevent duplicating input fields
30+
const DisputeRequestParams = [
31+
"arbitrator",
32+
"arbitrable",
33+
"arbitratorDisputeID",
34+
"externalDisputeID",
35+
"templateID",
36+
"templateUri",
37+
];
38+
39+
interface ICustomContextInputs {
40+
dataMapping: string;
41+
setCustomContext: (context: Record<string, string>) => void;
42+
}
43+
const CustomContextInputs: React.FC<ICustomContextInputs> = ({ dataMapping, setCustomContext }) => {
44+
const [customContext, setCustomContextInputs] = useState<Record<string, string>>();
45+
46+
const requiredVariables = useMemo(() => retrieveVariables(dataMapping), [dataMapping]);
47+
48+
useDebounce(
49+
() => {
50+
if (!customContext) return;
51+
setCustomContext(customContext);
52+
},
53+
300,
54+
[customContext]
55+
);
56+
57+
return requiredVariables.length ? (
58+
<Container>
59+
<WithHelpTooltip tooltipMsg="These are additional variables required by the data mapping to be passed as initial context. Please ignore the variables that will come from the result of the preceeding data mappings">
60+
<Header>Additional Context</Header>
61+
</WithHelpTooltip>
62+
{requiredVariables.map((variable, index) =>
63+
DisputeRequestParams.includes(variable) ? null : (
64+
<InputContainer id={`${variable}-${index}`}>
65+
<VariableName>{variable}:</VariableName>
66+
<Field
67+
type="text"
68+
name={variable}
69+
value={customContext?.[variable]}
70+
onChange={(e) => {
71+
setCustomContextInputs((prev) => ({ ...prev, [variable]: e.target.value }));
72+
}}
73+
placeholder="0x..."
74+
/>
75+
</InputContainer>
76+
)
77+
)}
78+
</Container>
79+
) : null;
80+
};
81+
82+
export default CustomContextInputs;

web-devtools/src/app/(main)/dispute-template/FetchDisputeRequestInput.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ const presets = [
5555
chainId: 421614,
5656
},
5757
{
58-
title: "Trump-Biden",
59-
txnHash: "0x83934c07f6476b2fd8c43cf856819134a58c007cb6938c6990fb3058b243ba0c",
58+
title: "Trump-Harris",
59+
txnHash: "0x86db91678cf3f8c4503e37340cf2cd93bffcba84f9c43a98c090f6a4c76d8793",
6060
chainId: 421614,
6161
},
6262
];

web-devtools/src/app/(main)/dispute-template/page.tsx

+5-21
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import ReactMarkdown from "components/ReactMarkdown";
2525

2626
import FetchDisputeRequestInput, { DisputeRequest } from "./FetchDisputeRequestInput";
2727
import FetchFromIDInput from "./FetchFromIdInput";
28+
import CustomContextInputs from "./CustomContextInputs";
2829

2930
const Container = styled.div`
3031
height: auto;
@@ -106,10 +107,6 @@ const UpperContainer = styled.div`
106107
)}
107108
`;
108109

109-
const StyledJSONEditor = styled(JSONEditor)`
110-
height: 300px;
111-
width: 100%;
112-
`;
113110
const StyledForm = styled.form`
114111
display: flex;
115112
flex-direction: column;
@@ -155,7 +152,7 @@ const DisputeTemplateView = () => {
155152
const [disputeDetails, setDisputeDetails] = useState<DisputeDetails | undefined>(undefined);
156153
const [disputeTemplateInput, setDisputeTemplateInput] = useState<string>("");
157154
const [dataMappingsInput, setDataMappingsInput] = useState<string>("");
158-
const [customContextInput, setCustomContextInput] = useState<string>("{}");
155+
const [customContext, setCustomContext] = useState<Record<string, string>>();
159156

160157
const [params, setParams] = useState<DisputeRequest>({
161158
_arbitrable: "0x10f7A6f42Af606553883415bc8862643A6e63fdA",
@@ -184,12 +181,6 @@ const DisputeTemplateView = () => {
184181
setLoading(true);
185182

186183
setTimeout(() => {
187-
let customContext = null;
188-
try {
189-
customContext = JSON.parse(customContextInput);
190-
} catch (error) {
191-
console.log("Error parsing custom context", error);
192-
}
193184
let initialContext = {
194185
arbitrator: debouncedParams._arbitrator,
195186
arbitrable: debouncedParams._arbitrable,
@@ -224,7 +215,7 @@ const DisputeTemplateView = () => {
224215
if (disputeTemplateInput || dataMappingsInput || debouncedParams) {
225216
scheduleFetchData();
226217
}
227-
}, [disputeTemplateInput, dataMappingsInput, debouncedParams, customContextInput]);
218+
}, [disputeTemplateInput, dataMappingsInput, debouncedParams, customContext]);
228219

229220
return (
230221
<>
@@ -291,18 +282,11 @@ const DisputeTemplateView = () => {
291282
name="_templateUri"
292283
value={params._templateUri}
293284
onChange={handleFormUpdate}
294-
placeholder="ipfs://... (optional)"
285+
placeholder="/ipfs/... (optional)"
295286
/>
296287
</StyledRow>
297288
<StyledRow>
298-
<StyledP>{"Custom Context :"}</StyledP>
299-
<StyledJSONEditor
300-
content={{ text: customContextInput }}
301-
mode={Mode.text}
302-
onChange={(val: any) => {
303-
setCustomContextInput(val.text);
304-
}}
305-
/>
289+
<CustomContextInputs dataMapping={dataMappingsInput} setCustomContext={setCustomContext} />
306290
</StyledRow>
307291
</StyledForm>
308292
<div>

web-devtools/tsconfig.json

-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
"./*"
3333
],
3434
"src*": [
35-
"../../kleros-sdk/src/*",
3635
"./*"
3736
],
3837
"svgs*": [

0 commit comments

Comments
 (0)