@@ -6,6 +6,7 @@ import type {
66 EnvironmentId ,
77 PullRequestRef ,
88 PullRequestReviewThread ,
9+ PullRequestThreadCommentsResult ,
910 PullRequestThreadComment ,
1011} from "@t3tools/contracts" ;
1112import {
@@ -24,6 +25,7 @@ import { cn } from "~/lib/utils";
2425import { Button } from "../ui/button" ;
2526import { Textarea } from "../ui/textarea" ;
2627import { isCommentSubmitShortcut } from "../diffs/commentSubmitShortcut" ;
28+ import { mergePullRequestThreadComments } from "./pullRequestDetail.logic" ;
2729import { PullRequestActorLabel } from "./pullRequestPresentation" ;
2830import { PullRequestMarkdown } from "./PullRequestMarkdown" ;
2931import { 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 ? (
0 commit comments