tududi/frontend/hooks/useModalManager.ts
Antonis Anastasiadis 220bc92b4a
Lint frontend (#131)
* Add lint-fix npm target

* Sync eslint+plugins with backend

* Add prettier

* Ignore no-explicit-any lint rule for now

* Silence eslint react warning

* Format frontend via prettier

* Lint frontend.

---------

Co-authored-by: antanst <>
2025-07-09 12:23:55 +03:00

28 lines
787 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;
};