Skip to content

Commit

Permalink
[Console] Fix hook for setting initial value in Monaco editor (#179865)
Browse files Browse the repository at this point in the history
## Summary

This is a follow-up to #178899
where we added a hook for setting the initial value in the Monaco editor
in Console. However, the hook introduced a bug - it wasn't possible to
type anything in the editor as every re-render called the function in
the hook, which set the value in the editor to be the initial value.
This PR fixes this.
  • Loading branch information
ElenaStoeva authored Apr 3, 2024
1 parent bb628f6 commit 7d080e9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ export const MonacoEditor = ({ initialTextValue }: EditorProps) => {

const [value, setValue] = useState(initialTextValue);

const setInitialValue = useSetInitialValue({ initialTextValue, setValue, toasts });
const setInitialValue = useSetInitialValue;

useEffect(() => {
setInitialValue();
}, [setInitialValue]);
setInitialValue({ initialTextValue, setValue, toasts });
}, [initialTextValue, setInitialValue, toasts]);

return (
<div
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,19 +75,17 @@ export const useSetInitialValue = (params: SetInitialValueParams) => {
loadBufferFromRemote(url);
}, 200);

return () => {
window.addEventListener('hashchange', onHashChange);
window.addEventListener('hashchange', onHashChange);

const loadFromParam = readLoadFromParam();
const loadFromParam = readLoadFromParam();

if (loadFromParam) {
loadBufferFromRemote(loadFromParam);
} else {
setValue(initialTextValue || DEFAULT_INPUT_VALUE);
}
if (loadFromParam) {
loadBufferFromRemote(loadFromParam);
} else {
setValue(initialTextValue || DEFAULT_INPUT_VALUE);
}

return () => {
window.removeEventListener('hashchange', onHashChange);
};
return () => {
window.removeEventListener('hashchange', onHashChange);
};
};

0 comments on commit 7d080e9

Please sign in to comment.