Skip to content

Commit 68dd754

Browse files
committed
fix(tabs): Ensure dropdown actions await compilation
1 parent 65a5de9 commit 68dd754

File tree

2 files changed

+56
-32
lines changed

2 files changed

+56
-32
lines changed

libs/remix-ui/tabs/src/lib/components/CompileDropdown.tsx

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,37 @@ interface CompileDropdownProps {
1515
compiledFileName?: string
1616
onNotify?: (msg: string) => void
1717
onOpen?: () => void
18-
onRequestCompileAndPublish?: (type: string) => void;
18+
onRequestCompileAndPublish?: (type: string) => void
19+
setCompileState: (state: 'idle' | 'compiling' | 'compiled') => void
1920
}
2021

21-
export const CompileDropdown: React.FC<CompileDropdownProps> = ({ tabPath, plugin, disabled, onNotify, onOpen, onRequestCompileAndPublish, compiledFileName }) => {
22+
export const CompileDropdown: React.FC<CompileDropdownProps> = ({ tabPath, plugin, disabled, onNotify, onOpen, onRequestCompileAndPublish, compiledFileName, setCompileState }) => {
2223
const [scriptFiles, setScriptFiles] = useState<string[]>([])
2324

25+
const compileThen = async (nextAction: () => void) => {
26+
setCompileState('compiling')
27+
28+
setTimeout(async () => {
29+
plugin.once('solidity', 'compilationFinished', (data) => {
30+
const hasErrors = data.errors && data.errors.filter(e => e.severity === 'error').length > 0
31+
if (hasErrors) {
32+
setCompileState('idle')
33+
plugin.call('notification', 'toast', 'Compilation failed')
34+
} else {
35+
setCompileState('compiled')
36+
nextAction()
37+
}
38+
})
39+
40+
try {
41+
await plugin.call('solidity', 'compile', tabPath)
42+
} catch (e) {
43+
console.error(e)
44+
setCompileState('idle')
45+
}
46+
}, 0)
47+
}
48+
2449
const fetchScripts = async () => {
2550
try {
2651
let files = {}
@@ -38,33 +63,31 @@ export const CompileDropdown: React.FC<CompileDropdownProps> = ({ tabPath, plugi
3863
}
3964

4065
const runScript = async (path: string) => {
41-
await plugin.call('solidity', 'compile', tabPath)
42-
const content = await plugin.call('fileManager', 'readFile', path)
43-
await plugin.call('scriptRunnerBridge', 'execute', content, path)
44-
onNotify?.(`Executed script: ${path}`)
66+
await compileThen(async () => {
67+
const content = await plugin.call('fileManager', 'readFile', path)
68+
await plugin.call('scriptRunnerBridge', 'execute', content, path)
69+
onNotify?.(`Executed script: ${path}`)
70+
})
4571
}
4672

4773
const runRemixAnalysis = async () => {
4874
_paq.push(['trackEvent', 'solidityCompiler', 'staticAnalysis', 'initiate'])
49-
await plugin.call('solidity', 'compile', tabPath)
50-
const isStaticAnalyzersActive = await plugin.call('manager', 'isActive', 'solidityStaticAnalysis')
51-
if (!isStaticAnalyzersActive) {
52-
await plugin.call('manager', 'activatePlugin', 'solidityStaticAnalysis')
53-
}
54-
plugin.call('menuicons', 'select', 'solidityStaticAnalysis')
55-
onNotify?.("Ran Remix static analysis")
75+
await compileThen(async () => {
76+
const isStaticAnalyzersActive = await plugin.call('manager', 'isActive', 'solidityStaticAnalysis')
77+
if (!isStaticAnalyzersActive) {
78+
await plugin.call('manager', 'activatePlugin', 'solidityStaticAnalysis')
79+
}
80+
plugin.call('menuicons', 'select', 'solidityStaticAnalysis')
81+
onNotify?.("Ran Remix static analysis")
82+
})
5683
}
5784

5885
const handleScanContinue = async () => {
59-
await plugin.call('solidity', 'compile', tabPath)
60-
61-
const firstSlashIndex = compiledFileName.indexOf('/');
62-
63-
const finalPath = firstSlashIndex > 0
64-
? compiledFileName.substring(firstSlashIndex + 1)
65-
: compiledFileName;
66-
67-
await handleSolidityScan(plugin, finalPath)
86+
await compileThen(async () => {
87+
const firstSlashIndex = compiledFileName.indexOf('/')
88+
const finalPath = firstSlashIndex > 0 ? compiledFileName.substring(firstSlashIndex + 1) : compiledFileName
89+
await handleSolidityScan(plugin, finalPath)
90+
})
6891
}
6992

7093
const runSolidityScan = async () => {

libs/remix-ui/tabs/src/lib/remix-ui-tabs.tsx

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ export const TabsUI = (props: TabsUIProps) => {
219219
* * @param {'ipfs' | 'swarm'} storageType - The type of storage to publish to.
220220
*/
221221
const handleCompileAndPublish = async (storageType: 'ipfs' | 'swarm') => {
222-
setCompileState('compiling');
222+
setCompileState('compiling')
223223
await props.plugin.call('notification', 'toast', `Switching to Solidity Compiler to publish...`)
224224

225225
await props.plugin.call('manager', 'activatePlugin', 'solidity')
@@ -229,19 +229,19 @@ export const TabsUI = (props: TabsUIProps) => {
229229
_paq.push(['trackEvent', 'editor', 'publishFromEditor', storageType])
230230

231231
setTimeout(() => {
232-
let buttonId;
232+
let buttonId
233233
if (storageType === 'ipfs') {
234-
buttonId = 'publishOnIpfs';
234+
buttonId = 'publishOnIpfs'
235235
} else {
236-
buttonId = 'publishOnSwarm';
236+
buttonId = 'publishOnSwarm'
237237
}
238238

239-
const buttonToClick = document.getElementById(buttonId);
239+
const buttonToClick = document.getElementById(buttonId)
240240

241241
if (buttonToClick) {
242-
buttonToClick.click();
242+
buttonToClick.click()
243243
} else {
244-
props.plugin.call('notification', 'toast', 'Could not find the publish button.');
244+
props.plugin.call('notification', 'toast', 'Could not find the publish button.')
245245
}
246246
}, 500)
247247

@@ -250,8 +250,8 @@ export const TabsUI = (props: TabsUIProps) => {
250250
await props.plugin.call('notification', 'toast', `Error publishing: ${e.message}`)
251251
}
252252

253-
setCompileState('idle');
254-
};
253+
setCompileState('idle')
254+
}
255255

256256
const handleRunScript = async (runnerKey: string) => {
257257
if (runnerKey === 'new_script') {
@@ -352,7 +352,7 @@ export const TabsUI = (props: TabsUIProps) => {
352352
} else {
353353
setCompileState('compiled')
354354
}
355-
});
355+
})
356356

357357
if (tabsState.currentExt === 'vy') {
358358
await props.plugin.call(compilerName, 'vyperCompileCustomAction')
@@ -437,6 +437,7 @@ export const TabsUI = (props: TabsUIProps) => {
437437
disabled={!(PlayExtList.includes(tabsState.currentExt)) || compileState === 'compiling'}
438438
onNotify={(msg) => console.log(msg)}
439439
onRequestCompileAndPublish={handleCompileAndPublish}
440+
setCompileState={setCompileState}
440441
/>
441442
</>
442443
)}

0 commit comments

Comments
 (0)