Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 14 additions & 15 deletions apps/mobile/src/features/review/ReviewCommentComposerSheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import { changeTone, DiffTokenText, ReviewChangeBar } from "./reviewDiffRenderin
import { highlightReviewSelectedLines } from "./highlightReviewSelectedLines";

const REVIEW_COMMENT_PREVIEW_MAX_LINES = 5;
const NO_LINES: ReturnType<typeof getSelectedReviewCommentLines> = [];
const NO_HIGHLIGHTS: ReturnType<typeof highlightReviewSelectedLines> = {};

type ReviewCommentComposerSheetProps = StaticScreenProps<{
readonly environmentId: EnvironmentId;
Expand All @@ -49,21 +51,18 @@ export function ReviewCommentComposerSheet(props: ReviewCommentComposerSheetProp
const [attachments, setAttachments] = useState<ReadonlyArray<DraftComposerImageAttachment>>([]);
const [previewFile, setPreviewFile] = useState<FilePreviewSource | null>(null);

const selectedLines = useMemo(
() => (target ? getSelectedReviewCommentLines(target) : []),
[target],
);
const highlightedLinesById = useMemo(
() =>
target
? highlightReviewSelectedLines({
filePath: target.filePath,
lines: selectedLines,
theme: selectedTheme,
})
: {},
[selectedLines, selectedTheme, target],
);
const { selectedLines, highlightedLinesById } = useMemo(() => {
if (!target) return { selectedLines: NO_LINES, highlightedLinesById: NO_HIGHLIGHTS };
const lines = getSelectedReviewCommentLines(target);
return {
selectedLines: lines,
highlightedLinesById: highlightReviewSelectedLines({
filePath: target.filePath,
lines,
theme: selectedTheme,
}),
};
}, [selectedTheme, target]);
const firstLine = selectedLines[0] ?? null;
const lastLine = selectedLines[selectedLines.length - 1] ?? null;
const firstNumber = firstLine ? getReviewUnifiedLineNumber(firstLine) : null;
Expand Down
2 changes: 0 additions & 2 deletions apps/mobile/src/features/review/ReviewSheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,6 @@ export function ReviewSheet(props: ReviewSheetProps) {
refreshSelectedSection,
selectSection,
isSelectedSectionPending,
diffPreviewRevision,
} = useReviewSections({
enabled: isEnvironmentReady,
environmentId,
Expand All @@ -485,7 +484,6 @@ export function ReviewSheet(props: ReviewSheetProps) {
environmentId,
cwd: selectedThreadCwd,
selectedSection,
revision: diffPreviewRevision,
draftMessage,
});
// Expo may return null before registration or in older binaries; use the fallback.
Expand Down
90 changes: 90 additions & 0 deletions apps/mobile/src/features/review/reviewCommentSelection.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import { describe, expect, it } from "vite-plus/test";

import {
clearReviewCommentTarget,
completeReviewCommentRange,
formatReviewCommentContext,
getReviewCommentSelection,
reviewCommentSelectionScope,
selectReviewCommentSelection,
setReviewCommentTarget,
startReviewCommentRange,
parseReviewCommentMessageSegments,
parseReviewInlineComments,
type ReviewCommentTarget,
} from "./reviewCommentSelection";
import { setReviewSelectedSectionId } from "./reviewState";

function makeTarget(): ReviewCommentTarget {
return {
Expand Down Expand Up @@ -143,3 +151,85 @@ describe("review comment serialization", () => {
expect(comment?.sectionTitle).toBe("Changes > 5");
});
});

describe("review comment selection store", () => {
const changes = { id: "git:branch-range", content: "hash-1" };
const scope = reviewCommentSelectionScope(1, "env:thread", changes)!;
const otherScope = reviewCommentSelectionScope(1, "env:thread", {
id: "turn:2",
content: "diff",
})!;
const target = makeTarget();
const pending = {
sectionId: target.sectionId,
sectionTitle: target.sectionTitle,
filePath: target.filePath,
lines: target.lines,
lineIndex: 0,
rowId: "row-1",
};

it("keeps a long-pressed range start when replacing an earlier target", () => {
setReviewCommentTarget(scope, target);
startReviewCommentRange(scope, pending);

expect(getReviewCommentSelection()).toEqual({ scope, target: null, pending });
});

it("keeps the range start after a same-line range end and drops it on a plain tap", () => {
startReviewCommentRange(scope, pending);
const sameLine = { ...target, startIndex: 0, endIndex: 0 };
completeReviewCommentRange(scope, sameLine);
expect(getReviewCommentSelection().pending).toEqual(pending);

setReviewCommentTarget(scope, sameLine);
expect(getReviewCommentSelection().pending).toBeNull();
});

it("clears the target and the range start together", () => {
startReviewCommentRange(scope, pending);
clearReviewCommentTarget();

expect(getReviewCommentSelection()).toEqual({ scope: null, target: null, pending: null });
});

it("reads as empty from another thread or section", () => {
setReviewCommentTarget(scope, target);

expect(selectReviewCommentSelection(getReviewCommentSelection(), scope).target).toBe(target);
expect(selectReviewCommentSelection(getReviewCommentSelection(), otherScope).target).toBeNull();
expect(selectReviewCommentSelection(getReviewCommentSelection(), null).target).toBeNull();
});

it("reads as empty in a reopened review sheet", () => {
setReviewCommentTarget(scope, target);
const reopenedScope = reviewCommentSelectionScope(2, "env:thread", changes);

expect(
selectReviewCommentSelection(getReviewCommentSelection(), reopenedScope).target,
).toBeNull();
});

it("reads as empty when the section disappears and comes back over a different diff", () => {
setReviewCommentTarget(scope, target);
// The working tree went clean, so the default showed; the section later returns.
const returned = reviewCommentSelectionScope(1, "env:thread", {
...changes,
content: "hash-2",
});
const unchanged = reviewCommentSelectionScope(1, "env:thread", { ...changes });

expect(selectReviewCommentSelection(getReviewCommentSelection(), returned).target).toBeNull();
expect(selectReviewCommentSelection(getReviewCommentSelection(), unchanged).target).toBe(
target,
);
});

it("drops the selection when the section changes, so switching back starts empty", () => {
setReviewCommentTarget(scope, target);
setReviewSelectedSectionId("env:thread", "turn:2");
setReviewSelectedSectionId("env:thread", "git:branch-range");

expect(selectReviewCommentSelection(getReviewCommentSelection(), scope).target).toBeNull();
});
});
121 changes: 107 additions & 14 deletions apps/mobile/src/features/review/reviewCommentSelection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,43 +36,136 @@ export type ReviewCommentMessageSegment =
readonly comment: ReviewInlineComment;
};

let currentTarget: ReviewCommentTarget | null = null;
/** The first line of a range picked with a long press, waiting for its other end. */
export interface PendingReviewCommentRange extends Pick<
ReviewCommentTarget,
"sectionId" | "sectionTitle" | "filePath" | "lines"
> {
readonly lineIndex: number;
readonly rowId: string;
}

/** The sheet, thread and section a selection was made in, and the diff it was made over. */
export interface ReviewCommentSelectionScope {
readonly key: string;
/** Compared by `===`: a git section's diff hash, or a turn's diff as answered. */
readonly content: string | null;
}

export interface ReviewCommentSelection {
readonly scope: ReviewCommentSelectionScope | null;
readonly target: ReviewCommentTarget | null;
readonly pending: PendingReviewCommentRange | null;
}

const EMPTY_SELECTION: ReviewCommentSelection = { scope: null, target: null, pending: null };
let selection = EMPTY_SELECTION;
const listeners = new Set<() => void>();
const REVIEW_COMMENT_BLOCK_PATTERN = /<review_comment\b([^>]*)>\s*([\s\S]*?)<\/review_comment>/g;
const REVIEW_COMMENT_ATTRIBUTE_PATTERN = /([a-zA-Z][a-zA-Z0-9_-]*)="([^"]*)"/g;
const REVIEW_COMMENT_FENCE_PATTERN = /(`{3,})([^\s`]*)[^\n]*\n([\s\S]*?)\n\1/g;

