Skip to content
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
8 changes: 2 additions & 6 deletions playground/ruff/src/Editor/Chrome.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,11 @@ export default function Chrome() {

const [theme, setTheme] = useTheme();

const handleShare = useCallback(() => {
const handleShare = useCallback(async () => {
if (settings == null || pythonSource == null) {
return;
}

persist(settings, pythonSource).catch((error) =>
// eslint-disable-next-line no-console
console.error(`Failed to share playground: ${error}`),
);
await persist(settings, pythonSource);
}, [pythonSource, settings]);

if (initPromise.current == null) {
Expand Down
2 changes: 1 addition & 1 deletion playground/shared/src/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export default function Header({
version: string | null;
onChangeTheme: (theme: Theme) => void;
onReset?(): void;
onShare: () => void;
onShare: () => Promise<void>;
}) {
return (
<div
Expand Down
33 changes: 23 additions & 10 deletions playground/shared/src/ShareButton.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
import { useEffect, useState } from "react";
import AstralButton from "./AstralButton";

export default function ShareButton({ onShare }: { onShare: () => void }) {
const [copied, setCopied] = useState(false);
type ShareStatus = "initial" | "copying" | "copied";

export default function ShareButton({
onShare,
}: {
onShare: () => Promise<void>;
}) {
const [status, setStatus] = useState<ShareStatus>("initial");

useEffect(() => {
if (copied) {
const timeout = setTimeout(() => setCopied(false), 2000);
if (status === "copied") {
const timeout = setTimeout(() => setStatus("initial"), 2000);
return () => clearTimeout(timeout);
}
}, [copied]);
}, [status]);

return copied ? (
return status === "copied" ? (
<AstralButton
type="button"
className="relative flex-none leading-6 py-1.5 px-3 cursor-auto dark:shadow-copied"
Expand All @@ -28,10 +34,17 @@ export default function ShareButton({ onShare }: { onShare: () => void }) {
<AstralButton
type="button"
className="relative flex-none leading-6 py-1.5 px-3 shadow-xs disabled:opacity-50"
disabled={copied}
onClick={() => {
setCopied(true);
onShare();
disabled={status === "copying"}
onClick={async () => {
setStatus("copying");
try {
await onShare();
setStatus("copied");
} catch (error) {
// eslint-disable-next-line no-console
console.error("Failed to share playground", error);
setStatus("initial");
}
}}
>
<span
Expand Down
7 changes: 2 additions & 5 deletions playground/ty/src/Playground.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,11 @@ export default function Playground() {

usePersistLocally(files);

const handleShare = useCallback(() => {
const handleShare = useCallback(async () => {
const serialized = serializeFiles(files);

if (serialized != null) {
persist(serialized).catch((error) => {
// eslint-disable-next-line no-console
console.error("Failed to share playground", error);
});
await persist(serialized);
}
}, [files]);

Expand Down
Loading