import { createContext, useContext, FC, useCallback, useMemo, useState, } from 'react' import GenericMessageModal, { GenericMessageModalOwnProps, } from '@/features/ide-react/components/modals/generic-message-modal' type ModalsContextValue = { genericModalVisible: boolean showGenericMessageModal: ( title: GenericMessageModalOwnProps['title'], message: GenericMessageModalOwnProps['message'] ) => void } const ModalsContext = createContext(undefined) export const ModalsContextProvider: FC = ({ children }) => { const [showGenericModal, setShowGenericModal] = useState(false) const [genericMessageModalData, setGenericMessageModalData] = useState({ title: '', message: '' }) const handleHideGenericModal = useCallback(() => { setShowGenericModal(false) }, []) const showGenericMessageModal = useCallback( ( title: GenericMessageModalOwnProps['title'], message: GenericMessageModalOwnProps['message'] ) => { setGenericMessageModalData({ title, message }) setShowGenericModal(true) }, [] ) const value = useMemo( () => ({ showGenericMessageModal, genericModalVisible: showGenericModal, }), [showGenericMessageModal, showGenericModal] ) return ( {children} ) } export function useModalsContext(): ModalsContextValue { const context = useContext(ModalsContext) if (!context) { throw new Error( 'useModalsContext is only available inside ModalsContextProvider' ) } return context }