RaspberryPiFoundation / editor-ui

Code Editor web component
https://editor-static.raspberrypi.org
Apache License 2.0
37 stars 8 forks source link

Fix infinite ♾️ remix loop when BYPASS_AUTH set in editor-api #1007

Closed floehopper closed 1 month ago

floehopper commented 1 month ago

Previously when BYPASS_AUTH was set to true in editor-api and a remix was triggered in editor-ui, the editor went into an ♾️ infinite loop creating remix after remix after remix of the same project...

This was happening, because saveTriggered was never being set to false in the "editor/remixProject/pending" case in EditorSlice like it is being in "editor/saveProject/pending". Thus this condition in the useProjectPersistence hook was always true and syncProject("remix") was repeatedly dispatched.

https://github.com/RaspberryPiFoundation/editor-ui/blob/2df8a9761bff2de4824a7815252eb62560e85356/src/redux/EditorSlice.js#L372-L375

https://github.com/RaspberryPiFoundation/editor-ui/blob/2df8a9761bff2de4824a7815252eb62560e85356/src/redux/EditorSlice.js#L397-L399

We were somehow (mostly?) getting away with this when BYPASS_AUTH was not set in editor-ui, because of the extra time taken to make real authentication requests from within the API request to create the remixed project. This meant that by the time the useProjectPersistence hook was executed for the 2nd time, isOwner(user, project) returned true and so syncProject("save") was dispatched instead of syncProject("remix"). This in turn meant that the "editor/saveProject/pending" case was executed and thus saveTriggered was set to false preventing the infinite loop at the cost of an unintended/unnecessary save of the remixed project just created.

By setting saveTriggered to false in the "editor/remixProject/pending" case like we do in the "editor/saveProject/pending" case, we can prevent the infinite loop and the extra saving of the project.

github-actions[bot] commented 1 month ago
github-actions[bot] commented 1 month ago