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

82 lines
2.2 KiB
TypeScript
Raw Normal View History

import { Modal, Button, Stack, Group, useMantineTheme } from '@mantine/core';
import { useState } from 'react';
2022-08-27 15:58:36 +02:00
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;
labels?: {
cancel?: string;
confirm?: string;
};
2022-06-11 11:33:15 +02:00
}
const AlertDialog: React.FC = () => {
const { locale } = useLocales();
const theme = useMantineTheme();
const [opened, setOpened] = useState(false);
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) => {
setOpened(false);
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);
setOpened(true);
2022-06-11 11:33:15 +02:00
});
useNuiEvent('closeAlertDialog', () => {
setOpened(false);
});
2022-06-11 11:33:15 +02:00
return (
<>
<Modal
opened={opened}
centered={dialogData.centered}
closeOnClickOutside={false}
onClose={() => {
setOpened(false);
closeAlert('cancel');
}}
withCloseButton={false}
overlayOpacity={0.5}
exitTransitionDuration={150}
transition="fade"
title={<ReactMarkdown>{dialogData.header}</ReactMarkdown>}
2022-06-11 11:33:15 +02:00
>
<Stack>
<ReactMarkdown>{dialogData.content}</ReactMarkdown>
<Group position="right" spacing={10}>
{dialogData.cancel && (
<Button uppercase variant="default" onClick={() => closeAlert('cancel')} mr={3}>
{dialogData.labels?.cancel || locale.ui.cancel}
</Button>
)}
<Button
uppercase
variant={dialogData.cancel ? 'light' : 'default'}
color={dialogData.cancel ? theme.primaryColor : undefined}
onClick={() => closeAlert('confirm')}
>
{dialogData.labels?.confirm || locale.ui.confirm}
</Button>
</Group>
</Stack>
</Modal>
2022-06-11 11:33:15 +02:00
</>
);
};
export default AlertDialog;