function emitChange() {
function setSelection(next: ReviewCommentSelection) {
selection = next;
listeners.forEach((listener) => listener());
}

export function subscribeReviewCommentTarget(listener: () => void): () => void {
function subscribeReviewCommentSelection(listener: () => void): () => void {
listeners.add(listener);
return () => {
listeners.delete(listener);
};
}

export function getReviewCommentTarget(): ReviewCommentTarget | null {
return currentTarget;
export function getReviewCommentSelection(): ReviewCommentSelection {
return selection;
}

export function setReviewCommentTarget(target: ReviewCommentTarget | null) {
currentTarget = target;
emitChange();
let lastReviewSheetId = 0;

/** A fresh id per mounted review sheet, so reopening the sheet starts without a selection. */
export function nextReviewSheetId(): number {
lastReviewSheetId += 1;
return lastReviewSheetId;
}

export function reviewCommentSelectionScope(
sheetId: number,
threadKey: string | null,
section: { readonly id: string; readonly content: string | null } | null,
): ReviewCommentSelectionScope | null {
return threadKey === null || section === null
? null
: { key: JSON.stringify([sheetId, threadKey, section.id]), content: section.content };
}

function isSameScope(
a: ReviewCommentSelectionScope | null,
b: ReviewCommentSelectionScope | null,
): boolean {
return a !== null && b !== null && a.key === b.key && a.content === b.content;
}

export function setReviewCommentTarget(
scope: ReviewCommentSelectionScope,
target: ReviewCommentTarget,
) {
setSelection({ scope, target, pending: null });
}

/**
* Keeps the pending range: when both ends are the same line the target is a single line, and
* the range still waits for a different end.
*/
export function completeReviewCommentRange(
scope: ReviewCommentSelectionScope,
target: ReviewCommentTarget,
) {
setSelection({
scope,
target,
pending: isSameScope(selection.scope, scope) ? selection.pending : null,
});
}

export function startReviewCommentRange(
scope: ReviewCommentSelectionScope,
pending: PendingReviewCommentRange,
) {
setSelection({ scope, target: null, pending });
}

export function clearReviewCommentTarget() {
currentTarget = null;
emitChange();
setSelection(EMPTY_SELECTION);
}

/** A selection made in another sheet, thread or section, or over another diff, reads as empty. */
export function selectReviewCommentSelection(
current: ReviewCommentSelection,
scope: ReviewCommentSelectionScope | null,
): ReviewCommentSelection {
return isSameScope(current.scope, scope) ? current : EMPTY_SELECTION;
}

export function useReviewCommentSelection(
scope: ReviewCommentSelectionScope | null,
): ReviewCommentSelection {
return selectReviewCommentSelection(
useSyncExternalStore(
subscribeReviewCommentSelection,
getReviewCommentSelection,
getReviewCommentSelection,
),
scope,
);
}

export function useReviewCommentTarget(): ReviewCommentTarget | null {
return useSyncExternalStore(
subscribeReviewCommentTarget,
getReviewCommentTarget,
getReviewCommentTarget,
);
subscribeReviewCommentSelection,
getReviewCommentSelection,
getReviewCommentSelection,
).target;
}

export function getSelectedReviewCommentLines(
Expand Down
32 changes: 31 additions & 1 deletion apps/mobile/src/features/review/reviewModel.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,36 @@
import { describe, expect, it } from "vite-plus/test";

import { applyReviewDiffMetadata, buildReviewParsedDiff } from "./reviewModel";
import {
applyReviewDiffMetadata,
buildReviewParsedDiff,
resolveReviewSelectedSectionId,
type ReviewSectionItem,
} from "./reviewModel";

function makeSection(id: string): ReviewSectionItem {
return {
id,
kind: id.startsWith("turn:") ? "turn" : "branch-range",
title: id,
subtitle: null,
diff: null,
isLoading: false,
};
}

describe("resolveReviewSelectedSectionId", () => {
const turn = makeSection("turn:2");
const changes = makeSection("git:branch-range");

it("shows Changes before anything is picked, not the first section", () => {
expect(resolveReviewSelectedSectionId([turn, changes], null)).toBe("git:branch-range");
});

it("shows the default while the picked section loads and returns to the pick once it does", () => {
expect(resolveReviewSelectedSectionId([changes], "turn:2")).toBe("git:branch-range");
expect(resolveReviewSelectedSectionId([turn, changes], "turn:2")).toBe("turn:2");
});
});

describe("buildReviewParsedDiff", () => {
it("keeps UI identities compact, stable, and isolated from parser cache keys", () => {
Expand Down
Loading
Loading