|
| 1 | +import { type ReactNode, useState } from "react"; |
| 2 | + |
| 3 | +import { Icon, type IconName } from "@/components/ui/icon"; |
| 4 | +import { InlineStack } from "@/components/ui/layout"; |
| 5 | + |
| 6 | +import TooltipButton from "../../Buttons/TooltipButton"; |
| 7 | +import { ConfirmationDialog } from "../../Dialogs"; |
| 8 | + |
| 9 | +export type Action = { |
| 10 | + label: string; |
| 11 | + destructive?: boolean; |
| 12 | + disabled?: boolean; |
| 13 | + hidden?: boolean; |
| 14 | + confirmation?: string; |
| 15 | + onClick: () => void; |
| 16 | + className?: string; |
| 17 | +} & ( |
| 18 | + | { icon: IconName; content?: never } |
| 19 | + | { content: ReactNode; icon?: never } |
| 20 | +); |
| 21 | + |
| 22 | +// Temporary: ReactNode included for backward compatibility with some existing buttons. In the long-term we should strive for only Action types. |
| 23 | +export type ActionOrReactNode = Action | ReactNode; |
| 24 | + |
| 25 | +interface ActionBlockProps { |
| 26 | + actions: ActionOrReactNode[]; |
| 27 | + className?: string; |
| 28 | +} |
| 29 | + |
| 30 | +export const ActionBlock = ({ actions, className }: ActionBlockProps) => { |
| 31 | + const [isOpen, setIsOpen] = useState(false); |
| 32 | + const [dialogAction, setDialogAction] = useState<Action | null>(null); |
| 33 | + |
| 34 | + const openConfirmationDialog = (action: Action) => { |
| 35 | + return () => { |
| 36 | + setDialogAction(action); |
| 37 | + setIsOpen(true); |
| 38 | + }; |
| 39 | + }; |
| 40 | + |
| 41 | + const handleConfirm = () => { |
| 42 | + setIsOpen(false); |
| 43 | + dialogAction?.onClick(); |
| 44 | + setDialogAction(null); |
| 45 | + }; |
| 46 | + |
| 47 | + const handleCancel = () => { |
| 48 | + setIsOpen(false); |
| 49 | + setDialogAction(null); |
| 50 | + }; |
| 51 | + |
| 52 | + return ( |
| 53 | + <> |
| 54 | + <InlineStack gap="2" className={className}> |
| 55 | + {actions.map((action, index) => { |
| 56 | + if (!action || typeof action !== "object" || !("label" in action)) { |
| 57 | + return <div key={index}>{action}</div>; |
| 58 | + } |
| 59 | + |
| 60 | + if (action.hidden) { |
| 61 | + return null; |
| 62 | + } |
| 63 | + |
| 64 | + return ( |
| 65 | + <TooltipButton |
| 66 | + key={action.label} |
| 67 | + variant={action.destructive ? "destructive" : "outline"} |
| 68 | + tooltip={action.label} |
| 69 | + onClick={ |
| 70 | + !!action.confirmation |
| 71 | + ? openConfirmationDialog(action) |
| 72 | + : action.onClick |
| 73 | + } |
| 74 | + disabled={action.disabled} |
| 75 | + className={action.className} |
| 76 | + > |
| 77 | + {action.content === undefined && action.icon ? ( |
| 78 | + <Icon name={action.icon} /> |
| 79 | + ) : ( |
| 80 | + action.content |
| 81 | + )} |
| 82 | + </TooltipButton> |
| 83 | + ); |
| 84 | + })} |
| 85 | + </InlineStack> |
| 86 | + |
| 87 | + <ConfirmationDialog |
| 88 | + isOpen={isOpen} |
| 89 | + title={dialogAction?.label} |
| 90 | + description={dialogAction?.confirmation} |
| 91 | + onConfirm={handleConfirm} |
| 92 | + onCancel={handleCancel} |
| 93 | + /> |
| 94 | + </> |
| 95 | + ); |
| 96 | +}; |
0 commit comments