Skip to content

Commit f447953

Browse files
feat(web): drag a linked pull request into the composer as a context chip
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent bb4e4e7 commit f447953

4 files changed

Lines changed: 108 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: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,25 @@ import { describe, expect, it } from "@effect/vitest";
22

33
import {
44
COMPOSER_MENTION_DRAG_TYPE,
5+
COMPOSER_PULL_REQUEST_DRAG_TYPE,
56
type ComposerMentionDropHost,
67
composerMentionFromTreePath,
78
dataTransferHasComposerMention,
89
makeComposerMentionDragHandlers,
910
} from "./composerMentionDrag.ts";
1011

11-
const makeDragEvent = (options?: { mention?: string; types?: ReadonlyArray<string> }) => {
12+
const makeDragEvent = (options?: {
13+
mention?: string;
14+
types?: ReadonlyArray<string>;
15+
format?: string;
16+
}) => {
1217
const mention = options?.mention ?? "[index.md](docs/index.md)";
18+
const format = options?.format ?? COMPOSER_MENTION_DRAG_TYPE;
1319
const calls: Array<string> = [];
1420
const event = {
1521
dataTransfer: {
16-
types: options?.types ?? [COMPOSER_MENTION_DRAG_TYPE, "text/plain"],
17-
getData: (format: string) => (format === COMPOSER_MENTION_DRAG_TYPE ? mention : ""),
22+
types: options?.types ?? [format, "text/plain"],
23+
getData: (requested: string) => (requested === format ? mention : ""),
1824
dropEffect: "none",
1925
},
2026
nativeEvent: {
@@ -33,6 +39,10 @@ const makeHost = (insertResult = true) => {
3339
log.push(`insert:${text}`);
3440
return insertResult;
3541
},
42+
insertPullRequestAtEnd: (pullRequest) => {
43+
log.push(`pr:${pullRequest.number}`);
44+
return insertResult;
45+
},
3646
setDragActive: (active) => void log.push(`active:${active}`),
3747
onInsertRejected: () => void log.push("rejected"),
3848
};
@@ -120,4 +130,34 @@ describe("makeComposerMentionDragHandlers", () => {
120130
handlers.onDrop(makeDragEvent({ mention: "" }).event);
121131
expect(log).toEqual(["active:false"]);
122132
});
133+
134+
it("drops a pull request row as a pull request, not as text", () => {
135+
const { host, log } = makeHost();
136+
const handlers = makeComposerMentionDragHandlers(host);
137+
const pullRequest = {
138+
number: 18094,
139+
title: "fix(server): queued runs land on the timeline where they started",
140+
url: "https://github.com/pingdotgg/t3code/pull/18094",
141+
headBranch: "fix/run-timeline-position",
142+
baseBranch: "main",
143+
state: "open",
144+
isDraft: false,
145+
};
146+
handlers.onDrop(
147+
makeDragEvent({
148+
format: COMPOSER_PULL_REQUEST_DRAG_TYPE,
149+
mention: JSON.stringify(pullRequest),
150+
}).event,
151+
);
152+
expect(log).toEqual(["active:false", "pr:18094"]);
153+
});
154+
155+
it("ignores a pull request payload that does not decode", () => {
156+
const { host, log } = makeHost();
157+
const handlers = makeComposerMentionDragHandlers(host);
158+
handlers.onDrop(
159+
makeDragEvent({ format: COMPOSER_PULL_REQUEST_DRAG_TYPE, mention: '{"number":0}' }).event,
160+
);
161+
expect(log).toEqual(["active:false"]);
162+
});
123163
});

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

Lines changed: 27 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,16 @@ 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 decodePullRequestPayload = Schema.decodeUnknownOption(
19+
Schema.fromJsonString(PullRequestContextMetadata),
20+
);
21+
1022
export function composerMentionFromTreePath(treePath: string): string | null {
1123
const relativePath = treePath.replace(/\/+$/, "");
1224
if (relativePath.length === 0) {
@@ -16,7 +28,9 @@ export function composerMentionFromTreePath(treePath: string): string | null {
1628
}
1729

1830
export function dataTransferHasComposerMention(types: ReadonlyArray<string>): boolean {
19-
return types.includes(COMPOSER_MENTION_DRAG_TYPE);
31+
return (
32+
types.includes(COMPOSER_MENTION_DRAG_TYPE) || types.includes(COMPOSER_PULL_REQUEST_DRAG_TYPE)
33+
);
2034
}
2135

2236
export interface ComposerMentionDragTransfer {
@@ -41,6 +55,7 @@ export interface ComposerMentionDragEvent {
4155
*/
4256
export interface ComposerMentionDropHost {
4357
insertMentionAtEnd(text: string): boolean;
58+
insertPullRequestAtEnd(pullRequest: PullRequestContextMetadata): boolean;
4459
setDragActive(active: boolean): void;
4560
onInsertRejected(): void;
4661
}
@@ -76,8 +91,8 @@ export function makeComposerMentionDragHandlers(
7691
if (!claim(event)) {
7792
return;
7893
}
79-
// The tree constrains its drags to effectAllowed "move"; naming any
80-
// other effect makes the browser cancel the drop without firing it.
94+
// The tree constrains its drags to effectAllowed "move" (pull request links allow
95+
// anything); naming any other effect makes the browser cancel the drop without firing it.
8196
event.dataTransfer.dropEffect = "move";
8297
host.setDragActive(true);
8398
},
@@ -86,6 +101,15 @@ export function makeComposerMentionDragHandlers(
86101
return;
87102
}
88103
host.setDragActive(false);
104+
if (event.dataTransfer.types.includes(COMPOSER_PULL_REQUEST_DRAG_TYPE)) {
105+
const pullRequest = decodePullRequestPayload(
106+
event.dataTransfer.getData(COMPOSER_PULL_REQUEST_DRAG_TYPE),
107+
);
108+
if (Option.isSome(pullRequest) && !host.insertPullRequestAtEnd(pullRequest.value)) {
109+
host.onInsertRejected();
110+
}
111+
return;
112+
}
89113
const mention = event.dataTransfer.getData(COMPOSER_MENTION_DRAG_TYPE);
90114
if (mention.length === 0) {
91115
return;

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import type { ProjectId, ScopedThreadRef, ThreadPullRequestLink } from "@t3tools/contracts";
1+
import type {
2+
ProjectId,
3+
PullRequestContextMetadata,
4+
ScopedThreadRef,
5+
ThreadPullRequestLink,
6+
} from "@t3tools/contracts";
27
import {
38
sourceControlClients,
49
UNKNOWN_SOURCE_CONTROL_CLIENT,
@@ -17,6 +22,7 @@ import {
1722
} from "lucide-react";
1823
import { useCallback, useMemo, useState } from "react";
1924

25+
import { COMPOSER_PULL_REQUEST_DRAG_TYPE } from "~/components/chat/composerMentionDrag";
2026
import { writeTextToClipboard } from "~/hooks/useCopyToClipboard";
2127
import { findProjectForChangeRequest, useOpenPrLink } from "~/lib/openPullRequestLink";
2228
import { cn } from "~/lib/utils";
@@ -159,6 +165,25 @@ function LinkRow({
159165
<a
160166
href={link.url}
161167
onClick={(event) => openPrLink(event, link.url, threadRef)}
168+
// The link's own URL still rides along for drops outside the app; the composer takes
169+
// this payload instead and adds the pull request as a context chip.
170+
onDragStart={
171+
snapshot === null
172+
? undefined
173+
: (event) =>
174+
event.dataTransfer.setData(
175+
COMPOSER_PULL_REQUEST_DRAG_TYPE,
176+
JSON.stringify({
177+
number: link.number,
178+
title: snapshot.title,
179+
url: link.url,
180+
headBranch: snapshot.headBranch,
181+
baseBranch: snapshot.baseBranch,
182+
state: snapshot.state,
183+
isDraft: snapshot.isDraft,
184+
} satisfies PullRequestContextMetadata),
185+
)
186+
}
162187
className="flex min-w-0 flex-1"
163188
>
164189
<PullRequestRowLines

0 commit comments

Comments
 (0)