diff --git a/apps/mobile/src/features/review/useReviewHeaderPresentation.tsx b/apps/mobile/src/features/review/useReviewHeaderPresentation.tsx index d0fda18cf60cd..cf25d74ac1112 100644 --- a/apps/mobile/src/features/review/useReviewHeaderPresentation.tsx +++ b/apps/mobile/src/features/review/useReviewHeaderPresentation.tsx @@ -2,6 +2,7 @@ import { useEnvironmentQuery } from "../../state/query"; import { useSelectedThreadGitActions } from "../../state/use-selected-thread-git-actions"; import { useSelectedThreadGitState } from "../../state/use-selected-thread-git-state"; import { useThreadSelection } from "../../state/use-thread-selection"; +import { resolveThreadOpenLinkedPullRequest } from "../../state/thread-pr-presentation"; import { vcsEnvironment } from "../../state/vcs"; import { useThreadGitMenuDefinition } from "../threads/ThreadGitControls"; import type { EnvironmentId, ThreadId } from "@t3tools/contracts"; @@ -27,7 +28,7 @@ export function useReviewHeaderPresentation(props: { readonly selectedSection: ReviewSectionItem | null; readonly onRefresh: () => Promise; }): ReviewHeaderPresentation { - const { selectedThread } = useThreadSelection(); + const { selectedThread, selectedEnvironmentRuntime } = useThreadSelection(); const gitState = useSelectedThreadGitState(); const gitActions = useSelectedThreadGitActions(); const gitStatusQuery = useEnvironmentQuery( @@ -46,6 +47,11 @@ export function useReviewHeaderPresentation(props: { threadId: props.threadId, currentBranch: selectedThread?.branch ?? null, gitStatus: gitStatusQuery.data, + linkedPullRequest: resolveThreadOpenLinkedPullRequest( + selectedThread?.pullRequests ?? [], + selectedEnvironmentRuntime?.serverConfig?.environment.capabilities.threadPullRequests === + true, + ), gitOperationLabel: gitState.gitOperationLabel, onPull: gitActions.onPullSelectedThreadBranch, onRunAction: gitActions.onRunSelectedThreadGitAction, diff --git a/apps/mobile/src/features/threads/DraftThreadHeader.tsx b/apps/mobile/src/features/threads/DraftThreadHeader.tsx index 043273ee79bec..67eaa2b6923f4 100644 --- a/apps/mobile/src/features/threads/DraftThreadHeader.tsx +++ b/apps/mobile/src/features/threads/DraftThreadHeader.tsx @@ -11,6 +11,8 @@ import type { import { useEnvironmentQuery } from "../../state/query"; import { useEnvironmentScope } from "../../state/session"; import { useAtomCommand } from "../../state/use-atom-command"; +import { useRemoteEnvironmentRuntime } from "../../state/use-remote-environment-registry"; +import { resolveThreadOpenLinkedPullRequest } from "../../state/thread-pr-presentation"; import { vcsActionManager, vcsEnvironment } from "../../state/vcs"; import { useVcsActionState } from "../../state/use-vcs-action-state"; import { ThreadHeader } from "./ThreadHeader"; @@ -22,6 +24,7 @@ export function DraftThreadHeader(props: { }) { const navigation = useNavigation(); const environmentId = props.thread.environmentId; + const runtime = useRemoteEnvironmentRuntime(environmentId); const canReadFiles = useEnvironmentScope(environmentId, AuthFilesystemReadScope); const canWriteGit = useEnvironmentScope(environmentId, AuthSourceControlWriteScope); const status = useEnvironmentQuery( @@ -46,6 +49,7 @@ export function DraftThreadHeader(props: { return ( { - const chains = resolveThreadPullRequestChains(supported ? (pullRequests ?? []) : []); + const chains = resolveThreadPullRequestChains(pullRequests ?? []); const open = chains.filter((chain) => chain.layers.some(isOpenLink)); const done = chains.filter((chain) => !chain.layers.some(isOpenLink)); return [...pullRequestListLines(open), ...pullRequestListLines(done)]; - }, [pullRequests, supported]); + }, [pullRequests]); } function isOpenLink(link: PullRequestListLine["link"]) { diff --git a/apps/mobile/src/features/threads/ThreadGitControls.tsx b/apps/mobile/src/features/threads/ThreadGitControls.tsx index 57d26e3b65d0c..7a1115fe1f2f9 100644 --- a/apps/mobile/src/features/threads/ThreadGitControls.tsx +++ b/apps/mobile/src/features/threads/ThreadGitControls.tsx @@ -26,6 +26,8 @@ import { type TerminalMenuSession, } from "../terminal/terminalMenu"; import { projectScriptMenuLabel } from "@t3tools/shared/projectScripts"; +import type { ThreadPr } from "../../state/thread-pr-presentation"; +import { resolveThreadGitViewablePr, resolveThreadGitViewPrItem } from "./threadGitMenu"; function truncateMiddle(value: string, maxLength: number): string { if (value.length <= maxLength) { @@ -83,6 +85,8 @@ export type ThreadGitMenuProps = { readonly currentBranch: string | null; readonly hasThread?: boolean; readonly gitStatus: VcsStatusResult | null; + /** Open PR linked to the thread, possibly on another ref; only adds a View PR row. */ + readonly linkedPullRequest?: ThreadPr | null; readonly gitOperationLabel: string | null; readonly onOpenFilesInspector?: () => void; readonly onOpenGitInspector?: () => void; @@ -153,11 +157,13 @@ function useThreadGitControlModel(props: ThreadGitMenuProps) { return "arrow.up.right.circle"; })(); + const viewablePr = resolveThreadGitViewablePr(gitStatus, props.linkedPullRequest ?? null); + const openExistingPr = async () => { const { shortLabel, singular } = sourceControlClients.get( gitStatus?.sourceControlProvider?.kind, ).changeRequest; - const prUrl = gitStatus?.pr?.state === "open" ? gitStatus.pr.url : null; + const prUrl = viewablePr?.url ?? null; if (!prUrl) { Dialog.alert(`No open ${shortLabel}`, `This branch does not have an open ${singular}.`); return; @@ -239,9 +245,11 @@ function useThreadGitControlModel(props: ThreadGitMenuProps) { return { currentBranchLabel, isRepo, + openExistingPr, openGitInspector, openReview, quickAction, + viewablePr, quickActionHint, quickActionIcon, runQuickAction, @@ -304,6 +312,11 @@ export function threadTerminalMenu(props: ThreadGitControlsProps): AppBarMenu | /** The thread's git menu, shared by the thread bar and Review. */ export function useThreadGitMenuDefinition(props: ThreadGitMenuProps): AppBarMenu | null { const model = useThreadGitControlModel(props); + const viewPr = resolveThreadGitViewPrItem( + props.gitStatus, + model.viewablePr, + model.quickAction.kind, + ); return { title: "Git controls", icon: "point.topleft.down.curvedto.point.bottomright.up", @@ -327,6 +340,19 @@ export function useThreadGitMenuDefinition(props: ThreadGitMenuProps): AppBarMen void model.runQuickAction(); }, }, + ...(viewPr + ? [ + { + id: "git-view-pr", + title: viewPr.title, + icon: PullRequestGlyph.pullRequest, + subtitle: viewPr.subtitle, + onPress: () => { + void model.openExistingPr(); + }, + }, + ] + : []), { id: "git-review", title: "Review changes", diff --git a/apps/mobile/src/features/threads/ThreadHeader.tsx b/apps/mobile/src/features/threads/ThreadHeader.tsx index d0045bf5f284e..68779f948e4b5 100644 --- a/apps/mobile/src/features/threads/ThreadHeader.tsx +++ b/apps/mobile/src/features/threads/ThreadHeader.tsx @@ -1,3 +1,5 @@ +import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell"; +import { useMemo } from "react"; import { AppBar } from "../../components/ui/AppBar"; import type { AppBarAction, AppBarMenu } from "../../components/ui/AppBar.types"; import { @@ -6,6 +8,12 @@ import { } from "../layout/AdaptiveWorkspaceLayout"; import type { ThreadInspectorMode } from "./thread-inspector-navigation"; import { useWorkspaceBackNavigation } from "../layout/workspace/useAndroidWorkspaceNavigation"; +import { PullRequestGlyph } from "../pull-requests/pullRequestGlyphs"; +import { + presentThreadLinkedPullRequests, + resolveThreadPrBadgeAction, +} from "../../state/thread-pr-presentation"; +import { useOpenThreadPullRequests } from "./use-open-thread-pull-requests"; import { threadTerminalMenu, useThreadGitMenuDefinition, @@ -15,6 +23,7 @@ import { export function ThreadHeader(props: { readonly title: string; readonly subtitle: string; + readonly thread: EnvironmentThreadShell; readonly gitControls: ThreadGitControlsProps; readonly onReturnToThread?: () => void; readonly draft?: boolean; @@ -27,19 +36,36 @@ export function ThreadHeader(props: { const { panes } = useWorkspacePaneState(); const gitMenu = useThreadGitMenuDefinition(props.gitControls); const terminalMenu = threadTerminalMenu(props.gitControls); + const openThreadPullRequests = useOpenThreadPullRequests(); + const linkedPullRequest = useMemo( + () => presentThreadLinkedPullRequests(props.thread.pullRequests), + [props.thread.pullRequests], + ); const filesVisible = props.inspectorMode === "files" && panes.auxiliaryPaneVisible; - const actions: AppBarAction[] = props.hasThreadCwd - ? [ - { - accessibilityLabel: filesVisible ? "Close files" : "Open files", - icon: "folder", - selected: filesVisible, - disabled: !props.gitControls.canOpenFiles, - separateBackground: false, - onPress: filesVisible ? toggleAuxiliaryPane : props.onOpenFilesInspector, - }, - ] - : []; + const actions: AppBarAction[] = [ + ...(props.hasThreadCwd + ? [ + { + accessibilityLabel: filesVisible ? "Close files" : "Open files", + icon: "folder" as const, + selected: filesVisible, + disabled: !props.gitControls.canOpenFiles, + separateBackground: false, + onPress: filesVisible ? toggleAuxiliaryPane : props.onOpenFilesInspector, + }, + ] + : []), + ...(linkedPullRequest !== null + ? [ + { + accessibilityLabel: resolveThreadPrBadgeAction(linkedPullRequest).accessibilityLabel, + icon: PullRequestGlyph.pullRequest, + separateBackground: false, + onPress: () => openThreadPullRequests(props.thread, linkedPullRequest), + }, + ] + : []), + ]; const menus = [gitMenu, terminalMenu].filter((menu): menu is AppBarMenu => menu !== null); return ( diff --git a/apps/mobile/src/features/threads/ThreadPullRequestsSheet.tsx b/apps/mobile/src/features/threads/ThreadPullRequestsSheet.tsx index dd20c0a162f7b..81629ff60e875 100644 --- a/apps/mobile/src/features/threads/ThreadPullRequestsSheet.tsx +++ b/apps/mobile/src/features/threads/ThreadPullRequestsSheet.tsx @@ -23,7 +23,7 @@ export function ThreadPullRequestsSheet( const insets = useSafeAreaInsets(); const { selectedThread, selectedThreadProject, selectedEnvironmentRuntime } = useThreadSelection(); - const lines = useLinkedPullRequestLines(selectedThread, selectedEnvironmentRuntime); + const lines = useLinkedPullRequestLines(selectedThread); const openCount = useMemo( () => lines.filter((line) => line.link.snapshot === null || line.link.snapshot.state === "open") diff --git a/apps/mobile/src/features/threads/ThreadRouteScreen.tsx b/apps/mobile/src/features/threads/ThreadRouteScreen.tsx index ab0b5cdb9bd4d..9e81c72c666e7 100644 --- a/apps/mobile/src/features/threads/ThreadRouteScreen.tsx +++ b/apps/mobile/src/features/threads/ThreadRouteScreen.tsx @@ -109,6 +109,7 @@ import { threadRouteOpensDraft, } from "./thread-route-hydration"; import { ThreadHeader } from "./ThreadHeader"; +import { resolveThreadOpenLinkedPullRequest } from "../../state/thread-pr-presentation"; import { resumeThread } from "./resume-thread"; import { useAndroidWorkspaceBack, @@ -784,6 +785,10 @@ function ThreadRouteContent( : undefined, currentBranch: selectedThread?.branch ?? null, gitStatus: gitStatus.data, + linkedPullRequest: resolveThreadOpenLinkedPullRequest( + selectedThread?.pullRequests ?? [], + routeEnvironmentRuntime?.serverConfig?.environment.capabilities.threadPullRequests === true, + ), gitOperationLabel: gitState.gitOperationLabel, canOpenTerminal: Boolean(selectedThreadProject?.workspaceRoot) && (canReadTerminal || canOperateTerminal), @@ -958,6 +963,7 @@ function ThreadRouteContent( = {}): VcsStatusResult { + return { + isRepo: true, + hasPrimaryRemote: true, + isDefaultRef: false, + refName: "feature/test", + hasWorkingTreeChanges: false, + workingTree: { files: [], insertions: 0, deletions: 0 }, + hasUpstream: true, + aheadCount: 0, + behindCount: 0, + pr: null, + ...overrides, + }; +} + +const openPr = { + number: 42, + title: "Feature", + url: "https://github.com/t3tools/t3code/pull/42", + baseRef: "main", + headRef: "feature/test", + state: "open" as const, +}; + +describe("thread git menu View PR row", () => { + it("stays out when the quick action is already View PR", () => { + const gitStatus = status({ pr: openPr }); + expect(resolveQuickAction(gitStatus, false).kind).toBe("open_pr"); + expect( + resolveThreadGitViewPrItem(gitStatus, resolveThreadGitViewablePr(gitStatus, null), "open_pr"), + ).toBeNull(); + }); + + it("keeps View PR in the first git menu when the quick action is Commit & push", () => { + const gitStatus = status({ + pr: openPr, + hasWorkingTreeChanges: true, + workingTree: { + files: [{ path: "a.ts", insertions: 1, deletions: 0 }], + insertions: 1, + deletions: 0, + }, + }); + expect(resolveQuickAction(gitStatus, false).kind).toBe("run_action"); + expect( + resolveThreadGitViewPrItem( + gitStatus, + resolveThreadGitViewablePr(gitStatus, null), + "run_action", + ), + ).toEqual({ title: "View PR", subtitle: "PR #42" }); + }); + + it("adds the linked PR beside Push & create PR for a branch without its own PR", () => { + const gitStatus = status({ refName: "feature-b", aheadCount: 1 }); + const linked = { ...openPr, number: 7, headRef: "feature-a" }; + const quickAction = resolveQuickAction(gitStatus, false); + expect(quickAction.label).toBe("Push & create PR"); + const viewable = resolveThreadGitViewablePr(gitStatus, linked); + expect(viewable).toBe(linked); + expect(resolveThreadGitViewPrItem(gitStatus, viewable, quickAction.kind)).toEqual({ + title: "View PR", + subtitle: "PR #7", + }); + }); + + it("prefers the checkout's own open PR over a linked one", () => { + const gitStatus = status({ pr: openPr }); + expect(resolveThreadGitViewablePr(gitStatus, { ...openPr, number: 7 })).toBe(gitStatus.pr); + }); + + it("omits the row when there is no open pull request", () => { + expect(resolveThreadGitViewPrItem(status(), null, "show_hint")).toBeNull(); + }); +}); diff --git a/apps/mobile/src/features/threads/threadGitMenu.ts b/apps/mobile/src/features/threads/threadGitMenu.ts new file mode 100644 index 0000000000000..db104b5676f25 --- /dev/null +++ b/apps/mobile/src/features/threads/threadGitMenu.ts @@ -0,0 +1,29 @@ +import { + changeRequestTerminologyFor, + type GitQuickAction, +} from "@t3tools/client-runtime/state/vcs"; +import type { VcsStatusResult } from "@t3tools/contracts"; + +import type { ThreadPr } from "../../state/thread-pr-presentation"; + +/** The checkout's open PR, else the thread's open linked PR; never feeds git action decisions. */ +export function resolveThreadGitViewablePr( + gitStatus: VcsStatusResult | null, + linkedPullRequest: ThreadPr | null, +): ThreadPr | null { + return gitStatus?.pr?.state === "open" ? gitStatus.pr : linkedPullRequest; +} + +/** Own row when a viewable PR exists and the quick action is not already View PR. */ +export function resolveThreadGitViewPrItem( + gitStatus: VcsStatusResult | null, + pr: ThreadPr | null, + quickActionKind: GitQuickAction["kind"], +): { readonly title: string; readonly subtitle: string } | null { + if (pr === null || quickActionKind === "open_pr") return null; + const { shortLabel } = changeRequestTerminologyFor(gitStatus); + return { + title: `View ${shortLabel}`, + subtitle: `${shortLabel} #${pr.number}`, + }; +} diff --git a/apps/mobile/src/state/thread-pr-presentation.ts b/apps/mobile/src/state/thread-pr-presentation.ts index a44f410b54ca3..4f7d14f06ae4f 100644 --- a/apps/mobile/src/state/thread-pr-presentation.ts +++ b/apps/mobile/src/state/thread-pr-presentation.ts @@ -133,6 +133,28 @@ export function resolveThreadPrBadgeAction(pr: ThreadPrPresentation) { : { kind: "open" as const, accessibilityLabel: `Open ${pr.accessibilityLabel}` }; } +/** The thread's open linked PR; unsynced links count only where the server syncs snapshots. */ +export function resolveThreadOpenLinkedPullRequest( + links: ReadonlyArray, + supportsSnapshots: boolean, +): ThreadPr | null { + const link = resolveThreadCurrentPullRequestLink(links); + const snapshot = link?.snapshot ?? null; + if (link === null || (snapshot === null ? !supportsSnapshots : snapshot.state !== "open")) { + return null; + } + return { + number: link.number, + title: snapshot?.title ?? `#${link.number}`, + url: link.url, + baseRef: snapshot?.baseBranch ?? "unknown", + headRef: snapshot?.headBranch ?? "unknown", + state: "open", + isDraft: snapshot?.isDraft === true, + updatedAt: snapshot?.updatedAt ?? null, + }; +} + /** Records `value` as the newest entry, evicting the least recently answered beyond `limit`. */ export function rememberBounded( current: ReadonlyMap, diff --git a/apps/mobile/src/state/use-thread-pr.test.ts b/apps/mobile/src/state/use-thread-pr.test.ts index 4edbb903dbebf..312cca5a8d3ce 100644 --- a/apps/mobile/src/state/use-thread-pr.test.ts +++ b/apps/mobile/src/state/use-thread-pr.test.ts @@ -4,6 +4,7 @@ import { SourceControlProviderKind, type ThreadPullRequestLink, } from "@t3tools/contracts"; +import { threadPullRequestsOf } from "@t3tools/client-runtime/thread-pull-requests"; import { describe, expect, it } from "vite-plus/test"; import { @@ -11,6 +12,7 @@ import { presentThreadLinkedPullRequests, rememberBounded, resolveThreadPrSource, + resolveThreadOpenLinkedPullRequest, } from "./thread-pr-presentation"; function linkedPr( @@ -175,3 +177,45 @@ describe("rememberBounded", () => { ]); }); }); + +describe("resolveThreadOpenLinkedPullRequest", () => { + it("returns the linked open pull request", () => { + expect(resolveThreadOpenLinkedPullRequest([linkedPr(42)], true)).toMatchObject({ + number: 42, + url: linkedPr(42).url, + state: "open", + }); + }); + + it("treats a link waiting for its first sync as open", () => { + expect( + resolveThreadOpenLinkedPullRequest([linkedPr(42, { snapshot: null })], true), + ).toMatchObject({ number: 42, state: "open" }); + }); + + it("ignores a legacy link whose state the server cannot report", () => { + const legacy = threadPullRequestsOf({ + linkedPullRequest: { + projectId: ProjectId.make("project"), + repository: "t3tools/t3code", + number: 42, + url: linkedPr(42).url, + }, + }); + expect(resolveThreadOpenLinkedPullRequest(legacy, false)).toBeNull(); + }); + + it("ignores links that are no longer open", () => { + const merged = linkedPr(42, { snapshot: { ...linkedPr(42).snapshot!, state: "merged" } }); + const closed = linkedPr(43, { snapshot: { ...linkedPr(43).snapshot!, state: "closed" } }); + expect(resolveThreadOpenLinkedPullRequest([merged, closed], true)).toBeNull(); + }); + + it("prefers an open link over a merged one", () => { + const merged = linkedPr(41, { + linkedAt: "2026-09-10T00:00:00.000Z", + snapshot: { ...linkedPr(41).snapshot!, state: "merged" }, + }); + expect(resolveThreadOpenLinkedPullRequest([merged, linkedPr(42)], true)?.number).toBe(42); + }); +});