Skip to content

Commit 0c7f965

Browse files
Add composer focus handoff for command palette
- Let the command palette return focus to the chat composer - Use project-specific icons in palette project actions - Share composer handle through context for imperative access
1 parent d0b529d commit 0c7f965

5 files changed

Lines changed: 73 additions & 51 deletions

File tree

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ import {
161161
waitForStartedServerThread,
162162
} from "./ChatView.logic";
163163
import { useLocalStorage } from "~/hooks/useLocalStorage";
164+
import { useComposerHandleContext } from "../composerHandleContext";
164165
import {
165166
useServerAvailableEditors,
166167
useServerConfig,
@@ -651,7 +652,8 @@ export default function ChatView(props: ChatViewProps) {
651652
const promptRef = useRef("");
652653
const composerImagesRef = useRef<ComposerImageAttachment[]>([]);
653654
const composerTerminalContextsRef = useRef<TerminalContextDraft[]>([]);
654-
const composerRef = useRef<ChatComposerHandle>(null);
655+
const localComposerRef = useRef<ChatComposerHandle | null>(null);
656+
const composerRef = useComposerHandleContext() ?? localComposerRef;
655657
const [showScrollToBottom, setShowScrollToBottom] = useState(false);
656658
const [expandedImage, setExpandedImage] = useState<ExpandedImagePreview | null>(null);
657659
const [optimisticUserMessages, setOptimisticUserMessages] = useState<ChatMessage[]>([]);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export function normalizeSearchText(value: string): string {
5454
export function buildProjectActionItems(input: {
5555
projects: ReadonlyArray<Project>;
5656
valuePrefix: string;
57-
icon: ReactNode;
57+
icon: (project: Project) => ReactNode;
5858
runProject: (project: Project) => Promise<void>;
5959
}): CommandPaletteActionItem[] {
6060
return input.projects.map((project) => ({
@@ -63,7 +63,7 @@ export function buildProjectActionItems(input: {
6363
searchTerms: [project.name, project.cwd],
6464
title: project.name,
6565
description: project.cwd,
66-
icon: input.icon,
66+
icon: input.icon(project),
6767
run: async () => {
6868
await input.runProject(project);
6969
},

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

Lines changed: 40 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {
77
ArrowDownIcon,
88
ArrowLeftIcon,
99
ArrowUpIcon,
10-
FolderIcon,
1110
MessageSquareIcon,
1211
SettingsIcon,
1312
SquarePenIcon,
@@ -16,6 +15,7 @@ import {
1615
useDeferredValue,
1716
useEffect,
1817
useMemo,
18+
useRef,
1919
useState,
2020
type KeyboardEvent,
2121
type ReactNode,
@@ -54,6 +54,7 @@ import {
5454
RECENT_THREAD_LIMIT,
5555
} from "./CommandPalette.logic";
5656
import { CommandPaletteResults } from "./CommandPaletteResults";
57+
import { ProjectFavicon } from "./ProjectFavicon";
5758
import { useServerKeybindings } from "../rpc/serverState";
5859
import { resolveShortcutCommand } from "../keybindings";
5960
import {
@@ -66,12 +67,15 @@ import {
6667
} from "./ui/command";
6768
import { Kbd, KbdGroup } from "./ui/kbd";
6869
import { toastManager } from "./ui/toast";
70+
import { ComposerHandleContext, useComposerHandleContext } from "../composerHandleContext";
71+
import type { ChatComposerHandle } from "./chat/ChatComposer";
6972

7073
export function CommandPalette({ children }: { children: ReactNode }) {
7174
const open = useCommandPaletteStore((store) => store.open);
7275
const setOpen = useCommandPaletteStore((store) => store.setOpen);
7376
const toggleOpen = useCommandPaletteStore((store) => store.toggleOpen);
7477
const keybindings = useServerKeybindings();
78+
const composerHandleRef = useRef<ChatComposerHandle | null>(null);
7579
const routeTarget = useParams({
7680
strict: false,
7781
select: (params) => resolveThreadRouteTarget(params),
@@ -104,10 +108,15 @@ export function CommandPalette({ children }: { children: ReactNode }) {
104108
}, [keybindings, terminalOpen, toggleOpen]);
105109

106110
return (
107-
<CommandDialog open={open} onOpenChange={setOpen}>
108-
{children}
109-
<CommandPaletteDialog />
110-
</CommandDialog>
111+
<ComposerHandleContext.Provider value={composerHandleRef}>
112+
<CommandDialog
113+
open={open}
114+
onOpenChange={setOpen}
115+
>
116+
{children}
117+
<CommandPaletteDialog />
118+
</CommandDialog>
119+
</ComposerHandleContext.Provider>
111120
);
112121
}
113122

@@ -131,6 +140,7 @@ function CommandPaletteDialog() {
131140
function OpenCommandPaletteDialog() {
132141
const navigate = useNavigate();
133142
const setOpen = useCommandPaletteStore((store) => store.setOpen);
143+
const composerHandleRef = useComposerHandleContext();
134144
const [query, setQuery] = useState("");
135145
const deferredQuery = useDeferredValue(query);
136146
const isActionsOnly = deferredQuery.startsWith(">");
@@ -186,7 +196,13 @@ function OpenCommandPaletteDialog() {
186196
buildProjectActionItems({
187197
projects,
188198
valuePrefix: "project",
189-
icon: <FolderIcon className={ITEM_ICON_CLASS} />,
199+
icon: (project) => (
200+
<ProjectFavicon
201+
environmentId={project.environmentId}
202+
cwd={project.cwd}
203+
className={ITEM_ICON_CLASS}
204+
/>
205+
),
190206
runProject: openProjectFromSearch,
191207
}),
192208
[openProjectFromSearch, projects],
@@ -197,7 +213,13 @@ function OpenCommandPaletteDialog() {
197213
buildProjectActionItems({
198214
projects,
199215
valuePrefix: "new-thread-in",
200-
icon: <FolderIcon className={ITEM_ICON_CLASS} />,
216+
icon: (project) => (
217+
<ProjectFavicon
218+
environmentId={project.environmentId}
219+
cwd={project.cwd}
220+
className={ITEM_ICON_CLASS}
221+
/>
222+
),
201223
runProject: async (project) => {
202224
await startNewThreadInProjectFromContext(
203225
{
@@ -219,7 +241,13 @@ function OpenCommandPaletteDialog() {
219241
buildProjectActionItems({
220242
projects,
221243
valuePrefix: "new-fresh-thread-in",
222-
icon: <FolderIcon className={ITEM_ICON_CLASS} />,
244+
icon: (project) => (
245+
<ProjectFavicon
246+
environmentId={project.environmentId}
247+
cwd={project.cwd}
248+
className={ITEM_ICON_CLASS}
249+
/>
250+
),
223251
runProject: async (project) => {
224252
await handleNewThread(scopeProjectRef(project.environmentId, project.id), {
225253
envMode: settings.defaultThreadEnvMode,
@@ -301,29 +329,6 @@ function OpenCommandPaletteDialog() {
301329
});
302330
},
303331
});
304-
305-
actionItems.push({
306-
kind: "action",
307-
value: "action:new-fresh-thread",
308-
searchTerms: ["new thread", "chat", "create", settings.defaultThreadEnvMode],
309-
title: (
310-
<>
311-
New {settings.defaultThreadEnvMode} thread in{" "}
312-
<span className="font-semibold">{activeProjectTitle}</span>
313-
</>
314-
),
315-
icon: <SquarePenIcon className={ITEM_ICON_CLASS} />,
316-
shortcutCommand: "chat.newLocal",
317-
run: async () => {
318-
await startNewLocalThreadFromContext({
319-
activeDraftThread,
320-
activeThread,
321-
defaultThreadEnvMode: settings.defaultThreadEnvMode,
322-
handleNewThread,
323-
projects,
324-
});
325-
},
326-
});
327332
}
328333

329334
actionItems.push({
@@ -335,23 +340,6 @@ function OpenCommandPaletteDialog() {
335340
addonIcon: <SquarePenIcon className={ADDON_ICON_CLASS} />,
336341
groups: [{ value: "projects", label: "Projects", items: projectThreadItems }],
337342
});
338-
339-
actionItems.push({
340-
kind: "submenu",
341-
value: "action:new-fresh-thread-in",
342-
searchTerms: [
343-
"new thread",
344-
"project",
345-
"pick",
346-
"choose",
347-
"select",
348-
settings.defaultThreadEnvMode,
349-
],
350-
title: `New ${settings.defaultThreadEnvMode} thread in...`,
351-
icon: <SquarePenIcon className={ITEM_ICON_CLASS} />,
352-
addonIcon: <SquarePenIcon className={ADDON_ICON_CLASS} />,
353-
groups: [{ value: "projects", label: "Projects", items: projectFreshThreadItems }],
354-
});
355343
}
356344

357345
actionItems.push({
@@ -410,6 +398,10 @@ function OpenCommandPaletteDialog() {
410398
aria-label="Command palette"
411399
className="overflow-hidden p-0"
412400
data-testid="command-palette"
401+
finalFocus={() => {
402+
composerHandleRef?.current?.focusAtEnd();
403+
return false;
404+
}}
413405
>
414406
<Command
415407
key={viewStack.length}

‎apps/web/src/composerFocus.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Module-level registry for the active chat composer's focus function.
3+
* ChatView registers itself here via useLayoutEffect so that other parts
4+
* of the app (e.g. command palette) can imperatively focus the input.
5+
*/
6+
7+
let focusFn: (() => void) | null = null;
8+
9+
export function registerComposerFocus(fn: () => void): () => void {
10+
focusFn = fn;
11+
return () => {
12+
if (focusFn === fn) focusFn = null;
13+
};
14+
}
15+
16+
export function focusComposerInput(): void {
17+
focusFn?.();
18+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { createContext, useContext, type MutableRefObject } from "react";
2+
import type { ChatComposerHandle } from "./components/chat/ChatComposer";
3+
4+
export type ComposerHandleRef = MutableRefObject<ChatComposerHandle | null>;
5+
6+
export const ComposerHandleContext = createContext<ComposerHandleRef | null>(null);
7+
8+
export function useComposerHandleContext(): ComposerHandleRef | null {
9+
return useContext(ComposerHandleContext);
10+
}

0 commit comments

Comments
 (0)