mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 07:29:16 +00:00
Support filtering log attributes through the picker and log details, preserving filters across URLs and query surfaces. Fixes DEBUG-223.
88 lines
2.7 KiB
TypeScript
88 lines
2.7 KiB
TypeScript
import { useDebounce } from '@uidotdev/usehooks'
|
|
import { useFeatureFlags, useParams } from 'common'
|
|
import { useState } from 'react'
|
|
|
|
import {
|
|
columnFiltersToLogsFilters,
|
|
isLogsFilterColumnValue,
|
|
logsFiltersToUrlParams,
|
|
} from './UnifiedLogs.filters'
|
|
import { QuerySearchParamsType } from './UnifiedLogs.types'
|
|
import { Option } from '@/components/ui/DataTable/DataTable.types'
|
|
import { useDataTable } from '@/components/ui/DataTable/providers/DataTableProvider'
|
|
import { useUnifiedLogsFacetCountQuery } from '@/data/logs/unified-logs-facet-count-query'
|
|
|
|
export function useUnifiedLogsPathnameOptions(value: string, options?: Option[]) {
|
|
const [inputValue, setInputValue] = useState('')
|
|
const { ref: projectRef } = useParams()
|
|
const { hasLoaded: flagsLoaded } = useFeatureFlags()
|
|
const { table, searchParameters, columnFilters, hasPendingFilterChange } = useDataTable<
|
|
unknown,
|
|
unknown,
|
|
QuerySearchParamsType
|
|
>()
|
|
const debouncedSearch = useDebounce(inputValue, 700)
|
|
|
|
const dateValue = columnFilters.find((filter) => filter.id === 'date')?.value
|
|
const date =
|
|
Array.isArray(dateValue) &&
|
|
dateValue.length === 2 &&
|
|
dateValue.every((value) => value instanceof Date)
|
|
? dateValue
|
|
: undefined
|
|
const scopedSearch = hasPendingFilterChange
|
|
? {
|
|
...searchParameters,
|
|
filter: logsFiltersToUrlParams(
|
|
columnFiltersToLogsFilters(columnFilters.filter(({ id }) => id !== 'date'))
|
|
),
|
|
date,
|
|
}
|
|
: searchParameters
|
|
const {
|
|
data: facetOptions,
|
|
error,
|
|
isPending,
|
|
isError,
|
|
isFetching,
|
|
} = useUnifiedLogsFacetCountQuery(
|
|
{
|
|
projectRef,
|
|
search: scopedSearch,
|
|
facet: value,
|
|
facetSearch: debouncedSearch,
|
|
},
|
|
{ enabled: flagsLoaded }
|
|
)
|
|
|
|
const column = table.getColumn(value)
|
|
const filterValue = columnFilters.find((filter) => filter.id === value)?.value
|
|
const selectedValues = isLogsFilterColumnValue(filterValue) ? filterValue.values : []
|
|
const visibleFacetOptions = inputValue === debouncedSearch ? (facetOptions ?? []) : []
|
|
const selectedOptions = selectedValues.map(
|
|
(selected) =>
|
|
visibleFacetOptions.find((option) => option.value === selected) ??
|
|
options?.find((option) => option.value === selected) ?? {
|
|
label: selected,
|
|
value: selected,
|
|
}
|
|
)
|
|
const filterOptions = [
|
|
...selectedOptions,
|
|
...visibleFacetOptions.filter((option) => !selectedValues.includes(option.value)),
|
|
]
|
|
|
|
return {
|
|
column,
|
|
error,
|
|
filterOptions,
|
|
inputValue,
|
|
isError,
|
|
isFetching,
|
|
isLoading: !flagsLoaded || isPending || inputValue !== debouncedSearch,
|
|
projectRef,
|
|
selectedValues,
|
|
setInputValue,
|
|
}
|
|
}
|