Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -27,7 +28,7 @@ export function useReviewHeaderPresentation(props: {
readonly selectedSection: ReviewSectionItem | null;
readonly onRefresh: () => Promise<void>;
}): ReviewHeaderPresentation {
const { selectedThread } = useThreadSelection();
const { selectedThread, selectedEnvironmentRuntime } = useThreadSelection();
const gitState = useSelectedThreadGitState();
const gitActions = useSelectedThreadGitActions();
const gitStatusQuery = useEnvironmentQuery(
Expand All @@ -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,
Expand Down
8 changes: 8 additions & 0 deletions apps/mobile/src/features/threads/DraftThreadHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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(
Expand All @@ -46,6 +49,7 @@ export function DraftThreadHeader(props: {
return (
<ThreadHeader
draft
thread={props.thread}
title="New thread"
subtitle=""
hasThreadCwd={props.cwd !== null}
Expand All @@ -57,6 +61,10 @@ export function DraftThreadHeader(props: {
hasThread: false,
currentBranch: props.thread.branch,
gitStatus: status.data,
linkedPullRequest: resolveThreadOpenLinkedPullRequest(
props.thread.pullRequests,
runtime?.serverConfig?.environment.capabilities.threadPullRequests === true,
),
gitOperationLabel: progress.currentLabel,
canOpenTerminal: false,
canOperateTerminal: false,
Expand Down
12 changes: 4 additions & 8 deletions apps/mobile/src/features/threads/LinkedPullRequestList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import * as Dialog from "../../components/ui/Dialog";
import { Menu } from "../../components/ui/Menu";
import { cn } from "../../lib/cn";
import { RelativeTime } from "../../components/RelativeTime";
import type { EnvironmentRuntimeState } from "../../state/remote-runtime-types";
import { useScaledTextRole } from "../settings/appearance/useScaledTextRole";
import { PullRequestGlyph } from "../pull-requests/pullRequestGlyphs";
import type { LinkedPullRequestActions } from "./useLinkedPullRequestActions";
Expand All @@ -34,18 +33,15 @@ import {
pullRequestStatus,
} from "../pull-requests/pullRequestStatus";

export function useLinkedPullRequestLines(
thread: EnvironmentThreadShell | null,
runtime: EnvironmentRuntimeState | null,
) {
const supported = (runtime?.serverConfig ?? null) !== null;
// Lists the links the thread already has; capabilities only gate changing them.
export function useLinkedPullRequestLines(thread: EnvironmentThreadShell | null) {
const pullRequests = thread?.pullRequests;
return useMemo(() => {
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"]) {
Expand Down
28 changes: 27 additions & 1 deletion apps/mobile/src/features/threads/ThreadGitControls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -239,9 +245,11 @@ function useThreadGitControlModel(props: ThreadGitMenuProps) {
return {
currentBranchLabel,
isRepo,
openExistingPr,
openGitInspector,
openReview,
quickAction,
viewablePr,
quickActionHint,
quickActionIcon,
runQuickAction,
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand Down
50 changes: 38 additions & 12 deletions apps/mobile/src/features/threads/ThreadHeader.tsx
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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,
Expand All @@ -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;
Expand All @@ -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 (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
6 changes: 6 additions & 0 deletions apps/mobile/src/features/threads/ThreadRouteScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,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,
Expand Down Expand Up @@ -811,6 +812,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),
Expand Down Expand Up @@ -1001,6 +1006,7 @@ function ThreadRouteContent(
<ThreadHeader
title={selectedThread.title}
subtitle={threadSyncLabel ?? headerSubtitle}
thread={selectedThread}
gitControls={threadGitControlProps}
hasThreadCwd={selectedThreadCwd !== null}
inspectorMode={inspectorMode}
Expand Down
2 changes: 1 addition & 1 deletion apps/mobile/src/features/threads/git/GitOverviewSheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ function GitOverviewSheetContent(props: GitOverviewSheetProps) {
context: selectedThread ?? undefined,
});
const { selectedThreadCwd, selectedThreadWorktreePath } = useSelectedThreadWorktree();
const linkedPrLines = useLinkedPullRequestLines(selectedThread, selectedEnvironmentRuntime);
const linkedPrLines = useLinkedPullRequestLines(selectedThread);
const linkedPrActions = useLinkedPullRequestActions({
thread: selectedThread,
project: selectedThreadProject,
Expand Down
82 changes: 82 additions & 0 deletions apps/mobile/src/features/threads/threadGitMenu.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import type { VcsStatusResult } from "@t3tools/contracts";
import { resolveQuickAction } from "@t3tools/client-runtime/state/vcs";
import { describe, expect, it } from "vite-plus/test";

import { resolveThreadGitViewablePr, resolveThreadGitViewPrItem } from "./threadGitMenu";

function status(overrides: Partial<VcsStatusResult> = {}): 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();
});
});
Loading
Loading