2022-08-27 15:58:36 +02:00
|
|
|
import { useNuiEvent } from '../../../hooks/useNuiEvent';
|
2022-12-17 14:27:55 +01:00
|
|
|
import { Box, Stack, Text, Flex, Transition } from '@mantine/core';
|
2022-08-27 15:58:36 +02:00
|
|
|
import { useEffect, useState } from 'react';
|
|
|
|
|
import { ContextMenuProps } from '../../../interfaces/context';
|
2022-12-17 14:27:55 +01:00
|
|
|
import ContextButton from './components/ContextButton';
|
2022-08-27 15:58:36 +02:00
|
|
|
import { fetchNui } from '../../../utils/fetchNui';
|
2022-12-15 17:43:14 -05:00
|
|
|
import ReactMarkdown from 'react-markdown';
|
2022-12-17 14:27:55 +01:00
|
|
|
import HeaderButton from './components/HeaderButton';
|
2022-03-27 18:17:26 +02:00
|
|
|
|
2022-03-28 16:49:07 +02:00
|
|
|
const openMenu = (id: string | undefined) => {
|
2022-11-06 11:01:39 +01:00
|
|
|
fetchNui<ContextMenuProps>('openContext', { id: id, back: true });
|
2022-03-28 16:49:07 +02:00
|
|
|
};
|
|
|
|
|
|
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-08-27 15:58:36 +02:00
|
|
|
title: '',
|
|
|
|
|
options: { '': { description: '', metadata: [] } },
|
2022-03-27 18:17:26 +02:00
|
|
|
});
|
|
|
|
|
|
2022-07-25 21:32:11 +02:00
|
|
|
const closeContext = () => {
|
2022-09-08 10:05:44 +02:00
|
|
|
if (contextMenu.canClose === false) return;
|
2022-07-25 21:32:11 +02:00
|
|
|
setVisible(false);
|
2022-08-27 15:58:36 +02:00
|
|
|
fetchNui('closeContext');
|
2022-07-25 21:32:11 +02:00
|
|
|
};
|
|
|
|
|
|
2022-04-01 21:22:14 +02:00
|
|
|
// Hides the context menu on ESC
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (!visible) return;
|
|
|
|
|
|
|
|
|
|
const keyHandler = (e: KeyboardEvent) => {
|
2022-08-27 15:58:36 +02:00
|
|
|
if (['Escape'].includes(e.code)) closeContext();
|
2022-04-01 21:22:14 +02:00
|
|
|
};
|
|
|
|
|
|
2022-08-27 15:58:36 +02:00
|
|
|
window.addEventListener('keydown', keyHandler);
|
2022-04-01 21:22:14 +02:00
|
|
|
|
2022-08-27 15:58:36 +02:00
|
|
|
return () => window.removeEventListener('keydown', keyHandler);
|
2022-04-01 21:22:14 +02:00
|
|
|
}, [visible]);
|
|
|
|
|
|
2022-08-27 15:58:36 +02:00
|
|
|
useNuiEvent('hideContext', () => setVisible(false));
|
2022-04-01 21:22:14 +02:00
|
|
|
|
2022-08-27 15:58:36 +02:00
|
|
|
useNuiEvent<ContextMenuProps>('showContext', async (data) => {
|
2022-03-28 16:49:07 +02:00
|
|
|
if (visible) {
|
|
|
|
|
setVisible(false);
|
|
|
|
|
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
|
|
|
}
|
2022-03-27 18:17:26 +02:00
|
|
|
setContextMenu(data);
|
|
|
|
|
setVisible(true);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return (
|
2022-12-17 14:27:55 +01:00
|
|
|
<Transition mounted={visible} transition="pop" duration={100} exitDuration={100}>
|
|
|
|
|
{(styles) => (
|
|
|
|
|
<Box style={styles} sx={{ position: 'absolute', top: '15%', right: '25%' }} w={320} h={580}>
|
|
|
|
|
<Flex justify="center" align="center" mb={10} gap={6}>
|
2022-03-28 16:49:07 +02:00
|
|
|
{contextMenu.menu && (
|
2022-12-17 14:27:55 +01:00
|
|
|
<HeaderButton icon="chevron-left" iconSize={16} handleClick={() => openMenu(contextMenu.menu)} />
|
2022-03-28 16:49:07 +02:00
|
|
|
)}
|
2022-12-17 14:27:55 +01:00
|
|
|
<Box sx={{ borderRadius: 4, flex: '1 85%' }} bg="dark.6">
|
|
|
|
|
<Text color="dark.0" p={6} align="center">
|
2022-12-15 17:43:14 -05:00
|
|
|
<ReactMarkdown>{contextMenu.title}</ReactMarkdown>
|
2022-03-28 16:49:07 +02:00
|
|
|
</Text>
|
|
|
|
|
</Box>
|
2022-12-17 14:27:55 +01:00
|
|
|
<HeaderButton icon="xmark" canClose={contextMenu.canClose} iconSize={18} handleClick={closeContext} />
|
2022-03-28 16:49:07 +02:00
|
|
|
</Flex>
|
2022-12-17 14:27:55 +01:00
|
|
|
<Box sx={{ height: 560, overflowY: 'scroll' }}>
|
|
|
|
|
<Stack spacing={3}>
|
|
|
|
|
{Object.entries(contextMenu.options).map((option, index) => (
|
|
|
|
|
<ContextButton option={option} key={`context-item-${index}`} />
|
|
|
|
|
))}
|
|
|
|
|
</Stack>
|
2022-03-27 18:17:26 +02:00
|
|
|
</Box>
|
|
|
|
|
</Box>
|
2022-12-17 14:27:55 +01:00
|
|
|
)}
|
|
|
|
|
</Transition>
|
2022-03-27 18:17:26 +02:00
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default ContextMenu;
|