Skip to content

Commit 912b825

Browse files
committed
fix(web): restore parent queries after nested search
1 parent 61d7660 commit 912b825

3 files changed

Lines changed: 268 additions & 33 deletions

File tree

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

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@ import {
1111
filterPinnedBrowseEntries,
1212
filterCommandPaletteGroups,
1313
reduceCommandPaletteUiState,
14+
createCommandPaletteSearchState,
15+
reduceCommandPaletteSearchState,
1416
type CommandPaletteActionItem,
1517
type CommandPaletteGroup,
18+
type CommandPaletteUiState,
1619
} from "./CommandPalette.logic";
1720

1821
describe("linked pull request thread navigation", () => {
@@ -803,3 +806,133 @@ describe("filterCommandPaletteGroups", () => {
803806
]);
804807
});
805808
});
809+
810+
describe("command palette search return", () => {
811+
const projectPicker = { addonIcon: null, groups: [] };
812+
813+
it("restores distinct queries through multiple nested pickers", () => {
814+
let state = createCommandPaletteSearchState(">new thread");
815+
state = reduceCommandPaletteSearchState(state, { type: "push", view: projectPicker });
816+
expect(state.query).toBe("");
817+
state = reduceCommandPaletteSearchState(state, { type: "query", query: "remote project" });
818+
state = reduceCommandPaletteSearchState(state, {
819+
type: "push",
820+
view: { ...projectPicker, initialQuery: "~/" },
821+
});
822+
state = reduceCommandPaletteSearchState(state, { type: "query", query: "~/work" });
823+
state = reduceCommandPaletteSearchState(state, { type: "back" });
824+
expect(state.query).toBe("remote project");
825+
expect(state.views).toHaveLength(1);
826+
state = reduceCommandPaletteSearchState(state, { type: "back" });
827+
expect(state).toEqual(createCommandPaletteSearchState(">new thread"));
828+
expect(reduceCommandPaletteSearchState(state, { type: "back" })).toBe(state);
829+
});
830+
831+
it("clearing a child prefix before Back does not erase the parent input", () => {
832+
let state = createCommandPaletteSearchState(">add project");
833+
state = reduceCommandPaletteSearchState(state, {
834+
type: "push",
835+
view: { ...projectPicker, initialQuery: "~/" },
836+
});
837+
state = reduceCommandPaletteSearchState(state, { type: "query", query: "" });
838+
state = reduceCommandPaletteSearchState(state, { type: "back" });
839+
expect(state.query).toBe(">add project");
840+
state = reduceCommandPaletteSearchState(state, { type: "query", query: "" });
841+
expect(state).toEqual(createCommandPaletteSearchState());
842+
});
843+
844+
it("new direct-open and fresh-open searches cannot return to old input", () => {
845+
const nested = reduceCommandPaletteSearchState(createCommandPaletteSearchState("old search"), {
846+
type: "push",
847+
view: projectPicker,
848+
});
849+
const newSearch = reduceCommandPaletteSearchState(nested, {
850+
type: "reset",
851+
query: "linked thread",
852+
});
853+
expect(reduceCommandPaletteSearchState(newSearch, { type: "back" })).toEqual(
854+
createCommandPaletteSearchState("linked thread"),
855+
);
856+
const directPicker = reduceCommandPaletteSearchState(
857+
reduceCommandPaletteSearchState(nested, { type: "reset" }),
858+
{ type: "push", view: projectPicker },
859+
);
860+
expect(reduceCommandPaletteSearchState(directPicker, { type: "back" })).toEqual(
861+
createCommandPaletteSearchState(),
862+
);
863+
expect(createCommandPaletteSearchState()).toEqual({ query: "", views: [] });
864+
});
865+
});
866+
867+
describe("palette-origin search overlay return", () => {
868+
const root: CommandPaletteUiState = { open: true, mode: "command", openIntent: null };
869+
const context = {
870+
query: ">find file",
871+
linkedThreadSearch: null,
872+
};
873+
874+
it.each(["files", "content"] as const)(
875+
"returns from %s without importing the child query",
876+
(mode) => {
877+
const child = reduceCommandPaletteUiState(root, {
878+
_tag: "OpenChildSearch",
879+
mode,
880+
context,
881+
});
882+
const returned = reduceCommandPaletteUiState(child, { _tag: "BackToCommand" });
883+
expect(returned.mode).toBe("command");
884+
expect(returned.restoredSearch).toEqual(context);
885+
expect(returned.parentSearch).toBeUndefined();
886+
expect(createCommandPaletteSearchState(returned.restoredSearch?.query)).toEqual({
887+
query: ">find file",
888+
views: [],
889+
});
890+
},
891+
);
892+
893+
it("retains linked-thread search data without carrying the old palette's views", () => {
894+
const linkedContext = {
895+
query: "https://github.com/acme/web/pull/7",
896+
linkedThreadSearch: {
897+
kind: "search" as const,
898+
query: "https://github.com/acme/web/pull/7",
899+
linkedThreads: {
900+
environmentId: EnvironmentId.make("remote"),
901+
threads: [],
902+
},
903+
},
904+
};
905+
const child = reduceCommandPaletteUiState(root, {
906+
_tag: "OpenChildSearch",
907+
mode: "content",
908+
context: linkedContext,
909+
});
910+
const returned = reduceCommandPaletteUiState(child, { _tag: "BackToCommand" });
911+
expect(returned.restoredSearch).toEqual(linkedContext);
912+
expect(createCommandPaletteSearchState(returned.restoredSearch?.query).views).toEqual([]);
913+
});
914+
915+
it("closing after a selection discards the return context and cannot reopen the parent", () => {
916+
const child = reduceCommandPaletteUiState(root, {
917+
_tag: "OpenChildSearch",
918+
mode: "files",
919+
context,
920+
});
921+
const closed = reduceCommandPaletteUiState(child, { _tag: "SetOpen", open: false });
922+
expect(reduceCommandPaletteUiState(closed, { _tag: "BackToCommand" })).toBe(closed);
923+
expect(reduceCommandPaletteUiState(closed, { _tag: "SetOpen", open: true })).toEqual(root);
924+
});
925+
926+
it("a fresh shortcut or direct intent discards a previous return context", () => {
927+
const child = reduceCommandPaletteUiState(root, {
928+
_tag: "OpenChildSearch",
929+
mode: "files",
930+
context,
931+
});
932+
const fresh = reduceCommandPaletteUiState(child, { _tag: "ToggleMode", mode: "content" });
933+
expect(reduceCommandPaletteUiState(fresh, { _tag: "BackToCommand" })).toEqual(root);
934+
const direct = reduceCommandPaletteUiState(child, { _tag: "OpenChangeTheme" });
935+
expect(direct.parentSearch).toBeUndefined();
936+
expect(direct.restoredSearch).toBeUndefined();
937+
});
938+
});

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

