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 { FitSheet } from "../../../components/ui/FitSheet"; |
| 2 | import { SheetHeader } from "../../../components/ui/SheetHeader"; |
| 3 | import { Button } from "../../../components/ui/Button"; |
| 4 | import { useNavigation, type StaticScreenProps } from "@react-navigation/native"; |
| 5 | import { T, useGT, Var } from "gt-react-native"; |
| 6 | import { useState } from "react"; |
| 7 | import { Pressable, View } from "react-native"; |
| 8 | import { formatDiffCount } from "@t3tools/client-runtime/diff-count"; |
| 9 | |
| 10 | import { SymbolView } from "../../../components/ui/AppSymbol"; |
| 11 | import { AppText as Text, AppTextInput as TextInput } from "../../../components/ui/AppText"; |
| 12 | import { cn } from "../../../lib/cn"; |
| 13 | import { useEnvironmentQuery } from "../../../state/query"; |
| 14 | import { useThreadSelection } from "../../../state/use-thread-selection"; |
| 15 | import { useSelectedThreadGitActions } from "../../../state/use-selected-thread-git-actions"; |
| 16 | import { useSelectedThreadGitState } from "../../../state/use-selected-thread-git-state"; |
| 17 | import { useSelectedThreadWorktree } from "../../../state/use-selected-thread-worktree"; |
| 18 | import { vcsEnvironment } from "../../../state/vcs"; |
| 19 | |
| 20 | type GitCommitSheetProps = StaticScreenProps<{ |
| 21 | readonly environmentId: string; |
| 22 | readonly threadId: string; |
| 23 | }>; |
| 24 | |
| 25 | export function GitCommitSheet(_props: GitCommitSheetProps) { |
| 26 | const gt = useGT(); |
| 27 | const navigation = useNavigation(); |
| 28 | const { selectedThread } = useThreadSelection(); |
| 29 | const { selectedThreadCwd } = useSelectedThreadWorktree(); |
| 30 | const gitState = useSelectedThreadGitState(); |
| 31 | const gitActions = useSelectedThreadGitActions(); |
| 32 | const { canWriteSourceControl, canChangeThreadBranch } = gitActions; |
| 33 | |
| 34 | const gitStatus = useEnvironmentQuery( |
| 35 | selectedThread !== null && selectedThreadCwd !== null |
| 36 | ? vcsEnvironment.status({ |
| 37 | environmentId: selectedThread.environmentId, |
| 38 | input: { cwd: selectedThreadCwd }, |
| 39 | }) |
| 40 | : null, |
| 41 | ); |
| 42 | |
| 43 | const busy = gitState.gitOperationLabel !== null; |
| 44 | const isDefaultRef = gitStatus.data?.isDefaultRef ?? false; |
| 45 | const allFiles = gitStatus.data?.workingTree?.files ?? []; |
| 46 | |
| 47 | const [dialogCommitMessage, setDialogCommitMessage] = useState(""); |
| 48 | const [excludedFiles, setExcludedFiles] = useState<ReadonlySet<string>>(new Set()); |
| 49 | |
| 50 | const selectedFiles = allFiles.filter((file) => !excludedFiles.has(file.path)); |
| 51 | const allSelected = excludedFiles.size === 0; |
| 52 | const noneSelected = selectedFiles.length === 0; |
| 53 | const selectedInsertions = selectedFiles.reduce((sum, file) => sum + file.insertions, 0); |
| 54 | const selectedDeletions = selectedFiles.reduce((sum, file) => sum + file.deletions, 0); |
| 55 | |
| 56 | const runCommitAction = async (featureBranch: boolean) => { |
| 57 | if (!canWriteSourceControl || (featureBranch && !canChangeThreadBranch)) return; |
| 58 | const commitMessage = dialogCommitMessage.trim(); |
| 59 | navigation.goBack(); |
| 60 | await gitActions.onRunSelectedThreadGitAction({ |
| 61 | action: "commit", |
| 62 | featureBranch, |
| 63 | ...(commitMessage ? { commitMessage } : {}), |
| 64 | ...(!allSelected ? { filePaths: selectedFiles.map((file) => file.path) } : {}), |
| 65 | }); |
| 66 | }; |
| 67 | |
| 68 | return ( |
| 69 | <FitSheet |
| 70 | header={ |
| 71 | <SheetHeader |
| 72 | title={gt("Commit changes")} |
| 73 | subtitle={gitStatus.data?.refName ?? gt("(detached HEAD)")} |
| 74 | onCancel={() => navigation.goBack()} |
| 75 | accessibilityLabel={gt("Cancel commit")} |
| 76 | /> |
| 77 | } |
| 78 | keyboardShouldPersistTaps="handled" |
| 79 | keyboardDismissMode="on-drag" |
| 80 | automaticallyAdjustKeyboardInsets |
| 81 | contentContainerClassName="px-5 pt-4 gap-5" |
| 82 | > |
| 83 | {isDefaultRef ? ( |
| 84 | <Text className="text-sm text-warning-foreground"> |
| 85 | <T>You’re on the default branch. You can create a new branch when committing.</T> |
| 86 | </Text> |
| 87 | ) : null} |
| 88 | <View className="gap-2"> |
| 89 | <Text className="text-sm font-t3-medium text-foreground"> |
| 90 | <T>Commit message</T> |
| 91 | </Text> |
| 92 | <TextInput |
| 93 | multiline |
| 94 | accessibilityLabel={gt("Commit message")} |
| 95 | value={dialogCommitMessage} |
| 96 | onChangeText={setDialogCommitMessage} |
| 97 | placeholder={gt("Describe your changes…")} |
| 98 | textAlignVertical="top" |
| 99 | className="min-h-24 rounded-xl bg-subtle px-4 py-3 text-base" |
| 100 | /> |
| 101 | <Text className="text-xs text-foreground-muted"> |
| 102 | <T>Leave empty to generate a message automatically.</T> |
| 103 | </Text> |
| 104 | </View> |
| 105 | <View> |
| 106 | <View className="flex-row items-center justify-between gap-2 pb-2"> |
| 107 | <View className="flex-1 gap-1"> |
| 108 | <Text className="text-base font-t3-bold text-foreground"> |
| 109 | <T>Changed files</T> |
| 110 | </Text> |
| 111 | <Text className="text-xs text-foreground-muted"> |
| 112 | <T> |
| 113 | <Var>{selectedFiles.length}</Var> of <Var>{allFiles.length}</Var> selected · + |
| 114 | <Var>{formatDiffCount(selectedInsertions)}</Var> − |
| 115 | <Var>{formatDiffCount(selectedDeletions)}</Var> |
| 116 | </T> |
| 117 | </Text> |
| 118 | </View> |
| 119 | {allFiles.length > 0 ? ( |
| 120 | <Button |
| 121 | label={ |
| 122 | allSelected |
| 123 | ? gt("Deselect all") |
| 124 | : gt("Select all", { $context: "selects every changed file to commit" }) |
| 125 | } |
| 126 | tone="text" |
| 127 | onPress={() => |
| 128 | setExcludedFiles( |
| 129 | allSelected ? new Set(allFiles.map((file) => file.path)) : new Set(), |
| 130 | ) |
| 131 | } |
| 132 | /> |
| 133 | ) : null} |
| 134 | </View> |
| 135 | {allFiles.length === 0 ? ( |
| 136 | <Text className="py-4 text-sm text-foreground-muted"> |
| 137 | <T>No changed files to commit.</T> |
| 138 | </Text> |
| 139 | ) : ( |
| 140 | allFiles.map((file) => { |
| 141 | const included = !excludedFiles.has(file.path); |
| 142 | const slash = file.path.lastIndexOf("/"); |
| 143 | const name = file.path.slice(slash + 1); |
| 144 | const directory = slash >= 0 ? file.path.slice(0, slash) : null; |
| 145 | return ( |
| 146 | <Pressable |
| 147 | key={file.path} |
| 148 | className="min-h-16 flex-row items-center gap-3 border-t border-border-subtle py-3" |
| 149 | accessibilityRole="checkbox" |
| 150 | accessibilityLabel={file.path} |
| 151 | accessibilityState={{ checked: included }} |
| 152 | onPress={() => |
| 153 | setExcludedFiles((current) => { |
| 154 | const next = new Set(current); |
| 155 | if (next.has(file.path)) next.delete(file.path); |
| 156 | else next.add(file.path); |
| 157 | return next; |
| 158 | }) |
| 159 | } |
| 160 | > |
| 161 | <View |
| 162 | className={cn( |
| 163 | "size-5 items-center justify-center rounded-full", |
| 164 | included ? "bg-primary" : "border border-input-border", |
| 165 | )} |
| 166 | > |
| 167 | {included ? ( |
| 168 | <SymbolView |
| 169 | name="checkmark" |
| 170 | size={13} |
| 171 | tintColorClassName="accent-primary-foreground" |
| 172 | /> |
| 173 | ) : null} |
| 174 | </View> |
| 175 | <View className="flex-1 gap-1"> |
| 176 | <Text |
| 177 | numberOfLines={1} |
| 178 | ellipsizeMode="middle" |
| 179 | className="text-sm font-t3-medium text-foreground" |
| 180 | > |
| 181 | {name} |
| 182 | </Text> |
| 183 | {directory ? ( |
| 184 | <Text |
| 185 | numberOfLines={1} |
| 186 | ellipsizeMode="middle" |
| 187 | className="text-xs text-foreground-muted" |
| 188 | > |
| 189 | {directory} |
| 190 | </Text> |
| 191 | ) : null} |
| 192 | </View> |
| 193 | <View className="flex-row gap-2"> |
| 194 | <Text className="text-xs text-adaptive-emerald-700-300"> |
| 195 | +{formatDiffCount(file.insertions)} |
| 196 | </Text> |
| 197 | <Text className="text-xs text-adaptive-rose-700-300"> |
| 198 | −{formatDiffCount(file.deletions)} |
| 199 | </Text> |
| 200 | </View> |
| 201 | </Pressable> |
| 202 | ); |
| 203 | }) |
| 204 | )} |
| 205 | </View> |
| 206 | <View className="gap-2"> |
| 207 | <Button |
| 208 | label={gt("Commit changes")} |
| 209 | icon="checkmark" |
| 210 | tone="primary" |
| 211 | fullWidth |
| 212 | disabled={!canWriteSourceControl || noneSelected || busy} |
| 213 | onPress={() => void runCommitAction(false)} |
| 214 | /> |
| 215 | <Button |
| 216 | label={gt("Commit on a new branch")} |
| 217 | icon="arrow.branch" |
| 218 | tone="text" |
| 219 | fullWidth |
| 220 | disabled={!canWriteSourceControl || !canChangeThreadBranch || noneSelected || busy} |
| 221 | onPress={() => void runCommitAction(true)} |
| 222 | /> |
| 223 | </View> |
| 224 | </FitSheet> |
| 225 | ); |
| 226 | } |