-
-
Notifications
You must be signed in to change notification settings - Fork 730
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
108 changes: 108 additions & 0 deletions
108
frontend/src/component/events/EventLog/EventActions.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import { type FC, useState } from 'react'; | ||
import { | ||
IconButton, | ||
ListItemText, | ||
MenuItem, | ||
MenuList, | ||
Popover, | ||
styled, | ||
Tooltip, | ||
Typography, | ||
} from '@mui/material'; | ||
import FileDownload from '@mui/icons-material/FileDownload'; | ||
import type { EventSchema } from '../../../openapi'; | ||
|
||
const StyledActions = styled('div')(({ theme }) => ({ | ||
display: 'flex', | ||
justifyContent: 'center', | ||
})); | ||
|
||
const StyledPopover = styled(Popover)(({ theme }) => ({ | ||
borderRadius: theme.shape.borderRadiusLarge, | ||
padding: theme.spacing(1, 1.5), | ||
})); | ||
|
||
interface IEventActions { | ||
events: EventSchema[]; | ||
} | ||
|
||
export const EventActions: FC<IEventActions> = ({ events }) => { | ||
const [anchorEl, setAnchorEl] = useState<null | HTMLElement>(null); | ||
|
||
const open = Boolean(anchorEl); | ||
const handleClick = (event: React.MouseEvent<HTMLButtonElement>) => { | ||
setAnchorEl(event.currentTarget); | ||
}; | ||
const handleClose = () => { | ||
setAnchorEl(null); | ||
}; | ||
|
||
const exportJson = () => { | ||
const jsonString = JSON.stringify(events); | ||
const blob = new Blob([jsonString], { type: 'application/json' }); | ||
const url = URL.createObjectURL(blob); | ||
|
||
const currentDate = new Date().toISOString().split('T')[0]; | ||
const fileName = `events_${currentDate}.json`; | ||
|
||
const a = document.createElement('a'); | ||
a.href = url; | ||
a.download = fileName; | ||
a.click(); | ||
|
||
URL.revokeObjectURL(url); | ||
setAnchorEl(null); | ||
}; | ||
|
||
const exportCsv = () => { | ||
setAnchorEl(null); | ||
}; | ||
|
||
return ( | ||
<StyledActions | ||
onClick={(e) => { | ||
e.preventDefault(); | ||
e.stopPropagation(); | ||
}} | ||
> | ||
<Tooltip title={'Export'} arrow describeChild> | ||
<div> | ||
<IconButton | ||
aria-label={'Export'} | ||
aria-haspopup='true' | ||
aria-expanded={open} | ||
onClick={handleClick} | ||
type='button' | ||
> | ||
<FileDownload /> | ||
</IconButton> | ||
</div> | ||
</Tooltip> | ||
<StyledPopover | ||
anchorEl={anchorEl} | ||
open={open} | ||
onClose={handleClose} | ||
transformOrigin={{ horizontal: 'right', vertical: 'top' }} | ||
anchorOrigin={{ horizontal: 'right', vertical: 'bottom' }} | ||
disableScrollLock={true} | ||
> | ||
<MenuList> | ||
<MenuItem onClick={exportCsv}> | ||
<ListItemText> | ||
<Typography variant='body2'> | ||
Export as CSV | ||
</Typography> | ||
</ListItemText> | ||
</MenuItem> | ||
<MenuItem onClick={exportJson}> | ||
<ListItemText> | ||
<Typography variant='body2'> | ||
Export as JSON | ||
</Typography> | ||
</ListItemText> | ||
</MenuItem> | ||
</MenuList> | ||
</StyledPopover> | ||
</StyledActions> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters