2022-04-01 21:41:53 +02:00
|
|
|
import { useNuiEvent } from "../../hooks/useNuiEvent";
|
2022-03-27 18:17:26 +02:00
|
|
|
import { Box, Text, Flex, ScaleFade } from "@chakra-ui/react";
|
2022-04-01 21:41:53 +02:00
|
|
|
import { debugData } from "../../utils/debugData";
|
2022-04-01 21:22:14 +02:00
|
|
|
import { useEffect, useState } from "react";
|
2022-05-03 15:08:28 +02:00
|
|
|
import { ContextMenuProps } from "../../interfaces/context";
|
2022-03-27 18:17:26 +02:00
|
|
|
import Item from "./Item";
|
2022-03-28 16:49:07 +02:00
|
|
|
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
|
2022-04-01 21:41:53 +02:00
|
|
|
import { fetchNui } from "../../utils/fetchNui";
|
2022-03-27 18:17:26 +02:00
|
|
|
|
2022-03-28 16:49:07 +02:00
|
|
|
debugData<ContextMenuProps>([
|
2022-03-27 18:17:26 +02:00
|
|
|
{
|
|
|
|
|
action: "showContext",
|
|
|
|
|
data: {
|
|
|
|
|
title: "Vehicle garage",
|
|
|
|
|
options: {
|
2022-03-28 16:49:07 +02:00
|
|
|
"Dinka Blista": {
|
2022-03-27 18:17:26 +02:00
|
|
|
description: "Super cool vehicle",
|
2022-03-28 16:49:07 +02:00
|
|
|
menu: "some_other_identifier",
|
2022-03-27 18:17:26 +02:00
|
|
|
metadata: {
|
2022-03-28 16:49:07 +02:00
|
|
|
Plate: "KLT 192",
|
|
|
|
|
Status: "In garage",
|
|
|
|
|
Health: "30%",
|
2022-03-27 18:17:26 +02:00
|
|
|
},
|
|
|
|
|
},
|
2022-03-28 16:49:07 +02:00
|
|
|
"Elegy Nitro": {
|
2022-03-27 18:17:26 +02:00
|
|
|
description: "Even cooler vehicle",
|
|
|
|
|
metadata: ["Plate: JGM 971", "Status: In garage"],
|
|
|
|
|
},
|
2022-03-28 16:49:07 +02:00
|
|
|
Burger: {
|
2022-03-27 18:17:26 +02:00
|
|
|
description: "Make a delicious burger",
|
|
|
|
|
metadata: {
|
2022-03-28 16:49:07 +02:00
|
|
|
Bun: 3,
|
|
|
|
|
Lettuce: 2,
|
|
|
|
|
Meat: 1,
|
|
|
|
|
Tomato: 1,
|
|
|
|
|
Cheese: 2,
|
2022-03-27 18:17:26 +02:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
]);
|
|
|
|
|
|
2022-03-28 16:49:07 +02:00
|
|
|
const openMenu = (id: string | undefined) => {
|
|
|
|
|
fetchNui<ContextMenuProps>("openContext", id);
|
|
|
|
|
};
|
|
|
|
|
|
2022-03-27 18:17:26 +02:00
|
|
|
const ContextMenu: React.FC = () => {
|
|
|
|
|
const [visible, setVisible] = useState(false);
|
2022-03-28 16:49:07 +02:00
|
|
|
const [contextMenu, setContextMenu] = useState<ContextMenuProps>({
|
2022-03-27 18:17:26 +02:00
|
|
|
title: "",
|
2022-03-28 16:49:07 +02:00
|
|
|
options: { "": { description: "", metadata: [] } },
|
2022-03-27 18:17:26 +02:00
|
|
|
});
|
|
|
|
|
|
2022-04-01 21:22:14 +02:00
|
|
|
// 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));
|
|
|
|
|
|
2022-03-28 16:49:07 +02:00
|
|
|
useNuiEvent<ContextMenuProps>("showContext", async (data) => {
|
|
|
|
|
if (visible) {
|
|
|
|
|
setVisible(false);
|
|
|
|
|
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
|
|
|
}
|
2022-03-27 18:17:26 +02:00
|
|
|
setContextMenu(data);
|
|
|
|
|
setVisible(true);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Flex
|
|
|
|
|
position="absolute"
|
|
|
|
|
w="75%"
|
|
|
|
|
h="80%"
|
|
|
|
|
justifyContent="flex-end"
|
|
|
|
|
alignItems="center"
|
|
|
|
|
>
|
|
|
|
|
<ScaleFade in={visible} unmountOnExit>
|
|
|
|
|
<Box w="xs" h={580}>
|
2022-03-28 16:49:07 +02:00
|
|
|
<Flex justifyContent="center" alignItems="center" mb={3}>
|
|
|
|
|
{contextMenu.menu && (
|
|
|
|
|
<Box
|
|
|
|
|
borderRadius="md"
|
|
|
|
|
bg="gray.800"
|
|
|
|
|
h="100%"
|
|
|
|
|
flex="1 15%"
|
|
|
|
|
textAlign="center"
|
|
|
|
|
marginRight={2}
|
|
|
|
|
p={2}
|
|
|
|
|
_hover={{ bg: "gray.700" }}
|
|
|
|
|
transition="300ms"
|
|
|
|
|
onClick={() => openMenu(contextMenu.menu)}
|
|
|
|
|
>
|
|
|
|
|
<FontAwesomeIcon icon="chevron-left" />
|
|
|
|
|
</Box>
|
|
|
|
|
)}
|
|
|
|
|
<Box borderRadius="md" bg="gray.800" flex="1 85%">
|
|
|
|
|
<Text
|
|
|
|
|
fontFamily="Poppins"
|
|
|
|
|
fontSize="md"
|
|
|
|
|
p={2}
|
|
|
|
|
textAlign="center"
|
|
|
|
|
fontWeight="light"
|
|
|
|
|
>
|
|
|
|
|
{contextMenu.title}
|
|
|
|
|
</Text>
|
|
|
|
|
</Box>
|
|
|
|
|
</Flex>
|
2022-03-27 18:17:26 +02:00
|
|
|
<Box maxH={560} overflowY="scroll">
|
|
|
|
|
{Object.entries(contextMenu.options).map((option, index) => (
|
|
|
|
|
<Item option={option} key={`context-item-${index}`} />
|
|
|
|
|
))}
|
|
|
|
|
</Box>
|
|
|
|
|
</Box>
|
|
|
|
|
</ScaleFade>
|
|
|
|
|
</Flex>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default ContextMenu;
|