tududi/frontend/hooks/useModalManager.ts
2025-06-27 22:36:04 +03:00

28 lines
No EOL
722 B
TypeScript

import { useEffect } from 'react';
import { useModal } from '../contexts/ModalContext';
/**
* Hook to automatically manage modal state with the global modal context
* @param isOpen - Whether the modal is currently open
* @returns Object with the modal context functions
*/
export const useModalManager = (isOpen: boolean) => {
const modalContext = useModal();
useEffect(() => {
if (isOpen) {
modalContext.openModal();
} else {
modalContext.closeModal();
}
// Cleanup function to ensure we close the modal if component unmounts while open
return () => {
if (isOpen) {
modalContext.closeModal();
}
};
}, [isOpen, modalContext]);
return modalContext;
};