Skip to content

Commit b0dd057

Browse files
saphidclaude
andcommitted
fix(web): going back in the command palette keeps the search that led there
Back from a nested picker (Backspace or the Back button) and Escape from file or content search now restore the palette query, and linked-thread results, that were showing before. Closing the palette, a fresh shortcut, or a direct intent still starts clean. Fixes #15936. Replaces #12873. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 250e052 commit b0dd057

3 files changed

Lines changed: 206 additions & 32 deletions

File tree

‎apps/web/src/components/CommandPalette.logic.test.ts‎

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@ import {
1414
filterCommandPaletteGroups,
1515
findHighlightedCommandPaletteItem,
1616
reduceCommandPaletteUiState,
17+
createCommandPaletteSearchState,
18+
reduceCommandPaletteSearchState,
1719
type CommandPaletteActionItem,
1820
type CommandPaletteGroup,
21+
type CommandPaletteUiState,
1922
} from "./CommandPalette.logic";
2023

2124
describe("linked pull request thread navigation", () => {
@@ -894,3 +897,95 @@ describe("virtualized command palette rows", () => {
894897
expect(findHighlightedCommandPaletteItem(groups, null)).toBeNull();
895898
});
896899
});
900+
901+
describe("command palette search return", () => {
902+
const picker = { addonIcon: null, groups: [] };
903+
904+
it("Back restores the query that led into each nested picker", () => {
905+
let state = createCommandPaletteSearchState(">new thread");
906+
state = reduceCommandPaletteSearchState(state, { type: "push", view: picker });
907+
expect(state.query).toBe("");
908+
state = reduceCommandPaletteSearchState(state, { type: "query", query: "remote project" });
909+
state = reduceCommandPaletteSearchState(state, {
910+
type: "push",
911+
view: { ...picker, initialQuery: "~/" },
912+
});
913+
expect(state.query).toBe("~/");
914+
state = reduceCommandPaletteSearchState(state, { type: "back" });
915+
expect(state.query).toBe("remote project");
916+
expect(state.views).toHaveLength(1);
917+
state = reduceCommandPaletteSearchState(state, { type: "back" });
918+
expect(state).toEqual(createCommandPaletteSearchState(">new thread"));
919+
expect(reduceCommandPaletteSearchState(state, { type: "back" })).toBe(state);
920+
});
921+
922+
it("clearing a child's prefilled query before Back keeps the parent query", () => {
923+
let state = createCommandPaletteSearchState(">add project");
924+
state = reduceCommandPaletteSearchState(state, {
925+
type: "push",
926+
view: { ...picker, initialQuery: "~/" },
927+
});
928+
state = reduceCommandPaletteSearchState(state, { type: "query", query: "" });
929+
state = reduceCommandPaletteSearchState(state, { type: "back" });
930+
expect(state.query).toBe(">add project");
931+
});
932+
933+
it("a reset drops the pickers it replaces", () => {
934+
const nested = reduceCommandPaletteSearchState(createCommandPaletteSearchState("old"), {
935+
type: "push",
936+
view: picker,
937+
});
938+
const reset = reduceCommandPaletteSearchState(nested, { type: "reset", query: "linked" });
939+
expect(reduceCommandPaletteSearchState(reset, { type: "back" })).toEqual(
940+
createCommandPaletteSearchState("linked"),
941+
);
942+
});
943+
});
944+
945+
describe("returning from file and content search", () => {
946+
const root: CommandPaletteUiState = { open: true, mode: "command", openIntent: null };
947+
const context = { query: "go to file", linkedThreadSearch: null };
948+
949+
it.each(["files", "content"] as const)("Escape from %s restores the palette query", (mode) => {
950+
const child = reduceCommandPaletteUiState(root, { _tag: "OpenChildSearch", mode, context });
951+
expect(child.mode).toBe(mode);
952+
const returned = reduceCommandPaletteUiState(child, { _tag: "BackToCommand" });
953+
expect(returned).toEqual({ ...root, restoredSearch: context });
954+
});
955+
956+
it("restores linked-thread search results with the query", () => {
957+
const linkedContext = {
958+
query: "https://github.com/acme/web/pull/7",
959+
linkedThreadSearch: {
960+
kind: "search" as const,
961+
query: "https://github.com/acme/web/pull/7",
962+
linkedThreads: { environmentId: EnvironmentId.make("remote"), threads: [] },
963+
},
964+
};
965+
const child = reduceCommandPaletteUiState(root, {
966+
_tag: "OpenChildSearch",
967+
mode: "content",
968+
context: linkedContext,
969+
});
970+
const returned = reduceCommandPaletteUiState(child, { _tag: "BackToCommand" });
971+
expect(returned.restoredSearch).toEqual(linkedContext);
972+
});
973+
974+
it("closing, a fresh shortcut, or a direct intent forgets the parent query", () => {
975+
const child = reduceCommandPaletteUiState(root, {
976+
_tag: "OpenChildSearch",
977+
mode: "files",
978+
context,
979+
});
980+
const closed = reduceCommandPaletteUiState(child, { _tag: "SetOpen", open: false });
981+
expect(reduceCommandPaletteUiState(closed, { _tag: "BackToCommand" })).toBe(closed);
982+
expect(reduceCommandPaletteUiState(closed, { _tag: "SetOpen", open: true })).toEqual(root);
983+
984+
const fresh = reduceCommandPaletteUiState(child, { _tag: "ToggleMode", mode: "content" });
985+
expect(reduceCommandPaletteUiState(fresh, { _tag: "BackToCommand" })).toEqual(root);
986+
987+
const direct = reduceCommandPaletteUiState(child, { _tag: "OpenChangeTheme" });
988+
expect(direct.parentSearch).toBeUndefined();
989+
expect(direct.restoredSearch).toBeUndefined();
990+
});
991+
});

