Skip to content

Commit d0bec12

Browse files
committed
feat(web): load review replies on demand
1 parent 60141f5 commit d0bec12

4 files changed

Lines changed: 101 additions & 3 deletions

File tree

‎apps/web/src/components/pullRequest/PullRequestCodeTab.tsx‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type {
77
PullRequestOmittedFileStat,
88
PullRequestRef,
99
PullRequestReviewThread,
10+
PullRequestThreadCommentsResult,
1011
} from "@t3tools/contracts";
1112
import {
1213
ChevronDownIcon,
@@ -321,6 +322,9 @@ export function PullRequestCodeTab({
321322
const updateComment = useAtomCommand(pullRequestEnvironment.updateComment, {
322323
reportFailure: false,
323324
});
325+
const loadThreadComments = useAtomCommand(pullRequestEnvironment.threadComments, {
326+
reportFailure: false,
327+
});
324328
const getDiffFileContents = useAtomCommand(pullRequestEnvironment.diffFileContents);
325329
const loadDiffFiles = useMemo(
326330
() =>
@@ -780,6 +784,20 @@ export function PullRequestCodeTab({
780784
fixPending={pendingFinding === pullRequestFindingKey({ kind: "thread", thread })}
781785
fixLabel={fixFindingLabel}
782786
{...(onFixFinding ? { onFix: () => onFixFinding({ kind: "thread", thread }) } : {})}
787+
onLoadMore={async (cursor): Promise<PullRequestThreadCommentsResult | null> => {
788+
const result = await loadThreadComments({
789+
environmentId,
790+
input: { ...reference, threadId: thread.id, cursor },
791+
});
792+
if (result._tag === "Failure") {
793+
toastManager.add({
794+
type: "error",
795+
title: "More comments could not be loaded",
796+
});
797+
return null;
798+
}
799+
return result.value;
800+
}}
783801
onReply={(body) =>
784802
runThreadCommand("Reply could not be posted", () =>
785803
replyToThread({
@@ -815,6 +833,7 @@ export function PullRequestCodeTab({
815833
detail,
816834
environmentId,
817835
fixFindingLabel,
836+
loadThreadComments,
818837
onRefresh,
819838
onFixFinding,
820839
pendingFinding,

‎apps/web/src/components/pullRequest/PullRequestReviewAnnotation.tsx‎

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type {
66
EnvironmentId,
77
PullRequestRef,
88
PullRequestReviewThread,
9+
PullRequestThreadCommentsResult,
910
PullRequestThreadComment,
1011
} from "@t3tools/contracts";
1112
import {
@@ -24,6 +25,7 @@ import { cn } from "~/lib/utils";
2425
import { Button } from "../ui/button";
2526
import { Textarea } from "../ui/textarea";
2627
import { isCommentSubmitShortcut } from "../diffs/commentSubmitShortcut";
28+
import { mergePullRequestThreadComments } from "./pullRequestDetail.logic";
2729
import { PullRequestActorLabel } from "./pullRequestPresentation";
2830
import { PullRequestMarkdown } from "./PullRequestMarkdown";
2931
import { PullRequestMarkdownEditor } from "./PullRequestMarkdownEditor";
@@ -98,6 +100,7 @@ export function ReviewThreadCard({
98100
fixLabel = "Fix in a thread",
99101
onFix,
100102
onReply,
103+
onLoadMore,
101104
canEditComment,
102105
onEditComment,
103106
onToggleResolved,
@@ -118,6 +121,8 @@ export function ReviewThreadCard({
118121
onFix?: () => void;
119122
/** Resolves to whether the host took it, so a reply that failed keeps the words it was given. */
120123
onReply: (body: string) => Promise<boolean>;
124+
/** Reads one more page only after the reader asks for it. */
125+
onLoadMore: (cursor: string) => Promise<PullRequestThreadCommentsResult | null>;
121126
/** Whether this reader wrote this remark, which is what rewriting one takes. */
122127
canEditComment: (comment: PullRequestThreadComment) => boolean;
123128
/** Resolves to whether the host took it, like `onReply`. */
@@ -132,6 +137,15 @@ export function ReviewThreadCard({
132137
const [editingId, setEditingId] = useState<string | null>(null);
133138
const [savingEdit, setSavingEdit] = useState(false);
134139
const sendingRef = useRef(false);
140+
const [loadedPage, setLoadedPage] = useState<
141+
(PullRequestThreadCommentsResult & { readonly threadId: string }) | null
142+
>(null);
143+
const [loadingMore, setLoadingMore] = useState(false);
144+
const currentPage = loadedPage?.threadId === thread.id ? loadedPage : null;
145+
const comments = mergePullRequestThreadComments(thread.comments, currentPage?.comments ?? []);
146+
const nextCommentsCursor =
147+
currentPage === null ? (thread.nextCommentsCursor ?? null) : currentPage.nextCursor;
148+
const commentCount = thread.commentCount ?? comments.length;
135149

136150
const saveEdit = async (commentId: string, body: string) => {
137151
if (savingEdit) return;
@@ -156,6 +170,24 @@ export function ReviewThreadCard({
156170
sendingRef.current = false;
157171
}
158172
};
173+
const loadMore = async () => {
174+
if (nextCommentsCursor === null || loadingMore) return;
175+
setLoadingMore(true);
176+
try {
177+
const page = await onLoadMore(nextCommentsCursor);
178+
if (page === null) return;
179+
setLoadedPage((previous) => ({
180+
threadId: thread.id,
181+
comments: mergePullRequestThreadComments(
182+
previous?.threadId === thread.id ? previous.comments : [],
183+
page.comments,
184+
),
185+
nextCursor: page.nextCursor,
186+
}));
187+
} finally {
188+
setLoadingMore(false);
189+
}
190+
};
159191

160192
return (
161193
<div
@@ -175,8 +207,8 @@ export function ReviewThreadCard({
175207
aria-expanded={expanded}
176208
onClick={() => setExpanded((current) => !current)}
177209
>
178-
{thread.isResolved ? "Resolved" : "Open"} · {thread.comments.length}{" "}
179-
{thread.comments.length === 1 ? "comment" : "comments"}
210+
{thread.isResolved ? "Resolved" : "Open"} · {commentCount}{" "}
211+
{commentCount === 1 ? "comment" : "comments"}
180212
</button>
181213
{thread.isOutdated ? <span>outdated</span> : null}
182214
{onFix ? (
@@ -207,7 +239,7 @@ export function ReviewThreadCard({
207239
{expanded ? (
208240
<>
209241
<div className="mt-2 space-y-3">
210-
{thread.comments.map((comment) => (
242+
{comments.map((comment) => (
211243
<article key={comment.id} className="group min-w-0">
212244
<div className="flex items-center gap-2 text-xs text-muted-foreground">
213245
<PullRequestActorLabel actor={comment.author} className="text-foreground" />
@@ -255,6 +287,17 @@ export function ReviewThreadCard({
255287
</article>
256288
))}
257289
</div>
290+
{nextCommentsCursor !== null ? (
291+
<Button
292+
size="xs"
293+
variant="ghost"
294+
className="mt-2 px-1"
295+
disabled={loadingMore}
296+
onClick={() => void loadMore()}
297+
>
298+
{loadingMore ? "Loading..." : "Load more comments"}
299+
</Button>
300+
) : null}
258301

259302
{canReply ? (
260303
replying ? (

‎apps/web/src/components/pullRequest/pullRequestDetail.logic.test.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
handoffPrompt,
1818
handoffReviewComments,
1919
isThreadOwnPullRequest,
20+
mergePullRequestThreadComments,
2021
orderPullRequestComments,
2122
pullRequestActionNeedsHostRefresh,
2223
pullRequestFindingKey,
@@ -80,6 +81,26 @@ describe("pull request activity refresh", () => {
8081
).toBe(false);
8182
});
8283
});
84+
describe("review thread comment pages", () => {
85+
it("appends new comments once and keeps refreshed base comments", () => {
86+
expect(
87+
mergePullRequestThreadComments(
88+
[
89+
{ id: "c1", body: "refreshed" },
90+
{ id: "c2", body: "already in base" },
91+
],
92+
[
93+
{ id: "c2", body: "stale page copy" },
94+
{ id: "c3", body: "next page" },
95+
],
96+
),
97+
).toEqual([
98+
{ id: "c1", body: "refreshed" },
99+
{ id: "c2", body: "already in base" },
100+
{ id: "c3", body: "next page" },
101+
]);
102+
});
103+
});
83104

84105
describe("pull request state description", () => {
85106
it("keeps draft and conflicts orthogonal to the terminal states", () => {

‎apps/web/src/components/pullRequest/pullRequestDetail.logic.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,21 @@ export function shouldRefreshPullRequestActivity(
2121
): boolean {
2222
return previous !== null && previous.key === next.key && previous.updatedAt !== next.updatedAt;
2323
}
24+
/** Appends fetched pages without replacing fresher comments already in the activity response. */
25+
export function mergePullRequestThreadComments<T extends { readonly id: string }>(
26+
base: ReadonlyArray<T>,
27+
loaded: ReadonlyArray<T>,
28+
): ReadonlyArray<T> {
29+
const seen = new Set(base.map((comment) => comment.id));
30+
return [
31+
...base,
32+
...loaded.filter((comment) => {
33+
if (seen.has(comment.id)) return false;
34+
seen.add(comment.id);
35+
return true;
36+
}),
37+
];
38+
}
2439

2540
/**
2641
* Whether the pull request on a right-panel surface is the thread's own one. Repository and

0 commit comments

Comments
 (0)