import { useMemo } from 'react'; import { Cross2Icon } from '@radix-ui/react-icons'; import { Table } from '@tanstack/react-table'; import { RefreshCw, X } from 'lucide-react'; import { useTranslation } from 'react-i18next'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Switch } from '@/components/ui/switch'; import { DataTableFacetedFilter } from '@/components/data-table-faceted-filter'; import { DateRangePicker } from '@/components/date-range-picker'; import { useUsageLogPermissions } from '../../../gql/useUsageLogPermissions'; import { useQueryChannels } from '../../channels/data'; import { UsageLogSource } from '../data/schema'; import { DataTableViewOptions } from './data-table-view-options'; import type { DateTimeRangeValue } from '@/utils/date-range'; interface DataTableToolbarProps { table: Table; dateRange?: DateTimeRangeValue; onDateRangeChange?: (range: DateTimeRangeValue | undefined) => void; onRefresh?: () => void; showRefresh?: boolean; autoRefresh?: boolean; onAutoRefreshChange?: (value: boolean) => void; } export function DataTableToolbar({ table, dateRange, onDateRangeChange, onRefresh, showRefresh = false, autoRefresh = false, onAutoRefreshChange, }: DataTableToolbarProps) { const { t } = useTranslation(); const permissions = useUsageLogPermissions(); const { canViewChannels } = permissions; const hasDateRange = !!dateRange?.from || !!dateRange?.to; const isFiltered = table.getState().columnFilters.length > 0 || hasDateRange; // Fetch channels data if user has permission const { data: channelsData } = useQueryChannels( canViewChannels ? { first: 100, orderBy: { field: 'CREATED_AT', direction: 'DESC' }, } : undefined ); // Prepare channel options for filter const channelOptions = useMemo(() => { if (!canViewChannels || !channelsData?.edges) return []; return channelsData.edges.map((edge) => ({ value: edge.node.id, label: edge.node.name || `Channel ${edge.node.id}`, })); }, [canViewChannels, channelsData]); const usageLogSources = [ { value: 'api' as UsageLogSource, label: t('usageLogs.source.api'), }, { value: 'playground' as UsageLogSource, label: t('usageLogs.source.playground'), }, { value: 'test' as UsageLogSource, label: t('usageLogs.source.test'), }, ]; return (
table.getColumn('id')?.setFilterValue(event.target.value)} className='h-8 w-[150px] lg:w-[250px]' /> {/* {table.getColumn('source') && ( )} */} {canViewChannels && table.getColumn('channel') && channelOptions.length > 0 && channelsData?.edges && ( )} {hasDateRange && ( )} {isFiltered && ( )}
{showRefresh && onAutoRefreshChange && (
)} {showRefresh && onRefresh && ( )} {/* */}
); }