mirror of https://github.com/astral-sh/ruff
refactor; add share status state
This commit is contained in:
parent
a5c81d7207
commit
21fe750933
|
|
@ -1,21 +1,23 @@
|
||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
import AstralButton from "./AstralButton";
|
import AstralButton from "./AstralButton";
|
||||||
|
|
||||||
|
type ShareStatus = "initial" | "copying" | "copied";
|
||||||
|
|
||||||
export default function ShareButton({
|
export default function ShareButton({
|
||||||
onShare,
|
onShare,
|
||||||
}: {
|
}: {
|
||||||
onShare: () => Promise<void>;
|
onShare: () => Promise<void>;
|
||||||
}) {
|
}) {
|
||||||
const [copied, setCopied] = useState(false);
|
const [status, setStatus] = useState<ShareStatus>("initial");
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (copied) {
|
if (status === "copied") {
|
||||||
const timeout = setTimeout(() => setCopied(false), 2000);
|
const timeout = setTimeout(() => setStatus("initial"), 2000);
|
||||||
return () => clearTimeout(timeout);
|
return () => clearTimeout(timeout);
|
||||||
}
|
}
|
||||||
}, [copied]);
|
}, [status]);
|
||||||
|
|
||||||
return copied ? (
|
return status === "copied" ? (
|
||||||
<AstralButton
|
<AstralButton
|
||||||
type="button"
|
type="button"
|
||||||
className="relative flex-none leading-6 py-1.5 px-3 cursor-auto dark:shadow-copied"
|
className="relative flex-none leading-6 py-1.5 px-3 cursor-auto dark:shadow-copied"
|
||||||
|
|
@ -32,10 +34,17 @@ export default function ShareButton({
|
||||||
<AstralButton
|
<AstralButton
|
||||||
type="button"
|
type="button"
|
||||||
className="relative flex-none leading-6 py-1.5 px-3 shadow-xs disabled:opacity-50"
|
className="relative flex-none leading-6 py-1.5 px-3 shadow-xs disabled:opacity-50"
|
||||||
disabled={copied}
|
disabled={status === "copying"}
|
||||||
onClick={async () => {
|
onClick={async () => {
|
||||||
|
setStatus("copying");
|
||||||
|
try {
|
||||||
await onShare();
|
await onShare();
|
||||||
setCopied(true);
|
setStatus("copied");
|
||||||
|
} catch (error) {
|
||||||
|
// eslint-disable-next-line no-console
|
||||||
|
console.error("Failed to share playground", error);
|
||||||
|
setStatus("initial");
|
||||||
|
}
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<span
|
<span
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue