Files
ox_lib/web/src/features/menu/Item.tsx
Luke 62d4812f24 feat(web/context): allow manually sorting the options
In order to have the options in the same order as defined, you need to not use keys, instead a normal index table and define the title for the option
2022-05-08 13:00:23 +02:00

137 lines
3.9 KiB
TypeScript

import {
Portal,
Popover,
PopoverTrigger,
PopoverBody,
PopoverContent,
Box,
Text,
Flex,
Spacer,
} from "@chakra-ui/react";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { Option, ContextMenuProps } from "../../interfaces/context";
import { fetchNui } from "../../utils/fetchNui";
interface DataProps {
event?: string;
serverEvent?: string;
args?: any;
}
const openMenu = (id: string | undefined) => {
fetchNui<ContextMenuProps>("openContext", id);
};
const clickContext = (data: DataProps) => {
if (!data.event && !data.serverEvent) return;
fetchNui("clickContext", data);
};
const Item: React.FC<{
option: [string, Option];
}> = ({ option }) => {
return (
<>
<Popover
placement="right-start"
trigger="hover"
eventListeners={{ scroll: true }}
isLazy
>
<PopoverTrigger>
<Box
bg="gray.800"
borderRadius="md"
h="fit-content"
w="100%"
p={2}
mb={1}
fontFamily="Poppins"
fontSize="md"
transition="300ms"
_hover={{ bg: "gray.700" }}
>
<Flex
w="100%"
alignItems="center"
onClick={() =>
option[1].menu
? openMenu(option[1].menu)
: clickContext({
event: option[1].event,
serverEvent: option[1].serverEvent,
args: option[1].args,
})
}
>
<Box>
<Box paddingBottom={option[1].description ? 1 : 0}>
<Text w="100%" fontWeight="medium">
{option[1].title ? option[1].title : option[0]}
</Text>
</Box>
{option[1].description && (
<Box paddingBottom={1}>
<Text>{option[1].description}</Text>
</Box>
)}
</Box>
{(option[1].menu || option[1].arrow) && (
<>
<Spacer />
<Box
alignSelf="center"
justifySelf="center"
mr={4}
fontSize="xl"
>
<FontAwesomeIcon icon="chevron-right" />
</Box>
</>
)}
</Flex>
<Portal>
{option[1].metadata && (
<PopoverContent
fontFamily="Poppins"
bg="gray.800"
outline="none"
border="none"
w="fit-content"
maxW="2xs"
>
<PopoverBody>
{Array.isArray(option[1].metadata) ? (
option[1].metadata.map(
(metadata: string, index: number) => (
<Text key={`context-metadata-${index}`}>
{metadata}
</Text>
)
)
) : (
<>
{typeof option[1].metadata === "object" &&
Object.entries(option[1].metadata).map(
(metadata: { [key: string]: any }, index) => (
<Text key={`context-metadata-${index}`}>
{metadata[0]}: {metadata[1]}
</Text>
)
)}
</>
)}
</PopoverBody>
</PopoverContent>
)}
</Portal>
</Box>
</PopoverTrigger>
</Popover>
</>
);
};
export default Item;