‎apps/web/src/components/CommandPalette.logic.ts‎

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ export interface CommandPaletteUiState {
7272
readonly open: boolean;
7373
readonly mode: SearchOverlayMode;
7474
readonly openIntent: CommandPaletteOpenIntent | null;
75+
readonly parentSearch?: CommandPaletteReturnContext;
76+
readonly restoredSearch?: CommandPaletteReturnContext;
7577
}
7678

7779
export type CommandPaletteUiAction =
@@ -85,7 +87,13 @@ export type CommandPaletteUiAction =
8587
| { readonly _tag: "OpenAddProject" }
8688
| { readonly _tag: "OpenNewThreadIn" }
8789
| { readonly _tag: "OpenChangeTheme" }
88-
| { readonly _tag: "ClearOpenIntent" };
90+
| { readonly _tag: "ClearOpenIntent" }
91+
| {
92+
readonly _tag: "OpenChildSearch";
93+
readonly mode: "files" | "content";
94+
readonly context: CommandPaletteReturnContext;
95+
}
96+
| { readonly _tag: "BackToCommand" };
8997

9098
export function reduceCommandPaletteUiState(
9199
state: CommandPaletteUiState,
@@ -95,11 +103,22 @@ export function reduceCommandPaletteUiState(
95103
case "SetOpen":
96104
return action.open
97105
? { open: true, mode: "command", openIntent: state.openIntent }
98-
: { ...state, open: false, openIntent: null };
106+
: { open: false, mode: state.mode, openIntent: null };
99107
case "ToggleMode":
100108
return state.open && state.mode === action.mode
101-
? { ...state, open: false, openIntent: null }
109+
? { open: false, mode: state.mode, openIntent: null }
102110
: { open: true, mode: action.mode, openIntent: null };
111+
case "OpenChildSearch":
112+
return { open: true, mode: action.mode, openIntent: null, parentSearch: action.context };
113+
case "BackToCommand":
114+
return state.open && state.mode !== "command"
115+
? {
116+
open: true,
117+
mode: "command",
118+
openIntent: null,
119+
...(state.parentSearch ? { restoredSearch: state.parentSearch } : {}),
120+
}
121+
: state;
103122
case "OpenSearch":
104123
return {
105124
open: true,
@@ -172,6 +191,52 @@ export interface CommandPaletteView {
172191
readonly initialQuery?: string;
173192
}
174193

194+
// File and content search replace the palette, so Escape remounts it. Keep
195+
// its input and linked-thread data to restore, not views whose actions
196+
// belonged to the unmounted palette.
197+
export interface CommandPaletteReturnContext {
198+
readonly query: string;
199+
readonly linkedThreadSearch: Extract<CommandPaletteOpenIntent, { kind: "search" }> | null;
200+
}
201+
202+
// Each nested view remembers the query that led into it, so Back restores it.
203+
export interface CommandPaletteSearchState {
204+
readonly query: string;
205+
readonly views: ReadonlyArray<{
206+
readonly view: CommandPaletteView;
207+
readonly parentQuery: string;
208+
}>;
209+
}
210+
211+
export function createCommandPaletteSearchState(query = ""): CommandPaletteSearchState {
212+
return { query, views: [] };
213+
}
214+
215+
export function reduceCommandPaletteSearchState(
216+
state: CommandPaletteSearchState,
217+
action:
218+
| { readonly type: "query"; readonly query: string }
219+
| { readonly type: "push"; readonly view: CommandPaletteView }
220+
| { readonly type: "back" }
221+
| { readonly type: "reset"; readonly query?: string },
222+
): CommandPaletteSearchState {
223+
switch (action.type) {
224+
case "query":
225+
return { ...state, query: action.query };
226+
case "push":
227+
return {
228+
query: action.view.initialQuery ?? "",
229+
views: [...state.views, { view: action.view, parentQuery: state.query }],
230+
};
231+
case "back": {
232+
const parent = state.views.at(-1);
233+
return parent ? { query: parent.parentQuery, views: state.views.slice(0, -1) } : state;
234+
}
235+
case "reset":
236+
return createCommandPaletteSearchState(action.query);
237+
}
238+
}
239+
175240
export type CommandPaletteRow =
176241
| {
177242
readonly kind: "label";

0 commit comments

Comments
 (0)