Skip to content

Commit f8592d6

Browse files
fix(web): simplify palette search restoration
1 parent dcfe21c commit f8592d6

2 files changed

Lines changed: 28 additions & 51 deletions

File tree

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,15 +181,12 @@ export interface CommandPaletteView {
181181
readonly initialQuery?: string;
182182
}
183183

184-
// File and content search replace the palette, so Escape remounts it. Keep
185-
// its input and linked-thread data to restore, not views whose actions
186-
// belonged to the unmounted palette.
184+
// Child search unmounts the palette; retain search data without its view closures.
187185
export interface CommandPaletteReturnContext {
188186
readonly query: string;
189187
readonly linkedThreadSearch: Extract<CommandPaletteOpenIntent, { kind: "search" }> | null;
190188
}
191189

192-
// Each nested view remembers the query that led into it, so Back restores it.
193190
export interface CommandPaletteSearchState {
194191
readonly query: string;
195192
readonly views: ReadonlyArray<{

‎apps/web/src/components/CommandPalette.tsx‎

Lines changed: 27 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,6 @@ export function CommandPalette({ children }: { children: ReactNode }) {
495495
const openAddProject = () => dispatch({ _tag: "OpenAddProject" });
496496
const openNewThreadIn = () => dispatch({ _tag: "OpenNewThreadIn" });
497497
const clearOpenIntent = () => dispatch({ _tag: "ClearOpenIntent" });
498-
const backToCommand = () => dispatch({ _tag: "BackToCommand" });
499498
const openChildSearch = (mode: "files" | "content", context: CommandPaletteReturnContext) =>
500499
dispatch({ _tag: "OpenChildSearch", mode, context });
501500
const keybindings = useAtomValue(primaryServerKeybindingsAtom);
@@ -520,11 +519,11 @@ export function CommandPalette({ children }: { children: ReactNode }) {
520519
if (event.isComposing || event.key !== "Escape") return;
521520
event.preventDefault();
522521
event.stopPropagation();
523-
backToCommand();
522+
dispatch({ _tag: "BackToCommand" });
524523
};
525524
window.addEventListener("keydown", onEscapeKeyDown, true);
526525
return () => window.removeEventListener("keydown", onEscapeKeyDown, true);
527-
}, [backToCommand, state.mode, state.open]);
526+
}, [state.mode, state.open]);
528527

