diff --git a/src/components/Arguments/components.tsx b/src/components/Arguments/components.tsx index 5269e6b2..01d7dd2c 100644 --- a/src/components/Arguments/components.tsx +++ b/src/components/Arguments/components.tsx @@ -244,18 +244,20 @@ export const ActionButton: React.FC = ({ active = true, onClick, }) => { - const { project, active: activeEditor, getActiveCode } = useProject(); + const { + project, + active: activeEditor, + getActiveCode, + showSavingMessage, + } = useProject(); const label = getLabel(type, project, activeEditor); - const { isSavingCode } = useProject(); - const sendingTransaction = false; const code = getActiveCode()[0].trim(); return ( diff --git a/src/components/Arguments/index.tsx b/src/components/Arguments/index.tsx index d9a00d14..730082f4 100644 --- a/src/components/Arguments/index.tsx +++ b/src/components/Arguments/index.tsx @@ -141,7 +141,7 @@ type ProcessingArgs = { }; const useTemplateType = (): ProcessingArgs => { - const { isSavingCode } = useProject(); + const { showSavingMessage } = useProject(); const { createScriptExecution, createTransactionExecution, @@ -149,7 +149,7 @@ const useTemplateType = (): ProcessingArgs => { } = useProject(); return { - disabled: isSavingCode, + disabled: showSavingMessage, scriptFactory: createScriptExecution, transactionFactory: createTransactionExecution, contractDeployment: updateAccountDeployedCode, @@ -218,7 +218,7 @@ const Arguments: React.FC = (props) => { const { project, active, - isSavingCode, + showSavingMessage, lastSigners, // updateAccountDeployedCode } = useProject(); @@ -356,7 +356,7 @@ const Arguments: React.FC = (props) => { let statusIcon = isOk ? : ; let statusMessage = isOk ? 'Ready' : 'Fix errors'; - const progress = isSavingCode || processingStatus; + const progress = showSavingMessage || processingStatus; if (progress) { statusIcon = ; diff --git a/src/components/CadenceEditor/ControlPanel/index.tsx b/src/components/CadenceEditor/ControlPanel/index.tsx index a8576152..ef0045bc 100644 --- a/src/components/CadenceEditor/ControlPanel/index.tsx +++ b/src/components/CadenceEditor/ControlPanel/index.tsx @@ -65,7 +65,7 @@ const ControlPanel: React.FC = (props) => { // =========================================================================== // GLOBAL HOOKS const { languageClient } = useContext(CadenceCheckerContext); - const { project, active, isSavingCode } = useProject(); + const { project, active, showSavingMessage } = useProject(); // HOOKS ------------------------------------------------------------------- const [executionArguments, setExecutionArguments] = useState({}); @@ -366,7 +366,7 @@ const ControlPanel: React.FC = (props) => { break; } - const progress = isSavingCode || processingStatus; + const progress = showSavingMessage || processingStatus; if (progress) { statusIcon = ; statusMessage = 'Please, wait...'; diff --git a/src/components/CadenceEditor/ControlPanel/utils.tsx b/src/components/CadenceEditor/ControlPanel/utils.tsx index 07dfea34..a79d61a2 100644 --- a/src/components/CadenceEditor/ControlPanel/utils.tsx +++ b/src/components/CadenceEditor/ControlPanel/utils.tsx @@ -98,7 +98,7 @@ export const getLabel = ( }; export const useTemplateType = (): ProcessingArgs => { - const { isSavingCode } = useProject(); + const { showSavingMessage } = useProject(); const { createScriptExecution, createTransactionExecution, @@ -106,7 +106,7 @@ export const useTemplateType = (): ProcessingArgs => { } = useProject(); return { - disabled: isSavingCode, + disabled: showSavingMessage, scriptFactory: createScriptExecution, transactionFactory: createTransactionExecution, contractDeployment: updateAccountDeployedCode, diff --git a/src/containers/Editor/components.tsx b/src/containers/Editor/components.tsx index fc4cc7b3..344fbd1c 100644 --- a/src/containers/Editor/components.tsx +++ b/src/containers/Editor/components.tsx @@ -121,7 +121,7 @@ const ShareSaveButton = ({ onSave, icon, }: ShareSaveButtonProps) => { - const { isSavingCode } = useProject(); + const { showSavingMessage } = useProject(); return ( {showShare ? ( @@ -129,7 +129,7 @@ const ShareSaveButton = ({ ) : ( onSave()} - disabled={isSavingCode} + disabled={showSavingMessage} Icon={icon} > {saveText} diff --git a/src/containers/Editor/layout.tsx b/src/containers/Editor/layout.tsx index 45582768..4719bce2 100644 --- a/src/containers/Editor/layout.tsx +++ b/src/containers/Editor/layout.tsx @@ -49,7 +49,7 @@ const EditorLayout: React.FC = () => { const { project, updateProject, - isSavingCode, + showSavingMessage, isLoading, active, setSelectedResourceAccount, @@ -197,7 +197,7 @@ const EditorLayout: React.FC = () => { }} > - {project && project.persist && isSavingCode && ( + {project && project.persist && showSavingMessage && ( { Autosaving... )} - {project && project.persist && !isSavingCode && ( + {project && project.persist && !showSavingMessage && ( void; transactionAccounts: number[]; isSavingCode: boolean; + showSavingMessage: boolean; } export const ProjectContext: React.Context = @@ -111,6 +112,8 @@ export const ProjectProvider: React.FC = ({ const [initialLoad, setInitialLoad] = useState(true); const [transactionAccounts, setTransactionAccounts] = useState([0]); const [isSavingCode, setIsSaving] = useState(false); + const showSavingMessageTimeoutRef = useRef(null); + const [showSavingMessage, setShowSavingMessage] = useState(false); const [lastSigners, setLastSigners] = useState(null); const [selectedResourceAccount, setSelectedResourceAccount] = useState< @@ -130,57 +133,85 @@ export const ProjectProvider: React.FC = ({ readme, ); - let timeout: any; + const showError = () => alert('Something went wrong, please try again'); + + // Update showSavingMessage based on isSavingCode. Delay the switch to false by 1 second + useEffect(() => { + clearTimeout(showSavingMessageTimeoutRef.current); + + if (isSavingCode === true) { + setShowSavingMessage(true); + } else { + showSavingMessageTimeoutRef.current = setTimeout(() => { + setShowSavingMessage(false); + }, 1000); + } + + return () => { + clearTimeout(showSavingMessageTimeoutRef.current); + }; + }, [isSavingCode]); const updateProject: any = async ( title: string, description: string, readme: string, ) => { - clearTimeout(timeout); setIsSaving(true); - const res = await mutator.saveProject( - project.transactionTemplates[active.index].id, - title, - description, - readme, - ); - timeout = setTimeout(() => { + let res; + try { + res = await mutator.saveProject( + project.transactionTemplates[active.index].id, + title, + description, + readme, + ); + } catch (e) { + console.error(e); setIsSaving(false); - }, 1000); + showError(); + } + setIsSaving(false); return res; }; const updateAccountDeployedCode: any = async () => { - clearTimeout(timeout); setIsSaving(true); - const res = await mutator.updateAccountDeployedCode( - project.accounts[active.index], - active.index, - ); - - const addr = project.accounts[active.index].address; - const acctNum = addr.charAt(addr.length - 1); - const acctHex = `0x0${acctNum}`; - const signer = [acctHex]; - setLastSigners(signer); - - timeout = setTimeout(() => { + let res; + try { + res = await mutator.updateAccountDeployedCode( + project.accounts[active.index], + active.index, + ); + + const addr = project.accounts[active.index].address; + const acctNum = addr.charAt(addr.length - 1); + const acctHex = `0x0${acctNum}`; + const signer = [acctHex]; + setLastSigners(signer); + } catch (e) { + console.error(e); setIsSaving(false); - }, 1000); + showError(); + } + setIsSaving(false); return res; }; const updateAccountDraftCode = async (value: string) => { - clearTimeout(timeout); setIsSaving(true); - const res = await mutator.updateAccountDraftCode( - project.accounts[active.index], - value, - ); - timeout = setTimeout(() => { + let res; + try { + res = await mutator.updateAccountDraftCode( + project.accounts[active.index], + value, + ); + } catch (e) { + console.error(e); setIsSaving(false); - }, 1000); + showError(); + } + setIsSaving(false); return res; }; @@ -189,12 +220,16 @@ export const ProjectProvider: React.FC = ({ script: string, title: string, ) => { - clearTimeout(timeout); setIsSaving(true); - const res = await mutator.updateScriptTemplate(templateId, script, title); - timeout = setTimeout(() => { + let res; + try { + res = await mutator.updateScriptTemplate(templateId, script, title); + } catch (e) { + console.error(e); setIsSaving(false); - }, 1000); + showError(); + } + setIsSaving(false); return res; }; @@ -203,44 +238,52 @@ export const ProjectProvider: React.FC = ({ script: string, title: string, ) => { - clearTimeout(timeout); setIsSaving(true); - const res = await mutator.updateTransactionTemplate( - templateId, - script, - title, - ); - timeout = setTimeout(() => { + let res; + try { + res = await mutator.updateTransactionTemplate(templateId, script, title); + } catch (e) { + console.error(e); setIsSaving(false); - }, 1000); + showError(); + } + setIsSaving(false); return res; }; const updateActiveScriptTemplate = async (script: string) => { - clearTimeout(timeout); setIsSaving(true); - const res = await mutator.updateScriptTemplate( - project.scriptTemplates[active.index].id, - script, - project.scriptTemplates[active.index].title, - ); - timeout = setTimeout(() => { + let res; + try { + res = await mutator.updateScriptTemplate( + project.scriptTemplates[active.index].id, + script, + project.scriptTemplates[active.index].title, + ); + } catch (e) { + console.error(e); setIsSaving(false); - }, 1000); + showError(); + } + setIsSaving(false); return res; }; const updateActiveTransactionTemplate = async (script: string) => { - clearTimeout(timeout); setIsSaving(true); - const res = await mutator.updateTransactionTemplate( - project.transactionTemplates[active.index].id, - script, - project.transactionTemplates[active.index].title, - ); - timeout = setTimeout(() => { + let res; + try { + res = await mutator.updateTransactionTemplate( + project.transactionTemplates[active.index].id, + script, + project.transactionTemplates[active.index].title, + ); + } catch (e) { + console.error(e); setIsSaving(false); - }, 1000); + showError(); + } + setIsSaving(false); return res; }; @@ -248,59 +291,74 @@ export const ProjectProvider: React.FC = ({ signingAccounts: Account[], args?: [string], ) => { - clearTimeout(timeout); setIsSaving(true); - const res = await mutator.createTransactionExecution( - project.transactionTemplates[active.index].script, - signingAccounts, - args, - ); - - let signers: string[] = []; - signingAccounts?.map((acct: any) => { - const addr = acct.address; - const acctNum = addr.charAt(addr.length - 1); - const acctHex = `0x0${acctNum}`; - signers.push(acctHex); - }); - setLastSigners(signers); - - timeout = setTimeout(() => { + let res; + try { + res = await mutator.createTransactionExecution( + project.transactionTemplates[active.index].script, + signingAccounts, + args, + ); + + let signers: string[] = []; + signingAccounts?.map((acct: any) => { + const addr = acct.address; + const acctNum = addr.charAt(addr.length - 1); + const acctHex = `0x0${acctNum}`; + signers.push(acctHex); + }); + setLastSigners(signers); + } catch (e) { + console.error(e); setIsSaving(false); - }, 1000); + showError(); + } + setIsSaving(false); return res; }; const createScriptExecution = async (args?: string[]) => { - clearTimeout(timeout); setIsSaving(true); - const res = await mutator.createScriptExecution( - project.scriptTemplates[active.index].script, - args, - ); - timeout = setTimeout(() => { + let res; + try { + res = await mutator.createScriptExecution( + project.scriptTemplates[active.index].script, + args, + ); + } catch (e) { + console.error(e); setIsSaving(false); - }, 1000); + showError(); + } + setIsSaving(false); return res; }; const deleteScriptTemplate = async (templateId: string) => { - clearTimeout(timeout); setIsSaving(true); - const res = await mutator.deleteScriptTemplate(templateId); - timeout = setTimeout(() => { + let res; + try { + res = await mutator.deleteScriptTemplate(templateId); + } catch (e) { + console.error(e); setIsSaving(false); - }, 1000); + showError(); + } + setIsSaving(false); return res; }; const deleteTransactionTemplate = async (templateId: string) => { - clearTimeout(timeout); setIsSaving(true); - const res = await mutator.deleteTransactionTemplate(templateId); - timeout = setTimeout(() => { + let res; + try { + res = await mutator.deleteTransactionTemplate(templateId); + } catch (e) { + console.error(e); setIsSaving(false); - }, 1000); + showError(); + } + setIsSaving(false); return res; }; @@ -490,6 +548,7 @@ export const ProjectProvider: React.FC = ({ project, isLoading, mutator, + showSavingMessage, isSavingCode, updateProject, updateAccountDeployedCode,