'use client' import { useEffect } from 'react' type Width = | 'sm:max-w-sm' | 'sm:max-w-md' | 'sm:max-w-lg' | 'sm:max-w-xl' | 'sm:max-w-2xl' | string type ModalProps = { id: string title: string children?: React.ReactNode show: boolean onClose?: () => void onSave?: () => void hideCloseButton?: boolean closeButtonColor?: 'blue' | 'red' | 'green' | 'teal' closeButtonTitle?: string disableSave?: boolean maxWidth?: Width } export default function Modal({ id, title, children, show, onClose, onSave, hideCloseButton = false, closeButtonColor = 'blue', closeButtonTitle = 'Speichern', disableSave, maxWidth = 'sm:max-w-lg', }: ModalProps) { /* ───────── Overlay-Lifecycle ───────── */ useEffect(() => { const modalEl = document.getElementById(id) const hs = (window as any).HSOverlay if (!modalEl || !hs) return /* ► Collection kann undefined oder ein Objekt sein. ► Wir sichern uns ab und behandeln nur echte Arrays. */ const getCollection = (): any[] => Array.isArray(hs.collection) ? hs.collection : [] const destroyIfExists = () => { const inst = getCollection().find((i) => i.element === modalEl) inst?.destroy?.() if (inst) { hs.collection = getCollection().filter((i) => i !== inst) } } const handleClose = () => onClose?.() modalEl.addEventListener('hsOverlay:close', handleClose) try { if (show) { destroyIfExists() hs.autoInit?.() hs.open?.(modalEl) } else { hs.close?.(modalEl) destroyIfExists() } } catch (err) { // eslint-disable-next-line no-console console.error('[Modal] HSOverlay Fehler:', err) } return () => { modalEl.removeEventListener('hsOverlay:close', handleClose) destroyIfExists() } }, [show, id, onClose]) /* ───────── Render ───────── */ return (