2022-06-11 11:33:15 +02:00
|
|
|
import {
|
|
|
|
|
AlertDialog as Dialog,
|
|
|
|
|
AlertDialogBody,
|
|
|
|
|
AlertDialogFooter,
|
|
|
|
|
AlertDialogHeader,
|
|
|
|
|
AlertDialogContent,
|
|
|
|
|
AlertDialogOverlay,
|
|
|
|
|
useDisclosure,
|
|
|
|
|
Button,
|
2022-08-27 15:58:36 +02:00
|
|
|
} from '@chakra-ui/react';
|
|
|
|
|
import { useRef, useState } from 'react';
|
|
|
|
|
import ReactMarkdown from 'react-markdown';
|
|
|
|
|
import { useNuiEvent } from '../../hooks/useNuiEvent';
|
|
|
|
|
import { fetchNui } from '../../utils/fetchNui';
|
|
|
|
|
import { useLocales } from '../../providers/LocaleProvider';
|
2022-06-11 11:33:15 +02:00
|
|
|
|
2022-08-27 15:40:41 +02:00
|
|
|
export interface AlertProps {
|
2022-06-11 11:33:15 +02:00
|
|
|
header: string;
|
|
|
|
|
content: string;
|
|
|
|
|
centered?: boolean;
|
2022-06-17 20:29:40 +02:00
|
|
|
cancel?: boolean;
|
2022-06-11 11:33:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const AlertDialog: React.FC = () => {
|
2022-06-12 12:29:48 +02:00
|
|
|
const { locale } = useLocales();
|
2022-06-11 11:33:15 +02:00
|
|
|
const { isOpen, onOpen, onClose } = useDisclosure();
|
|
|
|
|
const cancelRef = useRef(null);
|
2022-08-27 15:40:41 +02:00
|
|
|
const [dialogData, setDialogData] = useState<AlertProps>({
|
2022-08-27 15:58:36 +02:00
|
|
|
header: '',
|
|
|
|
|
content: '',
|
2022-06-11 11:33:15 +02:00
|
|
|
});
|
|
|
|
|
|
2022-06-17 20:29:40 +02:00
|
|
|
const closeAlert = (button: string) => {
|
2022-06-11 11:33:15 +02:00
|
|
|
onClose();
|
2022-08-27 15:58:36 +02:00
|
|
|
fetchNui('closeAlert', button);
|
2022-06-11 11:33:15 +02:00
|
|
|
};
|
|
|
|
|
|
2022-08-27 15:58:36 +02:00
|
|
|
useNuiEvent('sendAlert', (data: AlertProps) => {
|
2022-06-11 11:33:15 +02:00
|
|
|
setDialogData(data);
|
|
|
|
|
onOpen();
|
|
|
|
|
});
|
|
|
|
|
|
2022-09-29 11:39:30 +02:00
|
|
|
useNuiEvent('closeAlertDialog', () => {
|
|
|
|
|
onClose();
|
|
|
|
|
});
|
|
|
|
|
|
2022-06-11 11:33:15 +02:00
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
<Dialog
|
|
|
|
|
leastDestructiveRef={cancelRef}
|
|
|
|
|
onClose={onClose}
|
|
|
|
|
isOpen={isOpen}
|
|
|
|
|
isCentered={dialogData.centered}
|
|
|
|
|
closeOnOverlayClick={false}
|
2022-08-27 15:58:36 +02:00
|
|
|
onEsc={() => closeAlert('cancel')}
|
2022-06-11 11:33:15 +02:00
|
|
|
>
|
|
|
|
|
<AlertDialogOverlay />
|
|
|
|
|
<AlertDialogContent fontFamily="Inter">
|
|
|
|
|
<AlertDialogHeader fontSize="lg" fontWeight="bold">
|
|
|
|
|
{dialogData.header}
|
|
|
|
|
</AlertDialogHeader>
|
|
|
|
|
<AlertDialogBody>
|
|
|
|
|
<ReactMarkdown>{dialogData.content}</ReactMarkdown>
|
|
|
|
|
</AlertDialogBody>
|
|
|
|
|
<AlertDialogFooter>
|
2022-06-17 20:29:40 +02:00
|
|
|
{dialogData.cancel && (
|
2022-08-27 15:58:36 +02:00
|
|
|
<Button onClick={() => closeAlert('cancel')} mr={3}>
|
2022-06-17 20:29:40 +02:00
|
|
|
{locale.ui.cancel}
|
|
|
|
|
</Button>
|
|
|
|
|
)}
|
2022-08-27 15:58:36 +02:00
|
|
|
<Button colorScheme={dialogData.cancel ? 'blue' : undefined} onClick={() => closeAlert('confirm')}>
|
2022-06-17 20:29:40 +02:00
|
|
|
{locale.ui.confirm}
|
|
|
|
|
</Button>
|
2022-06-11 11:33:15 +02:00
|
|
|
</AlertDialogFooter>
|
|
|
|
|
</AlertDialogContent>
|
|
|
|
|
</Dialog>
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default AlertDialog;
|