mirror of
https://github.com/supabase/supabase.git
synced 2026-06-12 00:01:19 +08:00
## Context Supports selecting log rows and allow to copy / ask assistant for selected rows, similar to what we had for the old logs UI Selection will clear whenever the search parameters change <img width="1448" height="413" alt="image" src="https://github.com/user-attachments/assets/b81b359c-28c3-48a8-9895-e77327ebd33e" /> <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Multi-row selection with an action header showing selected count * Copy selected logs as JSON or Markdown from a dropdown * "Explain with AI" action to open the assistant pre-filled with selected logs * Clear selection button * **Refactor** * Row/detail selection now syncs with the URL for shareable views and improves next/previous navigation and panel behavior * **Style** * Minor visual tweak to column level indicator dot size <!-- review_stack_entry_start --> [](https://app.coderabbit.ai/change-stack/supabase/supabase/pull/45974) <!-- review_stack_entry_end --> <!-- end of auto-generated comment: release notes by coderabbit.ai -->
86 lines
2.8 KiB
TypeScript
86 lines
2.8 KiB
TypeScript
import type {
|
|
ColumnDef,
|
|
ColumnFiltersState,
|
|
PaginationState,
|
|
RowSelectionState,
|
|
SortingState,
|
|
Table,
|
|
VisibilityState,
|
|
} from '@tanstack/react-table'
|
|
import { createContext, ReactNode, useContext, useMemo } from 'react'
|
|
|
|
import { DataTableFilterField } from '../DataTable.types'
|
|
import { QuerySearchParamsType } from '@/components/interfaces/UnifiedLogs/UnifiedLogs.types'
|
|
import { ResponseError } from '@/types'
|
|
|
|
// REMINDER: read about how to move controlled state out of the useReactTable hook
|
|
// https://github.com/TanStack/table/discussions/4005#discussioncomment-7303569
|
|
|
|
interface DataTableStateContextType {
|
|
columnFilters: ColumnFiltersState
|
|
sorting: SortingState
|
|
rowSelection: RowSelectionState
|
|
columnOrder: string[]
|
|
columnVisibility: VisibilityState
|
|
pagination: PaginationState
|
|
enableColumnOrdering: boolean
|
|
searchParameters: QuerySearchParamsType
|
|
openRowId: string | undefined
|
|
setOpenRowId: (id: string | undefined) => void
|
|
}
|
|
|
|
interface DataTableBaseContextType<TData = unknown, TValue = unknown> {
|
|
table: Table<TData>
|
|
error: ResponseError | null
|
|
filterFields: DataTableFilterField<TData>[]
|
|
columns: ColumnDef<TData, TValue>[]
|
|
isFetching: boolean
|
|
isError: boolean
|
|
isLoading: boolean
|
|
isLoadingCounts: boolean
|
|
getFacetedUniqueValues?: (table: Table<TData>, columnId: string) => Map<string, number>
|
|
getFacetedMinMaxValues?: (table: Table<TData>, columnId: string) => undefined | [number, number]
|
|
}
|
|
|
|
interface DataTableContextType<TData = unknown, TValue = unknown>
|
|
extends DataTableStateContextType, DataTableBaseContextType<TData, TValue> {}
|
|
|
|
export const DataTableContext = createContext<DataTableContextType<any, any> | null>(null)
|
|
|
|
export function DataTableProvider<TData, TValue>({
|
|
children,
|
|
...props
|
|
}: Partial<DataTableStateContextType> &
|
|
DataTableBaseContextType<TData, TValue> & {
|
|
children: ReactNode
|
|
}) {
|
|
const value = useMemo(
|
|
() => ({
|
|
...props,
|
|
columnFilters: props.columnFilters ?? [],
|
|
sorting: props.sorting ?? [],
|
|
rowSelection: props.rowSelection ?? {},
|
|
columnOrder: props.columnOrder ?? [],
|
|
columnVisibility: props.columnVisibility ?? {},
|
|
pagination: props.pagination ?? { pageIndex: 0, pageSize: 10 },
|
|
enableColumnOrdering: props.enableColumnOrdering ?? false,
|
|
searchParameters: props.searchParameters ?? ({} as any),
|
|
openRowId: props.openRowId,
|
|
setOpenRowId: props.setOpenRowId ?? (() => {}),
|
|
}),
|
|
[props]
|
|
)
|
|
|
|
return <DataTableContext.Provider value={value}>{children}</DataTableContext.Provider>
|
|
}
|
|
|
|
export function useDataTable<TData, TValue>() {
|
|
const context = useContext(DataTableContext)
|
|
|
|
if (!context) {
|
|
throw new Error('useDataTable must be used within a DataTableProvider')
|
|
}
|
|
|
|
return context as DataTableContextType<TData, TValue>
|
|
}
|