import React, { Context, createContext, useContext, useEffect, useState } from 'react'; import { useNuiEvent } from '../hooks/useNuiEvent'; import { fetchNui } from '../utils/fetchNui'; const VisibilityCtx = createContext(null); interface VisibilityProviderValue { setVisible: (visible: boolean) => void; visible: boolean; } // This should be mounted at the top level of your application, it is currently set to // apply a CSS visibility value. If this is non-performant, this should be customized. export const VisibilityProvider: React.FC = ({ children }) => { const [visible, setVisible] = useState(false); useNuiEvent('openUI', setVisible); // Handle pressing escape/backspace useEffect(() => { // Only attach listener when we are visible if (!visible) return; const keyHandler = (e: KeyboardEvent) => { if (['Escape'].includes(e.code)) { fetchNui('exit'); setVisible(false); } }; window.addEventListener('keydown', keyHandler); return () => window.removeEventListener('keydown', keyHandler); }, [visible]); return (
{children}
); }; export const useVisibility = () => useContext(VisibilityCtx as Context);