Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Tighten Cmd K fuzzy matching (#52207)

Joshen Lim5a98369a8512
diff --git a/apps/studio/components/layouts/ProjectSettingsLayout/ProjectSettings.Commands.tsx b/apps/studio/components/layouts/ProjectSettingsLayout/ProjectSettings.Commands.tsx
index aefdcb0b6c8..6214ef053c1 100644
--- a/apps/studio/components/layouts/ProjectSettingsLayout/ProjectSettings.Commands.tsx
+++ b/apps/studio/components/layouts/ProjectSettingsLayout/ProjectSettings.Commands.tsx
@@ -39,7 +39,7 @@ export function useProjectSettingsGotoCommands(options?: CommandOptions) {
{
id: 'nav-project-settings-add-ons',
name: 'Add-ons',
- value: 'Add-ons addons add ons add on add-on',
+ keywords: ['addons', 'add ons', 'add on', 'add-on'],
route: `/project/${ref}/settings/addons`,
defaultHidden: true,
},
@@ -102,7 +102,12 @@ export function useProjectSettingsGotoCommands(options?: CommandOptions) {
{
id: 'nav-project-settings-integration',
name: 'Project Integrations',
- value: 'Branch integration, Vercel integration, GitHub integration, AWS PrivateLink',
+ keywords: [
+ 'Branch integration',
+ 'Vercel integration',
+ 'GitHub integration',
+ 'AWS PrivateLink',
+ ],
route: `/project/${ref}/settings/integrations`,
},
{
@@ -124,7 +129,7 @@ export function useProjectSettingsGotoCommands(options?: CommandOptions) {
{
id: 'nav-project-settings-restart-project',
name: 'Project availability',
- value: 'project availability restart project pause project resume project',
+ keywords: ['restart project', 'pause project', 'resume project'],
route: `/project/${ref}/settings/general#restart-project`,
defaultHidden: true,
},
diff --git a/packages/ui-patterns/src/CommandMenu/api/CommandMenu.tsx b/packages/ui-patterns/src/CommandMenu/api/CommandMenu.tsx
index 75316524880..0e0372045cf 100644
--- a/packages/ui-patterns/src/CommandMenu/api/CommandMenu.tsx
+++ b/packages/ui-patterns/src/CommandMenu/api/CommandMenu.tsx
@@ -1,5 +1,6 @@
'use client'
+import { defaultFilter } from 'cmdk'
import { useBreakpoint } from 'common'
import useDragToClose from 'common/hooks/useDragToClose'
import { AlertTriangle, ArrowLeft, Search } from 'lucide-react'
@@ -48,6 +49,25 @@ function Breadcrumb({ className }: { className?: string }) {
)
}
+// cmdk's default scorer fuzzy-matches scattered characters across long values, e.g. "colors"
+// matches "Branch integration, Vercel integration, ..." with a score around 0.00002. Genuine
+// prefix/substring/word matches score well above this threshold.
+const MIN_FILTER_SCORE = 0.01
+
+const KEYWORD_MATCH_SCORE = 0.5
+
+// `value` is fuzzy-matched; `keywords` only match as a substring so that long keyword strings
+// don't produce scattered-character matches (e.g. "blog" against "Branch integration, ...").
+const filterWithMinScore: typeof defaultFilter = (value, search, keywords) => {
+ const score = defaultFilter(value, search)
+ const normalizedSearch = search.trim().toLowerCase()
+ const hasKeywordMatch =
+ normalizedSearch.length > 0 &&
+ keywords?.some((keyword) => keyword.toLowerCase().includes(normalizedSearch))
+ const valueScore = score < MIN_FILTER_SCORE ? 0 : score
+ return hasKeywordMatch ? Math.max(valueScore, KEYWORD_MATCH_SCORE) : valueScore
+}
+
const CommandWrapper = forwardRef<
React.ElementRef<typeof Command>,
React.ComponentPropsWithoutRef<typeof Command>
@@ -55,6 +75,7 @@ const CommandWrapper = forwardRef<
return (
<Command
ref={ref}
+ filter={filterWithMinScore}
className={cn(
'h-full w-full flex flex-col overflow-hidden',
'**:[[cmdk-group]]:px-2 **:[[cmdk-group]]:bg-transparent! **:[[cmdk-group-heading]]:bg-transparent! **:[[cmdk-group-heading]]:px-2 **:[[cmdk-group-heading]]:font-medium [&_[cmdk-group-heading]]:text-border-stronger **:[[cmdk-input]]:h-12',
@@ -63,7 +84,6 @@ const CommandWrapper = forwardRef<
'[&_[cmdk-item]_svg]:stroke-1',
'[&_[cmdk-input-wrapper]_svg]:h-5',
'[&_[cmdk-input-wrapper]_svg]:w-5',
-
'[&_[cmdk-group]:not([hidden])_~[cmdk-group]]:pt-0',
className
)}
diff --git a/packages/ui-patterns/src/CommandMenu/internal/CommandMenuItem.tsx b/packages/ui-patterns/src/CommandMenu/internal/CommandMenuItem.tsx
index 1f44b2533ca..08296ae71be 100644
--- a/packages/ui-patterns/src/CommandMenu/internal/CommandMenuItem.tsx
+++ b/packages/ui-patterns/src/CommandMenu/internal/CommandMenuItem.tsx
@@ -99,6 +99,7 @@ const CommandMenuItem = forwardRef<
ref={ref}
onSelect={handleCommandSelect}
value={command.value ?? command.name}
+ keywords={command.keywords && [...command.keywords]}
forceMount={command.forceMount}
className={cn(
generateCommandClassNames(isRouteCommand(command)),
diff --git a/packages/ui-patterns/src/CommandMenu/internal/types.ts b/packages/ui-patterns/src/CommandMenu/internal/types.ts
index e3fe8eb7beb..b5a51802f11 100644
--- a/packages/ui-patterns/src/CommandMenu/internal/types.ts
+++ b/packages/ui-patterns/src/CommandMenu/internal/types.ts
@@ -4,6 +4,8 @@ export interface IBaseCommand {
id: string
name: string
value?: string
+ /** Extra search terms. Unlike `value`, these only match by substring, not fuzzily. */
+ keywords?: readonly string[]
className?: string
forceMount?: boolean
badge?: () => ReactNode