Lines changed: 66 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,
@@ -171,6 +190,50 @@ export interface CommandPaletteView {
171190
readonly initialQuery?: string;
172191
}
173192

193+
// File/content search actions only appear at the root. Retain input and data,
194+
// not view groups whose action callbacks belong to the unmounted palette.
195+
export interface CommandPaletteReturnContext {
196+
readonly query: string;
197+
readonly linkedThreadSearch: Extract<CommandPaletteOpenIntent, { kind: "search" }> | null;
198+
}
199+
200+
export interface CommandPaletteSearchState {
201+
readonly query: string;
202+
readonly views: ReadonlyArray<{
203+
readonly view: CommandPaletteView;
204+
readonly parentQuery: string;
205+
}>;
206+
}
207+
208+
export function createCommandPaletteSearchState(query = ""): CommandPaletteSearchState {
209+
return { query, views: [] };
210+
}
211+
212+
export function reduceCommandPaletteSearchState(
213+
state: CommandPaletteSearchState,
214+
action:
215+
| { readonly type: "query"; readonly query: string }
216+
| { readonly type: "push"; readonly view: CommandPaletteView }
217+
| { readonly type: "back" }
218+
| { readonly type: "reset"; readonly query?: string },
219+
): CommandPaletteSearchState {
220+
switch (action.type) {
221+
case "query":
222+
return { ...state, query: action.query };
223+
case "push":
224+
return {
225+
query: action.view.initialQuery ?? "",
226+
views: [...state.views, { view: action.view, parentQuery: state.query }],
227+
};
228+
case "back": {
229+
const parent = state.views.at(-1);
230+
return parent ? { query: parent.parentQuery, views: state.views.slice(0, -1) } : state;
231+
}
232+
case "reset":
233+
return createCommandPaletteSearchState(action.query);
234+
}
235+
}
236+
174237
export function enumerateCommandPaletteItems(
175238
items: ReadonlyArray<CommandPaletteActionItem>,
176239
): CommandPaletteActionItem[] {

0 commit comments

Comments
 (0)