|
| 1 | +import React from 'react'; |
| 2 | +import { withApollo } from '../lib/apollo'; |
| 3 | +import Typography from '@mui/material/Typography'; |
| 4 | +import Box from '@mui/material/Box'; |
| 5 | +import Layout from '../components/layout'; |
| 6 | +import { useSubscription } from '@apollo/client'; |
| 7 | +import gql from 'graphql-tag'; |
| 8 | +import OverlayLoading from '../components/ui/overlay-loading'; |
| 9 | +import { GetWebHookHistorySubscriptionSubscription } from '../types'; |
| 10 | +import Table from '@mui/material/Table'; |
| 11 | +import TableBody from '@mui/material/TableBody'; |
| 12 | +import TableCell from '@mui/material/TableCell'; |
| 13 | +import TableContainer from '@mui/material/TableContainer'; |
| 14 | +import TableHead from '@mui/material/TableHead'; |
| 15 | +import TableRow from '@mui/material/TableRow'; |
| 16 | +import Paper from '@mui/material/Paper'; |
| 17 | +import Button from '@mui/material/Button'; |
| 18 | +import Icon from '@mui/icons-material/Send'; |
| 19 | +import { Collapse } from '@mui/material'; |
| 20 | +import { KeyboardArrowDown, KeyboardArrowUp } from '@mui/icons-material'; |
| 21 | + |
| 22 | +const intl = new Intl.DateTimeFormat(undefined, { |
| 23 | + year: 'numeric', |
| 24 | + month: 'numeric', |
| 25 | + day: 'numeric', |
| 26 | + hour: 'numeric', |
| 27 | + minute: 'numeric', |
| 28 | + second: 'numeric', |
| 29 | +}); |
| 30 | + |
| 31 | +const Row = ({ |
| 32 | + webHookHistory, |
| 33 | +}: { |
| 34 | + webHookHistory: GetWebHookHistorySubscriptionSubscription['webHookHistory'][number]; |
| 35 | +}) => { |
| 36 | + const [open, setOpen] = React.useState(false); |
| 37 | + |
| 38 | + return ( |
| 39 | + <> |
| 40 | + <TableRow sx={{ '& > *': { borderBottom: 'unset' } }}> |
| 41 | + <TableCell component='th'> |
| 42 | + {intl.format(new Date(webHookHistory.createdAt * 1000))} |
| 43 | + </TableCell> |
| 44 | + <TableCell component='th'>{webHookHistory.status}</TableCell> |
| 45 | + <TableCell component='th'>{webHookHistory.event}</TableCell> |
| 46 | + <TableCell component='th'> |
| 47 | + {webHookHistory.data && webHookHistory.data.substr(0, 50)} |
| 48 | + </TableCell> |
| 49 | + <TableCell component='th'> |
| 50 | + <Button aria-label='expand row' size='small' onClick={() => setOpen(!open)}> |
| 51 | + {open ? <KeyboardArrowUp /> : <KeyboardArrowDown />} |
| 52 | + </Button> |
| 53 | + </TableCell> |
| 54 | + </TableRow> |
| 55 | + <TableRow> |
| 56 | + <TableCell |
| 57 | + sx={{ paddingBottom: 0, paddingTop: 0, overflow: 'auto', maxHeight: '400px' }} |
| 58 | + colSpan={5} |
| 59 | + > |
| 60 | + <Collapse in={open} timeout='auto' unmountOnExit> |
| 61 | + {webHookHistory.data && ( |
| 62 | + <small> |
| 63 | + <pre style={{ maxHeight: '600px' }}> |
| 64 | + {JSON.stringify(JSON.parse(webHookHistory.data), null, 2)} |
| 65 | + </pre> |
| 66 | + </small> |
| 67 | + )} |
| 68 | + </Collapse> |
| 69 | + </TableCell> |
| 70 | + </TableRow> |
| 71 | + </> |
| 72 | + ); |
| 73 | +}; |
| 74 | + |
| 75 | +const App = () => { |
| 76 | + const { loading, error, data } = useSubscription<GetWebHookHistorySubscriptionSubscription>(gql` |
| 77 | + subscription GetWebHookHistorySubscription { |
| 78 | + webHookHistory { |
| 79 | + createdAt |
| 80 | + data |
| 81 | + event |
| 82 | + status |
| 83 | + } |
| 84 | + } |
| 85 | + `); |
| 86 | + |
| 87 | + return ( |
| 88 | + <> |
| 89 | + <Typography variant={'h3'}>Web Hook History</Typography> |
| 90 | + <Box mt={4}> |
| 91 | + {loading ? ( |
| 92 | + <OverlayLoading /> |
| 93 | + ) : ( |
| 94 | + <TableContainer component={Paper}> |
| 95 | + <Table size='small'> |
| 96 | + <TableHead> |
| 97 | + <TableRow> |
| 98 | + <TableCell>Created at</TableCell> |
| 99 | + <TableCell>Status</TableCell> |
| 100 | + <TableCell>Event</TableCell> |
| 101 | + <TableCell>Data</TableCell> |
| 102 | + <TableCell></TableCell> |
| 103 | + </TableRow> |
| 104 | + </TableHead> |
| 105 | + <TableBody> |
| 106 | + {data?.webHookHistory.map((webHookHistory) => ( |
| 107 | + <Row |
| 108 | + key={webHookHistory.createdAt} |
| 109 | + webHookHistory={webHookHistory} |
| 110 | + /> |
| 111 | + ))} |
| 112 | + </TableBody> |
| 113 | + </Table> |
| 114 | + </TableContainer> |
| 115 | + )} |
| 116 | + </Box> |
| 117 | + </> |
| 118 | + ); |
| 119 | +}; |
| 120 | + |
| 121 | +App.getLayout = Layout; |
| 122 | + |
| 123 | +export default App; |
0 commit comments