From 21fe750933cfa36b4ad5616f0572c28de0b7e8c6 Mon Sep 17 00:00:00 2001 From: mahiro72 Date: Wed, 17 Dec 2025 02:46:01 +0900 Subject: [PATCH] refactor; add share status state --- playground/shared/src/ShareButton.tsx | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/playground/shared/src/ShareButton.tsx b/playground/shared/src/ShareButton.tsx index c6bbeeb83e..a79ab4d0a3 100644 --- a/playground/shared/src/ShareButton.tsx +++ b/playground/shared/src/ShareButton.tsx @@ -1,21 +1,23 @@ import { useEffect, useState } from "react"; import AstralButton from "./AstralButton"; +type ShareStatus = "initial" | "copying" | "copied"; + export default function ShareButton({ onShare, }: { onShare: () => Promise; }) { - const [copied, setCopied] = useState(false); + 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" ? ( { - await onShare(); - setCopied(true); + setStatus("copying"); + try { + await onShare(); + setStatus("copied"); + } catch (error) { + // eslint-disable-next-line no-console + console.error("Failed to share playground", error); + setStatus("initial"); + } }} >