Skip to content

Commit 7a00ce6

Browse files
authoredOct 28, 2024··
Merge pull request #1716 from kleros/chore/update-dispute-template-presets
feat(web-devtools): custom-context-input
2 parents 7b2ccd3 + ff618c0 commit 7a00ce6

File tree

5 files changed

+105
-9
lines changed

5 files changed

+105
-9
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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 [customContextInputs, setCustomContextInputs] = useState<Record<string, string>>();
45+
46+
const requiredVariables = useMemo(() => {
47+
try {
48+
return retrieveVariables(dataMapping);
49+
} catch (error) {
50+
console.error("Failed to parse dataMapping:", error);
51+
return [];
52+
}
53+
}, [dataMapping]);
54+
55+
useDebounce(
56+
() => {
57+
if (!customContextInputs) return;
58+
setCustomContext(customContextInputs);
59+
},
60+
300,
61+
[customContextInputs]
62+
);
63+
64+
return requiredVariables.length ? (
65+
<Container>
66+
<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">
67+
<Header>Additional Context</Header>
68+
</WithHelpTooltip>
69+
{requiredVariables.map((variable, index) =>
70+
DisputeRequestParams.includes(variable) ? null : (
71+
<InputContainer key={`${variable}-${index}`}>
72+
<VariableName>{variable}:</VariableName>
73+
<Field
74+
type="text"
75+
name={variable}
76+
value={customContextInputs?.[variable]}
77+
onChange={(e) => {
78+
setCustomContextInputs((prev) => ({ ...prev, [variable]: e.target.value }));
79+
}}
80+
placeholder="0x..."
81+
/>
82+
</InputContainer>
83+
)
84+
)}
85+
</Container>
86+
) : null;
87+
};
88+
89+
export default CustomContextInputs;

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,17 @@ const StyledA = styled.a`
4646
const presets = [
4747
{
4848
title: "Escrow",
49-
txnHash: "0x85e60cb407c9a38e625263cc762ff4283d01f38201825e1d20109d8664cfa7d4",
49+
txnHash: "0x2565b756e500240544f7fc36f938462c7efbbd2e343c57979f81fecdf1054e23",
5050
chainId: 421614,
5151
},
5252
{
5353
title: "Curated Lists",
54-
txnHash: "0x6e5ad6f7436ef8570b50b0fbec76a11ccedbed85030c670e59d8f6617a499108",
54+
txnHash: "0xa7981830bf8144ab2070f3a639bd36b204c4c48ee1fafef66abaf60272418ed4",
5555
chainId: 421614,
5656
},
5757
{
58-
title: "Trump-Biden",
59-
txnHash: "0x9a3a420174f3c55c2b3eb2e77266777b74028b845e528a90142b5b57aafbdb90",
58+
title: "Trump-Harris",
59+
txnHash: "0x86db91678cf3f8c4503e37340cf2cd93bffcba84f9c43a98c090f6a4c76d8793",
6060
chainId: 421614,
6161
},
6262
];

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

+11-3
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;
@@ -105,6 +106,7 @@ const UpperContainer = styled.div`
105106
`
106107
)}
107108
`;
109+
108110
const StyledForm = styled.form`
109111
display: flex;
110112
flex-direction: column;
@@ -150,6 +152,7 @@ const DisputeTemplateView = () => {
150152
const [disputeDetails, setDisputeDetails] = useState<DisputeDetails | undefined>(undefined);
151153
const [disputeTemplateInput, setDisputeTemplateInput] = useState<string>("");
152154
const [dataMappingsInput, setDataMappingsInput] = useState<string>("");
155+
const [customContext, setCustomContext] = useState<Record<string, string>>();
153156

154157
const [params, setParams] = useState<DisputeRequest>({
155158
_arbitrable: "0x10f7A6f42Af606553883415bc8862643A6e63fdA",
@@ -178,7 +181,7 @@ const DisputeTemplateView = () => {
178181
setLoading(true);
179182

180183
setTimeout(() => {
181-
const initialContext = {
184+
let initialContext = {
182185
arbitrator: debouncedParams._arbitrator,
183186
arbitrable: debouncedParams._arbitrable,
184187
arbitratorDisputeID: debouncedParams._arbitratorDisputeID,
@@ -187,6 +190,8 @@ const DisputeTemplateView = () => {
187190
templateUri: debouncedParams._templateUri,
188191
};
189192

193+
if (customContext) initialContext = { ...initialContext, ...customContext };
194+
190195
const fetchData = async () => {
191196
try {
192197
const data = dataMappingsInput ? await executeActions(JSON.parse(dataMappingsInput), initialContext) : {};
@@ -210,7 +215,7 @@ const DisputeTemplateView = () => {
210215
if (disputeTemplateInput || dataMappingsInput || debouncedParams) {
211216
scheduleFetchData();
212217
}
213-
}, [disputeTemplateInput, dataMappingsInput, debouncedParams]);
218+
}, [disputeTemplateInput, dataMappingsInput, debouncedParams, customContext]);
214219

215220
return (
216221
<>
@@ -277,9 +282,12 @@ const DisputeTemplateView = () => {
277282
name="_templateUri"
278283
value={params._templateUri}
279284
onChange={handleFormUpdate}
280-
placeholder="ipfs://... (optional)"
285+
placeholder="/ipfs/... (optional)"
281286
/>
282287
</StyledRow>
288+
<StyledRow>
289+
<CustomContextInputs dataMapping={dataMappingsInput} setCustomContext={setCustomContext} />
290+
</StyledRow>
283291
</StyledForm>
284292
<div>
285293
<FetchFromIDInput

‎web-devtools/src/components/JSONEditor.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,6 @@ const JSONEditor = (props: any) => {
5959
}
6060
}, [props]);
6161

62-
return <Container ref={refContainer}></Container>;
62+
return <Container ref={refContainer} className={props.className}></Container>;
6363
};
6464
export default JSONEditor;

‎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)
Please sign in to comment.