import { useState } from "react"; export default function useLoading(fn: ((e: React.MouseEvent) => Promise | T) | undefined, disabled?: boolean) { const [loading, setLoading] = useState(false); async function handle(e: React.MouseEvent) { e.stopPropagation(); if (loading || disabled) return; setLoading(true); try { if (typeof fn === "function") { await fn(e); } } catch (e) { console.error(e); } finally { setLoading(false); } } return { handle, loading }; }