diff --git a/client/src/playground/index.tsx b/client/src/playground/index.tsx index 662958693508..bb9725aaf76a 100644 --- a/client/src/playground/index.tsx +++ b/client/src/playground/index.tsx @@ -76,6 +76,9 @@ export default function Playground() { let [codeSrc, setCodeSrc] = useState(); const [isEmpty, setIsEmpty] = useState(true); const subdomain = useRef(crypto.randomUUID()); + const [initialContent, setInitialContent] = useState( + null + ); let { data: initialCode } = useSWRImmutable( !shared && gistId ? `/api/v1/play/${encodeURIComponent(gistId)}` : null, async (url) => { @@ -105,8 +108,13 @@ export default function Playground() { const diaRef = useRef(null); useEffect(() => { - initialCode && store(SESSION_KEY, initialCode); - }, [initialCode]); + if (initialCode) { + store(SESSION_KEY, initialCode); + if (Object.values(initialCode).some(Boolean)) { + setInitialContent(structuredClone(initialCode)); + } + } + }, [initialCode, setInitialContent]); const getEditorContent = useCallback(() => { const code = { @@ -181,6 +189,17 @@ export default function Playground() { setSearchParams([], { replace: true }); setCodeSrc(undefined); setEditorContent({ html: HTML_DEFAULT, css: CSS_DEFAULT, js: JS_DEFAULT }); + setInitialContent(null); + + updateWithEditorContent(); + }; + + const reset = async () => { + setEditorContent({ + html: initialContent?.html || HTML_DEFAULT, + css: initialContent?.css || CSS_DEFAULT, + js: initialContent?.js || JS_DEFAULT, + }); updateWithEditorContent(); }; @@ -192,6 +211,13 @@ export default function Playground() { } }; + const resetConfirm = async () => { + if (window.confirm("Do you really want to revert your changes?")) { + gleanClick(`${PLAYGROUND}: revert-click`); + await reset(); + } + }; + const updateWithEditorContent = () => { const { html, css, js } = getEditorContent(); setIsEmpty(!html && !css && !js); @@ -314,6 +340,16 @@ export default function Playground() { > clear + {initialContent && ( + + )}