Skip to content

Commit b67ac65

Browse files
feat(web): drag a linked pull request into the composer as a context chip (#20102)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent d513997 commit b67ac65

6 files changed

Lines changed: 117 additions & 8 deletions

File tree

‎apps/web/src/components/chat/ChatComposer.tsx‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6095,11 +6095,22 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
60956095
return setContextInsertionHandler(composerDraftTarget, insertContextReferencesAtCaretStable);
60966096
}, [composerDraftTarget, insertContextReferencesAtCaretStable, setContextInsertionHandler]);
60976097

6098-
// File-tree drags land as mentions. Handled in the capture phase so the
6098+
// File-tree drags land as mentions, pull request rows as their context chip. Handled in the capture phase so the
60996099
// editor never sees the drop; the load-bearing rules (native stop, "move"
61006100
// effect, no eager focus) live in makeComposerMentionDragHandlers.
61016101
const composerMentionDragHandlers = makeComposerMentionDragHandlers({
61026102
insertMentionAtEnd: (text) => insertComposerTextAtEnd(text, { ensureLeadingBoundary: true }),
6103+
insertPullRequestAtEnd: (pullRequest) => {
6104+
const comment = buildPullRequestReferenceContext(pullRequest);
6105+
const inserted = insertComposerTextAtEnd(
6106+
`${formatInlineContextReference(reviewCommentContextReference(comment))} `,
6107+
{ ensureLeadingBoundary: true },
6108+
);
6109+
if (inserted) {
6110+
addComposerDraftReviewComment(composerDraftTarget, comment, { appendReference: false });
6111+
}
6112+
return inserted;
6113+
},
61036114
setDragActive: setIsDragOverComposer,
61046115
onInsertRejected: () => {
61056116
toastManager.add({

‎apps/web/src/components/chat/composerMentionDrag.test.ts‎

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,26 @@ import { describe, expect, it } from "@effect/vitest";
22

33
import {
44
COMPOSER_MENTION_DRAG_TYPE,
5+
COMPOSER_PULL_REQUEST_DRAG_TYPE,
6+
encodePullRequestDragPayload,
57
type ComposerMentionDropHost,
68
composerMentionFromTreePath,
79
dataTransferHasComposerMention,
810
makeComposerMentionDragHandlers,
911
} from "./composerMentionDrag.ts";
1012

11-
const makeDragEvent = (options?: { mention?: string; types?: ReadonlyArray<string> }) => {
13+
const makeDragEvent = (options?: {
14+
mention?: string;
15+
types?: ReadonlyArray<string>;
16+
format?: string;
17+
}) => {
1218
const mention = options?.mention ?? "[index.md](docs/index.md)";
19+
const format = options?.format ?? COMPOSER_MENTION_DRAG_TYPE;
1320
const calls: Array<string> = [];
1421
const event = {
1522
dataTransfer: {
16-
types: options?.types ?? [COMPOSER_MENTION_DRAG_TYPE, "text/plain"],
17-
getData: (format: string) => (format === COMPOSER_MENTION_DRAG_TYPE ? mention : ""),
23+
types: options?.types ?? [format, "text/plain"],
24+
getData: (requested: string) => (requested === format ? mention : ""),
1825
dropEffect: "none",
1926
},
2027
nativeEvent: {
@@ -33,6 +40,10 @@ const makeHost = (insertResult = true) => {
3340
log.push(`insert:${text}`);
3441
return insertResult;
3542
},
43+
insertPullRequestAtEnd: (pullRequest) => {
44+
log.push(`pr:${pullRequest.number}`);
45+
return insertResult;
46+
},
3647
setDragActive: (active) => void log.push(`active:${active}`),
3748
onInsertRejected: () => void log.push("rejected"),
3849
};
@@ -120,4 +131,34 @@ describe("makeComposerMentionDragHandlers", () => {
120131
handlers.onDrop(makeDragEvent({ mention: "" }).event);
121132
expect(log).toEqual(["active:false"]);
122133
});
134+
135+
it("drops a pull request row as a pull request, not as text", () => {
136+
const { host, log } = makeHost();
137+
const handlers = makeComposerMentionDragHandlers(host);
138+
const pullRequest = {
139+
number: 18094,
140+
title: "fix(server): queued runs land on the timeline where they started",
141+
url: "https://github.com/pingdotgg/t3code/pull/18094",
142+
headBranch: "fix/run-timeline-position",
143+
baseBranch: "main",
144+
state: "open",
145+
isDraft: false,
146+
} as const;
147+
handlers.onDrop(
148+
makeDragEvent({
149+
format: COMPOSER_PULL_REQUEST_DRAG_TYPE,
150+
mention: encodePullRequestDragPayload(pullRequest),
151+
}).event,
152+
);
153+
expect(log).toEqual(["active:false", "pr:18094"]);
154+
});
155+
156+
it("ignores a pull request payload that does not decode", () => {
157+
const { host, log } = makeHost();
158+
const handlers = makeComposerMentionDragHandlers(host);
159+
handlers.onDrop(
160+
makeDragEvent({ format: COMPOSER_PULL_REQUEST_DRAG_TYPE, mention: '{"number":0}' }).event,
161+
);
162+
expect(log).toEqual(["active:false"]);
163+
});
123164
});

‎apps/web/src/components/chat/composerMentionDrag.ts‎

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import { PullRequestContextMetadata } from "@t3tools/contracts";
12
import { serializeComposerFileLink } from "@t3tools/shared/composerTrigger";
3+
import { Option, Schema } from "effect";
24

35
/**
46
* Drag payload type carrying a serialized composer mention. Set on drags that
@@ -7,6 +9,18 @@ import { serializeComposerFileLink } from "@t3tools/shared/composerTrigger";
79
*/
810
export const COMPOSER_MENTION_DRAG_TYPE = "application/x-t3code-composer-mention";
911

12+
/**
13+
* Drag payload type carrying a pull request, as JSON `PullRequestContextMetadata`. Set on drags
14+
* that start on a pull request row; the composer drops it as the same chip the `#` picker adds.
15+
*/
16+
export const COMPOSER_PULL_REQUEST_DRAG_TYPE = "application/x-t3code-pull-request";
17+
18+
const PullRequestDragPayload = Schema.fromJsonString(PullRequestContextMetadata);
19+
const decodePullRequestPayload = Schema.decodeUnknownOption(PullRequestDragPayload);
20+
21+
/** The `COMPOSER_PULL_REQUEST_DRAG_TYPE` data for a pull request row's drag. */
22+
export const encodePullRequestDragPayload = Schema.encodeSync(PullRequestDragPayload);
23+
1024
export function composerMentionFromTreePath(treePath: string): string | null {
1125
const relativePath = treePath.replace(/\/+$/, "");
1226
if (relativePath.length === 0) {
@@ -16,7 +30,9 @@ export function composerMentionFromTreePath(treePath: string): string | null {
1630
}
1731

1832
export function dataTransferHasComposerMention(types: ReadonlyArray<string>): boolean {
19-
return types.includes(COMPOSER_MENTION_DRAG_TYPE);
33+
return (
34+
types.includes(COMPOSER_MENTION_DRAG_TYPE) || types.includes(COMPOSER_PULL_REQUEST_DRAG_TYPE)
35+
);
2036
}
2137

2238
export interface ComposerMentionDragTransfer {
@@ -41,6 +57,7 @@ export interface ComposerMentionDragEvent {
4157
*/
4258
export interface ComposerMentionDropHost {
4359
insertMentionAtEnd(text: string): boolean;
60+
insertPullRequestAtEnd(pullRequest: PullRequestContextMetadata): boolean;
4461
setDragActive(active: boolean): void;
4562
onInsertRejected(): void;
4663
}
@@ -76,8 +93,8 @@ export function makeComposerMentionDragHandlers(
7693
if (!claim(event)) {
7794
return;
7895
}
79-
// The tree constrains its drags to effectAllowed "move"; naming any
80-
// other effect makes the browser cancel the drop without firing it.
96+
// The tree constrains its drags to effectAllowed "move" (pull request links allow
97+
// anything); naming any other effect makes the browser cancel the drop without firing it.
8198
event.dataTransfer.dropEffect = "move";
8299
host.setDragActive(true);
83100
},
@@ -86,6 +103,15 @@ export function makeComposerMentionDragHandlers(
86103
return;
87104
}
88105
host.setDragActive(false);
106+
if (event.dataTransfer.types.includes(COMPOSER_PULL_REQUEST_DRAG_TYPE)) {
107+
const pullRequest = decodePullRequestPayload(
108+
event.dataTransfer.getData(COMPOSER_PULL_REQUEST_DRAG_TYPE),
109+
);
110+
if (Option.isSome(pullRequest) && !host.insertPullRequestAtEnd(pullRequest.value)) {
111+
host.onInsertRejected();
112+
}
113+
return;
114+
}
89115
const mention = event.dataTransfer.getData(COMPOSER_MENTION_DRAG_TYPE);
90116
if (mention.length === 0) {
91117
return;

‎apps/web/src/components/pullRequest/ThreadPullRequestsPanel.tsx‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ import {
1818
import { LegendList } from "@legendapp/list/react";
1919
import { memo, useCallback, useMemo, useState } from "react";
2020

21+
import {
22+
COMPOSER_PULL_REQUEST_DRAG_TYPE,
23+
encodePullRequestDragPayload,
24+
} from "~/components/chat/composerMentionDrag";
2125
import { writeTextToClipboard } from "~/hooks/useCopyToClipboard";
2226
import { findProjectForChangeRequest, useOpenPrLink } from "~/lib/openPullRequestLink";
2327
import { cn } from "~/lib/utils";
@@ -159,6 +163,25 @@ function LinkRowImpl({
159163
<a
160164
href={link.url}
161165
onClick={(event) => openPrLink(event, link.url, threadRef)}
166+
// The link's own URL still rides along for drops outside the app; the composer takes
167+
// this payload instead and adds the pull request as a context chip.
168+
onDragStart={
169+
snapshot === null
170+
? undefined
171+
: (event) =>
172+
event.dataTransfer.setData(
173+
COMPOSER_PULL_REQUEST_DRAG_TYPE,
174+
encodePullRequestDragPayload({
175+
number: link.number,
176+
title: snapshot.title,
177+
url: link.url,
178+
headBranch: snapshot.headBranch,
179+
baseBranch: snapshot.baseBranch,
180+
state: snapshot.state,
181+
isDraft: snapshot.isDraft,
182+
}),
183+
)
184+
}
162185
className="flex min-w-0 flex-1"
163186
>
164187
<PullRequestRowLines

‎apps/web/src/components/pullRequest/pullRequestDetail.logic.test.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,6 +1165,13 @@ describe("asking about a change rather than working on it", () => {
11651165
expect(context.text).not.toContain("Walk through this pull request");
11661166
});
11671167

1168+
it("gives the same number in another repository its own chip", () => {
1169+
const fork = { ...base, url: "https://github.com/someone/t3code/pull/42" };
1170+
expect(buildPullRequestReferenceContext(fork).id).not.toBe(
1171+
buildPullRequestReferenceContext(base).id,
1172+
);
1173+
});
1174+
11681175
it("leaves the composer empty, and everything the agent needs in the chip", () => {
11691176
const handoff = buildAskAboutPullRequestHandoff(base);
11701177
expect(handoff.prompt).toBe("");

‎apps/web/src/components/pullRequest/pullRequestDetail.logic.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1109,7 +1109,8 @@ export function buildPullRequestReferenceContext(
11091109
input: PullRequestContextMetadata,
11101110
): ReviewCommentContext {
11111111
const comment = pullRequestContextComment(input, []);
1112-
return { ...comment, id: `pr-reference:${input.number}` };
1112+
// Keyed by URL: the draft upserts by id, and two repositories can share a number.
1113+
return { ...comment, id: `pr-reference:${input.url}` };
11131114
}
11141115

11151116
/** What the agent is asked to do with a question, as opposed to a task. */

0 commit comments

Comments
 (0)