Files
supabase/apps/studio/components/interfaces/App/CommandMenu/DatabaseEntitySearchCommands.tsx
T
Joshen Lim 7d63d764fc Joshenlim/fe 4712 cmd k support searching for database tables (#52205)
## Context

Looking into some kaizen improvements for Cmd+K, with the overarching
idea to make Cmd+K resource-aware so finding and inspecting anything in
a project doesn't depend on knowing where it lives in the dashboard.

Specifically looking into database entities for now - end state would be
that you could search for a table name for example, and Cmd K will pull
up everything *related* to that table for you to skim (e.g Policies,
Functions, Triggers, etc). Changes in this PR sets the foundation here
by starting with just the tables.
<img width="500" alt="image"
src="https://github.com/user-attachments/assets/65054781-2385-416c-aed9-f142380e4454"
/>

This might be similar to how we currently support searching for database
entities via these "Queries" section, but these only search within the
context of the entity category, not across (e.g only tables, only
policies)
<img width="500" alt="image"
src="https://github.com/user-attachments/assets/66ca16c6-9551-4134-b1e1-0b0c4b004085"
/>

The idea isn't to replace this, but provide another way to find your
database entities so that
- You can see related entities to what you're searching at a glance
- You don't need to go to a 2nd level nav in Cmd K 

Also fwiw the bulk of the LOC changes in here are mainly tests actually,
actual LOC changes are +385, -22
2026-10-23 23:24:56 +08:00

86 lines
3.2 KiB
TypeScript

import { useQuery } from '@tanstack/react-query'
import { useDebounce } from '@uidotdev/usehooks'
import { DatabaseIcon } from 'lucide-react'
import type { ICommand } from 'ui-patterns/CommandMenu'
import {
useCommandMenuOpen,
useQuery as useCommandMenuQuery,
useRegisterCommands,
} from 'ui-patterns/CommandMenu'
import { COMMAND_MENU_SECTIONS } from './CommandMenu.utils'
import {
ENTITY_SEARCH_DEBOUNCE_MS,
formatTableSearchDetails,
getEntitySearchQuery,
getTableEditorRoute,
matchesEntitySearch,
} from './DatabaseEntitySearch.utils'
import { orderCommandSectionsByPriority } from './ordering'
import { entitySearchQueryOptions } from '@/data/database/entity-search-query'
import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
/**
* Searches the project's database entities by name as the user types in the
* command menu, and registers matches as their own section. Filtering happens
* in the database (see getSearchEntitiesSql), so results are force-mounted to
* stop cmdk from filtering them a second time.
*/
export function useDatabaseEntitySearchCommands() {
const { data: project } = useSelectedProjectQuery()
const isOpen = useCommandMenuOpen()
const commandMenuQuery = useCommandMenuQuery()
const debouncedQuery = useDebounce(commandMenuQuery, ENTITY_SEARCH_DEBOUNCE_MS)
const currentSearch = getEntitySearchQuery(commandMenuQuery)
const debouncedSearch = getEntitySearchQuery(debouncedQuery)
const { data, isPlaceholderData } = useQuery(
entitySearchQueryOptions(
{
projectRef: project?.ref,
connectionString: project?.connectionString,
query: debouncedSearch.query,
},
{ enabled: isOpen && debouncedSearch.isSearchable }
)
)
// Results lag behind the input (debounce, request in flight, placeholder data
// from the previous search). Until they catch up, only keep the ones that
// still match what's typed, so e.g. `blog_posts` results never show for
// `colors`, while refining `blog` to `blog_p` narrows smoothly.
const canShowResults = isOpen && currentSearch.isSearchable
const isStale = isPlaceholderData || debouncedSearch.query !== currentSearch.query
const results = data?.tables ?? []
const visibleResults = isStale
? results.filter((table) => matchesEntitySearch(table, currentSearch.query))
: results
const tables = canShowResults ? visibleResults : []
const projectRef = project?.ref ?? '_'
const tableCommands: ICommand[] = tables.map((table) => {
const displayName = `${table.schema}.${table.name}`
return {
id: `entity-search-table-${table.id}`,
name: displayName,
value: `entity-search-table-${table.id} ${displayName}`,
forceMount: true,
icon: () => <DatabaseIcon />,
badge: () => (
<span className="text-xs text-foreground-lighter truncate pl-4 shrink-0">
{formatTableSearchDetails(table)}
</span>
),
route: getTableEditorRoute(projectRef, table),
}
})
useRegisterCommands(COMMAND_MENU_SECTIONS.DATABASE_TABLES, tableCommands, {
deps: [tables, projectRef],
enabled: tableCommands.length > 0,
forceMountSection: true,
orderSection: orderCommandSectionsByPriority,
sectionMeta: { priority: 2 },
})
}