* 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 <>
44 lines
1.1 KiB
TypeScript
44 lines
1.1 KiB
TypeScript
import React, { createContext, useContext, useState, ReactNode } from 'react';
|
|
|
|
interface ModalContextType {
|
|
isAnyModalOpen: boolean;
|
|
openModal: () => void;
|
|
closeModal: () => void;
|
|
modalCount: number;
|
|
}
|
|
|
|
const ModalContext = createContext<ModalContextType | undefined>(undefined);
|
|
|
|
export const useModal = () => {
|
|
const context = useContext(ModalContext);
|
|
if (context === undefined) {
|
|
throw new Error('useModal must be used within a ModalProvider');
|
|
}
|
|
return context;
|
|
};
|
|
|
|
interface ModalProviderProps {
|
|
children: ReactNode;
|
|
}
|
|
|
|
export const ModalProvider: React.FC<ModalProviderProps> = ({ children }) => {
|
|
const [modalCount, setModalCount] = useState(0);
|
|
|
|
const openModal = () => {
|
|
setModalCount((prev) => prev + 1);
|
|
};
|
|
|
|
const closeModal = () => {
|
|
setModalCount((prev) => Math.max(0, prev - 1));
|
|
};
|
|
|
|
const isAnyModalOpen = modalCount > 0;
|
|
|
|
return (
|
|
<ModalContext.Provider
|
|
value={{ isAnyModalOpen, openModal, closeModal, modalCount }}
|
|
>
|
|
{children}
|
|
</ModalContext.Provider>
|
|
);
|
|
};
|