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 { msg, useGT, useMessages } from "gt-react-native"; |
| 2 | |
| 3 | import * as Dialog from "../../components/ui/Dialog"; |
| 4 | import { useOpenPullRequest } from "../../native/useOpenPullRequest"; |
| 5 | import type { AppBarMenu } from "../../components/ui/AppBar.types"; |
| 6 | import { |
| 7 | AuthSourceControlWriteScope, |
| 8 | EnvironmentId, |
| 9 | type GitRunStackedActionResult, |
| 10 | type ProjectScript, |
| 11 | ThreadId, |
| 12 | type VcsStatusResult, |
| 13 | } from "@t3tools/contracts"; |
| 14 | import { |
| 15 | type GitActionRequestInput, |
| 16 | requiresDefaultBranchConfirmation, |
| 17 | resolveQuickAction, |
| 18 | } from "@t3tools/client-runtime/state/vcs"; |
| 19 | import { useNavigation } from "@react-navigation/native"; |
| 20 | import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients"; |
| 21 | import { changeRequestTermKind } from "../../lib/changeRequestTerm"; |
| 22 | |
| 23 | import { m, type MessageResolver } from "../../i18n/messages"; |
| 24 | import { useEnvironmentScope } from "../../state/session"; |
| 25 | import { PullRequestGlyph } from "../pull-requests/pullRequestGlyphs"; |
| 26 | import { |
| 27 | basename, |
| 28 | getTerminalStatusLabel, |
| 29 | projectScriptMenuIcon, |
| 30 | type TerminalMenuSession, |
| 31 | } from "../terminal/terminalMenu"; |
| 32 | import { projectScriptMenuLabel } from "@t3tools/shared/projectScriptsCopy"; |
| 33 | |
| 34 | function truncateMiddle(value: string, maxLength: number): string { |
| 35 | if (value.length <= maxLength) { |
| 36 | return value; |
| 37 | } |
| 38 | |
| 39 | const headLength = Math.ceil((maxLength - 1) / 2); |
| 40 | const tailLength = Math.floor((maxLength - 1) / 2); |
| 41 | return `${value.slice(0, headLength)}…${value.slice(value.length - tailLength)}`; |
| 42 | } |
| 43 | |
| 44 | function compactMenuBranchLabel(branch: string): string { |
| 45 | return truncateMiddle(branch, 24); |
| 46 | } |
| 47 | |
| 48 | function compactMenuStatus(gitStatus: VcsStatusResult | null, resolve: MessageResolver): string { |
| 49 | if (!gitStatus) { |
| 50 | return resolve(msg("Checking status")); |
| 51 | } |
| 52 | if (!gitStatus.isRepo) { |
| 53 | return resolve(msg("Not a repo")); |
| 54 | } |
| 55 | |
| 56 | const parts: string[] = []; |
| 57 | if (gitStatus.hasWorkingTreeChanges) { |
| 58 | parts.push( |
| 59 | resolve( |
| 60 | msg("{count, plural, one {{count} changed} other {{count} changed}}", { |
| 61 | count: gitStatus.workingTree.files.length, |
| 62 | }), |
| 63 | ), |
| 64 | ); |
| 65 | } else if (gitStatus.aheadCount === 0 && gitStatus.behindCount === 0) { |
| 66 | parts.push(resolve(msg("Clean", { $context: "git working tree has no changes" }))); |
| 67 | } |
| 68 | if (gitStatus.aheadCount > 0) { |
| 69 | parts.push( |
| 70 | resolve( |
| 71 | msg("{count, plural, one {{count} ahead} other {{count} ahead}}", { |
| 72 | count: gitStatus.aheadCount, |
| 73 | }), |
| 74 | ), |
| 75 | ); |
| 76 | } |
| 77 | if (gitStatus.behindCount > 0) { |
| 78 | parts.push( |
| 79 | resolve( |
| 80 | msg("{count, plural, one {{count} behind} other {{count} behind}}", { |
| 81 | count: gitStatus.behindCount, |
| 82 | }), |
| 83 | ), |
| 84 | ); |
| 85 | } |
| 86 | if (gitStatus.pr?.state === "open") { |
| 87 | const { shortLabel } = sourceControlClients.get( |
| 88 | gitStatus.sourceControlProvider?.kind, |
| 89 | ).changeRequest; |
| 90 | parts.push(`${shortLabel} #${gitStatus.pr.number}`); |
| 91 | } |
| 92 | |
| 93 | return parts.join(" · "); |
| 94 | } |
| 95 | |
| 96 | type QuickActionIcon = |
| 97 | | "arrow.down.circle" |
| 98 | | "arrow.up.right.circle" |
| 99 | | "checkmark.circle" |
| 100 | | "arrow.up.circle"; |
| 101 | |
| 102 | export type ThreadGitMenuProps = { |
| 103 | readonly environmentId: EnvironmentId | string; |
| 104 | readonly threadId: ThreadId | string; |
| 105 | readonly currentBranch: string | null; |
| 106 | readonly hasThread?: boolean; |
| 107 | readonly gitStatus: VcsStatusResult | null; |
| 108 | readonly gitOperationLabel: string | null; |
| 109 | readonly onOpenFilesInspector?: () => void; |
| 110 | readonly onOpenGitInspector?: () => void; |
| 111 | readonly onMergeBack?: () => void; |
| 112 | readonly onPull: () => Promise<void>; |
| 113 | readonly onRunAction: (input: GitActionRequestInput) => Promise<GitRunStackedActionResult | null>; |
| 114 | }; |
| 115 | |
| 116 | export type ThreadGitControlsProps = ThreadGitMenuProps & { |
| 117 | readonly canOpenTerminal: boolean; |
| 118 | readonly canOperateTerminal: boolean; |
| 119 | readonly canOpenFiles: boolean; |
| 120 | readonly projectScripts: ReadonlyArray<ProjectScript>; |
| 121 | readonly terminalSessions: ReadonlyArray<TerminalMenuSession>; |
| 122 | readonly onOpenTerminal: (terminalId?: string | null) => void; |
| 123 | readonly onOpenNewTerminal: () => void; |
| 124 | readonly onRunProjectScript: (script: ProjectScript) => Promise<void>; |
| 125 | }; |
| 126 | |
| 127 | function useThreadGitControlModel(props: ThreadGitMenuProps) { |
| 128 | const navigation = useNavigation(); |
| 129 | const gt = useGT(); |
| 130 | const messages = useMessages(); |
| 131 | const openPullRequest = useOpenPullRequest(); |
| 132 | const environmentId = props.environmentId; |
| 133 | const canWriteSourceControl = useEnvironmentScope( |
| 134 | environmentId ? EnvironmentId.make(String(environmentId)) : null, |
| 135 | AuthSourceControlWriteScope, |
| 136 | ); |
| 137 | const threadId = props.threadId; |
| 138 | const { gitStatus, gitOperationLabel, onPull, onRunAction } = props; |
| 139 | |
| 140 | const currentBranchLabel = gitStatus?.refName ?? props.currentBranch ?? gt("Detached HEAD"); |
| 141 | const busy = gitOperationLabel !== null; |
| 142 | const isRepo = gitStatus?.isRepo ?? true; |
| 143 | const hasPrimaryRemote = gitStatus?.hasPrimaryRemote ?? false; |
| 144 | const isDefaultRef = gitStatus?.isDefaultRef ?? false; |
| 145 | |
| 146 | const quickAction = (() => { |
| 147 | if (!isRepo) { |
| 148 | return { |
| 149 | label: gt("Git unavailable"), |
| 150 | disabled: true, |
| 151 | kind: "show_hint" as const, |
| 152 | hint: gt("This workspace is not a git repository."), |
| 153 | }; |
| 154 | } |
| 155 | const action = resolveQuickAction(gitStatus, busy, isDefaultRef, hasPrimaryRemote, messages); |
| 156 | return !canWriteSourceControl && (action.kind === "run_pull" || action.kind === "run_action") |
| 157 | ? { |
| 158 | ...action, |
| 159 | disabled: true, |
| 160 | hint: gt("This connection cannot change source control."), |
| 161 | } |
| 162 | : action; |
| 163 | })(); |
| 164 | |
| 165 | const quickActionHint = quickAction.disabled |
| 166 | ? (quickAction.hint ?? gt("This action is unavailable.")) |
| 167 | : null; |
| 168 | |
| 169 | const quickActionIcon: QuickActionIcon = (() => { |
| 170 | if (quickAction.kind === "run_pull") return "arrow.down.circle"; |
| 171 | if (quickAction.kind === "open_pr") return "arrow.up.right.circle"; |
| 172 | if (quickAction.kind === "run_action") { |
| 173 | if (quickAction.action === "commit") return "checkmark.circle"; |
| 174 | if (quickAction.action === "push" || quickAction.action === "commit_push") |
| 175 | return "arrow.up.circle"; |
| 176 | } |
| 177 | return "arrow.up.right.circle"; |
| 178 | })(); |
| 179 | |
| 180 | const openExistingPr = async () => { |
| 181 | const term = changeRequestTermKind( |
| 182 | sourceControlClients.get(gitStatus?.sourceControlProvider?.kind).changeRequest, |
| 183 | ); |
| 184 | const prUrl = gitStatus?.pr?.state === "open" ? gitStatus.pr.url : null; |
| 185 | if (!prUrl) { |
| 186 | Dialog.alert( |
| 187 | gt("{term, select, mr {No open MR} cr {No open change request} other {No open PR}}", { |
| 188 | term, |
| 189 | }), |
| 190 | gt( |
| 191 | "{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.}}", |
| 192 | { term }, |
| 193 | ), |
| 194 | ); |
| 195 | return; |
| 196 | } |
| 197 | if (!(await openPullRequest(prUrl))) { |
| 198 | Dialog.alert( |
| 199 | gt( |
| 200 | "{term, select, mr {Unable to open MR} cr {Unable to open change request} other {Unable to open PR}}", |
| 201 | { term }, |
| 202 | ), |
| 203 | gt( |
| 204 | "{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.}}", |
| 205 | { term }, |
| 206 | ), |
| 207 | ); |
| 208 | } |
| 209 | }; |
| 210 | |
| 211 | const runActionWithPrompt = async (input: GitActionRequestInput) => { |
| 212 | if (!canWriteSourceControl) return; |
| 213 | const confirmableAction = |
| 214 | input.action === "push" || |
| 215 | input.action === "create_pr" || |
| 216 | input.action === "commit_push" || |
| 217 | input.action === "commit_push_pr" |
| 218 | ? input.action |
| 219 | : null; |
| 220 | const branchName = gitStatus?.refName; |
| 221 | if ( |
| 222 | branchName && |
| 223 | confirmableAction && |
| 224 | !input.featureBranch && |
| 225 | requiresDefaultBranchConfirmation(input.action, isDefaultRef) |
| 226 | ) { |
| 227 | if (props.hasThread === false) { |
| 228 | Dialog.alert( |
| 229 | gt("Choose a feature branch"), |
| 230 | gt("Switch branches before publishing changes from this draft."), |
| 231 | ); |
| 232 | return; |
| 233 | } |
| 234 | navigation.navigate("GitConfirm", { |
| 235 | environmentId: String(environmentId), |
| 236 | threadId: String(threadId), |
| 237 | confirmAction: confirmableAction, |
| 238 | branchName, |
| 239 | includesCommit: String(input.action === "commit_push" || input.action === "commit_push_pr"), |
| 240 | }); |
| 241 | return; |
| 242 | } |
| 243 | |
| 244 | await onRunAction(input); |
| 245 | }; |
| 246 | |
| 247 | const runQuickAction = async () => { |
| 248 | if (quickAction.disabled) return; |
| 249 | if (quickAction.kind === "open_pr") { |
| 250 | await openExistingPr(); |
| 251 | return; |
| 252 | } |
| 253 | if (quickAction.kind === "run_pull") { |
| 254 | await onPull(); |
| 255 | return; |
| 256 | } |
| 257 | if (quickAction.kind === "run_action" && quickAction.action) { |
| 258 | await runActionWithPrompt({ action: quickAction.action }); |
| 259 | } |
| 260 | }; |
| 261 | |
| 262 | const openReview = () => { |
| 263 | navigation.navigate("ThreadReview", { |
| 264 | environmentId: EnvironmentId.make(String(environmentId)), |
| 265 | threadId: ThreadId.make(String(threadId)), |
| 266 | }); |
| 267 | }; |
| 268 | |
| 269 | const openGitInspector = () => { |
| 270 | if (props.onOpenGitInspector) { |
| 271 | props.onOpenGitInspector(); |
| 272 | return; |
| 273 | } |
| 274 | navigation.navigate("GitOverview", { |
| 275 | environmentId: String(environmentId), |
| 276 | threadId: String(threadId), |
| 277 | }); |
| 278 | }; |
| 279 | |
| 280 | return { |
| 281 | currentBranchLabel, |
| 282 | isRepo, |
| 283 | openGitInspector, |
| 284 | openReview, |
| 285 | quickAction, |
| 286 | quickActionHint, |
| 287 | quickActionIcon, |
| 288 | runQuickAction, |
| 289 | }; |
| 290 | } |
| 291 | |
| 292 | export function threadTerminalMenu( |
| 293 | props: ThreadGitControlsProps, |
| 294 | resolve: MessageResolver = m, |
| 295 | ): AppBarMenu | null { |
| 296 | if (!props.canOpenTerminal) return null; |
| 297 | return { |
| 298 | title: resolve(msg("Terminal")), |
| 299 | icon: "terminal", |
| 300 | separateBackground: false, |
| 301 | items: [ |
| 302 | ...(props.projectScripts.length > 0 |
| 303 | ? props.projectScripts.map((script) => ({ |
| 304 | id: `script-${script.id}`, |
| 305 | title: projectScriptMenuLabel(script, resolve), |
| 306 | icon: projectScriptMenuIcon(script.icon), |
| 307 | subtitle: script.command, |
| 308 | disabled: !props.canOperateTerminal, |
| 309 | onPress: () => void props.onRunProjectScript(script), |
| 310 | })) |
| 311 | : [ |
| 312 | { |
| 313 | id: "script-none", |
| 314 | title: resolve(msg("No project scripts")), |
| 315 | icon: "play", |
| 316 | subtitle: resolve(msg("This project has no saved scripts yet")), |
| 317 | disabled: true, |
| 318 | onPress: () => {}, |
| 319 | }, |
| 320 | ]), |
| 321 | ...props.terminalSessions.map((session) => ({ |
| 322 | id: `terminal-${session.terminalId}`, |
| 323 | title: session.displayLabel, |
| 324 | icon: "terminal", |
| 325 | subtitle: [ |
| 326 | getTerminalStatusLabel( |
| 327 | { |
| 328 | status: session.status, |
| 329 | hasRunningSubprocess: session.hasRunningSubprocess, |
| 330 | }, |
| 331 | resolve, |
| 332 | ), |
| 333 | basename(session.cwd), |
| 334 | ] |
| 335 | .filter(Boolean) |
| 336 | .join(" · "), |
| 337 | onPress: () => props.onOpenTerminal(session.terminalId), |
| 338 | })), |
| 339 | { |
| 340 | id: "terminal-new", |
| 341 | title: resolve(msg("Open new terminal")), |
| 342 | icon: "plus", |
| 343 | subtitle: resolve(msg("Start another shell for this thread")), |
| 344 | disabled: !props.canOperateTerminal, |
| 345 | onPress: props.onOpenNewTerminal, |
| 346 | }, |
| 347 | ], |
| 348 | }; |
| 349 | } |
| 350 | |
| 351 | /** The thread's git menu, shared by the thread bar and Review. */ |
| 352 | export function useThreadGitMenuDefinition(props: ThreadGitMenuProps): AppBarMenu | null { |
| 353 | const model = useThreadGitControlModel(props); |
| 354 | const gt = useGT(); |
| 355 | const messages = useMessages(); |
| 356 | return { |
| 357 | title: gt("Git controls"), |
| 358 | icon: "point.topleft.down.curvedto.point.bottomright.up", |
| 359 | separateBackground: false, |
| 360 | items: [ |
| 361 | { |
| 362 | id: "git-status", |
| 363 | title: compactMenuBranchLabel(model.currentBranchLabel), |
| 364 | icon: "point.topleft.down.curvedto.point.bottomright.up", |
| 365 | disabled: true, |
| 366 | subtitle: compactMenuStatus(props.gitStatus, messages), |
| 367 | onPress: () => {}, |
| 368 | }, |
| 369 | { |
| 370 | id: "git-quick-action", |
| 371 | title: model.quickAction.label, |
| 372 | icon: model.quickActionIcon, |
| 373 | disabled: model.quickAction.disabled, |
| 374 | subtitle: model.quickActionHint ?? undefined, |
| 375 | onPress: () => { |
| 376 | void model.runQuickAction(); |
| 377 | }, |
| 378 | }, |
| 379 | { |
| 380 | id: "git-review", |
| 381 | title: gt("Review changes"), |
| 382 | icon: "text.bubble", |
| 383 | disabled: !model.isRepo || props.hasThread === false, |
| 384 | subtitle: |
| 385 | props.hasThread === false |
| 386 | ? gt("Available after the first message") |
| 387 | : gt("Turn diffs and worktree changes"), |
| 388 | onPress: model.openReview, |
| 389 | }, |
| 390 | ...(props.onMergeBack |
| 391 | ? [ |
| 392 | { |
| 393 | id: "git-merge-back", |
| 394 | title: gt("Merge back to source"), |
| 395 | icon: PullRequestGlyph.merged, |
| 396 | subtitle: gt("Bring this thread's latest turn into its source"), |
| 397 | onPress: props.onMergeBack, |
| 398 | }, |
| 399 | ] |
| 400 | : []), |
| 401 | { |
| 402 | id: "git-more", |
| 403 | title: gt("More", { $context: "menu item that opens more git actions" }), |
| 404 | icon: "ellipsis", |
| 405 | disabled: props.hasThread === false, |
| 406 | subtitle: |
| 407 | props.hasThread === false |
| 408 | ? gt("Available after the first message") |
| 409 | : gt("Commit, files, branches"), |
| 410 | onPress: model.openGitInspector, |
| 411 | }, |
| 412 | ], |
| 413 | }; |
| 414 | } |