The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { errorDisplayMessage } from "@t3tools/client-runtime/errors"; |
| 2 | import { AsyncResult } from "effect/reactivity"; |
| 3 | import { useAtomRefresh } from "@effect/atom-react"; |
| 4 | import { StackActions, useNavigation, type StaticScreenProps } from "@react-navigation/native"; |
| 5 | import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients"; |
| 6 | import { changeRequestTermKind } from "../../lib/changeRequestTerm"; |
| 7 | import { settlePromise, squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime"; |
| 8 | import type { PullRequestReviewVerdict } from "@t3tools/contracts"; |
| 9 | import { msg, T, useGT, useMessages } from "gt-react-native"; |
| 10 | import { useRef, useState } from "react"; |
| 11 | import { useFocusEffect } from "@react-navigation/native"; |
| 12 | import { Pressable, View } from "react-native"; |
| 13 | import { clearSubmittedReview } from "./pullRequestReviewDrafts"; |
| 14 | import { usePullRequestReviewDraft } from "./usePullRequestReviewDraft"; |
| 15 | import { reviewPositionLabel } from "./pullRequestDiffComments"; |
| 16 | import { AppText as Text, AppTextInput as TextInput } from "../../components/ui/AppText"; |
| 17 | import { Button } from "../../components/ui/Button"; |
| 18 | import { SegmentedControl } from "../../components/ui/SegmentedControl"; |
| 19 | import { composerPullRequests } from "../../state/pull-requests"; |
| 20 | import { useEnvironmentQuery } from "../../state/query"; |
| 21 | import { useAtomCommand } from "../../state/use-atom-command"; |
| 22 | import { PullRequestSheetContent as SheetContent } from "./PullRequestSheetContent"; |
| 23 | import { pullRequestTarget, type PullRequestParams } from "./usePullRequestDiff"; |
| 24 | import { usePullRequestPermissions } from "./usePullRequestPermissions"; |
| 25 | import { PullRequestCompletionSuggestions } from "./PullRequestCompletionSuggestions"; |
| 26 | import { usePullRequestCompletions } from "./usePullRequestCompletions"; |
| 27 | import { m as resolveMessage } from "../../i18n/messages"; |
| 28 | |
| 29 | function useActionSubmission(params: PullRequestParams) { |
| 30 | const navigation = useNavigation(); |
| 31 | const gt = useGT(); |
| 32 | const refreshDetail = useAtomRefresh(composerPullRequests.detail(pullRequestTarget(params))); |
| 33 | const refreshActivity = useAtomRefresh(composerPullRequests.activity(pullRequestTarget(params))); |
| 34 | const pending = useRef(false); |
| 35 | const [busy, setBusy] = useState(false); |
| 36 | const [error, setError] = useState<string | null>(null); |
| 37 | const submit = async (operation: () => Promise<void>) => { |
| 38 | if (pending.current) return; |
| 39 | pending.current = true; |
| 40 | setBusy(true); |
| 41 | setError(null); |
| 42 | const result = await settlePromise(async () => { |
| 43 | await operation(); |
| 44 | refreshDetail(); |
| 45 | refreshActivity(); |
| 46 | navigation.goBack(); |
| 47 | }); |
| 48 | pending.current = false; |
| 49 | setBusy(false); |
| 50 | if (AsyncResult.isFailure(result)) { |
| 51 | const cause = squashAtomCommandFailure(result); |
| 52 | setError( |
| 53 | cause instanceof Error |
| 54 | ? (errorDisplayMessage(cause, resolveMessage) ?? cause.message) |
| 55 | : gt("The provider could not complete this action. Try again."), |
| 56 | ); |
| 57 | } |
| 58 | }; |
| 59 | return { busy, error, submit }; |
| 60 | } |
| 61 | |
| 62 | const VERDICTS = [ |
| 63 | { value: "comment", title: msg("Comment", { $context: "review verdict" }) }, |
| 64 | { value: "approve", title: msg("Approve", { $context: "review verdict" }) }, |
| 65 | { |
| 66 | value: "request-changes", |
| 67 | title: msg("Request changes"), |
| 68 | }, |
| 69 | ] as const; |
| 70 | |
| 71 | export function PullRequestReviewSheet({ |
| 72 | route, |
| 73 | }: StaticScreenProps<PullRequestParams & { readonly mode: "review" | "comment" }>) { |
| 74 | const params = route.params; |
| 75 | const gt = useGT(); |
| 76 | const m = useMessages(); |
| 77 | const target = pullRequestTarget(params); |
| 78 | const result = useEnvironmentQuery(composerPullRequests.detail(target)); |
| 79 | const { refresh: refreshDetail } = result; |
| 80 | useFocusEffect(refreshDetail); |
| 81 | const permissions = usePullRequestPermissions(params, result.data); |
| 82 | const submission = useActionSubmission(params); |
| 83 | const review = useAtomCommand(composerPullRequests.submitReview, { reportFailure: false }); |
| 84 | const comment = useAtomCommand(composerPullRequests.comment, { reportFailure: false }); |
| 85 | const navigation = useNavigation(); |
| 86 | const store = usePullRequestReviewDraft(params); |
| 87 | const body = params.mode === "review" ? store.draft.summary : store.draft.comment; |
| 88 | const setBody = (body: string) => |
| 89 | store.update((draft) => ({ |
| 90 | ...draft, |
| 91 | ...(params.mode === "review" ? { summary: body } : { comment: body }), |
| 92 | })); |
| 93 | const completions = usePullRequestCompletions({ |
| 94 | environmentId: params.environmentId, |
| 95 | projectId: params.projectId, |
| 96 | value: body, |
| 97 | onChangeValue: setBody, |
| 98 | support: result.data?.capabilities ?? null, |
| 99 | }); |
| 100 | const stale = store.draft.comments.some((comment) => comment.headSha !== result.data?.headSha); |
| 101 | const [requestedVerdict, setRequestedVerdict] = useState<PullRequestReviewVerdict>("comment"); |
| 102 | const offered = VERDICTS.filter((option) => permissions.verdicts.includes(option.value)); |
| 103 | const verdict = offered.find((option) => option.value === requestedVerdict) ?? offered[0]; |
| 104 | const reviewing = params.mode === "review"; |
| 105 | const canSubmitComments = !store.draft.comments.length || permissions.canInlineComment; |
| 106 | // Forgejo requires a summary when requesting changes, even with inline comments. |
| 107 | const summaryRequired = |
| 108 | verdict !== undefined && result.data |
| 109 | ? sourceControlClients.get(result.data.provider).reviewSummaryRequired(verdict.value) |
| 110 | : false; |
| 111 | const canSubmit = |
| 112 | store.ready && |
| 113 | (!reviewing || (!stale && canSubmitComments)) && |
| 114 | (reviewing |
| 115 | ? verdict !== undefined && |
| 116 | (summaryRequired |
| 117 | ? body.trim().length > 0 |
| 118 | : verdict.value === "approve" || |
| 119 | body.trim().length > 0 || |
| 120 | store.draft.comments.length > 0) |
| 121 | : permissions.canComment && body.trim().length > 0); |
| 122 | const perform = () => |
| 123 | submission.submit(async () => { |
| 124 | if (!canSubmit) |
| 125 | throw new Error(gt("Add your feedback and check your permissions before submitting.")); |
| 126 | const submitted = store.draft; |
| 127 | const response = |
| 128 | reviewing && verdict |
| 129 | ? await review({ |
| 130 | ...target, |
| 131 | input: { |
| 132 | ...target.input, |
| 133 | verdict: verdict.value, |
| 134 | body, |
| 135 | comments: submitted.comments.map( |
| 136 | ({ id: _id, headSha: _sha, code: _code, ...comment }) => comment, |
| 137 | ), |
| 138 | }, |
| 139 | }) |
| 140 | : await comment({ ...target, input: { ...target.input, body } }); |
| 141 | if (response._tag === "Failure") throw squashAtomCommandFailure(response); |
| 142 | store.update((current) => |
| 143 | reviewing |
| 144 | ? clearSubmittedReview(current, submitted) |
| 145 | : { ...current, comment: current.comment === submitted.comment ? "" : current.comment }, |
| 146 | ); |
| 147 | await store.flush(); |
| 148 | }); |
| 149 | return ( |
| 150 | <SheetContent |
| 151 | title={reviewing ? gt("Submit review") : gt("Add comment")} |
| 152 | subtitle={`#${params.number} · ${ |
| 153 | reviewing && result.data?.headSha |
| 154 | ? gt("reviewing {sha}", { sha: result.data.headSha.slice(0, 7) }) |
| 155 | : params.repository |
| 156 | }`} |
| 157 | busy={submission.busy} |
| 158 | footer={ |
| 159 | <Button |
| 160 | label={ |
| 161 | submission.busy |
| 162 | ? gt("Submitting…") |
| 163 | : reviewing |
| 164 | ? verdict?.value === "approve" |
| 165 | ? gt("Approve", { $context: "review verdict" }) |
| 166 | : verdict?.value === "request-changes" |
| 167 | ? gt("Request changes") |
| 168 | : gt("Submit review") |
| 169 | : gt("Post comment") |
| 170 | } |
| 171 | fullWidth |
| 172 | tone="contrast" |
| 173 | loading={submission.busy} |
| 174 | disabled={!canSubmit || result.isPending || result.error !== null} |
| 175 | onPress={() => void perform()} |
| 176 | /> |
| 177 | } |
| 178 | > |
| 179 | {reviewing && offered.length > 1 ? ( |
| 180 | <SegmentedControl |
| 181 | options={offered.map((option) => ({ value: option.value, label: m(option.title) }))} |
| 182 | selected={verdict?.value ?? "comment"} |
| 183 | onSelect={(value) => { |
| 184 | if (!submission.busy) setRequestedVerdict(value); |
| 185 | }} |
| 186 | size="compact" |
| 187 | /> |
| 188 | ) : null} |
| 189 | <Text className="px-4 text-xs text-foreground-muted"> |
| 190 | {reviewing ? gt("Summary") : gt("Comment", { $context: "noun, field label" })} |
| 191 | </Text> |
| 192 | <PullRequestCompletionSuggestions completions={completions} /> |
| 193 | <TextInput |
| 194 | multiline |
| 195 | selection={completions.selection} |
| 196 | onSelectionChange={completions.onSelectionChange} |
| 197 | onBlur={completions.onBlur} |
| 198 | accessibilityLabel={ |
| 199 | reviewing ? gt("Review summary") : gt("Comment", { $context: "noun, field label" }) |
| 200 | } |
| 201 | placeholder={ |
| 202 | reviewing |
| 203 | ? summaryRequired |
| 204 | ? gt("Summary required to request changes…") |
| 205 | : gt("Summarize your review…") |
| 206 | : gt("Write a comment…") |
| 207 | } |
| 208 | placeholderTextColorClassName="accent-foreground-muted" |
| 209 | editable={store.ready && !submission.busy} |
| 210 | value={body} |
| 211 | maxLength={65536} |
| 212 | onChangeText={setBody} |
| 213 | className="min-h-36 rounded-[20px] bg-grouped-card p-4 text-base text-foreground" |
| 214 | style={{ textAlignVertical: "top" }} |
| 215 | /> |
| 216 | {reviewing && store.draft.comments.length ? ( |
| 217 | <View className="gap-2"> |
| 218 | <Text className="text-sm font-t3-medium"> |
| 219 | {gt( |
| 220 | "{count, plural, one {{count} pending line comment} other {{count} pending line comments}}", |
| 221 | { count: store.draft.comments.length }, |
| 222 | )} |
| 223 | </Text> |
| 224 | {store.draft.comments.map((draft) => ( |
| 225 | <Pressable |
| 226 | key={draft.id} |
| 227 | accessibilityRole="button" |
| 228 | disabled={submission.busy} |
| 229 | onPress={() => { |
| 230 | const outdated = draft.headSha !== result.data?.headSha; |
| 231 | if (outdated) |
| 232 | navigation.dispatch( |
| 233 | StackActions.replace("PullRequestFile", { |
| 234 | ...params, |
| 235 | path: draft.path, |
| 236 | replacingDraftId: draft.id, |
| 237 | }), |
| 238 | ); |
| 239 | else |
| 240 | navigation.dispatch( |
| 241 | StackActions.replace("PullRequestInlineComment", { |
| 242 | ...params, |
| 243 | ...draft, |
| 244 | draftId: draft.id, |
| 245 | }), |
| 246 | ); |
| 247 | }} |
| 248 | className="gap-1 rounded-[20px] bg-grouped-card p-4" |
| 249 | > |
| 250 | <Text className="font-mono text-xs text-foreground-muted"> |
| 251 | {draft.path} · {reviewPositionLabel(draft.position, m)} |
| 252 | </Text> |
| 253 | <Text numberOfLines={3} className="text-sm"> |
| 254 | {draft.body} |
| 255 | </Text> |
| 256 | <Text className="text-2xs text-foreground-muted"> |
| 257 | {draft.headSha !== result.data?.headSha |
| 258 | ? gt("Branch changed · Tap to choose a new line") |
| 259 | : gt("Tap to edit")} |
| 260 | </Text> |
| 261 | </Pressable> |
| 262 | ))} |
| 263 | </View> |
| 264 | ) : null} |
| 265 | {reviewing && stale ? ( |
| 266 | <Text className="text-sm text-danger-foreground"> |
| 267 | <T> |
| 268 | The branch changed since you wrote a comment. Choose its current line before submitting. |
| 269 | </T> |
| 270 | </Text> |
| 271 | ) : null} |
| 272 | {reviewing && !canSubmitComments ? ( |
| 273 | <Text className="text-sm text-danger-foreground"> |
| 274 | {gt( |
| 275 | "You can no longer submit line comments on this {term, select, mr {merge request} cr {change request} other {pull request}}. Your drafts are kept here.", |
| 276 | { |
| 277 | term: changeRequestTermKind( |
| 278 | sourceControlClients.get(result.data?.provider).changeRequest, |
| 279 | ), |
| 280 | }, |
| 281 | )} |
| 282 | </Text> |
| 283 | ) : null} |
| 284 | {submission.error || result.error || store.error ? ( |
| 285 | <Text selectable className="text-sm text-danger-foreground"> |
| 286 | {submission.error ?? result.error ?? store.error} |
| 287 | </Text> |
| 288 | ) : null} |
| 289 | </SheetContent> |
| 290 | ); |
| 291 | } |