TypeScript
fix(web): copy selected pull request link from PR page (#10615)
mariaea2983afbbcd
diff --git a/apps/web/src/components/CommandPalette.tsx b/apps/web/src/components/CommandPalette.tsxindex ec1e79dc2d7..fcf86362707 100644--- a/apps/web/src/components/CommandPalette.tsx+++ b/apps/web/src/components/CommandPalette.tsx@@ -97,7 +97,11 @@ import {import { onOpenCommandPalette } from "../commandPaletteBus";import { isPreviewFocused } from "../lib/previewFocus";import { isTerminalFocused } from "../lib/terminalFocus";-import { selectActiveRightPanel, useRightPanelStore } from "../rightPanelStore";+import {+ PULL_REQUESTS_PANEL_REF,+ selectActiveRightPanel,+ useRightPanelStore,+} from "../rightPanelStore";import { getLatestThreadForProject, sortThreads } from "../lib/threadSort";import {cn,@@ -599,17 +603,25 @@ function OpenCommandPaletteDialog(props: {const { activeDraftThread, activeThread, defaultProjectRef, handleNewThread } =useHandleNewThread();const projects = useProjects();- const openPanelPullRequestUrl = useOpenPanelPullRequestUrl(- activeThread ? scopeThreadRef(activeThread.environmentId, activeThread.id) : null,- );+ const referenceThreadRef =+ pathname === "/pull-requests"+ ? environments.some(+ (environment) => environment.serverConfig?.environment.capabilities.pullRequests === true,+ )+ ? PULL_REQUESTS_PANEL_REF+ : null+ : activeThread+ ? scopeThreadRef(activeThread.environmentId, activeThread.id)+ : null;+ const openPanelPullRequestUrl = useOpenPanelPullRequestUrl(referenceThreadRef);const activeThreadReferenceCopyTarget =- activeThread == null+ referenceThreadRef === null || (pathname === "/pull-requests" && !openPanelPullRequestUrl)? null: resolveThreadReferenceCopyTarget({- threadId: activeThread.id,+ threadId: referenceThreadRef.threadId,openPanelPullRequestUrl,linkedPullRequestUrl:- activeThread.linkedPullRequest?.url ?? activeThread.branchPullRequest?.url ?? null,+ activeThread?.linkedPullRequest?.url ?? activeThread?.branchPullRequest?.url ?? null,});const copyActiveThreadReference = useCallback(async () => {const target = activeThreadReferenceCopyTarget;diff --git a/apps/web/src/hooks/useOpenPanelPullRequestUrl.ts b/apps/web/src/hooks/useOpenPanelPullRequestUrl.tsindex ec5a6c04306..e96f7eac9d0 100644--- a/apps/web/src/hooks/useOpenPanelPullRequestUrl.ts+++ b/apps/web/src/hooks/useOpenPanelPullRequestUrl.ts@@ -50,6 +50,7 @@ export function useOpenPanelPullRequestUrl(threadRef: ScopedThreadRef | null) {);return reference? (resolveDisplayedPullRequestDetail({ live: detail, cached: cachedDetail, reference })?.url ??+ reference.url ??gitHubPullRequestBrowserUrl(project?.repositoryIdentity,reference.repository,diff --git a/apps/web/src/rightPanelStore.test.ts b/apps/web/src/rightPanelStore.test.tsindex 4336aa2a35e..46091d3fd41 100644--- a/apps/web/src/rightPanelStore.test.ts+++ b/apps/web/src/rightPanelStore.test.ts@@ -587,6 +587,8 @@ describe("rightPanelStore", () => {const second = { projectId: "project-a", repository: "pingdotgg/t3code", number: 4910 };useRightPanelStore.getState().openPullRequest(refA, first);useRightPanelStore.getState().openPullRequest(refA, second);+ const url = "https://gitlab.example.com/pingdotgg/t3code/-/merge_requests/4909";+ useRightPanelStore.getState().openPullRequest(refA, { ...first, url });useRightPanelStore.getState().openPullRequest(refA, first);const state = selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA);@@ -595,6 +597,10 @@ describe("rightPanelStore", () => {pullRequestSurfaceId(second),]);expect(state.activeSurfaceId).toBe(pullRequestSurfaceId(first));+ expect(+ selectActiveRightPanelSurface(useRightPanelStore.getState().byThreadKey, refA),+ ).toMatchObject({ url });+ expect(state.surfaces[1]).not.toHaveProperty("url");});it("keeps one pull request read from two servers as two tabs", () => {diff --git a/apps/web/src/rightPanelStore.ts b/apps/web/src/rightPanelStore.tsindex acf673b042b..d167afeddb7 100644--- a/apps/web/src/rightPanelStore.ts+++ b/apps/web/src/rightPanelStore.ts@@ -7,8 +7,13 @@* terminal surfaces point at terminal session ids, file surfaces point at* workspace paths, and diff/files remain singleton surfaces.*/-import { scopedThreadKey } from "@t3tools/client-runtime/environment";-import type { ChatFileAttachment, ScopedThreadRef } from "@t3tools/contracts";+import { scopedThreadKey, scopeThreadRef } from "@t3tools/client-runtime/environment";+import {+ EnvironmentId,+ ThreadId,+ type ChatFileAttachment,+ type ScopedThreadRef,+} from "@t3tools/contracts";import { create } from "zustand";import { createJSONStorage, persist } from "zustand/middleware";@@ -65,6 +70,7 @@ export type RightPanelSurface =projectId: string;repository: string;number: number;+ url?: string;}| { id: "agents"; kind: "agents" };@@ -74,8 +80,14 @@ const RIGHT_PANEL_STORAGE_KEY = "t3code:right-panel-state:v2";// v11 stops persisting the pull-request list's shared panel, so a restart opens the page fresh.const RIGHT_PANEL_STORAGE_VERSION = 11;+/** A fixed workspace-level ref: each PR surface carries its own real environment. */+export const PULL_REQUESTS_PANEL_REF = scopeThreadRef(+ EnvironmentId.make("pull-requests-panel"),+ ThreadId.make("pull-requests-panel"),+);+/**- * The pull-request list's shared panel (see PULL_REQUESTS_PANEL_ID in the route) is session+ * The pull-request list's shared panel is session* state: reopening the app should show the list, not last session's tabs and detail fetches.*/const isPullRequestsPanelKey = (threadKey: string) => threadKey.endsWith(":pull-requests-panel");@@ -109,7 +121,13 @@ interface RightPanelStoreState {openAttachment: (ref: ScopedThreadRef, attachment: ChatFileAttachment) => void;openPullRequest: (ref: ScopedThreadRef,- target: { environmentId?: string; projectId: string; repository: string; number: number },+ target: {+ environmentId?: string;+ projectId: string;+ repository: string;+ number: number;+ url?: string;+ },) => void;openTerminal: (ref: ScopedThreadRef, terminalId: string) => void;splitTerminal: (@@ -210,6 +228,7 @@ export function pullRequestSurface(target: {projectId: string;repository: string;number: number;+ url?: string;}): PullRequestSurface {return {id: pullRequestSurfaceId(target),@@ -218,6 +237,7 @@ export function pullRequestSurface(target: {projectId: target.projectId,repository: target.repository,number: target.number,+ ...(typeof target.url === "string" ? { url: target.url } : {}),};}@@ -444,7 +464,16 @@ export const useRightPanelStore = create<RightPanelStoreState>()(openPullRequest: (ref, target) =>set((state) =>userAction(state, scopedThreadKey(ref), (current) => {- return upsertSurface(current, pullRequestSurface(target));+ const surface = pullRequestSurface(target);+ const next = upsertSurface(current, surface);+ return target.url+ ? {+ ...next,+ surfaces: next.surfaces.map((entry) =>+ entry.id === surface.id ? surface : entry,+ ),+ }+ : next;}),),openFile: (ref, relativePath, line) =>diff --git a/apps/web/src/routes/_chat.pull-requests.tsx b/apps/web/src/routes/_chat.pull-requests.tsxindex 7aab91d1b82..99986567cbf 100644--- a/apps/web/src/routes/_chat.pull-requests.tsx+++ b/apps/web/src/routes/_chat.pull-requests.tsx@@ -1,7 +1,6 @@import { RefreshIcon } from "~/components/ui/refresh-icon";import { Spinner } from "~/components/ui/spinner";-import { scopeThreadRef } from "@t3tools/client-runtime/environment";-import { pullRequestHostOf, resolveEnvironmentMachineKind, ThreadId } from "@t3tools/contracts";+import { pullRequestHostOf, resolveEnvironmentMachineKind } from "@t3tools/contracts";import type {EnvironmentId,ProjectId,@@ -120,8 +119,12 @@ import { Menu, MenuPopup, MenuRadioGroup, MenuRadioItem, MenuTrigger } from "../import { SidebarInset } from "../components/ui/sidebar";import { Tooltip, TooltipPopup, TooltipTrigger } from "../components/ui/tooltip";import { useLiveRefresh } from "../hooks/useLiveRefresh";+import { useOpenPanelPullRequestUrl } from "../hooks/useOpenPanelPullRequestUrl";+import { writeTextToClipboard } from "../hooks/useCopyToClipboard";+import { toastManager } from "../components/ui/toast";import { usePanelAnimationSettings, usePanelPresence } from "../panelAnimations";import {+ PULL_REQUESTS_PANEL_REF,pullRequestSurfaceId,selectActiveRightPanelSurface,selectSelectedRightPanelSurface,@@ -205,15 +208,6 @@ const PAGE_SIZE = 99;const MAX_PAGE_SIZE = 500;/** Stable empty map so the memos below do not see a new object on every render. */const EMPTY_VIEWERS: PullRequestListResult["viewers"] = {};-/** The list owns one environment-scoped right panel rather than borrowing a real thread's. */-const PULL_REQUESTS_PANEL_ID = ThreadId.make("pull-requests-panel");-/**- * A fixed sentinel, not a real server: the panel is one workspace-level surface list (each- * surface already carries the server it was read from), so its store key must not move when a- * capable server disconnects or reconnects. Real environment ids are server-generated UUIDs, so- * this string can never collide with one.- */-const PULL_REQUESTS_PANEL_ENVIRONMENT_ID = "pull-requests-panel" as EnvironmentId;/** Stable so a read that is not wanted right now does not re-key on every render. */const NO_LIST_TARGETS: ReadonlyArray<EnvironmentQueryTarget<PullRequestListInput>> = [];const EMPTY_PREVIEW_SESSIONS = {};@@ -424,13 +418,8 @@ function PullRequestsRouteView() {// read from, so tabs from two of them sit side by side instead of replacing each other. Its ref// uses a fixed sentinel environment, not whichever server happens to sort first, so the tab// strip survives a capable server disconnecting or losing the pull-requests capability.- const rightPanelRef = useMemo(- () =>- capableEnvironments.length === 0- ? null- : scopeThreadRef(PULL_REQUESTS_PANEL_ENVIRONMENT_ID, PULL_REQUESTS_PANEL_ID),- [capableEnvironments.length],- );+ const rightPanelRef = capableEnvironments.length === 0 ? null : PULL_REQUESTS_PANEL_REF;+ const openPanelPullRequestUrl = useOpenPanelPullRequestUrl(rightPanelRef);const rightPanelState = useRightPanelStore((state) =>selectThreadRightPanelState(state.byThreadKey, rightPanelRef),);@@ -453,7 +442,7 @@ function PullRequestsRouteView() {rightPanelState.isOpen && selectedPullRequestSurface !== null,rightPanelPresenceValue,panelAnimationsActive,- rightPanelRef === null ? null : PULL_REQUESTS_PANEL_ID,+ rightPanelRef?.threadId ?? null,panelAnimationDurationMs,);const rightPanelPresent = rightPanelPresence.present;@@ -1852,8 +1841,27 @@ function PullRequestsRouteView() {selectSurfaceInUrl(null);};- // This page has no ChatView, so the shared panel handles `rightPanel.close`- // itself. With nothing open the event falls through to its native meaning.+ // This page has no ChatView, so it handles the shared panel shortcuts itself.+ const copyPullRequestFromShortcut = useEffectEvent((event: KeyboardEvent) => {+ if (!openPanelPullRequestUrl) return;+ event.preventDefault();+ event.stopPropagation();+ if (event.repeat) return;+ const url = openPanelPullRequestUrl;+ void writeTextToClipboard(url, "pull request link").then(+ (didCopy) => {+ if (didCopy)+ toastManager.add({ type: "success", title: "PR link copied", description: url });+ },+ (error) => {+ toastManager.add({+ type: "error",+ title: "Failed to copy PR link",+ description: error instanceof Error ? error.message : "An error occurred.",+ });+ },+ );+ });const closeActiveSurfaceFromShortcut = useEffectEvent((event: KeyboardEvent) => {if (activePullRequestSurface === null) return;event.preventDefault();@@ -1867,6 +1875,7 @@ function PullRequestsRouteView() {context: { terminalFocus: isTerminalFocused() },});if (command === "rightPanel.close") closeActiveSurfaceFromShortcut(event);+ if (command === "thread.copyReference") copyPullRequestFromShortcut(event);};window.addEventListener("keydown", onKeyDown);return () => window.removeEventListener("keydown", onKeyDown);