ConfirmModal.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import { useEffect } from 'react';
  2. import { useTranslation } from 'react-i18next';
  3. import { AlertTriangle, Loader2 } from 'lucide-react';
  4. import { Card, CardContent } from './Card';
  5. import { Button } from './Button';
  6. interface ConfirmModalProps {
  7. title: string;
  8. message: string;
  9. confirmText?: string;
  10. cancelText?: string;
  11. cancelVariant?: 'primary' | 'secondary' | 'danger' | 'ghost';
  12. cardClassName?: string;
  13. variant?: 'danger' | 'warning' | 'default';
  14. isLoading?: boolean;
  15. loadingText?: string;
  16. onConfirm: () => void;
  17. onCancel: () => void;
  18. }
  19. export function ConfirmModal({
  20. title,
  21. message,
  22. confirmText,
  23. cancelText,
  24. cancelVariant,
  25. cardClassName,
  26. variant = 'default',
  27. isLoading = false,
  28. loadingText,
  29. onConfirm,
  30. onCancel,
  31. }: ConfirmModalProps) {
  32. const { t } = useTranslation();
  33. const resolvedConfirmText = confirmText ?? t('common.confirm');
  34. const resolvedCancelText = cancelText ?? t('common.cancel');
  35. const resolvedLoadingText = loadingText ?? t('common.loading');
  36. // Close on Escape key (but not while loading)
  37. useEffect(() => {
  38. const handleKeyDown = (e: KeyboardEvent) => {
  39. if (e.key === 'Escape' && !isLoading) onCancel();
  40. };
  41. window.addEventListener('keydown', handleKeyDown);
  42. return () => window.removeEventListener('keydown', handleKeyDown);
  43. }, [onCancel, isLoading]);
  44. const variantStyles = {
  45. danger: {
  46. icon: 'text-red-400',
  47. button: 'bg-red-500 hover:bg-red-600',
  48. },
  49. warning: {
  50. icon: 'text-yellow-400',
  51. button: 'bg-yellow-500 hover:bg-yellow-600',
  52. },
  53. default: {
  54. icon: 'text-bambu-green',
  55. button: 'bg-bambu-green hover:bg-bambu-green-dark',
  56. },
  57. };
  58. const styles = variantStyles[variant];
  59. return (
  60. <div
  61. className="fixed inset-0 bg-black/50 flex items-center justify-center z-50 p-4"
  62. onClick={isLoading ? undefined : onCancel}
  63. >
  64. <Card
  65. className={`w-full max-w-md ${cardClassName ?? ''}`}
  66. onClick={(e: React.MouseEvent) => e.stopPropagation()}
  67. >
  68. <CardContent className="p-6">
  69. <div className="flex items-start gap-4">
  70. <div className={`p-2 rounded-full bg-bambu-dark ${styles.icon}`}>
  71. <AlertTriangle className="w-6 h-6" />
  72. </div>
  73. <div className="flex-1">
  74. <h3 className="text-lg font-semibold text-white mb-2">{title}</h3>
  75. <p className="text-bambu-gray text-sm">{message}</p>
  76. </div>
  77. </div>
  78. <div className="flex gap-3 mt-6">
  79. <Button
  80. variant={cancelVariant ?? 'secondary'}
  81. onClick={onCancel}
  82. className="flex-1"
  83. disabled={isLoading}
  84. >
  85. {resolvedCancelText}
  86. </Button>
  87. <Button
  88. onClick={onConfirm}
  89. className={`flex-1 ${styles.button}`}
  90. disabled={isLoading}
  91. >
  92. {isLoading ? (
  93. <>
  94. <Loader2 className="w-4 h-4 mr-2 animate-spin" />
  95. {resolvedLoadingText}
  96. </>
  97. ) : (
  98. resolvedConfirmText
  99. )}
  100. </Button>
  101. </div>
  102. </CardContent>
  103. </Card>
  104. </div>
  105. );
  106. }