import { useNuiEvent } from "../../../hooks/useNuiEvent"; import { Box, Text, Flex, ScaleFade } from "@chakra-ui/react"; import { debugData } from "../../../utils/debugData"; import { useEffect, useState } from "react"; import { ContextMenuProps } from "../../../interfaces"; import Item from "./Item"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { fetchNui } from "../../../utils/fetchNui"; debugData([ { action: "showContext", data: { title: "Vehicle garage", options: { "Dinka Blista": { description: "Super cool vehicle", menu: "some_other_identifier", metadata: { Plate: "KLT 192", Status: "In garage", Health: "30%", }, }, "Elegy Nitro": { description: "Even cooler vehicle", metadata: ["Plate: JGM 971", "Status: In garage"], }, Burger: { description: "Make a delicious burger", metadata: { Bun: 3, Lettuce: 2, Meat: 1, Tomato: 1, Cheese: 2, }, }, }, }, }, ]); const openMenu = (id: string | undefined) => { fetchNui("openContext", id); }; const ContextMenu: React.FC = () => { const [visible, setVisible] = useState(false); const [contextMenu, setContextMenu] = useState({ title: "", options: { "": { description: "", metadata: [] } }, }); // Hides the context menu on ESC useEffect(() => { if (!visible) return; const keyHandler = (e: KeyboardEvent) => { if (["Escape"].includes(e.code)) { setVisible(false); fetchNui("closeContext"); } }; window.addEventListener("keydown", keyHandler); return () => window.removeEventListener("keydown", keyHandler); }, [visible]); useNuiEvent("hideContext", () => setVisible(false)); useNuiEvent("showContext", async (data) => { if (visible) { setVisible(false); await new Promise((resolve) => setTimeout(resolve, 100)); } setContextMenu(data); setVisible(true); }); return ( {contextMenu.menu && ( openMenu(contextMenu.menu)} > )} {contextMenu.title} {Object.entries(contextMenu.options).map((option, index) => ( ))} ); }; export default ContextMenu;