TypeScript
fix(web): prioritize open panel pull request when copying (#9877)
mariaa0eb23993a3b
diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsxindex b79c291fbda..a6ae0e773f4 100644--- a/apps/web/src/components/ChatView.tsx+++ b/apps/web/src/components/ChatView.tsx@@ -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;diff --git a/apps/web/src/components/CommandPalette.tsx b/apps/web/src/components/CommandPalette.tsxindex 27dddf00ee5..b0dd3febebc 100644--- a/apps/web/src/components/CommandPalette.tsx+++ b/apps/web/src/components/CommandPalette.tsx@@ -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;diff --git a/apps/web/src/hooks/useOpenPanelPullRequestUrl.ts b/apps/web/src/hooks/useOpenPanelPullRequestUrl.tsnew file mode 100644index 00000000000..ec5a6c04306--- /dev/null+++ b/apps/web/src/hooks/useOpenPanelPullRequestUrl.ts@@ -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;+}diff --git a/packages/shared/src/threadReference.test.ts b/packages/shared/src/threadReference.test.tsindex 693fabc66b1..60600c7e146 100644--- a/packages/shared/src/threadReference.test.ts+++ b/packages/shared/src/threadReference.test.ts@@ -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({diff --git a/packages/shared/src/threadReference.ts b/packages/shared/src/threadReference.tsindex cb4fc9dd5c2..20eac6fba03 100644--- a/packages/shared/src/threadReference.ts+++ b/packages/shared/src/threadReference.ts@@ -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",