Skip to content

Commit

Permalink
👌✨ add a button to remove columns
Browse files Browse the repository at this point in the history
  • Loading branch information
BenoitZugmeyer committed Sep 8, 2023
1 parent b04082e commit dca050b
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ function initColumnDrag(
if (!targetCell) {
return false
}
const siblings = Array.from(targetCell.parentElement!.children)
const siblings = Array.from(targetCell.parentElement!.querySelectorAll(':scope > [data-header-cell]'))
const columnIndex = siblings.indexOf(targetCell)

state = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,20 @@ export const EventRow = React.memo(

return (
<tr>
{columns.map((column): React.ReactElement => {
{columns.map((column, index): React.ReactElement => {
const isLast = index === columns.length - 1
switch (column.type) {
case 'date':
return <Cell key="date">{formatDate(event.date)}</Cell>
return (
<Cell key="date" isLast={isLast}>
{formatDate(event.date)}
</Cell>
)
case 'description':
return (
<Cell
key="description"
isLast={isLast}
sx={{
cursor: 'pointer',
}}
Expand Down Expand Up @@ -119,7 +125,7 @@ export const EventRow = React.memo(
)
case 'type':
return (
<Cell key="type">
<Cell key="type" isLast={isLast}>
{isRumEvent(event) || isTelemetryEvent(event) ? (
<Badge variant="outline" color={RUM_EVENT_TYPE_COLOR[event.type]}>
{event.type}
Expand All @@ -134,7 +140,7 @@ export const EventRow = React.memo(
case 'field': {
const value = facetRegistry.getFieldValueForEvent(event, column.path)
return (
<Cell key={`field-${column.path}`}>
<Cell key={`field-${column.path}`} isLast={isLast}>
{value !== undefined && (
<Json
value={value}
Expand All @@ -153,11 +159,12 @@ export const EventRow = React.memo(
}
)

function Cell(props: BoxProps & ComponentPropsWithoutRef<'div'>) {
function Cell({ isLast, ...props }: BoxProps & ComponentPropsWithoutRef<'div'> & { isLast: boolean }) {
return (
<Box
{...props}
component="td"
colSpan={isLast ? 2 : 1}
sx={[
{
verticalAlign: 'top',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Popover, Box, Text, Button, Flex, Autocomplete, Table, useMantineTheme } from '@mantine/core'
import { Popover, Box, Text, Button, Flex, Autocomplete, Table, useMantineTheme, CloseButton } from '@mantine/core'
import type { ForwardedRef, ReactNode } from 'react'
import React, { useMemo, useRef, useState, forwardRef } from 'react'
import type { EventFilters, FacetRegistry } from '../../../hooks/useEvents'
Expand Down Expand Up @@ -38,20 +38,17 @@ export function EventsList({
<Table>
<thead>
<tr ref={headerRowRef}>
{columns.map((column, index) => (
<th key={column.type === 'field' ? `field-${column.path}` : column.type} data-header-cell>
<Flex justify="space-between" gap="sm" align="center">
{getColumnTitle(column)}
{index === columns.length - 1 && (
<AddColumnPopover
columns={columns}
onColumnsChange={onColumnsChange}
facetRegistry={facetRegistry}
/>
)}
</Flex>
</th>
{columns.map((column) => (
<ColumnHeader
key={column.type === 'field' ? `field-${column.path}` : column.type}
columns={columns}
column={column}
onColumnsChange={onColumnsChange}
></ColumnHeader>
))}
<Box component="th" sx={{ width: 0 }}>
<AddColumnPopover columns={columns} onColumnsChange={onColumnsChange} facetRegistry={facetRegistry} />
</Box>
</tr>
</thead>

Expand All @@ -73,6 +70,50 @@ export function EventsList({
)
}

function ColumnHeader({
columns,
column,
onColumnsChange,
}: {
columns: EventListColumn[]
column: EventListColumn
onColumnsChange: (columns: EventListColumn[]) => void
}) {
return (
<Box
component="th"
key={column.type === 'field' ? `field-${column.path}` : column.type}
data-header-cell
sx={{
width:
column.type === 'date' || column.type === 'type'
? // Date and type columns cannot wrap and have mostly constant width. Let's
// set a width to 0 so they don't uselessly grow.
0
: // Let other column share the remaining space. This gives better result when
// things are wrapping.
undefined,

'& .mantine-CloseButton-root': {
opacity: 0,
},
'&:hover': {
'& .mantine-CloseButton-root': {
opacity: 1,
},
},
}}
>
<Flex gap="sm" align="center">
<Flex justify="space-between" gap="sm" align="center" sx={{ flex: 1 }}>
{getColumnTitle(column)}
<CloseButton size="xs" variant="filled" onClick={() => onColumnsChange(removeColumn(columns, column))} />
</Flex>
</Flex>
</Box>
)
}

function AddColumnPopover({
columns,
onColumnsChange,
Expand Down

0 comments on commit dca050b

Please sign in to comment.