|
| 1 | +import { DateTime } from 'luxon'; |
| 2 | +import { |
| 3 | + DropdownOption, |
| 4 | + MaterialReactTable, |
| 5 | + MRT_ColumnDef, |
| 6 | + useMaterialReactTable, |
| 7 | +} from 'material-react-table'; |
| 8 | +import { useMemo } from 'react'; |
| 9 | +import { TableCellProps } from '@mui/material'; |
| 10 | +import styled from 'styled-components'; |
| 11 | +import * as Sentry from '@sentry/nextjs'; |
| 12 | +import { filterOutNullishs } from 'components/AnalystDashboard/AllDashboard'; |
| 13 | +import ClearFilters from 'components/Table/ClearFilters'; |
| 14 | +import DateFilter from '../../Table/Filters/DateFilter'; |
| 15 | + |
| 16 | +const StyledLink = styled.button` |
| 17 | + color: ${(props) => props.theme.color.links}; |
| 18 | + text-decoration-line: underline; |
| 19 | + word-break: break-word; |
| 20 | +`; |
| 21 | + |
| 22 | +const handleDownload = async (uuid, fileName) => { |
| 23 | + const url = `/api/s3/download/${uuid}/${fileName}`; |
| 24 | + await fetch(url) |
| 25 | + .then((response) => response.json()) |
| 26 | + .then((response) => { |
| 27 | + window.open(response, '_blank'); |
| 28 | + }); |
| 29 | +}; |
| 30 | + |
| 31 | +const fileCell = ({ cell }) => { |
| 32 | + return ( |
| 33 | + <StyledLink |
| 34 | + data-testid="history-file-link" |
| 35 | + onClick={(e) => { |
| 36 | + e.preventDefault(); |
| 37 | + handleDownload(cell.row.original?.uuid, cell.getValue()).catch( |
| 38 | + (err) => { |
| 39 | + Sentry.captureException(err); |
| 40 | + } |
| 41 | + ); |
| 42 | + }} |
| 43 | + > |
| 44 | + {cell.getValue()} |
| 45 | + </StyledLink> |
| 46 | + ); |
| 47 | +}; |
| 48 | + |
| 49 | +const dateUploadedCell = ({ cell }) => { |
| 50 | + return DateTime.fromISO(cell.row.original.createdAt) |
| 51 | + .setZone('America/Los_Angeles') |
| 52 | + .toLocaleString(DateTime.DATETIME_FULL); |
| 53 | +}; |
| 54 | + |
| 55 | +const GisHistory = ({ historyTableList }) => { |
| 56 | + const columns = useMemo<MRT_ColumnDef<any>[]>(() => { |
| 57 | + const uniqueUsers = [ |
| 58 | + ...new Set(historyTableList.map((historyItem) => historyItem.name)), |
| 59 | + ].filter(filterOutNullishs); |
| 60 | + |
| 61 | + return [ |
| 62 | + { |
| 63 | + accessorKey: 'name', |
| 64 | + header: 'Uploaded by', |
| 65 | + size: 300, |
| 66 | + filterVariant: 'select', |
| 67 | + filterSelectOptions: (uniqueUsers as DropdownOption[]) || [], |
| 68 | + }, |
| 69 | + { |
| 70 | + accessorKey: 'file', |
| 71 | + header: 'File', |
| 72 | + size: 450, |
| 73 | + Cell: fileCell, |
| 74 | + }, |
| 75 | + { |
| 76 | + accessorKey: 'createdAt', |
| 77 | + header: 'Date uploaded', |
| 78 | + Cell: dateUploadedCell, |
| 79 | + size: 300, |
| 80 | + filterVariant: 'date', |
| 81 | + Filter: DateFilter, |
| 82 | + sortingFn: 'datetime', |
| 83 | + filterFn: (row, _columnIds, filterValue) => { |
| 84 | + if (!filterValue) return true; |
| 85 | + const rowDate = DateTime.fromISO(row.original.createdAt).toFormat( |
| 86 | + 'yyyy-MM-dd' |
| 87 | + ); |
| 88 | + return rowDate === filterValue; |
| 89 | + }, |
| 90 | + }, |
| 91 | + ]; |
| 92 | + }, [historyTableList]); |
| 93 | + |
| 94 | + const muiTableBodyCellProps = (): TableCellProps => { |
| 95 | + return { |
| 96 | + sx: { |
| 97 | + padding: '8px !important', |
| 98 | + }, |
| 99 | + }; |
| 100 | + }; |
| 101 | + |
| 102 | + const muiTableHeadCellProps = { |
| 103 | + sx: { |
| 104 | + wordBreak: 'break-word', |
| 105 | + texOverflow: 'wrap', |
| 106 | + '.Mui-TableHeadCell-Content-Labels': { |
| 107 | + width: '100%', |
| 108 | + justifyContent: 'space-between', |
| 109 | + }, |
| 110 | + '.Mui-TableHeadCell-Content-Wrapper ': { |
| 111 | + overflow: 'hidden', |
| 112 | + textOverflow: 'clip', |
| 113 | + padding: '0', |
| 114 | + }, |
| 115 | + '&:last-child': { |
| 116 | + paddingRight: '16px', |
| 117 | + }, |
| 118 | + '&:first-child': { |
| 119 | + paddingLeft: '16px', |
| 120 | + }, |
| 121 | + }, |
| 122 | + }; |
| 123 | + |
| 124 | + const table = useMaterialReactTable({ |
| 125 | + columns, |
| 126 | + data: historyTableList, |
| 127 | + enablePagination: false, |
| 128 | + enableBottomToolbar: false, |
| 129 | + enableGlobalFilter: false, |
| 130 | + muiTableContainerProps: { |
| 131 | + sx: { |
| 132 | + padding: '0 8px 8px 8px', |
| 133 | + maxHeight: '100%', |
| 134 | + }, |
| 135 | + }, |
| 136 | + muiTableBodyRowProps: { |
| 137 | + sx: { |
| 138 | + boxShadow: '0 3px 3px -2px #c4c4c4', |
| 139 | + }, |
| 140 | + }, |
| 141 | + muiTableBodyCellProps, |
| 142 | + muiTableHeadCellProps, |
| 143 | + renderTopToolbarCustomActions: () => ( |
| 144 | + <ClearFilters table={table} filters={table.getState().columnFilters} /> |
| 145 | + ), |
| 146 | + }); |
| 147 | + return ( |
| 148 | + <> |
| 149 | + <h2>Upload History</h2> |
| 150 | + <MaterialReactTable table={table} /> |
| 151 | + </> |
| 152 | + ); |
| 153 | +}; |
| 154 | + |
| 155 | +export default GisHistory; |
0 commit comments