Files
orca/mobile/src/session/mobile-diff-comments.ts
T
Kelvin Amoaba a3efdcb21b feat(mobile): GitHub review threads, viewed sync and line comments on the diff screen (#37073)
* feat(mobile): GitHub threads, viewed sync and line comments on the diff screen

Committed files of a GitHub pull request now show its review threads under
their lines with Reply and Resolve, take their viewed state from GitHub and
sync the checkbox and Viewed · next file back through setPRFileViewed. A tap
or a long-press drag picks lines and opens a sheet that sends the note to
the workspace's agent through the existing send path, or posts it as a
review comment. Threads and GitHub comments appear only while the local
branch is at the PR head, the same contents desktop shows them on;
otherwise the file says to push. openDiffForFile takes an optional line for
a thread's Open in diff.

* test(mobile): use a type import for the line-mutations recording adapter

* fix(mobile): name the staged or unstaged side in the jump sheet's file labels
2026-11-01 18:38:23 +00:00

170 lines
5.7 KiB
TypeScript

import type { DiffComment, DiffReviewScope } from '../../../src/shared/diff-comment-types'
import {
mobileDiffReviewCommentMatchesItem,
type MobileDiffReviewQueueItem
} from './mobile-diff-review-queue'
export type CreateMobileDiffCommentInput = {
worktreeId: string
filePath: string
oldPath?: string
/** Inclusive range start, below lineNumber; absent for a single line. */
startLine?: number
lineNumber: number
body: string
id: string
createdAt: number
scope?: DiffReviewScope
diffIdentity?: string
}
function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === 'object' && value !== null
}
function normalizeScope(value: unknown): DiffReviewScope | undefined {
return value === 'unstaged' || value === 'staged' || value === 'branch' ? value : undefined
}
export function normalizeMobileDiffComments(value: unknown, worktreeId: string): DiffComment[] {
if (!Array.isArray(value)) {
return []
}
return value.flatMap((candidate): DiffComment[] => {
if (!isRecord(candidate)) {
return []
}
const id = typeof candidate.id === 'string' ? candidate.id : ''
const filePath = typeof candidate.filePath === 'string' ? candidate.filePath : ''
const lineNumber = typeof candidate.lineNumber === 'number' ? candidate.lineNumber : Number.NaN
const body = typeof candidate.body === 'string' ? candidate.body.trim() : ''
const createdAt = typeof candidate.createdAt === 'number' ? candidate.createdAt : Date.now()
if (!id || !filePath || !Number.isFinite(lineNumber) || lineNumber < 0 || !body) {
return []
}
return [
{
id,
worktreeId: typeof candidate.worktreeId === 'string' ? candidate.worktreeId : worktreeId,
filePath,
source: candidate.source === 'markdown' ? 'markdown' : 'diff',
selectedText:
typeof candidate.selectedText === 'string' ? candidate.selectedText : undefined,
startLine: typeof candidate.startLine === 'number' ? candidate.startLine : undefined,
lineNumber,
body,
createdAt,
updatedAt: typeof candidate.updatedAt === 'number' ? candidate.updatedAt : undefined,
sentAt: typeof candidate.sentAt === 'number' ? candidate.sentAt : undefined,
scope: normalizeScope(candidate.scope),
oldPath: typeof candidate.oldPath === 'string' ? candidate.oldPath : undefined,
diffIdentity:
typeof candidate.diffIdentity === 'string' ? candidate.diffIdentity : undefined,
side: 'modified'
}
]
})
}
export function createMobileDiffComment(input: CreateMobileDiffCommentInput): DiffComment | null {
const body = input.body.trim()
if (!body || !Number.isFinite(input.lineNumber) || input.lineNumber < 0) {
return null
}
return {
id: input.id,
worktreeId: input.worktreeId,
filePath: input.filePath,
oldPath: input.oldPath,
source: 'diff',
...(input.startLine !== undefined && input.startLine < input.lineNumber
? { startLine: input.startLine }
: {}),
lineNumber: input.lineNumber,
body,
createdAt: input.createdAt,
scope: input.scope,
diffIdentity: input.diffIdentity,
side: 'modified'
}
}
export function addMobileDiffComment(
comments: readonly DiffComment[],
input: CreateMobileDiffCommentInput
): { comments: DiffComment[]; comment: DiffComment | null } {
const comment = createMobileDiffComment(input)
if (!comment) {
return { comments: [...comments], comment: null }
}
return { comments: [...comments, comment], comment }
}
export function removeMobileDiffComments(
comments: readonly DiffComment[],
ids: ReadonlySet<string>
): DiffComment[] {
if (ids.size === 0) {
return [...comments]
}
return comments.filter((comment) => !ids.has(comment.id))
}
function deliveredCommentMatches(comment: DiffComment, delivered: DiffComment): boolean {
return (
comment.id === delivered.id &&
comment.body === delivered.body &&
comment.filePath === delivered.filePath &&
comment.lineNumber === delivered.lineNumber &&
comment.selectedText === delivered.selectedText &&
comment.source === delivered.source &&
comment.startLine === delivered.startLine
)
}
export function removeDeliveredMobileDiffComments(
comments: readonly DiffComment[],
delivered: readonly DiffComment[]
): DiffComment[] {
if (delivered.length === 0) {
return [...comments]
}
const deliveredById = new Map(delivered.map((comment) => [comment.id, comment]))
return comments.filter((comment) => {
const snapshot = deliveredById.get(comment.id)
return !snapshot || !deliveredCommentMatches(comment, snapshot)
})
}
export function sendableMobileDiffComments(
comments: readonly DiffComment[],
sendingIds: ReadonlySet<string>
): DiffComment[] {
return comments.filter((comment) => !comment.sentAt && !sendingIds.has(comment.id))
}
/** The open file's notes by line, and which were written against an older version of its diff. */
export function mobileDiffReviewNotesForItem(
comments: readonly DiffComment[],
item: MobileDiffReviewQueueItem | null
): { commentsByLine: Map<number, DiffComment[]>; staleCommentIds: Set<string> } {
const commentsByLine = new Map<number, DiffComment[]>()
const staleCommentIds = new Set<string>()
if (!item) {
return { commentsByLine, staleCommentIds }
}
for (const comment of comments) {
if (!mobileDiffReviewCommentMatchesItem(comment, item)) {
continue
}
commentsByLine.set(comment.lineNumber, [
...(commentsByLine.get(comment.lineNumber) ?? []),
comment
])
if (comment.diffIdentity !== undefined && comment.diffIdentity !== item.diffIdentity) {
staleCommentIds.add(comment.id)
}
}
return { commentsByLine, staleCommentIds }
}