Files
OmniRoute/src/shared/components/Modal.tsx
Bob.Hou 25bc16d87e fix(dashboard): batch delete no longer toasts failure after success (#12711)
Validado numa worktree combinada com as 16 PRs desta leva sobre `release/v3.8.51`: typecheck:core limpo, check-file-size e check-changelog-integrity OK, complexity 2788/3218 e cognitive 1261/1437 (ambos sob a baseline), ESLint 0 erros nos 152 arquivos alterados, 771 testes unitários focados, 49 de integração e a suíte vitest:ui completa (2149) verdes.

Além do bug do toast, esta PR foi a que derrubou os três base-reds vivos do tip: o fragmento `changelog.d/fixes/reset-aware-model-family.md` sem o `- ` inicial, o registro do `tests/unit/reset-aware-request-scope-12600.test.ts` no `stryker.conf.json` e o `TS2554` do glm. O `check-changelog-integrity` voltou a passar aqui por causa dela.

O diagnóstico do MouseEvent é o que dá o valor: `onConfirm` chegava como handler de clique nativo e `handleBatchDeleteConfirm` tratava qualquer primeiro argumento truthy como callback. O cinto (`typeof`) e o suspensório (o wrap no ConfirmModal) juntos estão certos — só um dos dois deixaria a porta aberta para o próximo caller.
2026-09-07 08:56:36 -03:00

288 lines
7.9 KiB
TypeScript

"use client";
import { useEffect, useRef, useId } from "react";
import { useTranslations } from "next-intl";
import { cn } from "@/shared/utils/cn";
import Button, { type ButtonVariant } from "./Button";
// #6265 — preset for content-heavy modals: caps height on the OUTERMOST dialog
// wrapper only (single scroll owner) and keeps the inner body plain (no
// independent max-h/overflow), avoiding a double height cap that clips content.
export const TALL_MODAL_PROPS = {
className: "max-h-[90vh] overflow-y-auto",
bodyClassName: "p-6",
};
interface ModalProps {
isOpen: boolean;
onClose: () => void;
title?: React.ReactNode;
children?: React.ReactNode;
footer?: React.ReactNode;
size?: "sm" | "md" | "lg" | "xl" | "full";
closeOnOverlay?: boolean;
showCloseButton?: boolean;
className?: string;
bodyClassName?: string;
compactHeader?: boolean;
maxWidth?: string;
}
interface ConfirmModalProps {
isOpen: boolean;
onClose: () => void;
onConfirm: () => void | Promise<void>;
title?: React.ReactNode;
message: React.ReactNode;
confirmText?: React.ReactNode;
cancelText?: React.ReactNode;
variant?: ButtonVariant;
loading?: boolean;
}
export default function Modal({
isOpen,
onClose,
title,
children,
footer,
size = "md",
closeOnOverlay = true,
showCloseButton = true,
className,
bodyClassName,
compactHeader = false,
}: ModalProps) {
const t = useTranslations("common");
const titleId = useId();
const dialogRef = useRef(null);
const previouslyFocusedRef = useRef<HTMLElement | null>(null);
const sizes = {
sm: "max-w-sm",
md: "max-w-md",
lg: "max-w-lg",
xl: "max-w-xl",
full: "max-w-4xl",
};
// Lock body scroll when modal is open
useEffect(() => {
if (isOpen) {
document.body.style.overflow = "hidden";
} else {
document.body.style.overflow = "";
}
return () => {
document.body.style.overflow = "";
};
}, [isOpen]);
// Handle escape key
useEffect(() => {
const handleEscape = (e) => {
if (e.key === "Escape" && isOpen) {
onClose();
}
};
document.addEventListener("keydown", handleEscape);
return () => document.removeEventListener("keydown", handleEscape);
}, [isOpen, onClose]);
// Return keyboard users to the control that opened the dialog.
useEffect(() => {
if (!isOpen) return;
const activeElement = document.activeElement;
previouslyFocusedRef.current = activeElement instanceof HTMLElement ? activeElement : null;
return () => {
const previouslyFocused = previouslyFocusedRef.current;
previouslyFocusedRef.current = null;
if (previouslyFocused?.isConnected) {
previouslyFocused.focus();
}
};
}, [isOpen]);
// Focus trap
useEffect(() => {
if (!isOpen || !dialogRef.current) return;
const dialog = dialogRef.current;
const focusableSelector =
'button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])';
// Focus first focusable element
const firstFocusable = dialog.querySelector(focusableSelector);
const focusTimer = firstFocusable
? window.setTimeout(() => firstFocusable.focus(), 50)
: undefined;
const handleTab = (e) => {
if (e.key !== "Tab") return;
const focusable = [...dialog.querySelectorAll(focusableSelector)];
if (focusable.length === 0) return;
const first = focusable[0];
const last = focusable[focusable.length - 1];
if (e.shiftKey && document.activeElement === first) {
e.preventDefault();
last.focus();
} else if (!e.shiftKey && document.activeElement === last) {
e.preventDefault();
first.focus();
}
};
dialog.addEventListener("keydown", handleTab);
return () => {
if (focusTimer !== undefined) window.clearTimeout(focusTimer);
dialog.removeEventListener("keydown", handleTab);
};
}, [isOpen]);
if (!isOpen) return null;
return (
<div className="fixed inset-0 z-50 flex items-center justify-center p-4">
{/* Overlay */}
<div
className="absolute inset-0 bg-black/30 backdrop-blur-sm"
onClick={closeOnOverlay ? onClose : undefined}
aria-hidden="true"
/>
{/* Modal content */}
<div
ref={dialogRef}
role="dialog"
aria-modal="true"
aria-labelledby={title ? titleId : undefined}
className={cn(
"relative w-full bg-surface",
"border border-black/10 dark:border-white/10",
"rounded-card shadow-2xl",
"animate-in fade-in zoom-in-95 duration-200",
sizes[size],
className
)}
>
{/* Header */}
{(title || showCloseButton) && (
<div
className={cn(
"flex items-center justify-between border-b border-black/5 dark:border-white/5",
compactHeader ? "px-4 py-2.5" : "p-6"
)}
>
<div className="flex items-center min-w-0">
<div
className={cn(
"flex items-center gap-1.5 mr-3 shrink-0",
compactHeader ? "" : "gap-2 mr-4"
)}
aria-hidden="true"
>
<div
className={cn(
"rounded-full bg-[#FF5F56]",
compactHeader ? "w-2.5 h-2.5" : "w-3 h-3"
)}
/>
<div
className={cn(
"rounded-full bg-[#FFBD2E]",
compactHeader ? "w-2.5 h-2.5" : "w-3 h-3"
)}
/>
<div
className={cn(
"rounded-full bg-[#27C93F]",
compactHeader ? "w-2.5 h-2.5" : "w-3 h-3"
)}
/>
</div>
{title && (
<h2
id={titleId}
className={cn(
"font-semibold text-text-main truncate min-w-0",
compactHeader ? "text-sm" : "text-lg"
)}
>
{title}
</h2>
)}
</div>
{showCloseButton && (
<button
onClick={onClose}
aria-label={t("close")}
className="p-1.5 rounded-lg text-text-muted hover:bg-black/5 dark:hover:bg-white/5 transition-colors shrink-0"
>
<span className="material-symbols-outlined text-[20px]" aria-hidden="true">
close
</span>
</button>
)}
</div>
)}
{/* Body */}
<div className={bodyClassName ?? "p-6 max-h-[calc(80vh-140px)] overflow-y-auto"}>
{children}
</div>
{/* Footer */}
{footer && (
<div className="flex items-center justify-end gap-3 p-6 border-t border-black/5 dark:border-white/5">
{footer}
</div>
)}
</div>
</div>
);
}
// Confirm Modal helper
export function ConfirmModal({
isOpen,
onClose,
onConfirm,
title,
message,
confirmText,
cancelText,
variant = "danger",
loading = false,
}: ConfirmModalProps) {
const t = useTranslations("common");
const resolvedTitle = title ?? t("confirmTitle");
const resolvedConfirmText = confirmText ?? t("confirmAction");
const resolvedCancelText = cancelText ?? t("cancel");
return (
<Modal
isOpen={isOpen}
onClose={onClose}
title={resolvedTitle}
size="sm"
footer={
<>
<Button variant="ghost" onClick={onClose} disabled={loading}>
{resolvedCancelText}
</Button>
<Button variant={variant} onClick={() => void onConfirm()} loading={loading}>
{resolvedConfirmText}
</Button>
</>
}
>
<p className="text-text-muted">{message}</p>
</Modal>
);
}