Files
supabase/apps/studio/components/interfaces/QueryPerformance/hooks/useIndexInvalidation.ts
Ali Waseem e7a4ca9b74 Feature: show index advisor in table editor (#41166)
* added table advisor query

* updated to include table editor performance

* updated JSON B

* added side panel

* updated query indexes to show highlights context

* show index advisor in table editor

* updated invalidation logic

* added color updates

* added query indexes

* updated query performance type

* updated overflow and title

* put behind flag

* remove gap

* added on close

* Update apps/studio/data/database/table-index-advisor-query.ts

Co-authored-by: Charis <26616127+charislam@users.noreply.github.com>

* updated styling

---------

Co-authored-by: Charis <26616127+charislam@users.noreply.github.com>
2025-12-10 09:28:45 -07:00

58 lines
2.1 KiB
TypeScript

import { useQueryClient } from '@tanstack/react-query'
import { useRouter } from 'next/router'
import { parseAsString, useQueryStates } from 'nuqs'
import { useCallback } from 'react'
import {
QueryPerformanceSort,
useQueryPerformanceQuery,
} from 'components/interfaces/Reports/Reports.queries'
import { databaseIndexesKeys } from 'data/database-indexes/keys'
import { databaseKeys } from 'data/database/keys'
import { useSelectedProjectQuery } from 'hooks/misc/useSelectedProject'
import {
QUERY_PERFORMANCE_PRESET_MAP,
QUERY_PERFORMANCE_REPORT_TYPES,
} from '../QueryPerformance.constants'
import { useIndexAdvisorStatus } from './useIsIndexAdvisorStatus'
import { useTableEditorTableStateSnapshot } from 'state/table-editor-table'
import { useTableIndexAdvisor } from 'components/grid/context/TableIndexAdvisorContext'
export function useIndexInvalidation() {
const router = useRouter()
const queryClient = useQueryClient()
const { data: project } = useSelectedProjectQuery()
const { isIndexAdvisorEnabled } = useIndexAdvisorStatus()
const [{ preset: urlPreset, search: searchQuery, order, sort }] = useQueryStates({
sort: parseAsString,
search: parseAsString.withDefault(''),
order: parseAsString,
preset: parseAsString.withDefault('unified'),
})
const { invalidate: invalidateTableIndexAdvisor } = useTableIndexAdvisor()
const preset = QUERY_PERFORMANCE_PRESET_MAP[urlPreset as QUERY_PERFORMANCE_REPORT_TYPES]
const orderBy = !!sort ? ({ column: sort, order } as QueryPerformanceSort) : undefined
const roles = router?.query?.roles ?? []
const queryPerformanceQuery = useQueryPerformanceQuery({
searchQuery,
orderBy,
preset,
roles: typeof roles === 'string' ? [roles] : roles,
runIndexAdvisor: isIndexAdvisorEnabled,
})
return useCallback(() => {
queryPerformanceQuery.runQuery()
queryClient.invalidateQueries({
queryKey: databaseKeys.indexAdvisorFromQuery(project?.ref, ''),
})
queryClient.invalidateQueries({ queryKey: databaseIndexesKeys.list(project?.ref) })
invalidateTableIndexAdvisor()
}, [queryPerformanceQuery, queryClient, project?.ref, invalidateTableIndexAdvisor])
}