Skip to content

Commit 6567ffd

Browse files
saphidclaudegithub-actions[bot]juliusmarminge
authored
fix(web): going back in the command palette keeps the search that led there (#15940)
Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]> Co-authored-by: SAPHID <[email protected]> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Julius Marminge <[email protected]>
1 parent e5d6a09 commit 6567ffd

3 files changed

Lines changed: 219 additions & 62 deletions

File tree

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

Lines changed: 104 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,104 @@ 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("restores each parent query when backing out of nested pickers", () => {
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+
915+
state = reduceCommandPaletteSearchState(state, { type: "query", query: "" });
916+
state = reduceCommandPaletteSearchState(state, { type: "back" });
917+
expect(state.query).toBe("remote project");
918+
expect(state.views).toHaveLength(1);
919+
state = reduceCommandPaletteSearchState(state, { type: "back" });
920+
expect(state.query).toBe(">new thread");
921+
expect(state.views).toHaveLength(0);
922+
});
923+
924+
it("cannot return to an old picker after a new search replaces it", () => {
925+
let state = reduceCommandPaletteSearchState(createCommandPaletteSearchState("old"), {
926+
type: "push",
927+
view: picker,
928+
});
929+
state = reduceCommandPaletteSearchState(state, { type: "reset", query: "fresh" });
930+
state = reduceCommandPaletteSearchState(state, { type: "back" });
931+
expect(state.query).toBe("fresh");
932+
expect(state.views).toHaveLength(0);
933+
});
934+
});
935+
936+
describe("returning from file and content search", () => {
937+
const root: CommandPaletteUiState = { open: true, mode: "command", openIntent: null };
938+
const query = "https://github.com/acme/web/pull/7";
939+
const context = {
940+
query,
941+
linkedThreadSearch: {
942+
kind: "search" as const,
943+
query,
944+
linkedThreads: {
945+
environmentId: EnvironmentId.make("remote"),
946+
threads: [
947+
{
948+
id: ThreadId.make("linked-thread"),
949+
projectId: ProjectId.make("project"),
950+
title: "Completed work",
951+
archivedAt: "2026-09-01T00:00:00.000Z",
952+
},
953+
],
954+
},
955+
},
956+
};
957+
958+
it.each(["files", "content"] as const)(
959+
"Escape from %s restores the query and linked-thread results",
960+
(mode) => {
961+
const child = reduceCommandPaletteUiState(root, { _tag: "OpenChildSearch", mode, context });
962+
expect(child.mode).toBe(mode);
963+
const returned = reduceCommandPaletteUiState(child, { _tag: "BackToCommand" });
964+
expect(returned).toEqual({ ...root, restoredSearch: context });
965+
expect(reduceCommandPaletteUiState(returned, { _tag: "BackToCommand" })).toEqual(returned);
966+
},
967+
);
968+
969+
it("closing a child search discards its return context before reopening", () => {
970+
const child: CommandPaletteUiState = { ...root, mode: "files", parentSearch: context };
971+
const closed = reduceCommandPaletteUiState(child, { _tag: "SetOpen", open: false });
972+
expect(closed).toEqual({ open: false, mode: "files", openIntent: null });
973+
expect(reduceCommandPaletteUiState(closed, { _tag: "BackToCommand" })).toEqual(closed);
974+
expect(reduceCommandPaletteUiState(closed, { _tag: "SetOpen", open: true })).toEqual(root);
975+
});
976+
977+
it("a fresh overlay shortcut has no parent search to return to", () => {
978+
const child = reduceCommandPaletteUiState(root, {
979+
_tag: "OpenChildSearch",
980+
mode: "files",
981+
context,
982+
});
983+
const fresh = reduceCommandPaletteUiState(child, { _tag: "ToggleMode", mode: "content" });
984+
expect(fresh).toEqual({ open: true, mode: "content", openIntent: null });
985+
expect(reduceCommandPaletteUiState(fresh, { _tag: "BackToCommand" })).toEqual(root);
986+
});
987+
988+
it("a direct search intent replaces restored query and linked-thread context", () => {
989+
const child = reduceCommandPaletteUiState(root, {
990+
_tag: "OpenChildSearch",
991+
mode: "content",
992+
context,
993+
});
994+
const returned = reduceCommandPaletteUiState(child, { _tag: "BackToCommand" });
995+
expect(reduceCommandPaletteUiState(returned, { _tag: "OpenSearch", query: "fresh" })).toEqual({
996+
...root,
997+
openIntent: { kind: "search", query: "fresh" },
998+
});
999+
});
1000+
});

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

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ export interface CommandPaletteUiState {
6565
readonly open: boolean;
6666
readonly mode: SearchOverlayMode;
6767
readonly openIntent: CommandPaletteOpenIntent | null;
68+
readonly parentSearch?: CommandPaletteReturnContext;
69+
readonly restoredSearch?: CommandPaletteReturnContext;
6870
}
6971

