diff --git a/playground/ruff/src/Editor/Chrome.tsx b/playground/ruff/src/Editor/Chrome.tsx index 3fc9ddca2a..be744f2412 100644 --- a/playground/ruff/src/Editor/Chrome.tsx +++ b/playground/ruff/src/Editor/Chrome.tsx @@ -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) { diff --git a/playground/shared/src/Header.tsx b/playground/shared/src/Header.tsx index 7881fb5344..5b80f256b3 100644 --- a/playground/shared/src/Header.tsx +++ b/playground/shared/src/Header.tsx @@ -21,7 +21,7 @@ export default function Header({ version: string | null; onChangeTheme: (theme: Theme) => void; onReset?(): void; - onShare: () => void; + onShare: () => Promise; }) { return (
void }) { - const [copied, setCopied] = useState(false); +type ShareStatus = "initial" | "copying" | "copied"; + +export default function ShareButton({ + onShare, +}: { + onShare: () => Promise; +}) { + const [status, setStatus] = useState("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" ? ( void }) { { - 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"); + } }} > { + 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]);