Skip to content

: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.

feat(desktop): show recent command selections (#85466)

Shy Alter43520d997da4
diff --git a/products/desktop/packages/ui/src/features/command/CommandMenu.feedQuery.test.tsx b/products/desktop/packages/ui/src/features/command/CommandMenu.feedQuery.test.tsx
index d880933fd0b9..979214938802 100644
--- a/products/desktop/packages/ui/src/features/command/CommandMenu.feedQuery.test.tsx
+++ b/products/desktop/packages/ui/src/features/command/CommandMenu.feedQuery.test.tsx
@@ -133,6 +133,19 @@ describe("CommandMenu feed queries", () => {
).toBeTruthy();
});
+ it("shows a selected command in the recent section", async () => {
+ const user = userEvent.setup();
+ render(
+ <Theme>
+ <CommandMenu open onOpenChange={() => {}} />
+ </Theme>,
+ );
+
+ await user.click(await screen.findByText("Toggle left sidebar"));
+
+ expect(await screen.findByText("Recent")).toBeTruthy();
+ });
+
it("labels incomplete task search results", async () => {
taskResultsComplete = false;
const user = userEvent.setup();
diff --git a/products/desktop/packages/ui/src/features/command/CommandMenu.tsx b/products/desktop/packages/ui/src/features/command/CommandMenu.tsx
index 1088cbe2c897..4d5cd79f9847 100644
--- a/products/desktop/packages/ui/src/features/command/CommandMenu.tsx
+++ b/products/desktop/packages/ui/src/features/command/CommandMenu.tsx
@@ -46,6 +46,7 @@ import { getDefaultReviewMode } from "@posthog/ui/features/code-review/getDefaul
import { useReviewNavigationStore } from "@posthog/ui/features/code-review/reviewNavigationStore";
import { CommandKeyHints } from "@posthog/ui/features/command/CommandKeyHints";
import {
+ addRecentCommand,
matchesCommandSearch,
prioritizeExactCommandMatches,
} from "@posthog/ui/features/command/commandSearch";
@@ -233,6 +234,7 @@ export function CommandMenu({ open, onOpenChange }: CommandMenuProps) {
(state) => state.activeTasks,
);
const [query, setQuery] = useState("");
+ const [recentCommands, setRecentCommands] = useState<Command[]>([]);
const [remoteQuery, setRemoteQuery] = useState("");
// The legacy title search only ever surfaces while the palette is browsing
// (see `showRemoteSearch` below). The feed-query `mode` that decides that is
@@ -726,7 +728,7 @@ export function CommandMenu({ open, onOpenChange }: CommandMenuProps) {
remoteSearchAllowedRef.current = browsing && !scope;
}, [mode, scope]);
- const sections = useMemo(() => {
+ const baseSections = useMemo(() => {
const browsing = mode === "browsing" || mode === "completingKey";
const showCommands = browsing && (!scope || scope === "command");
const showChannels = browsing && (!scope || scope === "space");
@@ -753,6 +755,28 @@ export function CommandMenu({ open, onOpenChange }: CommandMenuProps) {
searchText,
]);
+ const sections = useMemo(() => {
+ if (query.trim() || recentCommands.length === 0) return baseSections;
+ const currentCommands = new Map(
+ baseSections.flatMap((section) =>
+ section.items.map((command) => [command.id, command] as const),
+ ),
+ );
+ const recentItems = recentCommands.map(
+ (command) => currentCommands.get(command.id) ?? command,
+ );
+ const recentIds = new Set(recentItems.map((command) => command.id));
+ return [
+ { label: "Recent", items: recentItems },
+ ...baseSections
+ .map((section) => ({
+ ...section,
+ items: section.items.filter((command) => !recentIds.has(command.id)),
+ }))
+ .filter((section) => section.items.length > 0),
+ ];
+ }, [baseSections, query, recentCommands]);
+
const paletteFilter = useCallback(
(command: { label: string; keywords?: string }) =>
matchesCommandSearch(command, searchText),
@@ -775,6 +799,9 @@ export function CommandMenu({ open, onOpenChange }: CommandMenuProps) {
action_type: cmd.action,
channel_id: cmd.channelId,
});
+ if (!cmd.keepOpen) {
+ setRecentCommands((recent) => addRecentCommand(recent, cmd));
+ }
cmd.onRun();
if (cmd.keepOpen) return;
onOpenChange(false);
diff --git a/products/desktop/packages/ui/src/features/command/commandSearch.test.ts b/products/desktop/packages/ui/src/features/command/commandSearch.test.ts
index 64180ba333e3..d83973636709 100644
--- a/products/desktop/packages/ui/src/features/command/commandSearch.test.ts
+++ b/products/desktop/packages/ui/src/features/command/commandSearch.test.ts
@@ -1,4 +1,5 @@
import {
+ addRecentCommand,
matchesCommandSearch,
prioritizeExactCommandMatches,
} from "@posthog/ui/features/command/commandSearch";
@@ -39,4 +40,17 @@ describe("prioritizeExactCommandMatches", () => {
matchesCommandSearch(command("release", "Release"), " release "),
).toBe(true);
});
+
+ it("keeps the five most recently selected commands without duplicates", () => {
+ const selected = ["one", "two", "three", "four", "five", "six"].reduce(
+ (recent, id) => addRecentCommand(recent, command(id, id)),
+ [] as Command[],
+ );
+
+ expect(
+ addRecentCommand(selected, command("four", "four")).map(
+ (item) => item.id,
+ ),
+ ).toEqual(["four", "six", "five", "three", "two"]);
+ });
});
diff --git a/products/desktop/packages/ui/src/features/command/commandSearch.ts b/products/desktop/packages/ui/src/features/command/commandSearch.ts
index af5b128c33c1..5d1d899b3255 100644
--- a/products/desktop/packages/ui/src/features/command/commandSearch.ts
+++ b/products/desktop/packages/ui/src/features/command/commandSearch.ts
@@ -1,5 +1,19 @@
import type { CommandSection } from "@posthog/ui/features/command/useSearchSections";
+export const RECENT_COMMAND_LIMIT = 5;
+
+export function addRecentCommand<T extends { id: string }>(
+ recentCommands: T[],
+ command: T,
+): T[] {
+ return [
+ command,
+ ...recentCommands.filter(
+ (recentCommand) => recentCommand.id !== command.id,
+ ),
+ ].slice(0, RECENT_COMMAND_LIMIT);
+}
+
export function matchesCommandSearch(
command: { label: string; keywords?: string },
query: string,