Skip to content

Commit dbc77ac

Browse files
committed
feat(mobile): link, unlink and watch a thread's pull requests from its sheet
1 parent 8baaf24 commit dbc77ac

12 files changed

Lines changed: 448 additions & 189 deletions

‎apps/mobile/src/features/threads/LinkedPullRequestList.tsx‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ import { Pressable, View } from "react-native";
1111
import { SymbolView } from "../../components/ui/AppSymbol";
1212
import { AppText as Text } from "../../components/ui/AppText";
1313
import * as Dialog from "../../components/ui/Dialog";
14+
import { Menu } from "../../components/ui/Menu";
1415
import { cn } from "../../lib/cn";
1516
import { relativeTime } from "../../lib/time";
1617
import type { EnvironmentRuntimeState } from "../../state/remote-runtime-types";
1718
import { PullRequestGlyph } from "../pull-requests/pullRequestGlyphs";
19+
import type { LinkedPullRequestActions } from "./useLinkedPullRequestActions";
1820
import { pullRequestListReviewStatus } from "../pull-requests/pullRequestList";
1921
import {
2022
PullRequestAvatar,
@@ -54,6 +56,7 @@ export const LinkedPullRequestRow = memo(function LinkedPullRequestRow(props: {
5456
readonly separator: boolean;
5557
readonly surfaceClassName: string;
5658
readonly openPullRequest: (url: string) => Promise<boolean>;
59+
readonly actions: LinkedPullRequestActions | null;
5760
}) {
5861
const { link, depth, stack } = props.line;
5962
const snapshot = link.snapshot;
@@ -72,18 +75,19 @@ export const LinkedPullRequestRow = memo(function LinkedPullRequestRow(props: {
7275
const watching = open && link.watch !== undefined;
7376
const additions = snapshot?.additions ?? 0;
7477
const deletions = snapshot?.deletions ?? 0;
75-
return (
78+
const row = (
7679
<Pressable
7780
accessibilityRole="button"
7881
accessibilityLabel={`Pull request ${link.number}, ${snapshot?.title ?? "waiting for host state"}${status ? `, ${status.label}` : ""}${conflict ? `, ${conflict.label.toLowerCase()}` : ""}, ${link.repository}${stack ? `, stack of ${stack.size}` : ""}${watching ? ", watching" : ""}${checks ? `, checks ${snapshot?.checksState}` : ""}${review ? `, ${review.accessibleLabel}` : ""}`}
82+
accessibilityHint={props.actions ? "Touch and hold for actions." : undefined}
7983
onPress={() => {
8084
void props.openPullRequest(link.url).then((opened) => {
8185
if (!opened) Dialog.alert("Unable to open PR", "The pull request could not be opened.");
8286
});
8387
}}
8488
// Steps in per layer like the web list, capped so deep stacks keep their width.
8589
style={{ paddingLeft: 16 + Math.min(depth, 3) * 16 }}
86-
className="flex-row gap-3 pr-4 active:bg-row-hover"
90+
className="flex-row gap-3 rounded-xl pr-4 active:bg-row-hover"
8791
>
8892
{depth > 0 ? <View className="-ml-2 w-px self-stretch bg-border" /> : null}
8993
<View className="self-start pt-2.5">
@@ -145,4 +149,16 @@ export const LinkedPullRequestRow = memo(function LinkedPullRequestRow(props: {
145149
</View>
146150
</Pressable>
147151
);
152+
const actions = props.actions;
153+
if (actions === null) return row;
154+
return (
155+
<Menu
156+
shouldOpenOnLongPress
157+
title={`#${link.number} · ${link.repository}`}
158+
actions={actions.menuActions(link)}
159+
onPressAction={({ nativeEvent }) => actions.onMenuAction(link, nativeEvent.event)}
160+
>
161+
{row}
162+
</Menu>
163+
);
148164
});

‎apps/mobile/src/features/threads/ThreadPullRequestsSheet.tsx‎

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
linkedPullRequestKey,
2020
useLinkedPullRequestLines,
2121
} from "./LinkedPullRequestList";
22+
import { useLinkedPullRequestActions } from "./useLinkedPullRequestActions";
2223

2324
const HEADER_SCROLL_EDGE_EFFECTS = nativeHeaderScrollEdgeEffects(Platform.OS, Platform.Version);
2425
const TITLE = "Linked pull requests";
@@ -29,7 +30,8 @@ export function ThreadPullRequestsSheet(
2930
const { layout } = useAdaptiveWorkspaceLayout();
3031
const navigation = useNavigation();
3132
const insets = useSafeAreaInsets();
32-
const { selectedThread, selectedEnvironmentRuntime } = useThreadSelection();
33+
const { selectedThread, selectedThreadProject, selectedEnvironmentRuntime } =
34+
useThreadSelection();
3335
const lines = useLinkedPullRequestLines(selectedThread, selectedEnvironmentRuntime);
3436
const openCount = useMemo(
3537
() =>
@@ -41,19 +43,26 @@ export function ThreadPullRequestsSheet(
4143
replace: true,
4244
context: selectedThread ?? undefined,
4345
});
46+
const actions = useLinkedPullRequestActions({
47+
thread: selectedThread,
48+
project: selectedThreadProject,
49+
runtime: selectedEnvironmentRuntime,
50+
});
51+
const rowInputs = useMemo(() => ({ lines, actions }), [lines, actions]);
4452

4553
const content = (
4654
<View className="flex-1 android:bg-sheet-solid ios:bg-screen">
4755
<LegendList
4856
data={lines}
4957
keyExtractor={linkedPullRequestKey}
50-
extraData={lines}
58+
extraData={rowInputs}
5159
renderItem={({ item, index }) => (
5260
<LinkedPullRequestRow
5361
line={item}
5462
separator={index < lines.length - 1}
5563
surfaceClassName={Platform.OS === "ios" ? "text-screen" : "text-sheet-solid"}
5664
openPullRequest={openPullRequest}
65+
actions={actions}
5766
/>
5867
)}
5968
estimatedItemSize={88}
@@ -62,7 +71,10 @@ export function ThreadPullRequestsSheet(
6271
style={{ flex: 1 }}
6372
contentInsetAdjustmentBehavior={Platform.OS === "ios" ? "automatic" : "never"}
6473
showsVerticalScrollIndicator={false}
65-
contentContainerStyle={{ paddingBottom: Math.max(insets.bottom, 18) + 18 }}
74+
contentContainerStyle={{
75+
paddingBottom: Math.max(insets.bottom, 18) + 18,
76+
paddingHorizontal: 8,
77+
}}
6678
ListHeaderComponent={
6779
lines.length > 0 ? (
6880
<Text className="px-4 pt-2 pb-1 text-xs text-foreground-muted">
@@ -105,6 +117,19 @@ export function ThreadPullRequestsSheet(
105117
titleFontSize={18}
106118
titleFontWeight="800"
107119
translucent
120+
headerRightBarButtonItems={
121+
actions.canLink
122+
? [
123+
{
124+
type: "button",
125+
accessibilityLabel: "Link pull request",
126+
icon: { name: "plus", type: "sfSymbol" },
127+
identifier: "thread-pull-requests-link",
128+
onPress: actions.promptLink,
129+
},
130+
]
131+
: undefined
132+
}
108133
/>
109134
</NativeScreen>
110135
</ScreenStack>
@@ -115,7 +140,22 @@ export function ThreadPullRequestsSheet(
115140
return (
116141
<View collapsable={false} className="flex-1 bg-header">
117142
<NativeStackScreenOptions options={{ sheetCornerRadius: 28 }} />
118-
<AndroidSheetHeader title={TITLE} hideBottomBorder onBack={() => navigation.goBack()} />
143+
<AndroidSheetHeader
144+
title={TITLE}
145+
hideBottomBorder
146+
onBack={() => navigation.goBack()}
147+
actions={
148+
actions.canLink
149+
? [
150+
{
151+
accessibilityLabel: "Link pull request",
152+
icon: "plus",
153+
onPress: actions.promptLink,
154+
},
155+
]
156+
: undefined
157+
}
158+
/>
119159
<ScreenContent insetHorizontal={layout.usesSplitView}>{content}</ScreenContent>
120160
</View>
121161
);

‎apps/mobile/src/features/threads/git/GitOverviewSheet.tsx‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import {
4646
linkedPullRequestKey,
4747
useLinkedPullRequestLines,
4848
} from "../LinkedPullRequestList";
49+
import { useLinkedPullRequestActions } from "../useLinkedPullRequestActions";
4950

5051
const HEADER_SCROLL_EDGE_EFFECTS = nativeHeaderScrollEdgeEffects(Platform.OS, Platform.Version);
5152

@@ -76,13 +77,19 @@ function GitOverviewSheetContent(props: GitOverviewSheetProps) {
7677
const isInspector = presentation === "inspector";
7778
const environmentId = EnvironmentId.make(props.route.params.environmentId);
7879
const threadId = ThreadId.make(props.route.params.threadId);
79-
const { selectedThread, selectedEnvironmentRuntime } = useThreadSelection();
80+
const { selectedThread, selectedThreadProject, selectedEnvironmentRuntime } =
81+
useThreadSelection();
8082
const openPullRequest = useOpenPullRequest({
8183
replace: presentation === "sheet",
8284
context: selectedThread ?? undefined,
8385
});
8486
const { selectedThreadCwd, selectedThreadWorktreePath } = useSelectedThreadWorktree();
8587
const linkedPrLines = useLinkedPullRequestLines(selectedThread, selectedEnvironmentRuntime);
88+
const linkedPrActions = useLinkedPullRequestActions({
89+
thread: selectedThread,
90+
project: selectedThreadProject,
91+
runtime: selectedEnvironmentRuntime,
92+
});
8693
const gitState = useSelectedThreadGitState();
8794
const gitActions = useSelectedThreadGitActions();
8895
const { canWriteSourceControl, canChangeThreadBranch } = gitActions;
@@ -345,6 +352,17 @@ function GitOverviewSheetContent(props: GitOverviewSheetProps) {
345352
})
346353
}
347354
/>
355+
{linkedPrActions.canLink ? (
356+
<>
357+
{Platform.OS !== "android" ? <View className="ml-12 h-px bg-border" /> : null}
358+
<SheetListRow
359+
icon="link"
360+
title="Link pull request"
361+
subtitle="Attach a pull request by URL or number"
362+
onPress={linkedPrActions.promptLink}
363+
/>
364+
</>
365+
) : null}
348366
</View>
349367

350368
{linkedPrLines.length > 0 ? (
@@ -360,6 +378,7 @@ function GitOverviewSheetContent(props: GitOverviewSheetProps) {
360378
separator={index < linkedPrLines.length - 1}
361379
surfaceClassName="text-card"
362380
openPullRequest={openPullRequest}
381+
actions={linkedPrActions}
363382
/>
364383
))}
365384
</View>
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
import {
2+
pullRequestLinkProject,
3+
resolveLinkPullRequestInput,
4+
} from "@t3tools/client-runtime/pull-request-link-input";
5+
import type {
6+
EnvironmentProject,
7+
EnvironmentThreadShell,
8+
} from "@t3tools/client-runtime/state/shell";
9+
import {
10+
isAtomCommandInterrupted,
11+
squashAtomCommandFailure,
12+
type AtomCommandResult,
13+
} from "@t3tools/client-runtime/state/runtime";
14+
import { planThreadPullRequestMutation } from "@t3tools/client-runtime/thread-pull-request-compatibility";
15+
import { AuthOrchestrationOperateScope, type ThreadPullRequestLink } from "@t3tools/contracts";
16+
import * as Clipboard from "expo-clipboard";
17+
import { useCallback, useMemo } from "react";
18+
import { Linking } from "react-native";
19+
20+
import * as Dialog from "../../components/ui/Dialog";
21+
import type { MenuProps } from "../../components/ui/Menu.types";
22+
import type { EnvironmentRuntimeState } from "../../state/remote-runtime-types";
23+
import { useEnvironmentScope } from "../../state/session";
24+
import { threadEnvironment } from "../../state/threads";
25+
import { useAtomCommand } from "../../state/use-atom-command";
26+
27+
type MenuAction = MenuProps["actions"][number];
28+
29+
export function useLinkedPullRequestActions(input: {
30+
readonly thread: EnvironmentThreadShell | null;
31+
readonly project: EnvironmentProject | null;
32+
readonly runtime: EnvironmentRuntimeState | null;
33+
}) {
34+
const { thread, project, runtime } = input;
35+
const capabilities = runtime?.serverConfig?.environment.capabilities;
36+
const canOperate = useEnvironmentScope(
37+
thread?.environmentId ?? null,
38+
AuthOrchestrationOperateScope,
39+
);
40+
const canEdit = canOperate && capabilities?.threadPullRequests === true;
41+
const canWatch = canOperate && capabilities?.threadPullRequestWatch === true;
42+
const canStartWatch =
43+
canWatch &&
44+
thread !== null &&
45+
thread.archivedAt === null &&
46+
thread.settledAt === null &&
47+
thread.settledOverride !== "settled" &&
48+
thread.lineage.relationshipToParent !== "subagent";
49+
const link = useAtomCommand(threadEnvironment.linkPullRequest, { reportFailure: false });
50+
const unlink = useAtomCommand(threadEnvironment.unlinkPullRequest, { reportFailure: false });
51+
const watch = useAtomCommand(threadEnvironment.watchPullRequest, { reportFailure: false });
52+
53+
const run = useCallback(
54+
async (failureTitle: string, pending: Promise<AtomCommandResult<unknown, unknown>>) => {
55+
const result = await pending;
56+
if (result._tag === "Success" || isAtomCommandInterrupted(result)) return;
57+
const cause = squashAtomCommandFailure(result);
58+
Dialog.alert(failureTitle, cause instanceof Error ? cause.message : "Try again.");
59+
},
60+
[],
61+
);
62+
63+
const setLinked = useCallback(
64+
(
65+
reference: { host: string; repository: string; number: number; url: string },
66+
linked: boolean,
67+
) => {
68+
if (thread === null) return;
69+
const mutation = planThreadPullRequestMutation({
70+
capabilities,
71+
threadId: thread.id,
72+
reference,
73+
legacyProjectId: null,
74+
linked,
75+
});
76+
if (mutation === null || mutation.type === "thread.meta.update") return;
77+
const environmentId = thread.environmentId;
78+
void run(
79+
linked ? "Couldn't link this pull request" : "Couldn't unlink this pull request",
80+
mutation.type === "thread.pull-request.link"
81+
? link({ environmentId, input: mutation.input })
82+
: unlink({ environmentId, input: mutation.input }),
83+
);
84+
},
85+
[capabilities, link, run, thread, unlink],
86+
);
87+
88+
const promptLink = useCallback(() => {
89+
Dialog.prompt({
90+
title: "Link pull request",
91+
initialValue: "",
92+
confirmText: "Link",
93+
onConfirm: (reference) => {
94+
const resolved = resolveLinkPullRequestInput({
95+
reference,
96+
project: pullRequestLinkProject(project?.repositoryIdentity),
97+
hasProject: () => true,
98+
});
99+
if (resolved === null) {
100+
Dialog.alert("Couldn't link this pull request", "Use a pull request URL, 123, or #123.");
101+
return;
102+
}
103+
if ("error" in resolved) {
104+
Dialog.alert("Couldn't link this pull request", resolved.error);
105+
return;
106+
}
107+
setLinked(resolved.link, true);
108+
},
109+
});
110+
}, [project, setLinked]);
111+
112+
const menuActions = useCallback(
113+
(link: ThreadPullRequestLink): MenuAction[] => {
114+
const open = link.snapshot === null || link.snapshot.state === "open";
115+
const watching = link.watch !== undefined;
116+
return [
117+
{ id: "copy", title: "Copy link", image: "link" },
118+
{ id: "browser", title: "Open in browser", image: "safari" },
119+
...((watching ? canWatch : canStartWatch) && open
120+
? [
121+
{
122+
id: "watch",
123+
title: watching ? "Stop watching" : "Watch for changes",
124+
image: watching ? "eye.slash" : "eye",
125+
},
126+
]
127+
: []),
128+
...(canEdit
129+
? [
130+
{
131+
id: "unlink",
132+
title: link.source === "stack" ? "Dismiss from thread" : "Unlink from thread",
133+
image: "xmark",
134+
attributes: { destructive: true },
135+
},
136+
]
137+
: []),
138+
];
139+
},
140+
[canEdit, canStartWatch, canWatch],
141+
);
142+
143+
const onMenuAction = useCallback(
144+
(link: ThreadPullRequestLink, action: string) => {
145+
if (action === "copy") void Clipboard.setStringAsync(link.url);
146+
else if (action === "browser") void Linking.openURL(link.url);
147+
else if (action === "watch" && thread !== null)
148+
void run(
149+
"Couldn't change watching",
150+
watch({
151+
environmentId: thread.environmentId,
152+
input: {
153+
threadId: thread.id,
154+
host: link.host,
155+
repository: link.repository,
156+
number: link.number,
157+
watching: link.watch === undefined,
158+
},
159+
}),
160+
);
161+
else if (action === "unlink") setLinked(link, false);
162+
},
163+
[run, setLinked, thread, watch],
164+
);
165+
166+
return useMemo(
167+
() => ({ canLink: canEdit, promptLink, menuActions, onMenuAction }),
168+
[canEdit, promptLink, menuActions, onMenuAction],
169+
);
170+
}
171+
172+
export type LinkedPullRequestActions = ReturnType<typeof useLinkedPullRequestActions>;

‎apps/web/src/components/BranchToolbarBranchSelector.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ import { vcsEnvironment } from "../state/vcs";
4444
import { cn } from "../lib/utils";
4545
import { THREAD_DETAILS_PANEL_ICON_CLASS } from "./chat/threadDetailsPanelStyles";
4646
import { ThreadDetailsPrRows } from "./chat/ThreadDetailsPrRows";
47-
import { parsePullRequestReference } from "../pullRequestReference";
47+
import { parsePullRequestReference } from "@t3tools/client-runtime/pull-request-link-input";
4848
import { getSourceControlPresentation } from "../sourceControlPresentation";
4949
import { useComposerMenuProps } from "./chat/composerEventScope";
5050
import {

0 commit comments

Comments
 (0)