:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(desktop): prioritize exact command searches (#81958)
Matt Pua30a8b749750e
diff --git a/products/desktop/packages/ui/src/features/command/CommandMenu.tsx b/products/desktop/packages/ui/src/features/command/CommandMenu.tsxindex 3b72e0f7ef86..8f6b0b7dc01b 100644--- a/products/desktop/packages/ui/src/features/command/CommandMenu.tsx+++ b/products/desktop/packages/ui/src/features/command/CommandMenu.tsx@@ -40,6 +40,10 @@ import { useTaskChannelMap } from "@posthog/ui/features/canvas/hooks/useTaskChanimport { getDefaultReviewMode } from "@posthog/ui/features/code-review/getDefaultReviewMode";import { useReviewNavigationStore } from "@posthog/ui/features/code-review/reviewNavigationStore";import { CommandKeyHints } from "@posthog/ui/features/command/CommandKeyHints";+import {+ matchesCommandSearch,+ prioritizeExactCommandMatches,+} from "@posthog/ui/features/command/commandSearch";import { useFileSearchStore } from "@posthog/ui/features/command/fileSearchStore";import {formatHotkeyParts,@@ -576,13 +580,17 @@ export function CommandMenu({ open, onOpenChange }: CommandMenuProps) {// Commands, channels, and tasks share a single filterable list.const sections = useMemo(- () => [- ...searchSections,- ...commandSections,- ...channelSections,- ...taskSections,- ],- [searchSections, commandSections, channelSections, taskSections],+ () =>+ prioritizeExactCommandMatches(+ [+ ...searchSections,+ ...commandSections,+ ...channelSections,+ ...taskSections,+ ],+ query,+ ),+ [searchSections, commandSections, channelSections, taskSections, query],);const allCommands = useMemo(@@ -622,11 +630,7 @@ export function CommandMenu({ open, onOpenChange }: CommandMenuProps) {setQuery(val);}}}- filter={(cmd, q) => {- if (!q) return true;- const haystack = `${cmd.label} ${cmd.keywords ?? ""}`.toLowerCase();- return haystack.includes(q.toLowerCase());- }}+ filter={matchesCommandSearch}><AutocompleteInputplaceholder={diff --git a/products/desktop/packages/ui/src/features/command/commandSearch.test.ts b/products/desktop/packages/ui/src/features/command/commandSearch.test.tsnew file mode 100644index 000000000000..64180ba333e3--- /dev/null+++ b/products/desktop/packages/ui/src/features/command/commandSearch.test.ts@@ -0,0 +1,42 @@+import {+ matchesCommandSearch,+ prioritizeExactCommandMatches,+} from "@posthog/ui/features/command/commandSearch";+import type {+ Command,+ CommandSection,+} from "@posthog/ui/features/command/useSearchSections";+import { describe, expect, it } from "vitest";++const command = (id: string, label: string): Command => ({+ id,+ label,+ icon: null,+ action: "open-task",+ onRun: () => {},+});++describe("prioritizeExactCommandMatches", () => {+ it("places an exact label match before partial matches from earlier sections", () => {+ const sections: CommandSection[] = [+ { label: "Actions", items: [command("new", "New release task")] },+ { label: "Tasks", items: [command("release", "Release")] },+ ];++ const prioritized = prioritizeExactCommandMatches(sections, " release ");++ expect(prioritized.map((section) => section.label)).toEqual([+ "Exact matches",+ "Actions",+ ]);+ expect(+ prioritized.map((section) => section.items.map((item) => item.id)),+ ).toEqual([["release"], ["new"]]);+ });++ it("matches a query with surrounding whitespace", () => {+ expect(+ matchesCommandSearch(command("release", "Release"), " release "),+ ).toBe(true);+ });+});diff --git a/products/desktop/packages/ui/src/features/command/commandSearch.ts b/products/desktop/packages/ui/src/features/command/commandSearch.tsnew file mode 100644index 000000000000..af5b128c33c1--- /dev/null+++ b/products/desktop/packages/ui/src/features/command/commandSearch.ts@@ -0,0 +1,39 @@+import type { CommandSection } from "@posthog/ui/features/command/useSearchSections";++export function matchesCommandSearch(+ command: { label: string; keywords?: string },+ query: string,+): boolean {+ const normalizedQuery = query.trim().toLowerCase();+ if (!normalizedQuery) return true;++ const haystack = `${command.label} ${command.keywords ?? ""}`.toLowerCase();+ return haystack.includes(normalizedQuery);+}++export function prioritizeExactCommandMatches(+ sections: CommandSection[],+ query: string,+): CommandSection[] {+ const normalizedQuery = query.trim().toLowerCase();+ if (!normalizedQuery) return sections;++ const exactMatches = sections.flatMap((section) =>+ section.items.filter(+ (item) => item.label.trim().toLowerCase() === normalizedQuery,+ ),+ );+ if (exactMatches.length === 0) return sections;++ return [+ { label: "Exact matches", items: exactMatches },+ ...sections+ .map((section) => ({+ ...section,+ items: section.items.filter(+ (item) => item.label.trim().toLowerCase() !== normalizedQuery,+ ),+ }))+ .filter((section) => section.items.length > 0),+ ];+}