forked from cruise-automation/webviz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwithStateReset.js
34 lines (26 loc) · 1.15 KB
/
withStateReset.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import * as monacoApi from "monaco-editor/esm/vs/editor/editor.api";
import React, { useEffect, useState } from "react";
import { resetRenderCountsForTests } from "webviz-core/src/panels/NumberOfRenders";
const StoryWrapper = (props) => {
const [showStory, setShowStory] = useState(false);
// Reset the state before rendering the next story
useEffect(() => {
// Remove leftover modals from the previous story
document.querySelectorAll("[data-modalcontainer]").forEach((el) => el.remove());
localStorage.clear();
// Reset the renderCounts for the NumberOfRenders panel
resetRenderCountsForTests();
// Reset cached monacoApi state
monacoApi.editor.getModels().forEach((model) => model.dispose());
// Unmount the old story before rendering the next one to clear out any stored state
setShowStory(false);
const timer = setImmediate(() => {
setShowStory(true);
});
return () => clearTimeout(timer);
}, [props]);
return (showStory && React.createElement(props.storyComponent)) || null;
};
export default function withStateReset(storyComponent) {
return <StoryWrapper storyComponent={storyComponent} />;
}