TypeScript
feat(web): drag a linked pull request into the composer as a context chip (#20102)
Julius Marmingeb67ac65b74fe
diff --git a/apps/web/src/components/chat/ChatComposer.tsx b/apps/web/src/components/chat/ChatComposer.tsxindex 822e79da8db..323efb3db1b 100644--- a/apps/web/src/components/chat/ChatComposer.tsx+++ b/apps/web/src/components/chat/ChatComposer.tsx@@ -6095,11 +6095,22 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)return setContextInsertionHandler(composerDraftTarget, insertContextReferencesAtCaretStable);}, [composerDraftTarget, insertContextReferencesAtCaretStable, setContextInsertionHandler]);- // File-tree drags land as mentions. Handled in the capture phase so the+ // File-tree drags land as mentions, pull request rows as their context chip. Handled in the capture phase so the// editor never sees the drop; the load-bearing rules (native stop, "move"// effect, no eager focus) live in makeComposerMentionDragHandlers.const composerMentionDragHandlers = makeComposerMentionDragHandlers({insertMentionAtEnd: (text) => insertComposerTextAtEnd(text, { ensureLeadingBoundary: true }),+ insertPullRequestAtEnd: (pullRequest) => {+ const comment = buildPullRequestReferenceContext(pullRequest);+ const inserted = insertComposerTextAtEnd(+ `${formatInlineContextReference(reviewCommentContextReference(comment))} `,+ { ensureLeadingBoundary: true },+ );+ if (inserted) {+ addComposerDraftReviewComment(composerDraftTarget, comment, { appendReference: false });+ }+ return inserted;+ },setDragActive: setIsDragOverComposer,onInsertRejected: () => {toastManager.add({diff --git a/apps/web/src/components/chat/composerMentionDrag.test.ts b/apps/web/src/components/chat/composerMentionDrag.test.tsindex eff11c1932f..5a09a2670cf 100644--- a/apps/web/src/components/chat/composerMentionDrag.test.ts+++ b/apps/web/src/components/chat/composerMentionDrag.test.ts@@ -2,19 +2,26 @@ import { describe, expect, it } from "@effect/vitest";import {COMPOSER_MENTION_DRAG_TYPE,+ COMPOSER_PULL_REQUEST_DRAG_TYPE,+ encodePullRequestDragPayload,type ComposerMentionDropHost,composerMentionFromTreePath,dataTransferHasComposerMention,makeComposerMentionDragHandlers,} from "./composerMentionDrag.ts";-const makeDragEvent = (options?: { mention?: string; types?: ReadonlyArray<string> }) => {+const makeDragEvent = (options?: {+ mention?: string;+ types?: ReadonlyArray<string>;+ format?: string;+}) => {const mention = options?.mention ?? "[index.md](docs/index.md)";+ const format = options?.format ?? COMPOSER_MENTION_DRAG_TYPE;const calls: Array<string> = [];const event = {dataTransfer: {- types: options?.types ?? [COMPOSER_MENTION_DRAG_TYPE, "text/plain"],- getData: (format: string) => (format === COMPOSER_MENTION_DRAG_TYPE ? mention : ""),+ types: options?.types ?? [format, "text/plain"],+ getData: (requested: string) => (requested === format ? mention : ""),dropEffect: "none",},nativeEvent: {@@ -33,6 +40,10 @@ const makeHost = (insertResult = true) => {log.push(`insert:${text}`);return insertResult;},+ insertPullRequestAtEnd: (pullRequest) => {+ log.push(`pr:${pullRequest.number}`);+ return insertResult;+ },setDragActive: (active) => void log.push(`active:${active}`),onInsertRejected: () => void log.push("rejected"),};@@ -120,4 +131,34 @@ describe("makeComposerMentionDragHandlers", () => {handlers.onDrop(makeDragEvent({ mention: "" }).event);expect(log).toEqual(["active:false"]);});++ it("drops a pull request row as a pull request, not as text", () => {+ const { host, log } = makeHost();+ const handlers = makeComposerMentionDragHandlers(host);+ const pullRequest = {+ number: 18094,+ title: "fix(server): queued runs land on the timeline where they started",+ url: "https://github.com/pingdotgg/t3code/pull/18094",+ headBranch: "fix/run-timeline-position",+ baseBranch: "main",+ state: "open",+ isDraft: false,+ } as const;+ handlers.onDrop(+ makeDragEvent({+ format: COMPOSER_PULL_REQUEST_DRAG_TYPE,+ mention: encodePullRequestDragPayload(pullRequest),+ }).event,+ );+ expect(log).toEqual(["active:false", "pr:18094"]);+ });++ it("ignores a pull request payload that does not decode", () => {+ const { host, log } = makeHost();+ const handlers = makeComposerMentionDragHandlers(host);+ handlers.onDrop(+ makeDragEvent({ format: COMPOSER_PULL_REQUEST_DRAG_TYPE, mention: '{"number":0}' }).event,+ );+ expect(log).toEqual(["active:false"]);+ });});diff --git a/apps/web/src/components/chat/composerMentionDrag.ts b/apps/web/src/components/chat/composerMentionDrag.tsindex 43b1bfd800d..d8c4eecbdf7 100644--- a/apps/web/src/components/chat/composerMentionDrag.ts+++ b/apps/web/src/components/chat/composerMentionDrag.ts@@ -1,4 +1,6 @@+import { PullRequestContextMetadata } from "@t3tools/contracts";import { serializeComposerFileLink } from "@t3tools/shared/composerTrigger";+import { Option, Schema } from "effect";/*** Drag payload type carrying a serialized composer mention. Set on drags that@@ -7,6 +9,18 @@ import { serializeComposerFileLink } from "@t3tools/shared/composerTrigger";*/export const COMPOSER_MENTION_DRAG_TYPE = "application/x-t3code-composer-mention";+/**+ * Drag payload type carrying a pull request, as JSON `PullRequestContextMetadata`. Set on drags+ * that start on a pull request row; the composer drops it as the same chip the `#` picker adds.+ */+export const COMPOSER_PULL_REQUEST_DRAG_TYPE = "application/x-t3code-pull-request";++const PullRequestDragPayload = Schema.fromJsonString(PullRequestContextMetadata);+const decodePullRequestPayload = Schema.decodeUnknownOption(PullRequestDragPayload);++/** The `COMPOSER_PULL_REQUEST_DRAG_TYPE` data for a pull request row's drag. */+export const encodePullRequestDragPayload = Schema.encodeSync(PullRequestDragPayload);+export function composerMentionFromTreePath(treePath: string): string | null {const relativePath = treePath.replace(/\/+$/, "");if (relativePath.length === 0) {@@ -16,7 +30,9 @@ export function composerMentionFromTreePath(treePath: string): string | null {}export function dataTransferHasComposerMention(types: ReadonlyArray<string>): boolean {- return types.includes(COMPOSER_MENTION_DRAG_TYPE);+ return (+ types.includes(COMPOSER_MENTION_DRAG_TYPE) || types.includes(COMPOSER_PULL_REQUEST_DRAG_TYPE)+ );}export interface ComposerMentionDragTransfer {@@ -41,6 +57,7 @@ export interface ComposerMentionDragEvent {*/export interface ComposerMentionDropHost {insertMentionAtEnd(text: string): boolean;+ insertPullRequestAtEnd(pullRequest: PullRequestContextMetadata): boolean;setDragActive(active: boolean): void;onInsertRejected(): void;}@@ -76,8 +93,8 @@ export function makeComposerMentionDragHandlers(if (!claim(event)) {return;}- // The tree constrains its drags to effectAllowed "move"; naming any- // other effect makes the browser cancel the drop without firing it.+ // The tree constrains its drags to effectAllowed "move" (pull request links allow+ // anything); naming any other effect makes the browser cancel the drop without firing it.event.dataTransfer.dropEffect = "move";host.setDragActive(true);},@@ -86,6 +103,15 @@ export function makeComposerMentionDragHandlers(return;}host.setDragActive(false);+ if (event.dataTransfer.types.includes(COMPOSER_PULL_REQUEST_DRAG_TYPE)) {+ const pullRequest = decodePullRequestPayload(+ event.dataTransfer.getData(COMPOSER_PULL_REQUEST_DRAG_TYPE),+ );+ if (Option.isSome(pullRequest) && !host.insertPullRequestAtEnd(pullRequest.value)) {+ host.onInsertRejected();+ }+ return;+ }const mention = event.dataTransfer.getData(COMPOSER_MENTION_DRAG_TYPE);if (mention.length === 0) {return;diff --git a/apps/web/src/components/pullRequest/ThreadPullRequestsPanel.tsx b/apps/web/src/components/pullRequest/ThreadPullRequestsPanel.tsxindex fc565b5327d..6cb2dd7d448 100644--- a/apps/web/src/components/pullRequest/ThreadPullRequestsPanel.tsx+++ b/apps/web/src/components/pullRequest/ThreadPullRequestsPanel.tsx@@ -18,6 +18,10 @@ import {import { LegendList } from "@legendapp/list/react";import { memo, useCallback, useMemo, useState } from "react";+import {+ COMPOSER_PULL_REQUEST_DRAG_TYPE,+ encodePullRequestDragPayload,+} from "~/components/chat/composerMentionDrag";import { writeTextToClipboard } from "~/hooks/useCopyToClipboard";import { findProjectForChangeRequest, useOpenPrLink } from "~/lib/openPullRequestLink";import { cn } from "~/lib/utils";@@ -159,6 +163,25 @@ function LinkRowImpl({<ahref={link.url}onClick={(event) => openPrLink(event, link.url, threadRef)}+ // The link's own URL still rides along for drops outside the app; the composer takes+ // this payload instead and adds the pull request as a context chip.+ onDragStart={+ snapshot === null+ ? undefined+ : (event) =>+ event.dataTransfer.setData(+ COMPOSER_PULL_REQUEST_DRAG_TYPE,+ encodePullRequestDragPayload({+ number: link.number,+ title: snapshot.title,+ url: link.url,+ headBranch: snapshot.headBranch,+ baseBranch: snapshot.baseBranch,+ state: snapshot.state,+ isDraft: snapshot.isDraft,+ }),+ )+ }className="flex min-w-0 flex-1"><PullRequestRowLinesdiff --git a/apps/web/src/components/pullRequest/pullRequestDetail.logic.test.ts b/apps/web/src/components/pullRequest/pullRequestDetail.logic.test.tsindex afc78b0c6e2..6b26bf203af 100644--- a/apps/web/src/components/pullRequest/pullRequestDetail.logic.test.ts+++ b/apps/web/src/components/pullRequest/pullRequestDetail.logic.test.ts@@ -1165,6 +1165,13 @@ describe("asking about a change rather than working on it", () => {expect(context.text).not.toContain("Walk through this pull request");});+ it("gives the same number in another repository its own chip", () => {+ const fork = { ...base, url: "https://github.com/someone/t3code/pull/42" };+ expect(buildPullRequestReferenceContext(fork).id).not.toBe(+ buildPullRequestReferenceContext(base).id,+ );+ });+it("leaves the composer empty, and everything the agent needs in the chip", () => {const handoff = buildAskAboutPullRequestHandoff(base);expect(handoff.prompt).toBe("");diff --git a/apps/web/src/components/pullRequest/pullRequestDetail.logic.ts b/apps/web/src/components/pullRequest/pullRequestDetail.logic.tsindex b20b809d16a..bef7b0dd3cd 100644--- a/apps/web/src/components/pullRequest/pullRequestDetail.logic.ts+++ b/apps/web/src/components/pullRequest/pullRequestDetail.logic.ts@@ -1109,7 +1109,8 @@ export function buildPullRequestReferenceContext(input: PullRequestContextMetadata,): ReviewCommentContext {const comment = pullRequestContextComment(input, []);- return { ...comment, id: `pr-reference:${input.number}` };+ // Keyed by URL: the draft upserts by id, and two repositories can share a number.+ return { ...comment, id: `pr-reference:${input.url}` };}/** What the agent is asked to do with a question, as opposed to a task. */