Skip to content

Commit f06bfa6

Browse files
authored
fix(web): drag command palette threads into the composer (#33990)
1 parent 81c3da3 commit f06bfa6

7 files changed

Lines changed: 106 additions & 5 deletions

File tree

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

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { EnvironmentId, ProjectId, ThreadId } from "@t3tools/contracts";
22
import { describe, expect, it, vi } from "vite-plus/test";
33
import { page, userEvent } from "vite-plus/test/browser";
44

5-
import { render } from "../../test/render";
5+
import { act, render } from "../../test/render";
66
import { openCommandPalette } from "../commandPaletteBus";
77
import { useCommandPaletteStore } from "../commandPaletteStore";
88
import { makeThreadFixture } from "../test-fixtures";
@@ -26,6 +26,56 @@ function setup(state: { projects?: ReadonlyArray<Project>; threads?: ReadonlyArr
2626
}
2727

2828
describe("CommandPalette open intents", () => {
29+
it("drags a searched thread into the current composer without navigating", async () => {
30+
const environmentId = EnvironmentId.make("remote");
31+
const threadId = ThreadId.make("dragged-thread");
32+
setup({
33+
threads: [makeThreadFixture({ environmentId, id: threadId, title: "Drag this thread" })],
34+
});
35+
const onDrop = vi.fn();
36+
await using screen = await render(
37+
<CommandPalette>
38+
<div
39+
data-thread-context-drop="true"
40+
ref={(node) => {
41+
node?.addEventListener("t3-thread-context-drop", onDrop);
42+
}}
43+
style={{ position: "fixed", bottom: 0, width: 300, height: 100 }}
44+
>
45+
Composer
46+
</div>
47+
</CommandPalette>,
48+
);
49+
const pathname = screen.router?.state.location.pathname;
50+
openCommandPalette({ query: "Drag this thread" });
51+
const row = page.getByRole("option", { name: /Drag this thread/ });
52+
await expect.element(row).toBeVisible();
53+
const bounds = row.element().getBoundingClientRect();
54+
const pointer = (type: string, x: number, y: number) =>
55+
new PointerEvent(type, {
56+
bubbles: true,
57+
cancelable: true,
58+
pointerId: 1,
59+
isPrimary: true,
60+
button: 0,
61+
buttons: type === "pointerup" ? 0 : 1,
62+
clientX: x,
63+
clientY: y,
64+
});
65+
await act(() => {
66+
row.element().dispatchEvent(pointer("pointerdown", bounds.x + 20, bounds.y + 10));
67+
document.dispatchEvent(pointer("pointermove", bounds.x + 30, bounds.y + 20));
68+
});
69+
expect(useCommandPaletteStore.getState().open).toBe(false);
70+
await act(() => {
71+
document.dispatchEvent(pointer("pointermove", 50, window.innerHeight - 30));
72+
document.dispatchEvent(pointer("pointerup", 50, window.innerHeight - 30));
73+
});
74+
expect(onDrop).toHaveBeenCalledOnce();
75+
expect((onDrop.mock.calls[0]![0] as CustomEvent).detail).toEqual([{ environmentId, threadId }]);
76+
expect(screen.router?.state.location.pathname).toBe(pathname);
77+
});
78+
2979
it("opens New thread in... with focus in the search input, not the Back button", async () => {
3080
setup();
3181
await using _screen = await render(<CommandPalette>{null}</CommandPalette>);
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import type { ScopedThreadRef } from "@t3tools/contracts";
2+
import { flushSync } from "react-dom";
3+
import { dispatchCommandPalette } from "../commandPaletteStore";
4+
import { SidebarPointerSensor } from "./Sidebar.pointer";
5+
import {
6+
dropThreadContext,
7+
endThreadContextDrag,
8+
moveThreadContextDrag,
9+
} from "./chat/threadContextDrag";
10+
11+
export function startPaletteThreadDrag(
12+
event: PointerEvent,
13+
thread: ScopedThreadRef,
14+
title: string,
15+
) {
16+
if (!event.isPrimary || event.button !== 0 || event.pointerType === "touch") return;
17+
// The document-owned sensor must survive closing and unmounting the palette on activation.
18+
return new SidebarPointerSensor({
19+
event,
20+
active: `${thread.environmentId}:${thread.threadId}`,
21+
options: {
22+
distance: 6,
23+
onAttach: () => {},
24+
onFinish: endThreadContextDrag,
25+
onDrop: (point) => dropThreadContext(point, [thread]),
26+
},
27+
onStart: () => {
28+
flushSync(() => dispatchCommandPalette({ _tag: "SetOpen", open: false }));
29+
},
30+
onMove: (point) => moveThreadContextDrag(point, { title, count: 1 }),
31+
onPending: () => {},
32+
onAbort: () => {},
33+
onCancel: () => {},
34+
onEnd: () => {},
35+
});
36+
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
type AddProjectRemoteSourceReadiness,
2020
} from "@t3tools/client-runtime/operations/projects";
2121
import {
22+
type ScopedThreadRef,
2223
type EnvironmentId,
2324
type ProjectId,
2425
type EnvironmentMachineKind,
@@ -48,6 +49,7 @@ export function buildLinkedThreadActionItems(
4849
return input.threads.map((thread) => ({
4950
kind: "action",
5051
value: `thread:${input.environmentId}:${thread.id}`,
52+
threadRef: { environmentId: input.environmentId, threadId: thread.id },
5153
title: thread.title || "Untitled thread",
5254
description: thread.archivedAt === null ? "Linked thread" : "Archived thread",
5355
searchTerms: [input.query, thread.title],
@@ -194,6 +196,7 @@ export interface CommandPaletteItem {
194196
readonly timestamp?: string;
195197
/** Thread rows rank by this, with use history, instead of by `searchTerms`. */
196198
readonly threadSearch?: ThreadSearchSubject;
199+
readonly threadRef?: ScopedThreadRef;
197200
/** Shown in place of the description when the match is not in the title. */
198201
readonly matchHint?: SearchMatchHint;
199202
readonly icon: ReactNode;
@@ -466,6 +469,7 @@ export function buildThreadActionItems<TThread extends BuildThreadActionItemsThr
466469
{
467470
kind: "action" as const,
468471
value: `thread:${thread.environmentId}:${thread.id}`,
472+
threadRef: { environmentId: thread.environmentId, threadId: thread.id },
469473
searchTerms: [thread.title],
470474
threadSearch: threadSearchSubject(
471475
{

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { ThreadContextDragGhost } from "./chat/ThreadContextDragGhost";
12
import { threadPullRequestLinkMode } from "@t3tools/client-runtime/thread-pull-request-compatibility";
23
import { visibleThreadPullRequests } from "@t3tools/shared/threadPullRequests";
34

@@ -308,6 +309,7 @@ export function CommandPalette({ children }: { children: ReactNode }) {
308309

309310
return (
310311
<ComposerHandleContext value={composerHandleRef}>
312+
<ThreadContextDragGhost />
311313
<CommandDialog
312314
open={state.open}
313315
onOpenChange={(open, eventDetails) => {

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { startPaletteThreadDrag } from "./CommandPalette.drag";
12
import { useLegendListRef } from "~/hooks/useLegendListRef";
23
import { type ResolvedKeybindingsConfig } from "@t3tools/contracts";
34
import { LegendList, type LegendListRef } from "@legendapp/list/react";
@@ -241,6 +242,16 @@ function CommandPaletteResultRow(props: {
241242
{...(props.index === undefined ? {} : { index: props.index })}
242243
value={props.item.value}
243244
active={props.isActive}
245+
onPointerDown={(event) => {
246+
if (props.item.threadRef) {
247+
startPaletteThreadDrag(
248+
event.nativeEvent,
249+
props.item.threadRef,
250+
props.item.searchTitle ??
251+
(typeof props.item.title === "string" ? props.item.title : "Thread"),
252+
);
253+
}
254+
}}
244255
onMouseDown={(event) => {
245256
event.preventDefault();
246257
}}

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { attachSidebarTree } from "../lib/sidebarTree";
22
import { type EnvironmentId, type ScopedProjectRef } from "@t3tools/contracts";
33
import type { EnvironmentServerConfig } from "@t3tools/client-runtime/connection";
4-
import { ThreadContextDragGhost } from "./chat/ThreadContextDragGhost";
54
import {
65
dropThreadContext,
76
endThreadContextDrag,
@@ -2992,7 +2991,6 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve
29922991
const newThreadInProjectShortcutLabel = shortcutLabelForCommand(keybindings, "chat.newLocal");
29932992
return (
29942993
<>
2995-
<ThreadContextDragGhost />
29962994
<SidebarChromeHeader isElectron={isElectron} />
29972995
<SidebarContent
29982996
ref={sidebarContentRef}

‎apps/web/src/components/ui/command.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ function CommandDialogTrigger(props: CommandDialogPrimitive.Trigger.Props) {
2929
function CommandDialogBackdrop({ className, ...props }: CommandDialogPrimitive.Backdrop.Props) {
3030
return (
3131
<CommandDialogPrimitive.Backdrop
32-
className={cn(DIALOG_BACKDROP_CLASS, className)}
32+
className={cn(DIALOG_BACKDROP_CLASS, "data-ending-style:pointer-events-none", className)}
3333
data-slot="command-dialog-backdrop"
3434
{...props}
3535
/>
@@ -74,7 +74,7 @@ function CommandDialogPopup({
7474
ref={popupRef}
7575
className={cn(
7676
DIALOG_POPUP_CLASS,
77-
"pointer-events-auto max-h-105 max-w-xl text-foreground",
77+
"pointer-events-auto max-h-105 max-w-xl text-foreground data-ending-style:pointer-events-none",
7878
className,
7979
)}
8080
data-slot="command-dialog-popup"

0 commit comments

Comments
 (0)