import { useCallback, useState } from 'react'; import type { Meta, StoryObj } from '@storybook/react'; import { ConfirmDialog, ConfirmDialogProps } from 'reablocks'; import { ConfirmDialogActions } from 'reablocks'; import { useConfirmDialogContext } from 'reablocks'; import { Button } from 'reablocks'; const meta: Meta = { title: 'Components/Layers/Confirm Dialog', component: ConfirmDialog, argTypes: { header: { control: 'text' }, content: { control: 'text' }, confirmLabel: { control: 'text' }, cancelLabel: { control: 'text' }, onConfirm: { action: 'confirmed' }, onCancel: { action: 'cancelled' } } }; export default meta; type Story = StoryObj; export const Default: Story = { render: args => { const [isOpen, setIsOpen] = useState(false); const handleConfirm = useCallback( function (): void { if (args.onConfirm) { args.onConfirm(); } setIsOpen(false); }, [args, setIsOpen] ); const handleCancel = useCallback( function (): void { if (args.onCancel) { args.onCancel(); } setIsOpen(false); }, [args, setIsOpen] ); return ( <> ); }, args: { header: 'Confirm Action', content: 'Are you sure you want to proceed?', confirmLabel: 'Confirm', cancelLabel: 'Cancel' } }; export const AsyncAction: Story = { render: args => { const [isOpen, setIsOpen] = useState(false); const [lastResult, setLastResult] = useState(null); const handleConfirm = useCallback(async () => { await new Promise(resolve => setTimeout(resolve, 2000)); if (args.onConfirm) { args.onConfirm(); } setLastResult(`Saved at ${new Date().toLocaleTimeString()}`); setIsOpen(false); }, [args]); const handleCancel = useCallback(() => { if (args.onCancel) { args.onCancel(); } setIsOpen(false); }, [args]); return ( <> {lastResult &&

{lastResult}

} ); }, args: { header: 'Save Changes', content: 'This will commit your changes to the server. The dialog stays open and shows a loader until the action completes.', confirmLabel: 'Save', cancelLabel: 'Cancel' } }; export const Destructive: Story = { render: args => { const [isOpen, setIsOpen] = useState(false); const handleConfirm = useCallback( function (): void { if (args.onConfirm) { args.onConfirm(); } setIsOpen(false); }, [args, setIsOpen] ); const handleCancel = useCallback( function (): void { if (args.onCancel) { args.onCancel(); } setIsOpen(false); }, [args, setIsOpen] ); return ( <> ); }, args: { header: 'Delete Item', content: 'Are you sure you want to delete this item? This action cannot be undone.', confirmLabel: 'Delete', cancelLabel: 'Cancel', variant: 'destructive' } }; const PublishActions = ({ onSaveDraft }: { onSaveDraft: () => void }) => { const { onConfirm, onCancel, isLoading } = useConfirmDialogContext(); return ( <> ); }; export const CustomActions: Story = { render: args => { const [isOpen, setIsOpen] = useState(false); const [lastResult, setLastResult] = useState(null); const handlePublish = useCallback(async () => { await new Promise(resolve => setTimeout(resolve, 1500)); setLastResult('Published'); setIsOpen(false); }, []); const handleSaveDraft = useCallback(() => { setLastResult('Saved as draft'); setIsOpen(false); }, []); return ( <> {lastResult &&

{lastResult}

} setIsOpen(false)} > ); }, args: { header: 'Publish Post', content: 'Choose how to handle this draft. Publishing pushes it live; the dialog stays open and disables actions while the request runs.' } }; export const AsyncDestructive: Story = { render: args => { const [isOpen, setIsOpen] = useState(false); const [lastResult, setLastResult] = useState(null); const handleConfirm = useCallback(async () => { await new Promise(resolve => setTimeout(resolve, 2000)); if (args.onConfirm) { args.onConfirm(); } setLastResult(`Deleted at ${new Date().toLocaleTimeString()}`); setIsOpen(false); }, [args]); const handleCancel = useCallback(() => { if (args.onCancel) { args.onCancel(); } setIsOpen(false); }, [args]); return ( <> {lastResult &&

{lastResult}

} ); }, args: { header: 'Delete Item', content: 'Are you sure you want to delete this item? This action cannot be undone. The dialog stays open and shows a loader until the deletion completes.', confirmLabel: 'Delete', cancelLabel: 'Cancel', variant: 'destructive' } };