529528
useEffect(() => {
530529
const onKeyDown = (event: globalThis.KeyboardEvent) => {
@@ -630,7 +629,7 @@ export function CommandPalette({ children }: { children: ReactNode }) {
630629
onOpenChange={(open, eventDetails) => {
631630
if (!open && eventDetails.reason === "escape-key" && state.mode !== "command") {
632631
eventDetails.cancel();
633-
backToCommand();
632+
dispatch({ _tag: "BackToCommand" });
634633
return;
635634
}
636635
setOpen(open);
@@ -643,7 +642,7 @@ export function CommandPalette({ children }: { children: ReactNode }) {
643642
mode={state.mode}
644643
openIntent={state.openIntent}
645644
setOpen={setOpen}
646-
openOverlayMode={openChildSearch}
645+
openChildSearch={openChildSearch}
647646
initialContext={state.restoredSearch}
648647
clearOpenIntent={clearOpenIntent}
649648
/>
@@ -656,7 +655,7 @@ function CommandPaletteDialog(props: {
656655
readonly mode: SearchOverlayMode;
657656
readonly openIntent: CommandPaletteOpenIntent | null;
658657
readonly setOpen: (open: boolean) => void;
659-
readonly openOverlayMode: (
658+
readonly openChildSearch: (
660659
mode: "files" | "content",
661660
context: CommandPaletteReturnContext,
662661
) => void;
@@ -696,7 +695,7 @@ function CommandPaletteDialog(props: {
696695
<OpenCommandPaletteDialog
697696
openIntent={props.openIntent}
698697
setOpen={props.setOpen}
699-
openOverlayMode={props.openOverlayMode}
698+
openChildSearch={props.openChildSearch}
700699
initialContext={props.initialContext}
701700
clearOpenIntent={props.clearOpenIntent}
702701
/>
@@ -708,7 +707,7 @@ function CommandPaletteDialog(props: {
708707
function OpenCommandPaletteDialog(props: {
709708
readonly openIntent: CommandPaletteOpenIntent | null;
710709
readonly setOpen: (open: boolean) => void;
711-
readonly openOverlayMode: (
710+
readonly openChildSearch: (
712711
mode: "files" | "content",
713712
context: CommandPaletteReturnContext,
714713
) => void;
@@ -717,18 +716,19 @@ function OpenCommandPaletteDialog(props: {
717716
}) {
718717
const navigate = useNavigate();
719718
const pathname = useLocation({ select: (location) => location.pathname });
720-
const { clearOpenIntent, openIntent, openOverlayMode, setOpen } = props;
719+
const { clearOpenIntent, openIntent, openChildSearch, setOpen } = props;
720+
const initialSearch = props.initialContext ?? {
721+
query: openIntent?.kind === "search" ? openIntent.query : "",
722+
linkedThreadSearch: openIntent?.kind === "search" ? openIntent : null,
723+
};
721724
const [searchState, dispatchSearch] = useReducer(
722725
reduceCommandPaletteSearchState,
723-
createCommandPaletteSearchState(
724-
props.initialContext?.query ?? (openIntent?.kind === "search" ? openIntent.query : ""),
725-
),
726+
initialSearch.query,
727+
createCommandPaletteSearchState,
726728
);
727729
const { query, views: viewStack } = searchState;
728730
const setQuery = (query: string) => dispatchSearch({ type: "query", query });
729-
const [linkedThreadSearch, setLinkedThreadSearch] = useState(
730-
props.initialContext?.linkedThreadSearch ?? (openIntent?.kind === "search" ? openIntent : null),
731-
);
731+
const [linkedThreadSearch, setLinkedThreadSearch] = useState(initialSearch.linkedThreadSearch);
732732
const deferredQuery = useDeferredValue(query);
733733
const isActionsOnly = deferredQuery.startsWith(">");
734734
const [highlightedItemValue, setHighlightedItemValue] = useState<string | null>(null);
@@ -1307,14 +1307,6 @@ function OpenCommandPaletteDialog(props: {
13071307
setHighlightedItemValue(null);
13081308
});
13091309

1310-
function pushView(item: CommandPaletteSubmenuItem): void {
1311-
pushPaletteView({
1312-
addonIcon: item.addonIcon,
1313-
groups: item.groups,
1314-
...(item.initialQuery ? { initialQuery: item.initialQuery } : {}),
1315-
});
1316-
}
1317-
13181310
function popView(): void {
13191311
browseNavigation.invalidate();
13201312
setAddProjectCloneFlow(null);
@@ -1796,7 +1788,7 @@ function OpenCommandPaletteDialog(props: {
17961788
keepOpen: true,
17971789
shortcutCommand: "filePicker.toggle",
17981790
run: async () => {
1799-
openOverlayMode("files", { query, linkedThreadSearch });
1791+
openChildSearch("files", { query, linkedThreadSearch });
18001792
},
18011793
});
18021794

@@ -1809,7 +1801,7 @@ function OpenCommandPaletteDialog(props: {
18091801
keepOpen: true,
18101802
shortcutCommand: "projectSearch.toggle",
18111803
run: async () => {
1812-
openOverlayMode("content", { query, linkedThreadSearch });
1804+
openChildSearch("content", { query, linkedThreadSearch });
18131805
},
18141806
});
18151807

@@ -1956,40 +1948,28 @@ function OpenCommandPaletteDialog(props: {
19561948
// parent's intent and may navigate, so it runs after commit but before paint.
19571949
const projectThreadItemCount = projectThreadItems.length;
19581950
const applyOpenIntent = useEffectEvent((intent: CommandPaletteOpenIntent) => {
1951+
clearOpenIntent();
1952+
browseNavigation.invalidate();
1953+
cloneLookupGeneration.current += 1;
1954+
setIsRemoteProjectLookingUp(false);
1955+
setAddProjectEnvironmentId(null);
1956+
setAddProjectCloneFlow(null);
1957+
setNewProjectFlow(null);
1958+
setLinkedThreadSearch(intent.kind === "search" ? intent : null);
1959+
dispatchSearch({ type: "reset", query: intent.kind === "search" ? intent.query : "" });
19591960
switch (intent.kind) {
19601961
case "search":
1961-
browseNavigation.invalidate();
1962-
cloneLookupGeneration.current += 1;
1963-
setIsRemoteProjectLookingUp(false);
1964-
setAddProjectCloneFlow(null);
1965-
setNewProjectFlow(null);
1966-
dispatchSearch({ type: "reset", query: intent.query });
1967-
setLinkedThreadSearch(intent);
1968-
clearOpenIntent();
19691962
return;
19701963
case "add-project":
1971-
clearOpenIntent();
19721964
openAddProjectFlow();
19731965
return;
19741966
case "new-thread-in":
1975-
clearOpenIntent();
1976-
browseNavigation.invalidate();
1977-
setAddProjectCloneFlow(null);
1978-
setNewProjectFlow(null);
1979-
dispatchSearch({ type: "reset" });
19801967
pushPaletteView({
19811968
addonIcon: <SquarePenIcon className={ADDON_ICON_CLASS} />,
19821969
groups: [{ value: "projects", label: "Projects", items: projectThreadItems }],
19831970
});
19841971
return;
19851972
case "change-theme":
1986-
clearOpenIntent();
1987-
browseNavigation.invalidate();
1988-
cloneLookupGeneration.current += 1;
1989-
setIsRemoteProjectLookingUp(false);
1990-
setAddProjectCloneFlow(null);
1991-
setNewProjectFlow(null);
1992-
dispatchSearch({ type: "reset" });
19931973
pushPaletteView({
19941974
addonIcon: <PaletteIcon className={ADDON_ICON_CLASS} />,
19951975
groups: [{ value: "themes", label: "Change theme", items: [] }],
@@ -2995,7 +2975,7 @@ function OpenCommandPaletteDialog(props: {
29952975
}
29962976

29972977
if (item.kind === "submenu") {
2998-
pushView(item);
2978+
pushPaletteView(item);
29992979
return;
30002980
}
30012981

0 commit comments

Comments
 (0)