-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathSelectArbitrable.tsx
165 lines (145 loc) · 4.26 KB
/
SelectArbitrable.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import React, { useCallback, useEffect, useMemo, useRef, useState } from "react";
import styled, { css } from "styled-components";
import { type Address, type PublicClient } from "viem";
import { usePublicClient } from "wagmi";
import { Copiable, DropdownSelect, Field } from "@kleros/ui-components-library";
import { DEFAULT_CHAIN } from "consts/chains";
import { useRulerContext } from "context/RulerContext";
import { klerosCoreAddress } from "hooks/contracts/generated";
import { shortenAddress } from "utils/shortenAddress";
import { validateAddress } from "utils/validateAddressOrEns";
import { landscapeStyle } from "styles/landscapeStyle";
const Container = styled.div`
width: 100%;
display: flex;
flex-wrap: wrap;
justify-content: space-around;
gap: 16px;
align-items: center;
margin: 16px 0;
padding: 8px 16px;
border-radius: 3px;
background-color: ${({ theme }) => theme.klerosUIComponentsWhiteBackground};
`;
const AddressContainer = styled.div`
display: flex;
gap: 8px;
flex-wrap: wrap;
align-items: center;
justify-content: center;
`;
const Arbitrables = styled.div`
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: center;
align-items: center;
gap: 8px;
`;
const StyledLabel = styled.label``;
const SelectContainer = styled.div`
position: relative;
`;
const StyledField = styled(Field)`
width: auto;
${landscapeStyle(
() => css`
min-width: 250px;
`
)}
`;
const StyledDropdown = styled(DropdownSelect)`
position: absolute;
z-index: 0;
top: 40px;
left: 0;
width: 100%;
> button {
display: none;
}
> div {
z-index: 1;
width: 100%;
> div {
width: 100%;
}
}
.simplebar-content {
> div {
width: 100%;
}
}
`;
const ErrorMessage = styled.div`
color: red;
font-size: 14px;
margin-top: 4px;
`;
const SelectArbitrable: React.FC = () => {
const { arbitrable, setArbitrable, knownArbitrables } = useRulerContext();
const publicClient = usePublicClient({ chainId: 1 }) as PublicClient;
const ref = useRef<HTMLDivElement>(null);
const [isClient, setIsClient] = useState(false);
const [inputValue, setInputValue] = useState("");
const [error, setError] = useState<string | null>(null);
// hydration workaround, local storage is inevitably going to be different, so knownArbitrables will be different
// server and client side
useEffect(() => {
setIsClient(true);
}, []);
const items = useMemo(
() =>
!isClient ? [] : knownArbitrables.map((arbitrable) => ({ text: shortenAddress(arbitrable), value: arbitrable })),
[isClient, knownArbitrables]
);
const openDropdown = useCallback(() => {
if (!ref.current || knownArbitrables.length === 0) return;
const child = ref.current.firstElementChild?.firstChild as HTMLButtonElement;
child.click();
}, [knownArbitrables, ref]);
const handleInputChange = useCallback(
async (value: string) => {
setInputValue(value);
setError(null);
if (value) {
const isValid = await validateAddress(value, publicClient);
if (isValid) {
setArbitrable(value as Address);
} else {
setError("Invalid address or ENS name");
}
} else {
setArbitrable("" as Address);
}
},
[publicClient, setArbitrable]
);
return (
<Container>
<AddressContainer>
<StyledLabel>Ruler Address:</StyledLabel>
<Copiable copiableContent={klerosCoreAddress[DEFAULT_CHAIN]} info="Ruler Address">
<StyledLabel>{shortenAddress(klerosCoreAddress[DEFAULT_CHAIN])}</StyledLabel>
</Copiable>
</AddressContainer>
<Arbitrables suppressHydrationWarning={true}>
<StyledLabel>Arbitrable:</StyledLabel>
<SelectContainer ref={ref}>
<StyledDropdown
defaultValue={arbitrable}
items={items}
callback={(val) => handleInputChange(val.toString())}
/>
<StyledField
value={inputValue}
placeholder="Enter Arbitrable"
onChange={(e) => handleInputChange(e.target.value)}
onClick={openDropdown}
/>
{error && <ErrorMessage>{error}</ErrorMessage>}
</SelectContainer>
</Arbitrables>
</Container>
);
};
export default SelectArbitrable;