The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import * as Dialog from "../../../components/ui/Dialog"; |
| 2 | import { useOpenPullRequest } from "../../../native/useOpenPullRequest"; |
| 3 | import { |
| 4 | type GitActionRequestInput, |
| 5 | buildMenuItems, |
| 6 | getGitActionDisabledReason, |
| 7 | requiresDefaultBranchConfirmation, |
| 8 | } from "@t3tools/client-runtime/state/vcs"; |
| 9 | import { EnvironmentId, ThreadId } from "@t3tools/contracts"; |
| 10 | import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients"; |
| 11 | import { changeRequestTermKind } from "../../../lib/changeRequestTerm"; |
| 12 | import { |
| 13 | CommonActions, |
| 14 | StackActions, |
| 15 | useNavigation, |
| 16 | type StaticScreenProps, |
| 17 | } from "@react-navigation/native"; |
| 18 | import { useGT, useMessages } from "gt-react-native"; |
| 19 | import { use, useEffect, useState } from "react"; |
| 20 | import { RefreshControl } from "../../../components/ui/RefreshControl"; |
| 21 | import { NativeScrollView } from "../../../native/NativeScrollViewMarker"; |
| 22 | |
| 23 | import { useSafeAreaInsets } from "react-native-safe-area-context"; |
| 24 | import { AppBar } from "../../../components/ui/AppBar"; |
| 25 | import { ListRow } from "../../../components/ui/ListRow"; |
| 26 | import { ListSection } from "../../../components/ui/ListSection"; |
| 27 | import { groupedListInsets } from "../../../components/ui/listMetrics"; |
| 28 | import { ScreenContent } from "../../../components/ui/ScreenContent"; |
| 29 | import { SheetPane } from "../../../components/ui/SheetPane"; |
| 30 | import { InspectorSheetContext } from "../../layout/workspace-inspector-sheet"; |
| 31 | import { AppText as Text } from "../../../components/ui/AppText"; |
| 32 | import { useEnvironmentQuery } from "../../../state/query"; |
| 33 | import { useThreadSelection } from "../../../state/use-thread-selection"; |
| 34 | import { useSelectedThreadGitActions } from "../../../state/use-selected-thread-git-actions"; |
| 35 | import { useSelectedThreadGitState } from "../../../state/use-selected-thread-git-state"; |
| 36 | import { useSelectedThreadWorktree } from "../../../state/use-selected-thread-worktree"; |
| 37 | import { vcsEnvironment } from "../../../state/vcs"; |
| 38 | import { menuItemIconName, statusSummary } from "./gitSheetComponents"; |
| 39 | import { |
| 40 | LinkedPullRequestRow, |
| 41 | linkedPullRequestKey, |
| 42 | useLinkedPullRequestLines, |
| 43 | } from "../LinkedPullRequestList"; |
| 44 | import { useLinkedPullRequestActions } from "../useLinkedPullRequestActions"; |
| 45 | |
| 46 | type GitOverviewSheetProps = StaticScreenProps<{ |
| 47 | readonly environmentId: string; |
| 48 | readonly threadId: string; |
| 49 | }> & { |
| 50 | readonly presentation?: "sheet" | "inspector"; |
| 51 | }; |
| 52 | |
| 53 | export function GitOverviewSheet(props: GitOverviewSheetProps) { |
| 54 | const navigation = useNavigation(); |
| 55 | const { environmentId, threadId } = props.route.params; |
| 56 | const isBlankLink = environmentId.trim().length === 0 || threadId.trim().length === 0; |
| 57 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- pops the navigator when the route link names a blank environment or thread |
| 58 | useEffect(() => { |
| 59 | if (isBlankLink) navigation.goBack(); |
| 60 | }, [isBlankLink, navigation]); |
| 61 | if (isBlankLink) return null; |
| 62 | return <GitOverviewSheetContent {...props} />; |
| 63 | } |
| 64 | |
| 65 | function GitOverviewSheetContent(props: GitOverviewSheetProps) { |
| 66 | const gt = useGT(); |
| 67 | const m = useMessages(); |
| 68 | const navigation = useNavigation(); |
| 69 | const insets = useSafeAreaInsets(); |
| 70 | const presentation = props.presentation ?? "sheet"; |
| 71 | const isInspector = presentation === "inspector"; |
| 72 | const closeInspectorSheet = use(InspectorSheetContext); |
| 73 | const presentRoute = (present: () => void) => |
| 74 | closeInspectorSheet ? closeInspectorSheet(present) : present(); |
| 75 | const environmentId = EnvironmentId.make(props.route.params.environmentId); |
| 76 | const threadId = ThreadId.make(props.route.params.threadId); |
| 77 | const { selectedThread, selectedThreadProject, selectedEnvironmentRuntime } = |
| 78 | useThreadSelection(); |
| 79 | const openPullRequest = useOpenPullRequest({ |
| 80 | replace: presentation === "sheet", |
| 81 | context: selectedThread ?? undefined, |
| 82 | }); |
| 83 | const { selectedThreadCwd, selectedThreadWorktreePath } = useSelectedThreadWorktree(); |
| 84 | const linkedPrLines = useLinkedPullRequestLines(selectedThread, selectedEnvironmentRuntime); |
| 85 | const linkedPrActions = useLinkedPullRequestActions({ |
| 86 | thread: selectedThread, |
| 87 | project: selectedThreadProject, |
| 88 | runtime: selectedEnvironmentRuntime, |
| 89 | }); |
| 90 | const gitState = useSelectedThreadGitState(); |
| 91 | const gitActions = useSelectedThreadGitActions(); |
| 92 | const { canWriteSourceControl, canChangeThreadBranch } = gitActions; |
| 93 | |
| 94 | const gitStatus = useEnvironmentQuery( |
| 95 | selectedThread !== null && selectedThreadCwd !== null |
| 96 | ? vcsEnvironment.status({ |
| 97 | environmentId: selectedThread.environmentId, |
| 98 | input: { cwd: selectedThreadCwd }, |
| 99 | }) |
| 100 | : null, |
| 101 | ); |
| 102 | |
| 103 | const currentBranchLabel = |
| 104 | gitStatus.data?.refName ?? selectedThread?.branch ?? gt("Detached HEAD"); |
| 105 | const currentStatusSummary = statusSummary(gitStatus.data, m); |
| 106 | const currentWorktreePath = selectedThreadWorktreePath; |
| 107 | const gitOperationLabel = gitState.gitOperationLabel; |
| 108 | const busy = gitOperationLabel !== null; |
| 109 | const isRepo = gitStatus.data?.isRepo ?? true; |
| 110 | const hasPrimaryRemote = gitStatus.data?.hasPrimaryRemote ?? false; |
| 111 | const isDefaultRef = gitStatus.data?.isDefaultRef ?? false; |
| 112 | |
| 113 | const menuItems = isRepo ? buildMenuItems(gitStatus.data, busy, hasPrimaryRemote, m) : []; |
| 114 | |
| 115 | const sheetMenuItems = menuItems.map((item) => ({ |
| 116 | item: { |
| 117 | ...item, |
| 118 | disabled: item.disabled || (!canWriteSourceControl && item.kind !== "open_pr"), |
| 119 | }, |
| 120 | disabledReason: |
| 121 | !canWriteSourceControl && item.kind !== "open_pr" |
| 122 | ? gt("This connection cannot change source control.") |
| 123 | : getGitActionDisabledReason( |
| 124 | { |
| 125 | item, |
| 126 | gitStatus: gitStatus.data, |
| 127 | isBusy: busy, |
| 128 | hasOriginRemote: hasPrimaryRemote, |
| 129 | }, |
| 130 | m, |
| 131 | ), |
| 132 | })); |
| 133 | |
| 134 | const term = changeRequestTermKind( |
| 135 | sourceControlClients.get(gitStatus.data?.sourceControlProvider?.kind).changeRequest, |
| 136 | ); |
| 137 | |
| 138 | const openExistingPr = async () => { |
| 139 | const prUrl = gitStatus.data?.pr?.state === "open" ? gitStatus.data.pr.url : null; |
| 140 | if (!prUrl) { |
| 141 | Dialog.alert( |
| 142 | gt("{term, select, mr {No open MR} cr {No open change request} other {No open PR}}", { |
| 143 | term, |
| 144 | }), |
| 145 | gt( |
| 146 | "{term, select, mr {This branch does not have an open merge request.} cr {This branch does not have an open change request.} other {This branch does not have an open pull request.}}", |
| 147 | { term }, |
| 148 | ), |
| 149 | ); |
| 150 | return; |
| 151 | } |
| 152 | if (!(await openPullRequest(prUrl))) { |
| 153 | Dialog.alert( |
| 154 | gt( |
| 155 | "{term, select, mr {Unable to open MR} cr {Unable to open change request} other {Unable to open PR}}", |
| 156 | { term }, |
| 157 | ), |
| 158 | gt( |
| 159 | "{term, select, mr {The merge request could not be opened.} cr {The change request could not be opened.} other {The pull request could not be opened.}}", |
| 160 | { term }, |
| 161 | ), |
| 162 | ); |
| 163 | } |
| 164 | }; |
| 165 | |
| 166 | const runActionWithPrompt = async (input: GitActionRequestInput) => { |
| 167 | if (!canWriteSourceControl) return; |
| 168 | const confirmableAction = |
| 169 | input.action === "push" || |
| 170 | input.action === "create_pr" || |
| 171 | input.action === "commit_push" || |
| 172 | input.action === "commit_push_pr" |
| 173 | ? input.action |
| 174 | : null; |
| 175 | const branchName = gitStatus.data?.refName; |
| 176 | if ( |
| 177 | branchName && |
| 178 | confirmableAction && |
| 179 | !input.featureBranch && |
| 180 | requiresDefaultBranchConfirmation(input.action, isDefaultRef) |
| 181 | ) { |
| 182 | presentRoute(() => |
| 183 | navigation.navigate("GitConfirm", { |
| 184 | environmentId: String(environmentId), |
| 185 | threadId: String(threadId), |
| 186 | confirmAction: confirmableAction, |
| 187 | branchName, |
| 188 | includesCommit: String( |
| 189 | input.action === "commit_push" || input.action === "commit_push_pr", |
| 190 | ), |
| 191 | }), |
| 192 | ); |
| 193 | return; |
| 194 | } |
| 195 | |
| 196 | // Close a sheet so the thread shows the action's progress. |
| 197 | if (!isInspector) navigation.goBack(); |
| 198 | else closeInspectorSheet?.(); |
| 199 | await gitActions.onRunSelectedThreadGitAction(input); |
| 200 | }; |
| 201 | |
| 202 | const onPressMenuItem = async (item: (typeof menuItems)[number]) => { |
| 203 | if (item.disabled) return; |
| 204 | if (item.kind === "open_pr") { |
| 205 | await openExistingPr(); |
| 206 | return; |
| 207 | } |
| 208 | if (!canWriteSourceControl) return; |
| 209 | if (item.dialogAction === "commit") { |
| 210 | presentRoute(() => |
| 211 | navigation.navigate("GitCommit", { |
| 212 | environmentId: String(environmentId), |
| 213 | threadId: String(threadId), |
| 214 | }), |
| 215 | ); |
| 216 | return; |
| 217 | } |
| 218 | if (item.dialogAction === "push") { |
| 219 | await runActionWithPrompt({ action: "push" }); |
| 220 | return; |
| 221 | } |
| 222 | if (item.dialogAction === "create_pr") { |
| 223 | await runActionWithPrompt({ action: "create_pr" }); |
| 224 | } |
| 225 | }; |
| 226 | |
| 227 | const rowStatusDetail = (item: (typeof menuItems)[number]): string | undefined => { |
| 228 | const status = gitStatus.data; |
| 229 | if (status == null) { |
| 230 | return undefined; |
| 231 | } |
| 232 | if (item.dialogAction === "commit" && status.hasWorkingTreeChanges) { |
| 233 | const fileCount = status.workingTree?.files.length ?? 0; |
| 234 | return gt("{count, plural, one {{count} file changed} other {{count} files changed}}", { |
| 235 | count: fileCount, |
| 236 | }); |
| 237 | } |
| 238 | if (item.dialogAction === "push" && (status.aheadCount ?? 0) > 0) { |
| 239 | const ahead = status.aheadCount ?? 0; |
| 240 | return gt("{count, plural, one {{count} commit ahead} other {{count} commits ahead}}", { |
| 241 | count: ahead, |
| 242 | }); |
| 243 | } |
| 244 | if (item.kind === "open_pr" && status.pr?.number != null) { |
| 245 | return gt( |
| 246 | "{term, select, mr {MR #{number} {state, select, open {open} closed {closed} merged {merged} other {{state}}}} cr {change request #{number} {state, select, open {open} closed {closed} merged {merged} other {{state}}}} other {PR #{number} {state, select, open {open} closed {closed} merged {merged} other {{state}}}}}", |
| 247 | { |
| 248 | term, |
| 249 | number: status.pr.number, |
| 250 | state: status.pr.state ?? "open", |
| 251 | }, |
| 252 | ); |
| 253 | } |
| 254 | return undefined; |
| 255 | }; |
| 256 | |
| 257 | const behindCount = gitStatus.data?.behindCount ?? 0; |
| 258 | |
| 259 | // Background refreshes also report pending; using that query flag would stick the RefreshControl spinner. |
| 260 | const [isRefreshing, setIsRefreshing] = useState(false); |
| 261 | const handleRefresh = async () => { |
| 262 | setIsRefreshing(true); |
| 263 | await gitActions.refreshSelectedThreadGitStatus(); |
| 264 | setIsRefreshing(false); |
| 265 | }; |
| 266 | |
| 267 | const content = ( |
| 268 | <NativeScrollView |
| 269 | nestedScrollEnabled |
| 270 | alwaysBounceVertical |
| 271 | className="flex-1 bg-screen" |
| 272 | contentInsetAdjustmentBehavior="automatic" |
| 273 | showsVerticalScrollIndicator={false} |
| 274 | contentContainerStyle={{ |
| 275 | ...groupedListInsets(isInspector), |
| 276 | paddingTop: 8, |
| 277 | paddingBottom: Math.max(insets.bottom, 18) + 18, |
| 278 | }} |
| 279 | refreshControl={ |
| 280 | <RefreshControl refreshing={isRefreshing} onRefresh={() => void handleRefresh()} /> |
| 281 | } |
| 282 | > |
| 283 | <ListSection> |
| 284 | {sheetMenuItems.map(({ item, disabledReason }) => ( |
| 285 | <ListRow |
| 286 | key={`${item.id}-${item.label}`} |
| 287 | icon={menuItemIconName(item.icon)} |
| 288 | label={item.label} |
| 289 | description={disabledReason ?? rowStatusDetail(item)} |
| 290 | disabled={item.disabled} |
| 291 | onPress={() => void onPressMenuItem(item)} |
| 292 | /> |
| 293 | ))} |
| 294 | {behindCount > 0 ? ( |
| 295 | <ListRow |
| 296 | icon="arrow.down.circle" |
| 297 | label={gt("Pull latest")} |
| 298 | description={ |
| 299 | canWriteSourceControl |
| 300 | ? gt( |
| 301 | "{count, plural, one {{count} commit behind upstream} other {{count} commits behind upstream}}", |
| 302 | { count: behindCount }, |
| 303 | ) |
| 304 | : gt("This connection cannot change source control.") |
| 305 | } |
| 306 | disabled={!canWriteSourceControl || busy || !isRepo} |
| 307 | onPress={() => void gitActions.onPullSelectedThreadBranch()} |
| 308 | /> |
| 309 | ) : null} |
| 310 | <ListRow |
| 311 | icon="text.bubble" |
| 312 | label={gt("Review changes")} |
| 313 | description={gt("Inspect changes, uncommitted edits, and turn diffs")} |
| 314 | disabled={busy || !isRepo} |
| 315 | onPress={() => { |
| 316 | const params = { environmentId, threadId }; |
| 317 | if (presentation === "sheet") { |
| 318 | navigation.dispatch(StackActions.replace("ThreadReview", params)); |
| 319 | } else { |
| 320 | presentRoute(() => |
| 321 | navigation.dispatch(CommonActions.navigate("ThreadReview", params)), |
| 322 | ); |
| 323 | } |
| 324 | }} |
| 325 | /> |
| 326 | <ListRow |
| 327 | icon="point.topleft.down.curvedto.point.bottomright.up" |
| 328 | label={gt("Branches & worktrees")} |
| 329 | description={ |
| 330 | canChangeThreadBranch |
| 331 | ? gt("Switch branch, create branch, or move to a worktree") |
| 332 | : gt("View branches and worktrees") |
| 333 | } |
| 334 | disabled={busy || !isRepo} |
| 335 | onPress={() => |
| 336 | presentRoute(() => |
| 337 | navigation.navigate("GitBranches", { |
| 338 | environmentId: String(environmentId), |
| 339 | threadId: String(threadId), |
| 340 | }), |
| 341 | ) |
| 342 | } |
| 343 | /> |
| 344 | {linkedPrActions.canLink ? ( |
| 345 | <ListRow |
| 346 | icon="link" |
| 347 | label={gt( |
| 348 | "{term, select, mr {Link merge request} cr {Link change request} other {Link pull request}}", |
| 349 | { |
| 350 | term: changeRequestTermKind(linkedPrActions.terminology), |
| 351 | }, |
| 352 | )} |
| 353 | description={gt( |
| 354 | "{term, select, mr {Attach a merge request by URL or number} cr {Attach a change request by URL or number} other {Attach a pull request by URL or number}}", |
| 355 | { |
| 356 | term: changeRequestTermKind(linkedPrActions.terminology), |
| 357 | }, |
| 358 | )} |
| 359 | onPress={linkedPrActions.promptLink} |
| 360 | /> |
| 361 | ) : null} |
| 362 | </ListSection> |
| 363 | |
| 364 | {linkedPrLines.length > 0 ? ( |
| 365 | <ListSection title={gt("Linked {terms}", { terms: linkedPrActions.terminology.plural })}> |
| 366 | {linkedPrLines.map((line, index) => ( |
| 367 | <LinkedPullRequestRow |
| 368 | key={linkedPullRequestKey(line)} |
| 369 | line={line} |
| 370 | separator={index < linkedPrLines.length - 1} |
| 371 | surfaceClassName="text-grouped-card" |
| 372 | openPullRequest={openPullRequest} |
| 373 | actions={linkedPrActions} |
| 374 | /> |
| 375 | ))} |
| 376 | </ListSection> |
| 377 | ) : null} |
| 378 | |
| 379 | {currentWorktreePath ? ( |
| 380 | <ListSection title={gt("Worktree")}> |
| 381 | <Text selectable textRole="footnote" className="p-4 text-foreground-muted"> |
| 382 | {currentWorktreePath} |
| 383 | </Text> |
| 384 | </ListSection> |
| 385 | ) : null} |
| 386 | </NativeScrollView> |
| 387 | ); |
| 388 | |
| 389 | const refresh = { |
| 390 | accessibilityLabel: gt("Refresh repository status"), |
| 391 | disabled: busy || isRefreshing, |
| 392 | onPress: () => void handleRefresh(), |
| 393 | }; |
| 394 | |
| 395 | return ( |
| 396 | <SheetPane surface={isInspector ? "screen" : "sheet"}> |
| 397 | <AppBar |
| 398 | title={currentBranchLabel} |
| 399 | subtitle={currentStatusSummary} |
| 400 | hideBottomBorder={!isInspector} |
| 401 | onClose={isInspector ? undefined : () => navigation.goBack()} |
| 402 | refresh={refresh} |
| 403 | /> |
| 404 | <ScreenContent insetHorizontal={isInspector ? true : undefined}>{content}</ScreenContent> |
| 405 | </SheetPane> |
| 406 | ); |
| 407 | } |