Open History to see the last commit
1import {
2 type GitActionRequestInput,
3 buildMenuItems,
4 getGitActionDisabledReason,
5 requiresDefaultBranchConfirmation,
6} from "@t3tools/client-runtime/state/vcs";
7import type { EnvironmentId, ThreadId } from "@t3tools/contracts";
8import { useLocalSearchParams, useRouter } from "expo-router";
9import { SymbolView } from "expo-symbols";
10import { useCallback, useEffect, useMemo } from "react";
11import { Alert, Pressable, ScrollView, View } from "react-native";
12import { useSafeAreaInsets } from "react-native-safe-area-context";
13import { useThemeColor } from "../../../lib/useThemeColor";
14
15import { AppText as Text } from "../../../components/AppText";
16import { tryOpenExternalUrl } from "../../../lib/openExternalUrl";
17import { buildThreadReviewRoutePath } from "../../../lib/routes";
18import { useEnvironmentQuery } from "../../../state/query";
19import { useThreadSelection } from "../../../state/use-thread-selection";
20import { useSelectedThreadGitActions } from "../../../state/use-selected-thread-git-actions";
21import { useSelectedThreadGitState } from "../../../state/use-selected-thread-git-state";
22import { useSelectedThreadWorktree } from "../../../state/use-selected-thread-worktree";
23import { vcsEnvironment } from "../../../state/vcs";
24import { MetaCard, SheetListRow, menuItemIconName, statusSummary } from "./gitSheetComponents";
25
26export function GitOverviewSheet() {
27 const router = useRouter();
28 const insets = useSafeAreaInsets();
29 const { environmentId, threadId } = useLocalSearchParams<{
30 environmentId: EnvironmentId;
31 threadId: ThreadId;
32 }>();
33 const { selectedThread } = useThreadSelection();
34 const { selectedThreadCwd, selectedThreadWorktreePath } = useSelectedThreadWorktree();
35 const gitState = useSelectedThreadGitState();
36 const gitActions = useSelectedThreadGitActions();
37
38 const iconColor = useThemeColor("--color-icon");
39 const borderColor = useThemeColor("--color-border");
40
41 const gitStatus = useEnvironmentQuery(
42 selectedThread !== null && selectedThreadCwd !== null
43 ? vcsEnvironment.status({
44 environmentId: selectedThread.environmentId,
45 input: { cwd: selectedThreadCwd },
46 })
47 : null,
48 );
49
50 const currentBranchLabel = gitStatus.data?.refName ?? selectedThread?.branch ?? "Detached HEAD";
51 const currentWorktreePath = selectedThreadWorktreePath;
52 const gitOperationLabel = gitState.gitOperationLabel;
53 const busy = gitOperationLabel !== null;
54 const isRepo = gitStatus.data?.isRepo ?? true;
55 const hasPrimaryRemote = gitStatus.data?.hasPrimaryRemote ?? false;
56 const isDefaultRef = gitStatus.data?.isDefaultRef ?? false;
57
58 const menuItems = useMemo(
59 () => (isRepo ? buildMenuItems(gitStatus.data, busy, hasPrimaryRemote) : []),
60 [busy, gitStatus.data, hasPrimaryRemote, isRepo],
61 );
62
63 const sheetMenuItems = useMemo(
64 () =>
65 menuItems.map((item) => ({
66 item,
67 disabledReason: getGitActionDisabledReason({
68 item,
69 gitStatus: gitStatus.data,
70 isBusy: busy,
71 hasOriginRemote: hasPrimaryRemote,
72 }),
73 })),
74 [busy, gitStatus.data, hasPrimaryRemote, menuItems],
75 );
76
77 useEffect(() => {
78 void gitActions.refreshSelectedThreadGitStatus({ quiet: true });
79 }, [gitActions]);
80
81 const openExistingPr = useCallback(async () => {
82 const prUrl = gitStatus.data?.pr?.state === "open" ? gitStatus.data.pr.url : null;
83 if (!prUrl) {
84 Alert.alert("No open PR", "This branch does not have an open pull request.");
85 return;
86 }
87 if (!(await tryOpenExternalUrl(prUrl, "pull-request"))) {
88 Alert.alert("Unable to open PR", "The pull request could not be opened.");
89 }
90 }, [gitStatus.data]);
91
92 const runActionWithPrompt = useCallback(
93 async (input: GitActionRequestInput) => {
94 const confirmableAction =
95 input.action === "push" ||
96 input.action === "create_pr" ||
97 input.action === "commit_push" ||
98 input.action === "commit_push_pr"
99 ? input.action
100 : null;
101 const branchName = gitStatus.data?.refName;
102 if (
103 branchName &&
104 confirmableAction &&
105 !input.featureBranch &&
106 requiresDefaultBranchConfirmation(input.action, isDefaultRef)
107 ) {
108 router.push({
109 pathname: "/threads/[environmentId]/[threadId]/git-confirm",
110 params: {
111 environmentId,
112 threadId,
113 confirmAction: confirmableAction,
114 branchName,
115 includesCommit: String(
116 input.action === "commit_push" || input.action === "commit_push_pr",
117 ),
118 },
119 });
120 return;
121 }
122
123 router.dismiss();
124 await gitActions.onRunSelectedThreadGitAction(input);
125 },
126 [environmentId, gitActions, gitStatus.data, isDefaultRef, router, threadId],
127 );
128
129 const onPressMenuItem = useCallback(
130 async (item: (typeof menuItems)[number]) => {
131 if (item.disabled) return;
132 if (item.kind === "open_pr") {
133 await openExistingPr();
134 return;
135 }
136 if (item.dialogAction === "commit") {
137 router.push({
138 pathname: "/threads/[environmentId]/[threadId]/git/commit",
139 params: { environmentId, threadId },
140 });
141 return;
142 }
143 if (item.dialogAction === "push") {
144 await runActionWithPrompt({ action: "push" });
145 return;
146 }
147 if (item.dialogAction === "create_pr") {
148 await runActionWithPrompt({ action: "create_pr" });
149 }
150 },
151 [environmentId, openExistingPr, router, runActionWithPrompt, threadId],
152 );
153
154 return (
155 <View collapsable={false} className="flex-1 bg-sheet">
156 <View style={{ minHeight: 16, paddingTop: 8 }} />
157
158 <View className="items-center gap-1 px-5 pb-3 pt-4">
159 <Pressable
160 className="absolute right-3 top-4 h-9 w-9 items-center justify-center rounded-full bg-subtle"
161 style={{ zIndex: 1, opacity: busy ? 0.45 : 1 }}
162 disabled={busy}
163 onPress={() => void gitActions.refreshSelectedThreadGitStatus()}
164 >
165 <SymbolView
166 name="arrow.clockwise"
167 size={16}
168 tintColor={iconColor}
169 type="monochrome"
170 weight="medium"
171 />
172 </Pressable>
173 <Text
174 className="text-xs font-t3-bold uppercase text-foreground-muted"
175 style={{ letterSpacing: 1 }}
176 >
177 Branch
178 </Text>
179 <Text className="text-3xl font-t3-bold">{currentBranchLabel}</Text>
180 <Text className="text-foreground-secondary text-sm font-medium leading-[19px]">
181 {statusSummary(gitStatus.data)}
182 </Text>
183 </View>
184
185 <ScrollView
186 showsVerticalScrollIndicator={false}
187 style={{ flex: 1 }}
188 contentInset={{ bottom: Math.max(insets.bottom, 18) + 18 }}
189 contentContainerStyle={{
190 paddingHorizontal: 20,
191 paddingTop: 8,
192 gap: 14,
193 }}
194 >
195 <View className="overflow-hidden rounded-[22px] border border-border bg-card px-4 py-1">
196 {sheetMenuItems.map(({ item, disabledReason }, index) => (
197 <View key={`${item.id}-${item.label}`}>
198 {index > 0 ? (
199 <View className="ml-12 h-px" style={{ backgroundColor: borderColor }} />
200 ) : null}
201 <SheetListRow
202 icon={menuItemIconName(item.icon)}
203 title={item.label}
204 subtitle={disabledReason}
205 disabled={item.disabled}
206 onPress={() => void onPressMenuItem(item)}
207 />
208 </View>
209 ))}
210 {(gitStatus.data?.behindCount ?? 0) > 0 ? (
211 <>
212 <View className="ml-12 h-px" style={{ backgroundColor: borderColor }} />
213 <SheetListRow
214 icon="arrow.down.circle"
215 title="Pull latest"
216 subtitle="Sync this branch with upstream"
217 disabled={busy || !isRepo}
218 onPress={() => void gitActions.onPullSelectedThreadBranch()}
219 />
220 </>
221 ) : null}
222 <View className="ml-12 h-px" style={{ backgroundColor: borderColor }} />
223 <SheetListRow
224 icon="text.bubble"
225 title="Review changes"
226 subtitle="Inspect turn diffs, worktree changes, and base branch diff"
227 disabled={busy || !isRepo}
228 onPress={() => router.push(buildThreadReviewRoutePath({ environmentId, threadId }))}
229 />
230 <View className="ml-12 h-px" style={{ backgroundColor: borderColor }} />
231 <SheetListRow
232 icon="point.topleft.down.curvedto.point.bottomright.up"
233 title="Branches & worktrees"
234 subtitle="Switch branch, create branch, or move to a worktree"
235 disabled={busy || !isRepo}
236 onPress={() =>
237 router.push({
238 pathname: "/threads/[environmentId]/[threadId]/git/branches",
239 params: { environmentId, threadId },
240 })
241 }
242 />
243 </View>
244
245 {currentWorktreePath ? <MetaCard label="Worktree" value={currentWorktreePath} /> : null}
246 </ScrollView>
247 </View>
248 );
249}