fix(web): prioritize open panel pull request when copying (#9877)

This commit is contained in:
maria authored and GitHub committed 2026-09-04 23:40:02 -04:00
1 parent b01771c238
commit a0eb23993a
5 files changed
+108 -4

No files matched your search

+10 -1
View File
@@ -222,6 +222,7 @@ import {
import { useNowMinute } from "../hooks/useNowMinute";
import { usePanelAnimationSettings, usePanelPresence } from "../panelAnimations";
import { useNewThreadHandler } from "../hooks/useHandleNewThread";
import { useOpenPanelPullRequestUrl } from "../hooks/useOpenPanelPullRequestUrl";
import { useThreadActions } from "../hooks/useThreadActions";
import { resolveAppModelSelectionForInstance } from "../modelSelection";
import { confirmTerminalClose, isTerminalCloseConfirmPending } from "../lib/terminalCloseConfirm";
@@ -5077,16 +5078,24 @@ export default function ChatView(props: ChatViewProps) {
threadRepository,
],
);
const openPanelPullRequestUrl = useOpenPanelPullRequestUrl(activeThreadRef);
const activeThreadReferenceCopyTarget = useMemo(
() =>
activeThreadId === null || !isServerThread
? null
: resolveThreadReferenceCopyTarget({
threadId: activeThreadId,
openPanelPullRequestUrl,
linkedPullRequestUrl: linkedThreadPullRequest?.url ?? null,
detectedPullRequestUrl: activeThreadPr?.url ?? null,
}),
[activeThreadId, activeThreadPr?.url, isServerThread, linkedThreadPullRequest?.url],
[
activeThreadId,
activeThreadPr?.url,
isServerThread,
linkedThreadPullRequest?.url,
openPanelPullRequestUrl,
],
);
const copyActiveThreadReference = useCallback(() => {
const target = activeThreadReferenceCopyTarget;
+7 -1
View File
@@ -71,6 +71,7 @@ import { useAtomValue } from "@effect/atom-react";
import { isDesktopLocalConnectionTarget } from "../connection/desktopLocal";
import { useDesktopLocalBootstraps } from "../connection/useDesktopLocalBootstraps";
import { useHandleNewThread } from "../hooks/useHandleNewThread";
import { useOpenPanelPullRequestUrl } from "../hooks/useOpenPanelPullRequestUrl";
import { writeTextToClipboard } from "../hooks/useCopyToClipboard";
import { useClientSettings } from "../hooks/useSettings";
import { useTheme } from "../hooks/useTheme";
@@ -633,11 +634,15 @@ function OpenCommandPaletteDialog(props: {
),
retainTerminalOnBranchMismatch: activeThread.worktreePath === null,
})?.url ?? null);
const openPanelPullRequestUrl = useOpenPanelPullRequestUrl(
activeThread ? scopeThreadRef(activeThread.environmentId, activeThread.id) : null,
);
const activeThreadReferenceCopyTarget =
activeThread == null
? null
: resolveThreadReferenceCopyTarget({
threadId: activeThread.id,
openPanelPullRequestUrl,
linkedPullRequestUrl: activeThread.linkedPullRequest?.url ?? null,
detectedPullRequestUrl,
});
@@ -2302,9 +2307,10 @@ function OpenCommandPaletteDialog(props: {
}
return;
}
if (command === "thread.copyReference" && activeThreadReferenceCopyTarget !== null) {
if (command === "thread.copyReference") {
event.preventDefault();
event.stopPropagation();
if (activeThreadReferenceCopyTarget === null) return;
setOpen(false);
void copyActiveThreadReference();
return;
@@ -0,0 +1,59 @@
import { scopeProjectRef } from "@t3tools/client-runtime/environment";
import { EnvironmentId, ProjectId, type ScopedThreadRef } from "@t3tools/contracts";
import { useMemo } from "react";
import {
readPullRequestDetailSnapshot,
resolveDisplayedPullRequestDetail,
} from "../components/pullRequest/pullRequestDetail.logic";
import { gitHubPullRequestBrowserUrl } from "../lib/openPullRequestLink";
import { selectActiveRightPanelSurface, useRightPanelStore } from "../rightPanelStore";
import { useProject } from "../state/entities";
import { pullRequestEnvironment } from "../state/pullRequests";
import { useEnvironmentQuery } from "../state/query";
export function useOpenPanelPullRequestUrl(threadRef: ScopedThreadRef | null) {
const surface = useRightPanelStore((state) =>
selectActiveRightPanelSurface(state.byThreadKey, threadRef),
);
const reference = surface?.kind === "pull-request" ? surface : null;
const environmentId = reference?.environmentId
? EnvironmentId.make(reference.environmentId)
: threadRef?.environmentId;
const project = useProject(
reference && environmentId
? scopeProjectRef(environmentId, ProjectId.make(reference.projectId))
: null,
);
const detail = useEnvironmentQuery(
reference && environmentId
? pullRequestEnvironment.detail({
environmentId,
input: {
projectId: ProjectId.make(reference.projectId),
repository: reference.repository,
number: reference.number,
},
})
: null,
).data;
const cachedDetail = useMemo(
() =>
reference && environmentId
? readPullRequestDetailSnapshot(
typeof window === "undefined" ? undefined : window.localStorage,
environmentId,
reference,
)
: null,
[environmentId, reference],
);
return reference
? (resolveDisplayedPullRequestDetail({ live: detail, cached: cachedDetail, reference })?.url ??
gitHubPullRequestBrowserUrl(
project?.repositoryIdentity,
reference.repository,
reference.number,
))
: undefined;
}
@@ -3,6 +3,32 @@ import { describe, expect, it } from "vite-plus/test";
import { resolveThreadReferenceCopyTarget } from "./threadReference.ts";
describe("resolveThreadReferenceCopyTarget", () => {
it("does not copy another reference while the open panel URL is unavailable", () => {
expect(
resolveThreadReferenceCopyTarget({
threadId: "thread-1",
openPanelPullRequestUrl: null,
linkedPullRequestUrl: "https://github.com/t3/pr/12",
detectedPullRequestUrl: "https://github.com/t3/pr/13",
}),
).toBeNull();
});
it("prefers the open panel pull request over linked and detected pull requests", () => {
expect(
resolveThreadReferenceCopyTarget({
threadId: "thread-1",
openPanelPullRequestUrl: "https://github.com/t3/pr/14",
linkedPullRequestUrl: "https://github.com/t3/pr/12",
detectedPullRequestUrl: "https://github.com/t3/pr/13",
}),
).toMatchObject({
kind: "pull-request",
value: "https://github.com/t3/pr/14",
successTitle: "PR link copied",
});
});
it("prefers a durable linked pull request", () => {
expect(
resolveThreadReferenceCopyTarget({
+6 -2
View File
@@ -8,10 +8,14 @@ export interface ThreadReferenceCopyTarget {
export function resolveThreadReferenceCopyTarget(input: {
readonly threadId: string;
/** Undefined means no PR panel; null means its URL is not available yet. */
readonly openPanelPullRequestUrl?: string | null | undefined;
readonly linkedPullRequestUrl?: string | null;
readonly detectedPullRequestUrl?: string | null;
}): ThreadReferenceCopyTarget {
const pullRequestUrl = input.linkedPullRequestUrl ?? input.detectedPullRequestUrl;
}): ThreadReferenceCopyTarget | null {
if (input.openPanelPullRequestUrl === null) return null;
const pullRequestUrl =
input.openPanelPullRequestUrl ?? input.linkedPullRequestUrl ?? input.detectedPullRequestUrl;
return pullRequestUrl
? {
kind: "pull-request",