From 75c35f9b7b46290a991cfa12093334082a863952 Mon Sep 17 00:00:00 2001 From: Roshaan Siddiqui Date: Thu, 13 Jul 2023 14:47:50 -0500 Subject: [PATCH 1/2] feat: allow to edit indexer when forking --- frontend/src/components/Editor/Editor.js | 32 ++++---- .../src/components/Editor/EditorButtons.jsx | 23 ++++-- .../Form/IndexerConfigOptionsInputGroup.jsx | 2 +- .../components/Modals/ForkIndexerModal.jsx | 75 ++++++++++++------- 4 files changed, 83 insertions(+), 49 deletions(-) diff --git a/frontend/src/components/Editor/Editor.js b/frontend/src/components/Editor/Editor.js index 18c133c4d..5d0dd3545 100644 --- a/frontend/src/components/Editor/Editor.js +++ b/frontend/src/components/Editor/Editor.js @@ -34,6 +34,7 @@ const Editor = ({ debugMode, isCreateNewIndexer, indexerNameField, + setAccountId, } = useContext(IndexerDetailsContext); const DEBUG_LIST_STORAGE_KEY = `QueryAPI:debugList:${indexerDetails.accountId}#${indexerDetails.indexerName}` @@ -127,10 +128,23 @@ const Editor = ({ } }; + + const forkIndexer = async(indexerName) => { + let code = indexingCode; + + setAccountId(currentUserAccountId) + + let prevAccountId = indexerDetails.accountId.replaceAll(".", "_"); + let newAccountId = currentUserAccountId.replaceAll(".", "_"); + let prevIndexerName = indexerDetails.indexerName.replaceAll("-", "_").trim().toLowerCase(); + let newIndexerName = indexerName.replaceAll("-", "_").trim().toLowerCase(); + code = code.replaceAll(prevAccountId, newAccountId); + code = code.replaceAll(prevIndexerName, newIndexerName); + setIndexingCode(formatIndexingCode(code)) + } + const registerFunction = async (indexerName, indexerConfig) => { let formatted_schema = checkSQLSchemaFormatting(); - let isForking = indexerDetails.accountId !== currentUserAccountId; - let innerCode = indexingCode.match(/getBlock\s*\([^)]*\)\s*{([\s\S]*)}/)[1]; indexerName = indexerName.replaceAll(" ", "_"); if (formatted_schema == undefined) { @@ -140,14 +154,6 @@ const Editor = ({ ); return; } - - if (isForking) { - let prevAccountName = indexerDetails.accountId.replace(".", "_"); - let newAccountName = currentUserAccountId.replace(".", "_"); - - innerCode = innerCode.replaceAll(prevAccountName, newAccountName); - } - setError(() => undefined); request("register-function", { @@ -170,8 +176,8 @@ const Editor = ({ const handleReload = async () => { if (isCreateNewIndexer) { setShowResetCodeModel(false); - setIndexingCode(defaultCode); - setSchema(defaultSchema); + setIndexingCode((formatIndexingCode(indexerDetails.code))); + setSchema(formatSQL(indexerDetails.schema)) return; } @@ -331,7 +337,7 @@ const Editor = ({ blockHeightError={blockHeightError} />
{accountId} - {!isCreateNewIndexer && ( + {indexerName && ( {indexerName} @@ -169,21 +169,33 @@ const EditorButtons = ({ - {currentUserAccountId && ( + {(!isUserIndexer && !isCreateNewIndexer) ? ( {getActionButtonText()}} + overlay={Fork Indexer} + > + + + ) : ( + Publish} > )} - @@ -208,6 +220,5 @@ const EditorButtons = ({ ); }; - export default EditorButtons; diff --git a/frontend/src/components/Form/IndexerConfigOptionsInputGroup.jsx b/frontend/src/components/Form/IndexerConfigOptionsInputGroup.jsx index ca84bd625..8804242fa 100644 --- a/frontend/src/components/Form/IndexerConfigOptionsInputGroup.jsx +++ b/frontend/src/components/Form/IndexerConfigOptionsInputGroup.jsx @@ -51,7 +51,7 @@ const IndexerConfigOptions = ({ updateConfig }) => { Indexer Name { +export const ForkIndexerModal = ({ registerFunction, forkIndexer }) => { const { indexerDetails, showForkIndexerModal, setShowForkIndexerModal, + setIsCreateNewIndexer, + setIndexerName, + setIndexerConfig, + isCreateNewIndexer, } = useContext(IndexerDetailsContext); - const [indexerConfig, setIndexerConfig] = useState({ filter: "social.near", startBlockHeight: null }) - const [indexerName, setIndexerName] = useState("") - const [error, setError] = useState(null) + const [indexerConfig, setIndexerConfigField] = useState({ + filter: "social.near", + startBlockHeight: null, + }); + const [indexerName, setIndexerNameField] = useState(""); + const [error, setError] = useState(null); - const updateConfig = (indexerName, filter, startBlockHeight, option) => { - let finalStartBlockHeight = option === "latestBlockHeight" ? null : startBlockHeight; - setIndexerConfig({ filter, startBlockHeight: finalStartBlockHeight }) - setIndexerName(indexerName) - } - const register = async () => { + + const fork = async () => { if (!indexerName) { - setError("Please provide an Indexer Name") - return + setError("Please provide an Indexer Name"); + return; } if (indexerName === indexerDetails.indexerName) { - setError("Please provide a different Indexer Name than the orginal Indexer") - return + setError( + "Please provide a different Indexer Name than the orginal Indexer" + ); + return; } - if (!validateContractId(indexerConfig.filter)) { - setError("Please provide a valid contract name") - return + setError("Please provide a valid contract name"); + return; } - setError(null) - registerFunction(indexerName, indexerConfig) - setShowForkIndexerModal(false) - } + setError(null); + setIndexerName(indexerName); + setIsCreateNewIndexer(true); + forkIndexer(indexerName, indexerConfig); + setShowForkIndexerModal(false); + }; + // return ( Enter Indexer Details - + + Indexer Name + setIndexerNameField(e.target.value.trim().toLowerCase())} + /> + {error && ( {error} @@ -60,11 +74,14 @@ export const ForkIndexerModal = ({ )} - - From 0a87c56bafea9978e6afdc340d9e5ebfc187e225 Mon Sep 17 00:00:00 2001 From: Roshaan Siddiqui Date: Fri, 14 Jul 2023 11:53:50 -0500 Subject: [PATCH 2/2] chore: clean up --- frontend/src/components/Editor/Editor.js | 24 ------------------- .../components/Modals/ForkIndexerModal.jsx | 12 +--------- 2 files changed, 1 insertion(+), 35 deletions(-) diff --git a/frontend/src/components/Editor/Editor.js b/frontend/src/components/Editor/Editor.js index 5d0dd3545..4d217e6b6 100644 --- a/frontend/src/components/Editor/Editor.js +++ b/frontend/src/components/Editor/Editor.js @@ -91,28 +91,6 @@ const Editor = ({ localStorage.setItem(DEBUG_LIST_STORAGE_KEY, heights); }, [heights]); - // useEffect(() => { - // if (selectedOption == "latestBlockHeight") { - // setBlockHeightError(null); - // return; - // } - // - // if (height - blockHeight > BLOCKHEIGHT_LIMIT) { - // setBlockHeightError( - // `Warning: Please enter a valid start block height. At the moment we only support historical indexing of the last ${BLOCKHEIGHT_LIMIT} blocks or ${BLOCKHEIGHT_LIMIT / 3600 - // } hrs. Choose a start block height between ${height - BLOCKHEIGHT_LIMIT - // } - ${height}.` - // ); - // } else if (blockHeight > height) { - // setBlockHeightError( - // `Warning: Start Block Hieght can not be in the future. Please choose a value between ${height - BLOCKHEIGHT_LIMIT - // } - ${height}.` - // ); - // } else { - // setBlockHeightError(null); - // } - // }, [blockHeight, height, selectedOption]); - const checkSQLSchemaFormatting = () => { try { let formatted_sql = formatSQL(schema); @@ -131,9 +109,7 @@ const Editor = ({ const forkIndexer = async(indexerName) => { let code = indexingCode; - setAccountId(currentUserAccountId) - let prevAccountId = indexerDetails.accountId.replaceAll(".", "_"); let newAccountId = currentUserAccountId.replaceAll(".", "_"); let prevIndexerName = indexerDetails.indexerName.replaceAll("-", "_").trim().toLowerCase(); diff --git a/frontend/src/components/Modals/ForkIndexerModal.jsx b/frontend/src/components/Modals/ForkIndexerModal.jsx index 8de767afb..71b7cb37b 100644 --- a/frontend/src/components/Modals/ForkIndexerModal.jsx +++ b/frontend/src/components/Modals/ForkIndexerModal.jsx @@ -14,14 +14,9 @@ export const ForkIndexerModal = ({ registerFunction, forkIndexer }) => { setIndexerConfig, isCreateNewIndexer, } = useContext(IndexerDetailsContext); - const [indexerConfig, setIndexerConfigField] = useState({ - filter: "social.near", - startBlockHeight: null, - }); const [indexerName, setIndexerNameField] = useState(""); const [error, setError] = useState(null); - const fork = async () => { if (!indexerName) { setError("Please provide an Indexer Name"); @@ -35,18 +30,13 @@ export const ForkIndexerModal = ({ registerFunction, forkIndexer }) => { return; } - if (!validateContractId(indexerConfig.filter)) { - setError("Please provide a valid contract name"); - return; - } setError(null); setIndexerName(indexerName); setIsCreateNewIndexer(true); - forkIndexer(indexerName, indexerConfig); + forkIndexer(indexerName); setShowForkIndexerModal(false); }; - // return (