Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(playground): disable Share/Clear buttons if empty #10935

Merged
merged 5 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions client/src/playground/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ main.play {
button {
text-transform: capitalize;

&:disabled {
cursor: not-allowed;
}

&.red {
--button-color: var(--text-primary-red);
}
Expand Down
46 changes: 28 additions & 18 deletions client/src/playground/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export default function Playground() {
let [vConsole, setVConsole] = useState<VConsole[]>([]);
let [state, setState] = useState(State.initial);
let [codeSrc, setCodeSrc] = useState<string | undefined>();
const [isEmpty, setIsEmpty] = useState<boolean>(true);
caugner marked this conversation as resolved.
Show resolved Hide resolved
const subdomain = useRef<string>(crypto.randomUUID());
let { data: initialCode } = useSWRImmutable<EditorContent>(
!shared && gistId ? `/api/v1/play/${encodeURIComponent(gistId)}` : null,
Expand Down Expand Up @@ -132,41 +133,47 @@ export default function Playground() {
},
[getEditorContent]
);

const setEditorContent = ({ html, css, js, src }: EditorContent) => {
htmlRef.current?.setContent(html);
cssRef.current?.setContent(css);
jsRef.current?.setContent(js);
if (src) {
setCodeSrc(src.split("/").slice(0, -1).join("/"));
}
setIsEmpty(!html && !css && !js);
};

useEffect(() => {
if (state === State.initial || state === State.remote) {
if (initialCode && Object.values(initialCode).some(Boolean)) {
htmlRef.current?.setContent(initialCode?.html);
cssRef.current?.setContent(initialCode?.css);
jsRef.current?.setContent(initialCode?.js);
if (initialCode.src) {
setCodeSrc(
initialCode?.src &&
`${initialCode.src.split("/").slice(0, -1).join("/")}`
);
}
setEditorContent(initialCode);
} else {
htmlRef.current?.setContent(HTML_DEFAULT);
cssRef.current?.setContent(CSS_DEFAULT);
jsRef.current?.setContent(JS_DEFAULT);
setEditorContent({
html: HTML_DEFAULT,
css: CSS_DEFAULT,
js: JS_DEFAULT,
});
}
setState(State.ready);
}
}, [initialCode, state]);

useEffect(() => {
window.addEventListener("message", messageListener);
return () => {
window.removeEventListener("message", messageListener);
};
}, [messageListener]);

const clear = async () => {
setSearchParams([], { replace: true });
setCodeSrc(undefined);
htmlRef.current?.setContent(HTML_DEFAULT);
cssRef.current?.setContent(CSS_DEFAULT);
jsRef.current?.setContent(JS_DEFAULT);
setEditorContent({ html: HTML_DEFAULT, css: CSS_DEFAULT, js: JS_DEFAULT });

updateWithEditorContent();
};

const clearConfirm = async () => {
if (window.confirm("Do you really want to clear everything?")) {
gleanClick(`${PLAYGROUND}: reset-click`);
Expand All @@ -175,6 +182,9 @@ export default function Playground() {
};

const updateWithEditorContent = () => {
const { html, css, js } = getEditorContent();
setIsEmpty(!html && !css && !js);

const loading = [
{},
{
Expand Down Expand Up @@ -221,9 +231,7 @@ export default function Playground() {
plugins: [prettierPluginBabel, prettierPluginESTree],
}),
};
htmlRef.current?.setContent(formatted.html);
cssRef.current?.setContent(formatted.css);
jsRef.current?.setContent(formatted.js);
setEditorContent(formatted);
} catch (e) {
console.error(e);
}
Expand Down Expand Up @@ -277,6 +285,7 @@ export default function Playground() {
<Button
type="secondary"
id="share"
isDisabled={isEmpty}
onClickHandler={() => {
gleanClick(`${PLAYGROUND}: share-click`);
setDialogState(DialogState.share);
Expand All @@ -287,6 +296,7 @@ export default function Playground() {
</Button>
<Button
type="secondary"
isDisabled={isEmpty}
id="clear"
extraClasses="red"
onClickHandler={clearConfirm}
Expand Down
Loading