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 type { PullRequestReviewPosition, PullRequestReviewThread } from "@t3tools/contracts"; |
| 2 | import { msg } from "gt-react-native"; |
| 3 | |
| 4 | import { m, type MessageResolver } from "../../i18n/messages"; |
| 5 | import type { NativeReviewDiffRow } from "../diffs/nativeReviewDiffSurface"; |
| 6 | import type { NativeReviewDiffData } from "../review/nativeReviewDiffAdapter"; |
| 7 | import type { PendingReviewComment } from "./pullRequestReviewDrafts"; |
| 8 | |
| 9 | export function reviewPosition(row: NativeReviewDiffRow): PullRequestReviewPosition | null { |
| 10 | if (row.kind !== "line") return null; |
| 11 | if (row.change === "add" && row.newLineNumber) |
| 12 | return { kind: "added", newLine: row.newLineNumber }; |
| 13 | if (row.change === "delete" && row.oldLineNumber) |
| 14 | return { kind: "deleted", oldLine: row.oldLineNumber }; |
| 15 | if (row.oldLineNumber && row.newLineNumber) |
| 16 | return { |
| 17 | kind: "context", |
| 18 | oldLine: row.oldLineNumber, |
| 19 | newLine: row.newLineNumber, |
| 20 | side: "right", |
| 21 | }; |
| 22 | return null; |
| 23 | } |
| 24 | export function discussionCommentCountLabel(count: number, resolve: MessageResolver = m) { |
| 25 | return resolve(msg("{count, plural, one {{count} comment} other {{count} comments}}", { count })); |
| 26 | } |
| 27 | export function reviewPositionLabel( |
| 28 | position: PullRequestReviewPosition, |
| 29 | resolve: MessageResolver = m, |
| 30 | ) { |
| 31 | const oldSide = |
| 32 | position.kind === "deleted" || (position.kind === "context" && position.side === "left"); |
| 33 | const line = reviewLine(position); |
| 34 | return oldSide |
| 35 | ? resolve(msg("Old line {line}", { line })) |
| 36 | : resolve(msg("Line {line}", { line })); |
| 37 | } |
| 38 | function reviewLine(position: PullRequestReviewPosition) { |
| 39 | if (position.kind === "deleted" || (position.kind === "context" && position.side === "left")) |
| 40 | return position.oldLine; |
| 41 | return position.newLine; |
| 42 | } |
| 43 | export function annotatePullRequestDiff( |
| 44 | data: NativeReviewDiffData, |
| 45 | threads: readonly PullRequestReviewThread[], |
| 46 | drafts: readonly PendingReviewComment[], |
| 47 | headSha: string | undefined, |
| 48 | resolve: MessageResolver = m, |
| 49 | ): NativeReviewDiffData { |
| 50 | if (threads.length === 0 && drafts.length === 0) return data; |
| 51 | const anchors = new Map<string, NativeReviewDiffRow[]>(); |
| 52 | const firstByPath = new Map<string, NativeReviewDiffRow>(); |
| 53 | const lines = new Map<string, NativeReviewDiffRow>(); |
| 54 | const paths = new Map(data.files.map((file) => [file.id, file.path])); |
| 55 | for (const original of data.rows) { |
| 56 | const row = { |
| 57 | ...original, |
| 58 | filePath: original.filePath ?? (original.fileId ? paths.get(original.fileId) : undefined), |
| 59 | }; |
| 60 | if (row.kind === "file" && row.filePath) firstByPath.set(row.filePath, row); |
| 61 | if (row.kind !== "line" || !row.filePath) continue; |
| 62 | if (row.oldLineNumber) |
| 63 | lines.set(JSON.stringify([row.filePath, "left", row.oldLineNumber]), row); |
| 64 | if (row.newLineNumber) |
| 65 | lines.set(JSON.stringify([row.filePath, "right", row.newLineNumber]), row); |
| 66 | } |
| 67 | const attach = ( |
| 68 | anchor: NativeReviewDiffRow | undefined, |
| 69 | id: string, |
| 70 | body: string, |
| 71 | label: string, |
| 72 | ) => { |
| 73 | if (!anchor) return; |
| 74 | const rows = anchors.get(anchor.id) ?? []; |
| 75 | rows.push({ |
| 76 | kind: "comment", |
| 77 | id, |
| 78 | fileId: anchor.fileId, |
| 79 | filePath: anchor.filePath, |
| 80 | commentText: body, |
| 81 | commentRangeLabel: label, |
| 82 | }); |
| 83 | anchors.set(anchor.id, rows); |
| 84 | }; |
| 85 | for (const thread of threads) { |
| 86 | if (thread.isOutdated) continue; |
| 87 | const anchor = |
| 88 | thread.line === null |
| 89 | ? firstByPath.get(thread.path) |
| 90 | : lines.get(JSON.stringify([thread.path, thread.side, thread.line])); |
| 91 | const first = thread.comments[0]; |
| 92 | attach( |
| 93 | anchor, |
| 94 | `discussion:${thread.id}`, |
| 95 | `${first?.author?.login ?? resolve(msg("Discussion"))}${ |
| 96 | thread.isResolved |
| 97 | ? ` · ${resolve(msg("Resolved", { $context: "review thread state" }))}` |
| 98 | : "" |
| 99 | }\n${first?.body ?? ""}\n${resolve( |
| 100 | msg( |
| 101 | "{count, plural, one {{count} comment · Tap to open} other {{count} comments · Tap to open}}", |
| 102 | { count: thread.commentCount ?? thread.comments.length }, |
| 103 | ), |
| 104 | )}`, |
| 105 | thread.line === null ? "file" : String(thread.line), |
| 106 | ); |
| 107 | } |
| 108 | for (const draft of drafts) { |
| 109 | if (draft.headSha !== headSha) continue; |
| 110 | const side = |
| 111 | draft.position.kind === "deleted" |
| 112 | ? "left" |
| 113 | : draft.position.kind === "context" |
| 114 | ? draft.position.side |
| 115 | : "right"; |
| 116 | const line = reviewLine(draft.position); |
| 117 | attach( |
| 118 | lines.get(JSON.stringify([draft.path, side, line])), |
| 119 | `draft:${draft.id}`, |
| 120 | `${resolve(msg("Pending review · Tap to edit"))}\n${draft.body}`, |
| 121 | String(line), |
| 122 | ); |
| 123 | } |
| 124 | return { ...data, rows: data.rows.flatMap((row) => [row, ...(anchors.get(row.id) ?? [])]) }; |
| 125 | } |