7072
export type CommandPaletteUiAction =
@@ -78,7 +80,13 @@ export type CommandPaletteUiAction =
7880
| { readonly _tag: "OpenAddProject" }
7981
| { readonly _tag: "OpenNewThreadIn" }
8082
| { readonly _tag: "OpenChangeTheme" }
81-
| { readonly _tag: "ClearOpenIntent" };
83+
| { readonly _tag: "ClearOpenIntent" }
84+
| {
85+
readonly _tag: "OpenChildSearch";
86+
readonly mode: "files" | "content";
87+
readonly context: CommandPaletteReturnContext;
88+
}
89+
| { readonly _tag: "BackToCommand" };
8290

8391
export function reduceCommandPaletteUiState(
8492
state: CommandPaletteUiState,
@@ -88,11 +96,22 @@ export function reduceCommandPaletteUiState(
8896
case "SetOpen":
8997
return action.open
9098
? { open: true, mode: "command", openIntent: state.openIntent }
91-
: { ...state, open: false, openIntent: null };
99+
: { open: false, mode: state.mode, openIntent: null };
92100
case "ToggleMode":
93101
return state.open && state.mode === action.mode
94-
? { ...state, open: false, openIntent: null }
102+
? { open: false, mode: state.mode, openIntent: null }
95103
: { open: true, mode: action.mode, openIntent: null };
104+
case "OpenChildSearch":
105+
return { open: true, mode: action.mode, openIntent: null, parentSearch: action.context };
106+
case "BackToCommand":
107+
return state.open && state.mode !== "command"
108+
? {
109+
open: true,
110+
mode: "command",
111+
openIntent: null,
112+
...(state.parentSearch ? { restoredSearch: state.parentSearch } : {}),
113+
}
114+
: state;
96115
case "OpenSearch":
97116
return {
98117
open: true,
@@ -162,6 +181,49 @@ export interface CommandPaletteView {
162181
readonly initialQuery?: string;
163182
}
164183

184+
// Child search unmounts the palette; retain search data without its view closures.
185+
export interface CommandPaletteReturnContext {
186+
readonly query: string;
187+
readonly linkedThreadSearch: Extract<CommandPaletteOpenIntent, { kind: "search" }> | null;
188+
}
189+
190+
export interface CommandPaletteSearchState {
191+
readonly query: string;
192+
readonly views: ReadonlyArray<{
193+
readonly view: CommandPaletteView;
194+
readonly parentQuery: string;
195+
}>;
196+
}
197+
198+
export function createCommandPaletteSearchState(query = ""): CommandPaletteSearchState {
199+
return { query, views: [] };
200+
}
201+
202+
export function reduceCommandPaletteSearchState(
203+
state: CommandPaletteSearchState,
204+
action:
205+
| { readonly type: "query"; readonly query: string }
206+
| { readonly type: "push"; readonly view: CommandPaletteView }
207+
| { readonly type: "back" }
208+
| { readonly type: "reset"; readonly query?: string },
209+
): CommandPaletteSearchState {
210+
switch (action.type) {
211+
case "query":
212+
return { ...state, query: action.query };
213+
case "push":
214+
return {
215+
query: action.view.initialQuery ?? "",
216+
views: [...state.views, { view: action.view, parentQuery: state.query }],
217+
};
218+
case "back": {
219+
const parent = state.views.at(-1);
220+
return parent ? { query: parent.parentQuery, views: state.views.slice(0, -1) } : state;
221+
}
222+
case "reset":
223+
return createCommandPaletteSearchState(action.query);
224+
}
225+
}
226+
165227
export type CommandPaletteRow =
166228
| {
167229
readonly kind: "label";

0 commit comments

Comments
 (0)