|
| 1 | +import { useMemo, useState } from 'react' |
| 2 | +import { MaterialReactTable, useMaterialReactTable, type MRT_ColumnDef } from 'material-react-table' |
| 3 | +import { Box, Chip, Tooltip, Snackbar } from '@mui/material' |
| 4 | +import ContentCopyIcon from '@mui/icons-material/ContentCopy' |
| 5 | +import { ResourceHashVersions, ResourceHashVersion } from '../../common/types/overviewApiTypes' |
| 6 | + |
| 7 | +interface ResourceHashesTableProps { |
| 8 | + data: ResourceHashVersions | undefined |
| 9 | + isLoading?: boolean |
| 10 | +} |
| 11 | + |
| 12 | +interface FlatResourceHash { |
| 13 | + type: 'cluster' | 'listener' | 'route' | 'secret' |
| 14 | + name: string |
| 15 | + version: string |
| 16 | +} |
| 17 | + |
| 18 | +const truncateHash = (hash: string): string => { |
| 19 | + if (hash.length <= 19) return hash |
| 20 | + return `${hash.slice(0, 8)}...${hash.slice(-8)}` |
| 21 | +} |
| 22 | + |
| 23 | +const CopyableHash = ({ hash }: { hash: string }) => { |
| 24 | + const [showSnackbar, setShowSnackbar] = useState(false) |
| 25 | + |
| 26 | + const handleCopy = async () => { |
| 27 | + try { |
| 28 | + await navigator.clipboard.writeText(hash) |
| 29 | + setShowSnackbar(true) |
| 30 | + } catch (err) { |
| 31 | + console.error('Failed to copy:', err) |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + return ( |
| 36 | + <> |
| 37 | + <Tooltip title={`Click to copy: ${hash}`} arrow placement='top'> |
| 38 | + <Box |
| 39 | + component='code' |
| 40 | + onClick={handleCopy} |
| 41 | + sx={{ |
| 42 | + fontFamily: 'monospace', |
| 43 | + fontSize: '0.85rem', |
| 44 | + backgroundColor: 'action.hover', |
| 45 | + px: 1, |
| 46 | + py: 0.5, |
| 47 | + borderRadius: 1, |
| 48 | + cursor: 'pointer', |
| 49 | + display: 'inline-flex', |
| 50 | + alignItems: 'center', |
| 51 | + gap: 0.5, |
| 52 | + '&:hover': { |
| 53 | + backgroundColor: 'action.selected' |
| 54 | + } |
| 55 | + }} |
| 56 | + > |
| 57 | + {truncateHash(hash)} |
| 58 | + <ContentCopyIcon sx={{ fontSize: '0.75rem', opacity: 0.5 }} /> |
| 59 | + </Box> |
| 60 | + </Tooltip> |
| 61 | + <Snackbar |
| 62 | + open={showSnackbar} |
| 63 | + autoHideDuration={2000} |
| 64 | + onClose={() => setShowSnackbar(false)} |
| 65 | + message='Hash copied to clipboard' |
| 66 | + anchorOrigin={{ vertical: 'bottom', horizontal: 'center' }} |
| 67 | + /> |
| 68 | + </> |
| 69 | + ) |
| 70 | +} |
| 71 | + |
| 72 | +const TypeChip = ({ type }: { type: string }) => { |
| 73 | + const config: Record<string, { color: 'primary' | 'secondary' | 'success' | 'warning' }> = { |
| 74 | + listener: { color: 'primary' }, |
| 75 | + cluster: { color: 'secondary' }, |
| 76 | + route: { color: 'success' }, |
| 77 | + secret: { color: 'warning' } |
| 78 | + } |
| 79 | + |
| 80 | + const { color } = config[type] || { color: 'primary' as const } |
| 81 | + |
| 82 | + return <Chip label={type} color={color} size='small' variant='outlined' /> |
| 83 | +} |
| 84 | + |
| 85 | +export const ResourceHashesTable = ({ data, isLoading = false }: ResourceHashesTableProps) => { |
| 86 | + const flatData = useMemo<FlatResourceHash[]>(() => { |
| 87 | + if (!data) return [] |
| 88 | + |
| 89 | + const result: FlatResourceHash[] = [] |
| 90 | + |
| 91 | + const addResources = ( |
| 92 | + resources: ResourceHashVersion[] | null, |
| 93 | + type: FlatResourceHash['type'] |
| 94 | + ) => { |
| 95 | + if (resources) { |
| 96 | + resources.forEach(r => result.push({ type, name: r.name, version: r.version })) |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + addResources(data.listeners, 'listener') |
| 101 | + addResources(data.clusters, 'cluster') |
| 102 | + addResources(data.routes, 'route') |
| 103 | + addResources(data.secrets, 'secret') |
| 104 | + |
| 105 | + return result |
| 106 | + }, [data]) |
| 107 | + |
| 108 | + const columns = useMemo<MRT_ColumnDef<FlatResourceHash>[]>( |
| 109 | + () => [ |
| 110 | + { |
| 111 | + accessorKey: 'type', |
| 112 | + header: 'Type', |
| 113 | + size: 120, |
| 114 | + Cell: ({ cell }) => <TypeChip type={cell.getValue<string>()} /> |
| 115 | + }, |
| 116 | + { |
| 117 | + accessorKey: 'name', |
| 118 | + header: 'Resource Name', |
| 119 | + size: 350 |
| 120 | + }, |
| 121 | + { |
| 122 | + accessorKey: 'version', |
| 123 | + header: 'Hash', |
| 124 | + size: 180, |
| 125 | + Cell: ({ cell }) => <CopyableHash hash={cell.getValue<string>()} /> |
| 126 | + } |
| 127 | + ], |
| 128 | + [] |
| 129 | + ) |
| 130 | + |
| 131 | + const table = useMaterialReactTable({ |
| 132 | + columns, |
| 133 | + data: flatData, |
| 134 | + enableColumnActions: false, |
| 135 | + enableColumnFilters: true, |
| 136 | + enablePagination: true, |
| 137 | + enableSorting: true, |
| 138 | + enableDensityToggle: false, |
| 139 | + enableFullScreenToggle: false, |
| 140 | + enableHiding: false, |
| 141 | + initialState: { |
| 142 | + density: 'compact', |
| 143 | + sorting: [{ id: 'type', desc: false }], |
| 144 | + pagination: { pageSize: 25, pageIndex: 0 } |
| 145 | + }, |
| 146 | + state: { |
| 147 | + isLoading |
| 148 | + }, |
| 149 | + muiTableContainerProps: { |
| 150 | + sx: { maxHeight: '500px' } |
| 151 | + }, |
| 152 | + muiTablePaperProps: { |
| 153 | + elevation: 0, |
| 154 | + sx: { border: '1px solid', borderColor: 'divider' } |
| 155 | + } |
| 156 | + }) |
| 157 | + |
| 158 | + return <MaterialReactTable table={table} /> |
| 159 | +} |
| 160 | + |
| 161 | +export default ResourceHashesTable |
0 commit comments