import { Column, Table } from '@tanstack/react-table' import { endOfDay, endOfHour, startOfDay, startOfHour } from 'date-fns' import { CalendarClockIcon, CalendarDaysIcon, CalendarSearchIcon, ChevronLeftIcon, ChevronRightIcon, ChevronsLeftRightIcon, CopyIcon, EqualIcon, FilterIcon, type LucideIcon, } from 'lucide-react' import { ComponentPropsWithRef, useEffect, useState } from 'react' import { cn, copyToClipboard, DropdownMenu, DropdownMenuContent, DropdownMenuGroup, DropdownMenuItem, DropdownMenuSeparator, DropdownMenuTrigger, } from 'ui' import { addLogsFilter, EQUALITY_FILTER_OPERATORS, LOGS_FILTER_OPERATOR_LABELS, type LogsFilterOperator, } from '@/components/interfaces/UnifiedLogs/UnifiedLogs.filters' import { DataTableFilterField } from '@/components/ui/DataTable/DataTable.types' interface DataTableSheetRowActionProps< TData, TFields extends DataTableFilterField, > extends ComponentPropsWithRef { fieldValue?: TFields['value'] filterFields: TFields[] value: string | number table?: Table label?: string onAddFilter?: (operator: LogsFilterOperator) => void filterOperators?: readonly LogsFilterOperator[] } export const OPERATOR_ICONS: Partial> = { '=': EqualIcon, '<>': ChevronsLeftRightIcon, } const FILTERABLE_FIELD_TYPES = ['checkbox', 'input', 'slider', 'timerange'] export function getRowFilterTarget>({ fieldValue, filterFields, table, }: { fieldValue?: TFields['value'] filterFields: TFields[] table?: Table }): { field?: TFields; column?: Column } { if (!fieldValue) return {} const field = filterFields.find((f) => f.value === fieldValue) if (!field || !FILTERABLE_FIELD_TYPES.includes(field.type)) return {} return { field, column: table?.getAllColumns().find((c) => c.id === fieldValue.toString()) } } export type RowFilterAction = { operator: LogsFilterOperator; apply: () => void } export function getRowFilterActions>({ fieldValue, filterFields, table, value, onAddFilter, filterOperators = EQUALITY_FILTER_OPERATORS, }: { fieldValue?: TFields['value'] filterFields: TFields[] table?: Table value: string | number onAddFilter?: (operator: LogsFilterOperator) => void filterOperators?: readonly LogsFilterOperator[] }): RowFilterAction[] { const { field, column } = getRowFilterTarget({ fieldValue, filterFields, table }) if (!!field && !!column && !!table) { if (field.type !== 'checkbox' && field.type !== 'input') return [] return EQUALITY_FILTER_OPERATORS.map((operator) => ({ operator, apply: () => addLogsFilter(table, { field: String(field.value), operator, value }), })) } if (!column && !!onAddFilter) { return filterOperators.map((operator) => ({ operator, apply: () => onAddFilter(operator) })) } return [] } export function DataTableSheetRowAction>({ fieldValue, filterFields, value, children, className, table, label, onAddFilter, filterOperators = EQUALITY_FILTER_OPERATORS, onKeyDown, ...props }: DataTableSheetRowActionProps) { const [open, setOpen] = useState(false) /** * [Joshen] This imo is just a temporary solution and needs to be addressed at the * UI component level RE how we want to handle DropdownContent when scrolling, as its * not specific to unified logs. * * DropdownMenuContent here exceeds the scrolling parent as its portalled. Opting to * close the dropdown menu here when scrolling as a workaround. */ useEffect(() => { if (!open) return const onScroll = () => setOpen(false) document.addEventListener('scroll', onScroll, true) return () => document.removeEventListener('scroll', onScroll, true) }, [open]) const { field, column } = getRowFilterTarget({ fieldValue, filterFields, table }) const filterActions = getRowFilterActions({ fieldValue, filterFields, table, value, onAddFilter, filterOperators, }) function renderFilterActions(actions: RowFilterAction[]) { return actions.map(({ operator, apply }) => { const Icon = OPERATOR_ICONS[operator] ?? FilterIcon return ( {LOGS_FILTER_OPERATOR_LABELS[operator]} ) }) } function renderOptions() { if (!field) return null switch (field.type) { case 'checkbox': case 'input': return {renderFilterActions(filterActions)} case 'slider': return ( column?.setFilterValue([0, value])} className="flex items-center gap-2" > {/* FIXME: change icon as it is not clear */} Less or equal than column?.setFilterValue([value, 5000])} className="flex items-center gap-2" > {/* FIXME: change icon as it is not clear */} Greater or equal than column?.setFilterValue([value])} className="flex items-center gap-2" > Equal to ) case 'timerange': const date = new Date(value) return ( column?.setFilterValue([date])} className="flex items-center gap-2" > Exact timestamp { const start = startOfHour(date) const end = endOfHour(date) column?.setFilterValue([start, end]) }} className="flex items-center gap-2" > Same hour { const start = startOfDay(date) const end = endOfDay(date) column?.setFilterValue([start, end]) }} className="flex items-center gap-2" > Same day ) default: return null } } return ( { if (e.key === 'ArrowDown') { // REMINDER: default behavior is to open the dropdown menu // But because we use it to navigate between rows, we need to prevent it // and only use "Enter" to select the option e.preventDefault() } onKeyDown?.(e) }} {...props} > {children} event.stopPropagation()} > {!!field && !!column && ( <> {renderOptions()} )} {!column && filterActions.length > 0 && ( <> {renderFilterActions(filterActions)} )} copyToClipboard(String(value))} className="flex items-center gap-2" > Copy {label} ) }