Skip to content

Commit 1eb9b4d

Browse files
committed
feat(web): open the project picker in a chosen environment
Adding a project always started by asking which environment to add it to, even when the caller already knew. The sidebar is about to ask from an environment section header, where re-asking would be the palette ignoring where the click came from. The open detail and the palette's open intent carry an optional environmentId. When it is set, the flow skips straight to browsing sources in that environment; when it is absent the picker behaves exactly as before. The key is absent rather than undefined so the two cases stay distinguishable under exactOptionalPropertyTypes. Written by Claude Opus 5 in Claude Code.
1 parent d1f2b64 commit 1eb9b4d

4 files changed

Lines changed: 83 additions & 32 deletions

File tree

‎apps/web/src/commandPaletteBus.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ const COMMAND_PALETTE_OPEN_EVENT = "t3code:open-command-palette";
44

55
export interface CommandPaletteOpenDetail {
66
readonly open?: "add-project" | "new-thread-in";
7+
/**
8+
* Environment to add the project to, skipping the environment step of the
9+
* flow. Set when the caller already knows the answer — an environment
10+
* section header in the sidebar asks for a project *in that environment*,
11+
* and re-asking would be the palette ignoring where the click came from.
12+
*/
13+
readonly environmentId?: string;
714
}
815

916
export function openCommandPalette(detail?: CommandPaletteOpenDetail): void {

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,24 @@ describe("reduceCommandPaletteUiState", () => {
9393
});
9494
});
9595

