({
action,
requireAreYouSure = false,
...props
}: Omit<ComponentPropsWithRef<typeof Button>, "onClick"> & {
action: () => Promise<{ error: boolean; message: string }>
requireAreYouSure?: boolean
})
| 18 | } from "./ui/alert-dialog" |
| 19 | |
| 20 | export function ActionButton({ |
| 21 | action, |
| 22 | requireAreYouSure = false, |
| 23 | ...props |
| 24 | }: Omit<ComponentPropsWithRef<typeof Button>, "onClick"> & { |
| 25 | action: () => Promise<{ error: boolean; message: string }> |
| 26 | requireAreYouSure?: boolean |
| 27 | }) { |
| 28 | { |
| 29 | const [isLoading, startTransition] = useTransition() |
| 30 | |
| 31 | function performAction() { |
| 32 | startTransition(async () => { |
| 33 | const data = await action() |
| 34 | actionToast({ actionData: data }) |
| 35 | }) |
| 36 | } |
| 37 | |
| 38 | if (requireAreYouSure) { |
| 39 | return ( |
| 40 | <AlertDialog open={isLoading ? true : undefined}> |
| 41 | <AlertDialogTrigger asChild> |
| 42 | <Button {...props} /> |
| 43 | </AlertDialogTrigger> |
| 44 | <AlertDialogContent> |
| 45 | <AlertDialogHeader> |
| 46 | <AlertDialogTitle>Are you sure?</AlertDialogTitle> |
| 47 | <AlertDialogDescription> |
| 48 | This action cannot be undone. |
| 49 | </AlertDialogDescription> |
| 50 | </AlertDialogHeader> |
| 51 | <AlertDialogFooter> |
| 52 | <AlertDialogCancel>Cancel</AlertDialogCancel> |
| 53 | <AlertDialogAction disabled={isLoading} onClick={performAction}> |
| 54 | <LoadingTextSwap isLoading={isLoading}>Yes</LoadingTextSwap> |
| 55 | </AlertDialogAction> |
| 56 | </AlertDialogFooter> |
| 57 | </AlertDialogContent> |
| 58 | </AlertDialog> |
| 59 | ) |
| 60 | } |
| 61 | |
| 62 | return ( |
| 63 | <Button {...props} disabled={isLoading} onClick={performAction}> |
| 64 | <LoadingTextSwap isLoading={isLoading}> |
| 65 | {props.children} |
| 66 | </LoadingTextSwap> |
| 67 | </Button> |
| 68 | ) |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | function LoadingTextSwap({ |
| 73 | isLoading, |
nothing calls this directly
no outgoing calls
no test coverage detected