Skip to content

Commit d316348

Browse files
authored
feat(playground): add Reset button (#10934)
* feat(playground): add Reset button * fix(playground): remember initial content for reset * fix(playground): hide "Reset" button on new playground * fix(playground): clear should make Reset button disappear
1 parent afeb857 commit d316348

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

Diff for: client/src/playground/index.tsx

+38-2
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ export default function Playground() {
7676
let [codeSrc, setCodeSrc] = useState<string | undefined>();
7777
const [isEmpty, setIsEmpty] = useState<boolean>(true);
7878
const subdomain = useRef<string>(crypto.randomUUID());
79+
const [initialContent, setInitialContent] = useState<EditorContent | null>(
80+
null
81+
);
7982
let { data: initialCode } = useSWRImmutable<EditorContent>(
8083
!shared && gistId ? `/api/v1/play/${encodeURIComponent(gistId)}` : null,
8184
async (url) => {
@@ -105,8 +108,13 @@ export default function Playground() {
105108
const diaRef = useRef<HTMLDialogElement | null>(null);
106109

107110
useEffect(() => {
108-
initialCode && store(SESSION_KEY, initialCode);
109-
}, [initialCode]);
111+
if (initialCode) {
112+
store(SESSION_KEY, initialCode);
113+
if (Object.values(initialCode).some(Boolean)) {
114+
setInitialContent(structuredClone(initialCode));
115+
}
116+
}
117+
}, [initialCode, setInitialContent]);
110118

111119
const getEditorContent = useCallback(() => {
112120
const code = {
@@ -181,6 +189,17 @@ export default function Playground() {
181189
setSearchParams([], { replace: true });
182190
setCodeSrc(undefined);
183191
setEditorContent({ html: HTML_DEFAULT, css: CSS_DEFAULT, js: JS_DEFAULT });
192+
setInitialContent(null);
193+
194+
updateWithEditorContent();
195+
};
196+
197+
const reset = async () => {
198+
setEditorContent({
199+
html: initialContent?.html || HTML_DEFAULT,
200+
css: initialContent?.css || CSS_DEFAULT,
201+
js: initialContent?.js || JS_DEFAULT,
202+
});
184203

185204
updateWithEditorContent();
186205
};
@@ -192,6 +211,13 @@ export default function Playground() {
192211
}
193212
};
194213

214+
const resetConfirm = async () => {
215+
if (window.confirm("Do you really want to revert your changes?")) {
216+
gleanClick(`${PLAYGROUND}: revert-click`);
217+
await reset();
218+
}
219+
};
220+
195221
const updateWithEditorContent = () => {
196222
const { html, css, js } = getEditorContent();
197223
setIsEmpty(!html && !css && !js);
@@ -314,6 +340,16 @@ export default function Playground() {
314340
>
315341
clear
316342
</Button>
343+
{initialContent && (
344+
<Button
345+
type="secondary"
346+
id="reset"
347+
extraClasses="red"
348+
onClickHandler={resetConfirm}
349+
>
350+
reset
351+
</Button>
352+
)}
317353
</menu>
318354
</aside>
319355
<Editor

0 commit comments

Comments
 (0)