96+
it("carries the environment a project should be added to", () => {
97+
expect(
98+
reduceCommandPaletteUiState(closedState, {
99+
_tag: "OpenAddProject",
100+
environmentId: "remote",
101+
}),
102+
).toEqual({
103+
open: true,
104+
mode: "command",
105+
openIntent: { kind: "add-project", environmentId: "remote" },
106+
});
107+
// No environment means "ask", and the key has to be absent rather than
108+
// undefined for the flow to tell the two apart.
109+
expect(
110+
reduceCommandPaletteUiState(closedState, { _tag: "OpenAddProject" }).openIntent,
111+
).not.toHaveProperty("environmentId");
112+
});
113+
96114
it("preserves the mode on close and resets it on open", () => {
97115
const filesOpen = reduceCommandPaletteUiState(closedState, {
98116
_tag: "ToggleMode",

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ export type SearchOverlayMode = "command" | "files" | "content";
3939

4040
export interface CommandPaletteOpenIntent {
4141
readonly kind: "add-project" | "new-thread-in";
42+
/** Preselected environment for "add-project"; absent means "ask". */
43+
readonly environmentId?: string;
4244
}
4345

4446
export interface CommandPaletteUiState {
@@ -50,7 +52,7 @@ export interface CommandPaletteUiState {
5052
export type CommandPaletteUiAction =
5153
| { readonly _tag: "SetOpen"; readonly open: boolean }
5254
| { readonly _tag: "ToggleMode"; readonly mode: SearchOverlayMode }
53-
| { readonly _tag: "OpenAddProject" }
55+
| { readonly _tag: "OpenAddProject"; readonly environmentId?: string }
5456
| { readonly _tag: "OpenNewThreadIn" }
5557
| { readonly _tag: "ClearOpenIntent" };
5658

@@ -68,7 +70,14 @@ export function reduceCommandPaletteUiState(
6870
? { ...state, open: false, openIntent: null }
6971
: { open: true, mode: action.mode, openIntent: null };
7072
case "OpenAddProject":
71-
return { open: true, mode: "command", openIntent: { kind: "add-project" } };
73+
return {
74+
open: true,
75+
mode: "command",
76+
openIntent:
77+
action.environmentId === undefined
78+
? { kind: "add-project" }
79+
: { kind: "add-project", environmentId: action.environmentId },
80+
};
7281
case "OpenNewThreadIn":
7382
return { open: true, mode: "command", openIntent: { kind: "new-thread-in" } };
7483
case "ClearOpenIntent":

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

Lines changed: 47 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,15 @@ export function CommandPalette({ children }: { children: ReactNode }) {
401401
(mode: SearchOverlayMode) => dispatch({ _tag: "ToggleMode", mode }),
402402
[],
403403
);
404-
const openAddProject = useCallback(() => dispatch({ _tag: "OpenAddProject" }), []);
404+
const openAddProject = useCallback(
405+
(environmentId?: string) =>
406+
dispatch(
407+
environmentId === undefined
408+
? { _tag: "OpenAddProject" }
409+
: { _tag: "OpenAddProject", environmentId },
410+
),
411+
[],
412+
);
405413
const openNewThreadIn = useCallback(() => dispatch({ _tag: "OpenNewThreadIn" }), []);
406414
const clearOpenIntent = useCallback(() => dispatch({ _tag: "ClearOpenIntent" }), []);
407415
const keybindings = useAtomValue(primaryServerKeybindingsAtom);
@@ -476,7 +484,7 @@ export function CommandPalette({ children }: { children: ReactNode }) {
476484
if (detail.open === "new-thread-in") {
477485
openNewThreadIn();
478486
} else if (detail.open === "add-project") {
479-
openAddProject();
487+
openAddProject(detail.environmentId);
480488
} else {
481489
setOpen(true);
482490
}
@@ -1407,42 +1415,51 @@ function OpenCommandPaletteDialog(props: {
14071415
[addProjectEnvironmentItems],
14081416
);
14091417

1410-
const openAddProjectFlow = useCallback(() => {
1411-
if (addProjectEnvironmentOptions.length > 1 || defaultAddProjectEnvironmentId === null) {
1412-
pushPaletteView({
1413-
addonIcon: <FolderPlusIcon className={ADDON_ICON_CLASS} />,
1414-
groups: addProjectEnvironmentGroups,
1415-
});
1416-
return;
1417-
}
1418+
const openAddProjectFlow = useCallback(
1419+
(preselectedEnvironmentId?: EnvironmentId) => {
1420+
// A caller that already knows the environment skips the picker: asking
1421+
// again would ignore where the request came from.
1422+
if (preselectedEnvironmentId !== undefined) {
1423+
void startAddProjectSourceSelection(preselectedEnvironmentId);
1424+
return;
1425+
}
1426+
if (addProjectEnvironmentOptions.length > 1 || defaultAddProjectEnvironmentId === null) {
1427+
pushPaletteView({
1428+
addonIcon: <FolderPlusIcon className={ADDON_ICON_CLASS} />,
1429+
groups: addProjectEnvironmentGroups,
1430+
});
1431+
return;
1432+
}
14181433

1419-
const environmentId = defaultAddProjectEnvironmentId;
1420-
if (!environmentId) {
1421-
toastManager.add(
1422-
stackedThreadToast({
1423-
type: "error",
1424-
title: "Unable to browse projects",
1425-
description: "No environment is available.",
1426-
}),
1427-
);
1428-
return;
1429-
}
1434+
const environmentId = defaultAddProjectEnvironmentId;
1435+
if (!environmentId) {
1436+
toastManager.add(
1437+
stackedThreadToast({
1438+
type: "error",
1439+
title: "Unable to browse projects",
1440+
description: "No environment is available.",
1441+
}),
1442+
);
1443+
return;
1444+
}
14301445

1431-
void startAddProjectSourceSelection(environmentId);
1432-
}, [
1433-
addProjectEnvironmentGroups,
1434-
addProjectEnvironmentOptions.length,
1435-
defaultAddProjectEnvironmentId,
1436-
pushPaletteView,
1437-
startAddProjectSourceSelection,
1438-
]);
1446+
void startAddProjectSourceSelection(environmentId);
1447+
},
1448+
[
1449+
addProjectEnvironmentGroups,
1450+
addProjectEnvironmentOptions.length,
1451+
defaultAddProjectEnvironmentId,
1452+
pushPaletteView,
1453+
startAddProjectSourceSelection,
1454+
],
1455+
);
14391456

14401457
useLayoutEffect(() => {
14411458
if (openIntent?.kind !== "add-project") {
14421459
return;
14431460
}
14441461
clearOpenIntent();
1445-
openAddProjectFlow();
1462+
openAddProjectFlow(openIntent.environmentId as EnvironmentId | undefined);
14461463
}, [clearOpenIntent, openAddProjectFlow, openIntent]);
14471464

14481465
useLayoutEffect(() => {

0 commit comments

Comments
 (0)