Files
ox_lib/web/src/features/dialog/AlertDialog.tsx

78 lines
2.0 KiB
TypeScript
Raw Normal View History

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
export interface AlertProps {
2022-06-11 11:33:15 +02:00
header: string;
content: string;
centered?: boolean;
cancel?: boolean;
2022-06-11 11:33:15 +02:00
}
const AlertDialog: React.FC = () => {
const { locale } = useLocales();
2022-06-11 11:33:15 +02:00
const { isOpen, onOpen, onClose } = useDisclosure();
const cancelRef = useRef(null);
const [dialogData, setDialogData] = useState<AlertProps>({
2022-08-27 15:58:36 +02:00
header: '',
content: '',
2022-06-11 11:33:15 +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();
});
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>
{dialogData.cancel && (
2022-08-27 15:58:36 +02:00
<Button onClick={() => closeAlert('cancel')} mr={3}>
{locale.ui.cancel}
</Button>
)}
2022-08-27 15:58:36 +02:00
<Button colorScheme={dialogData.cancel ? 'blue' : undefined} onClick={() => closeAlert('confirm')}>
{locale.ui.confirm}
</Button>
2022-06-11 11:33:15 +02:00
</AlertDialogFooter>
</AlertDialogContent>
</Dialog>
</>
);
};
export default AlertDialog;