Skip to content

Commit ea2356e

Browse files
authored
Merge branch 'dev' into feat(web)/lazy-loading
2 parents f7ff395 + a8e8699 commit ea2356e

File tree

15 files changed

+104
-99
lines changed

15 files changed

+104
-99
lines changed

kleros-sdk/src/dataMappings/actions/jsonAction.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ import { JsonMapping } from "../utils/actionTypes";
22
import { createResultObject } from "src/dataMappings/utils/createResultObject";
33

44
export const jsonAction = (mapping: JsonMapping) => {
5-
const { value: source, seek, populate } = mapping;
6-
return createResultObject(source, seek, populate);
5+
const { value, seek, populate } = mapping;
6+
7+
// Parse the source if it's a JSON string
8+
const parsedValue = typeof value === "string" ? JSON.parse(value) : value;
9+
10+
return createResultObject(parsedValue, seek, populate);
711
};

kleros-sdk/src/dataMappings/executeActions.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export const executeActions = async (mappings, initialContext = {}) => {
4242

4343
for (const mapping of mappings) {
4444
const actionResult = await executeAction(mapping, context);
45+
4546
if (actionResult) {
4647
Object.keys(actionResult).forEach((key) => {
4748
context[key] = actionResult[key];
Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,22 @@
1-
// Can this be replaced by Mustache ?
2-
export const createResultObject = (sourceData, seek, populate) => {
1+
export const createResultObject = (sourceData, seek: string[], populate: string[]) => {
32
const result = {};
43

5-
seek.forEach((key, idx) => {
6-
let foundValue = sourceData;
7-
8-
if (key.includes(".")) {
9-
const keyParts = key.split(".");
10-
for (const part of keyParts) {
11-
if (foundValue[part] !== undefined) {
12-
foundValue = foundValue[part];
13-
} else {
14-
foundValue = undefined;
15-
break;
16-
}
17-
}
18-
} else {
19-
if (typeof sourceData !== "object" || key === "0") {
20-
foundValue = sourceData;
21-
} else {
22-
foundValue = sourceData[key];
4+
const getNestedValue = (obj: any, path: string) => {
5+
return path.split(".").reduce((acc, part) => {
6+
if (acc && part.includes("[")) {
7+
const [key, index] = part.replace(/\]/g, "").split("[");
8+
return acc[key]?.[index];
239
}
24-
}
10+
return acc ? acc[part] : undefined;
11+
}, obj);
12+
};
2513

26-
console.log(`Seek key: ${key}, Found value:`, foundValue);
14+
seek.forEach((key, idx) => {
15+
const foundValue = getNestedValue(sourceData, key);
2716
if (foundValue !== undefined) {
2817
result[populate[idx]] = foundValue;
29-
console.log(`Populate key: ${populate[idx]}, Value to add:`, foundValue);
3018
}
3119
});
32-
console.log("Result object:", result);
20+
3321
return result;
3422
};
Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1-
export const replacePlaceholdersWithValues = (mapping: any, context: any) => {
2-
let mappingAsString = JSON.stringify(mapping);
1+
import mustache from "mustache";
32

4-
const replacedMapping = mappingAsString.replace(/\{\{(\w+)\}\}/g, (_, variableName) => {
5-
if (context.hasOwnProperty(variableName)) {
6-
return context[variableName];
3+
export const replacePlaceholdersWithValues = (mapping: any, context: any) => {
4+
const replace = (obj) => {
5+
if (typeof obj === "string") {
6+
return mustache.render(obj, context);
7+
} else if (Array.isArray(obj)) {
8+
return obj.map(replace);
9+
} else if (typeof obj === "object" && obj !== null) {
10+
return Object.fromEntries(Object.entries(obj).map(([key, value]) => [key, replace(value)]));
711
} else {
8-
throw new Error(`Variable '${variableName}' not found in context.`);
12+
return obj;
913
}
10-
});
14+
};
1115

12-
return JSON.parse(replacedMapping);
16+
return replace(mapping);
1317
};

web/src/components/DisputePreview/Alias.tsx

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ import Skeleton from "react-loading-skeleton";
55
import { isAddress } from "viem";
66
import { useEnsAddress } from "wagmi";
77

8-
import { Alias } from "context/NewDisputeContext";
9-
import { isUndefined } from "utils/index";
10-
118
import { AddressOrName, IdenticonOrAvatar } from "../ConnectWallet/AccountDisplay";
129

1310
const AliasContainer = styled.div`
@@ -26,28 +23,29 @@ const TextContainer = styled.div`
2623
`;
2724

2825
interface IAlias {
29-
alias: Alias;
26+
name: string;
27+
address: `0x${string}`;
3028
}
3129

32-
const AliasDisplay: React.FC<IAlias> = ({ alias }) => {
30+
const AliasDisplay: React.FC<IAlias> = ({ name, address }) => {
3331
const { data: addressFromENS, isLoading } = useEnsAddress({
3432
query: {
3533
// if alias.address is not an Address, we treat it as ENS and try to fetch address from there
36-
enabled: !isAddress(alias.address),
34+
enabled: !isAddress(address),
3735
},
38-
name: alias.address,
36+
name: address,
3937
chainId: 1,
4038
});
4139

4240
// try fetching ens name, else go with address
43-
const address = addressFromENS ?? (alias.address as `0x${string}`);
41+
const resolvedAddress = addressFromENS ?? (address as `0x${string}`);
4442

4543
return (
4644
<AliasContainer>
47-
{isLoading ? <Skeleton width={30} height={24} /> : <IdenticonOrAvatar address={address} size="24" />}
45+
{isLoading ? <Skeleton width={30} height={24} /> : <IdenticonOrAvatar address={resolvedAddress} size="24" />}
4846
<TextContainer>
49-
{isLoading ? <Skeleton width={30} height={24} /> : <AddressOrName address={address} />}&nbsp;
50-
{!isUndefined(alias.name) && alias.name !== "" ? <label>({alias.name})</label> : null}
47+
{isLoading ? <Skeleton width={30} height={24} /> : <AddressOrName address={resolvedAddress} />}&nbsp;
48+
<label>({name})</label>
5149
</TextContainer>
5250
</AliasContainer>
5351
);

web/src/components/DisputePreview/DisputeContext.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ export const DisputeContext: React.FC<IDisputeContext> = ({ disputeDetails, isRp
103103
<>
104104
<Divider />
105105
<AliasesContainer>
106-
{disputeDetails.aliases.map((alias) => (
107-
<AliasDisplay alias={alias} key={alias.address} />
106+
{Object.keys(disputeDetails.aliases).map((key) => (
107+
<AliasDisplay name={key} key={key} address={disputeDetails.aliases[key]} />
108108
))}
109109
</AliasesContainer>
110110
</>

web/src/components/EnsureChain.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from "react";
22

3-
import { useChainId } from "wagmi";
3+
import { useAccount } from "wagmi";
44

55
import { DEFAULT_CHAIN } from "consts/chains";
66

@@ -12,7 +12,7 @@ interface IEnsureChain {
1212
}
1313

1414
export const EnsureChain: React.FC<IEnsureChain> = ({ children, className }) => {
15-
const chainId = useChainId();
15+
const { chainId } = useAccount();
1616

1717
return chainId === DEFAULT_CHAIN ? children : <ConnectWallet {...{ className }} />;
1818
};

web/src/components/Field.tsx

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,6 @@ const FieldContainer = styled.div<FieldContainerProps>`
2323
width: 14px;
2424
}
2525
26-
.link {
27-
color: ${({ theme }) => theme.primaryBlue};
28-
:hover {
29-
cursor: pointer;
30-
}
31-
}
3226
${({ isList }) =>
3327
isList &&
3428
css`
@@ -62,6 +56,12 @@ const FieldContainer = styled.div<FieldContainerProps>`
6256
`};
6357
`;
6458

59+
const LinkContainer = styled.div``;
60+
61+
const StyledLink = styled(Link)`
62+
color: ${({ theme }) => theme.primaryBlue};
63+
`;
64+
6565
type FieldContainerProps = {
6666
width?: string;
6767
isList?: boolean;
@@ -97,15 +97,16 @@ const Field: React.FC<IField> = ({
9797
<Icon />
9898
{(!displayAsList || isOverview || isJurorBalance) && <label>{name}:</label>}
9999
{link ? (
100-
<Link
101-
className="link value"
102-
to={link}
103-
onClick={(event) => {
104-
event.stopPropagation();
105-
}}
106-
>
107-
{value}
108-
</Link>
100+
<LinkContainer className="value">
101+
<StyledLink
102+
to={link}
103+
onClick={(event) => {
104+
event.stopPropagation();
105+
}}
106+
>
107+
{value}
108+
</StyledLink>
109+
</LinkContainer>
109110
) : (
110111
<label className="value">{value}</label>
111112
)}

web/src/context/NewDisputeContext.tsx

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@ export type Answer = {
1212
reserved?: boolean;
1313
};
1414

15-
export type Alias = {
15+
export type AliasArray = {
1616
id?: string;
1717
name: string;
1818
address: string | Address;
1919
isValid?: boolean;
2020
};
2121

22+
export type Alias = Record<string, string>;
2223
export interface IDisputeTemplate {
2324
answers: Answer[];
24-
aliases?: Alias[];
2525
arbitrableAddress?: string;
2626
arbitrableChainID?: string;
2727
arbitratorAddress?: string;
@@ -34,6 +34,7 @@ export interface IDisputeTemplate {
3434
question: string;
3535
specification?: string;
3636
title: string;
37+
aliases?: Alias;
3738
// attachment: Attachment;
3839
// type: string;
3940
}
@@ -42,6 +43,7 @@ interface IDisputeData extends IDisputeTemplate {
4243
courtId?: string;
4344
numberOfJurors: number;
4445
arbitrationCost?: string;
46+
aliasesArray?: AliasArray[];
4547
}
4648

4749
interface INewDisputeContext {
@@ -64,7 +66,7 @@ const initialDisputeData: IDisputeData = {
6466
{ title: "", id: "1" },
6567
{ title: "", id: "2" },
6668
],
67-
aliases: [
69+
aliasesArray: [
6870
{ name: "", address: "", id: "1" },
6971
{ name: "", address: "", id: "2" },
7072
],
@@ -113,13 +115,22 @@ export const NewDisputeProvider: React.FC<{ children: React.ReactNode }> = ({ ch
113115
};
114116

115117
const constructDisputeTemplate = (disputeData: IDisputeData) => {
116-
const baseTemplate = { ...disputeData } as IDisputeTemplate;
118+
const baseTemplate = { ...disputeData };
117119

118-
if (!isUndefined(baseTemplate.aliases)) {
119-
baseTemplate.aliases = baseTemplate.aliases.filter((item) => item.address !== "" && item.isValid);
120-
if (baseTemplate.aliases.length === 0) delete baseTemplate.aliases;
120+
if (!isUndefined(baseTemplate.aliasesArray)) {
121+
baseTemplate.aliasesArray = baseTemplate.aliasesArray.filter((item) => item.address !== "" && item.isValid);
122+
if (baseTemplate.aliasesArray.length === 0) delete baseTemplate.aliasesArray;
123+
else {
124+
const aliases: Alias = {};
125+
126+
for (const alias of baseTemplate.aliasesArray) {
127+
aliases[alias.name] = alias.address;
128+
}
129+
130+
baseTemplate.aliases = aliases;
131+
}
121132
}
122133
if (!isUndefined(baseTemplate.policyURI) && baseTemplate.policyURI === "") delete baseTemplate.policyURI;
123134

124-
return baseTemplate;
135+
return baseTemplate as IDisputeTemplate;
125136
};

web/src/hooks/queries/usePopulatedDisputeData.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { populateTemplate } from "@kleros/kleros-sdk/src/dataMappings/utils/popu
99
import { GENESIS_BLOCK_ARBSEPOLIA } from "consts/index";
1010
import { useGraphqlBatcher } from "context/GraphqlBatcher";
1111
import { iArbitrableV2Abi } from "hooks/contracts/generated";
12+
import { useEvidenceGroup } from "queries/useEvidenceGroup";
1213
import { debounceErrorToast } from "utils/debounceErrorToast";
1314
import { isUndefined } from "utils/index";
1415

@@ -32,6 +33,7 @@ export const usePopulatedDisputeData = (disputeID?: string, arbitrableAddress?:
3233
const { data: crossChainData, isError } = useIsCrossChainDispute(disputeID, arbitrableAddress);
3334
const isEnabled = !isUndefined(disputeID) && !isUndefined(crossChainData) && !isUndefined(arbitrableAddress);
3435
const { graphqlBatcher } = useGraphqlBatcher();
36+
const { data: externalDisputeID } = useEvidenceGroup(disputeID, arbitrableAddress);
3537

3638
return useQuery<DisputeDetails>({
3739
queryKey: [`DisputeTemplate${disputeID}${arbitrableAddress}`],
@@ -59,6 +61,8 @@ export const usePopulatedDisputeData = (disputeID?: string, arbitrableAddress?:
5961
const initialContext = {
6062
disputeID: disputeID,
6163
arbitrable: arbitrableAddress,
64+
graphApiKey: import.meta.env.REACT_APP_GRAPH_API_KEY,
65+
externalDisputeID: externalDisputeID,
6266
};
6367

6468
const data = dataMappings ? await executeActions(JSON.parse(dataMappings), initialContext) : {};

0 commit comments

Comments
 (0)