ConfirmModal.tsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import { useEffect, type ReactNode } 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. // Tailwind z-index utility applied to the fixed overlay. Defaults to
  14. // ``z-50``. Use a higher value (e.g. ``z-[110]``) when this confirm
  15. // dialog is rendered from inside another modal that uses ``z-[100]`` —
  16. // without it the confirm dialog sits behind its parent (#1336 follow-up).
  17. overlayZIndex?: string;
  18. variant?: 'danger' | 'warning' | 'default';
  19. isLoading?: boolean;
  20. loadingText?: string;
  21. // Disable the confirm button without a loading spinner. Used when an
  22. // external precondition forbids the action (e.g. #1734 — a related queue
  23. // item is mid-print, so the archive delete must be blocked at the UI
  24. // layer too even though the backend will 409 anyway).
  25. confirmDisabled?: boolean;
  26. // Optional extra content rendered between the message and the buttons —
  27. // used for opt-in checkboxes (e.g. the "Also remove from statistics"
  28. // toggle in the archive delete confirmation, #1343).
  29. children?: ReactNode;
  30. // When false, a click on the backdrop and Escape do nothing; only the
  31. // buttons close it. For a dialog holding typed input (the maintenance
  32. // reset comment) that a stray click would otherwise throw away.
  33. dismissible?: boolean;
  34. onConfirm: () => void;
  35. onCancel: () => void;
  36. }
  37. export function ConfirmModal({
  38. title,
  39. message,
  40. confirmText,
  41. cancelText,
  42. cancelVariant,
  43. cardClassName,
  44. overlayZIndex,
  45. variant = 'default',
  46. isLoading = false,
  47. loadingText,
  48. confirmDisabled = false,
  49. children,
  50. dismissible = true,
  51. onConfirm,
  52. onCancel,
  53. }: ConfirmModalProps) {
  54. const { t } = useTranslation();
  55. const resolvedConfirmText = confirmText ?? t('common.confirm');
  56. const resolvedCancelText = cancelText ?? t('common.cancel');
  57. const resolvedLoadingText = loadingText ?? t('common.loading');
  58. // Close on Escape key (but not while loading, nor when not dismissible)
  59. useEffect(() => {
  60. const handleKeyDown = (e: KeyboardEvent) => {
  61. if (e.key === 'Escape' && !isLoading && dismissible) onCancel();
  62. };
  63. window.addEventListener('keydown', handleKeyDown);
  64. return () => window.removeEventListener('keydown', handleKeyDown);
  65. }, [onCancel, isLoading, dismissible]);
  66. const variantStyles = {
  67. danger: {
  68. icon: 'text-red-600 dark:text-red-400',
  69. button: 'bg-red-500 hover:bg-red-600',
  70. },
  71. warning: {
  72. icon: 'text-yellow-600 dark:text-yellow-400',
  73. button: 'bg-yellow-500 hover:bg-yellow-600 text-black',
  74. },
  75. default: {
  76. icon: 'text-bambu-green',
  77. button: 'bg-bambu-green hover:bg-bambu-green-dark',
  78. },
  79. };
  80. const styles = variantStyles[variant];
  81. return (
  82. <div
  83. className={`fixed inset-0 bg-black/50 flex items-center justify-center p-4 ${overlayZIndex ?? 'z-50'}`}
  84. onClick={isLoading || !dismissible ? undefined : onCancel}
  85. >
  86. <Card
  87. className={`w-full max-w-md ${cardClassName ?? ''}`}
  88. onClick={(e: React.MouseEvent) => e.stopPropagation()}
  89. >
  90. <CardContent className="p-6">
  91. <div className="flex items-start gap-4">
  92. <div className={`p-2 rounded-full bg-bambu-dark ${styles.icon}`}>
  93. <AlertTriangle className="w-6 h-6" />
  94. </div>
  95. <div className="flex-1">
  96. <h3 className="text-lg font-semibold text-white mb-2">{title}</h3>
  97. <p className="text-bambu-gray text-sm whitespace-pre-line">{message}</p>
  98. {children && <div className="mt-4">{children}</div>}
  99. </div>
  100. </div>
  101. <div className="flex gap-3 mt-6">
  102. <Button
  103. variant={cancelVariant ?? 'secondary'}
  104. onClick={onCancel}
  105. className="flex-1"
  106. disabled={isLoading}
  107. >
  108. {resolvedCancelText}
  109. </Button>
  110. <Button
  111. onClick={onConfirm}
  112. className={`flex-1 ${styles.button}`}
  113. disabled={isLoading || confirmDisabled}
  114. >
  115. {isLoading ? (
  116. <>
  117. <Loader2 className="w-4 h-4 mr-2 animate-spin" />
  118. {resolvedLoadingText}
  119. </>
  120. ) : (
  121. resolvedConfirmText
  122. )}
  123. </Button>
  124. </div>
  125. </CardContent>
  126. </Card>
  127. </div>
  128. );
  129. }