Files
ox_lib/web/src/features/menu/context/Item.tsx

125 lines
4.0 KiB
TypeScript
Raw Normal View History

import {
Portal,
Popover,
PopoverTrigger,
PopoverBody,
PopoverContent,
Box,
Text,
Flex,
Spacer,
2022-06-11 11:49:25 +02:00
Image,
HStack,
2022-08-27 15:58:36 +02:00
} from '@chakra-ui/react';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { Option, ContextMenuProps } from '../../../interfaces/context';
import { fetchNui } from '../../../utils/fetchNui';
2022-03-28 16:49:07 +02:00
const openMenu = (id: string | undefined) => {
fetchNui<ContextMenuProps>('openContext', {id: id, back: false});
2022-03-28 16:49:07 +02:00
};
const clickContext = (id: string) => {
2022-08-27 15:58:36 +02:00
fetchNui('clickContext', id);
2022-03-28 16:49:07 +02:00
};
const Item: React.FC<{
option: [string, Option];
}> = ({ option }) => {
return (
<>
2022-08-27 15:58:36 +02:00
<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"
2022-08-27 15:58:36 +02:00
_hover={{ bg: 'gray.700' }}
>
2022-03-28 16:49:07 +02:00
<Flex
w="100%"
alignItems="center"
2022-08-27 15:58:36 +02:00
onClick={() => (option[1].menu ? openMenu(option[1].menu) : clickContext(option[0]))}
2022-03-28 16:49:07 +02:00
>
{option[1]?.icon && (
<FontAwesomeIcon
fixedWidth
icon={option[1].icon}
fontSize={20}
style={{
marginRight: 10,
2022-08-27 15:58:36 +02:00
justifySelf: 'center',
color: option[1].iconColor,
}}
/>
)}
<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) && option[1].arrow !== false && (
<>
<Spacer />
2022-08-27 15:58:36 +02:00
<Box alignSelf="center" justifySelf="center" mr={4} fontSize="xl">
<FontAwesomeIcon icon="chevron-right" />
</Box>
</>
)}
</Flex>
<Portal>
{(option[1].metadata || option[1].image) && (
<PopoverContent
fontFamily="Poppins"
bg="gray.800"
outline="none"
border="none"
w="fit-content"
maxW="2xs"
>
<PopoverBody>
2022-06-11 11:49:25 +02:00
<>
{option[1].image && <Image src={option[1].image} />}
{Array.isArray(option[1].metadata) ? (
2022-08-27 15:58:36 +02:00
option[1].metadata.map((metadata: string | { label: string; value: any }, index: number) => (
<Text key={`context-metadata-${index}`}>
{typeof metadata === 'string' ? `${metadata}` : `${metadata.label}: ${metadata.value}`}
</Text>
))
2022-06-11 11:49:25 +02:00
) : (
<>
2022-08-27 15:58:36 +02:00
{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>
))}
2022-06-11 11:49:25 +02:00
</>
)}
</>
</PopoverBody>
</PopoverContent>
)}
</Portal>
</Box>
</PopoverTrigger>
</Popover>
</>
);
};
export default Item;