import "./AsyncButton.css"; import { useState } from "react"; import Spinner from "../Icons/Spinner"; interface AsyncButtonProps extends React.ButtonHTMLAttributes { disabled?: boolean; onClick(e: React.MouseEvent): Promise | void; children?: React.ReactNode; } export default function AsyncButton(props: AsyncButtonProps) { const [loading, setLoading] = useState(false); async function handle(e: React.MouseEvent) { if (loading || props.disabled) return; setLoading(true); try { if (typeof props.onClick === "function") { const f = props.onClick(e); if (f instanceof Promise) { await f; } } } finally { setLoading(false); } } return ( ); }