-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathIndexerConfigOptionsInputGroup.jsx
148 lines (134 loc) · 5.22 KB
/
IndexerConfigOptionsInputGroup.jsx
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
import React, { useContext, useState, useEffect } from "react";
import { InputGroup, Alert } from "react-bootstrap";
import Form from "react-bootstrap/Form";
import { IndexerDetailsContext } from '../../contexts/IndexerDetailsContext';
import { validateContractIds } from "../../utils/validators";
const GENESIS_BLOCK_HEIGHT = 9820210;
const START_BLOCK = {
CONTINUE: "startBlockContinue",
LATEST: "startBlockLatest",
HEIGHT: "startBlockHeight",
}
const IndexerConfigOptions = ({ updateConfig }) => {
const { indexerDetails, showPublishModal, isCreateNewIndexer, latestHeight } = useContext(IndexerDetailsContext);
const [blockHeight, setBlockHeight] = useState("0");
const [contractFilter, setContractFilter] = useState("social.near");
const [startBlock, setStartBlock] = useState(START_BLOCK.LATEST);
const [isContractFilterValid, setIsContractFilterValid] = useState(true);
const [indexerNameField, setIndexerNameField] = useState(indexerDetails.indexerName || "");
const [blockHeightError, setBlockHeightError] = useState(null)
useEffect(() => {
if (indexerDetails.rule?.affected_account_id) {
setContractFilter(indexerDetails.rule.affected_account_id)
}
if (indexerDetails.startBlock?.HEIGHT) {
setStartBlock(START_BLOCK.HEIGHT)
setBlockHeight(indexerDetails.startBlock.HEIGHT)
return;
}
if (indexerDetails.startBlock == "LATEST") {
setStartBlock(START_BLOCK.LATEST)
return;
}
if (indexerDetails.startBlock == "CONTINUE") {
setStartBlock(START_BLOCK.CONTINUE)
return;
}
}, [indexerDetails])
const onChangeStartBlock = (e) => {
setStartBlock(e.target.value)
if (e.target.value === START_BLOCK.CONTINUE) {
handleSetContractFilter(indexerDetails.rule.affected_account_id)
}
}
function handleSetContractFilter(contractFilter) {
setContractFilter(contractFilter);
const isContractFilterValid = validateContractIds(contractFilter);
setIsContractFilterValid(isContractFilterValid);
}
useEffect(() => {
if (startBlock == START_BLOCK.HEIGHT && blockHeight <= GENESIS_BLOCK_HEIGHT) {
setBlockHeightError(() => `Choose a block height greater than the Genesis BlockHeight ${GENESIS_BLOCK_HEIGHT}. Latest Block Height is ${latestHeight}`)
return
}
setBlockHeightError(() => null)
updateConfig(indexerNameField, contractFilter, blockHeight, startBlock)
},
[indexerNameField, contractFilter, startBlock, blockHeight]
)
return (
<>
<InputGroup size="sm" >
<InputGroup.Text> Indexer Name </InputGroup.Text>
<Form.Control
type="text"
placeholder="indexer_name"
aria-label="IndexerName"
value={indexerNameField}
disabled={!isCreateNewIndexer && showPublishModal}
onChange={(e) => setIndexerNameField(e.target.value.toLowerCase().trim())}
/>
</InputGroup>
<InputGroup size="sm" className="pt-3">
<InputGroup.Checkbox
value={START_BLOCK.LATEST}
checked={startBlock === START_BLOCK.LATEST}
onChange={onChangeStartBlock}
aria-label="Checkbox for following text input"
/>
<InputGroup.Text>Start from latest block</InputGroup.Text>
</InputGroup>
{!isCreateNewIndexer && (
<InputGroup size="sm" className="pt-3">
<InputGroup.Checkbox
value={START_BLOCK.CONTINUE}
checked={startBlock === START_BLOCK.CONTINUE}
onChange={onChangeStartBlock}
aria-label="Checkbox for following text input"
/>
<InputGroup.Text>Continue from last processed block</InputGroup.Text>
</InputGroup>
)}
<InputGroup size="sm" className="pt-3">
<InputGroup.Checkbox
value={START_BLOCK.HEIGHT}
checked={startBlock === START_BLOCK.HEIGHT}
onChange={onChangeStartBlock}
aria-label="Checkbox for following text input"
/>
<InputGroup.Text>Start from block height</InputGroup.Text>
<Form.Control
value={blockHeight}
onChange={(e) => setBlockHeight(parseInt(e.target.value))}
type="number"
/>
{blockHeightError && (
<Alert className="px-3 mt-3" variant="danger">
{blockHeightError}
</Alert>
)}
</InputGroup>
<InputGroup size="sm" hasValidation={true} className="pt-3">
<InputGroup.Text>Contract Filter</InputGroup.Text>
<Form.Control
value={startBlock === START_BLOCK.CONTINUE ? indexerDetails.rule.affected_account_id : contractFilter}
onChange={(e) => handleSetContractFilter(e.target.value)}
isValid={isContractFilterValid}
type="text"
placeholder="social.near"
required={true}
disabled={startBlock === START_BLOCK.CONTINUE}
/>
<Form.Control.Feedback type="invalid">
Please provide a valid contract name.
</Form.Control.Feedback>
{startBlock === START_BLOCK.CONTINUE && (
<Alert className="px-3 mt-3" variant="warning">
Contract filter cannot be changed for "Continue" option.
</Alert>
)}
</InputGroup>
</>
);
};
export default IndexerConfigOptions;