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 { |
| 2 | parsePullRequestAnchor, |
| 3 | pullRequestAnchorMatchesUrl, |
| 4 | resolvePullRequestReviewAnchor, |
| 5 | } from "@t3tools/shared/pullRequestAnchor"; |
| 6 | import { holdLinkedNodeInView } from "./holdLinkedNodeInView"; |
| 7 | import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients"; |
| 8 | import { useAtomCommand } from "~/state/use-atom-command"; |
| 9 | import type { |
| 10 | EnvironmentId, |
| 11 | PullRequestActor, |
| 12 | PullRequestComment, |
| 13 | PullRequestDetailView, |
| 14 | PullRequestRef, |
| 15 | ScopedThreadRef, |
| 16 | } from "@t3tools/contracts"; |
| 17 | import { |
| 18 | ChevronDownIcon, |
| 19 | CircleCheckIcon, |
| 20 | ExternalLinkIcon, |
| 21 | FileCode2Icon, |
| 22 | GitCommitHorizontalIcon, |
| 23 | MessageSquareIcon, |
| 24 | MessageSquarePlusIcon, |
| 25 | } from "lucide-react"; |
| 26 | import { msg, T, useGT, useMessages, Var } from "gt-react"; |
| 27 | |
| 28 | import { m } from "~/i18n/messages"; |
| 29 | import { useEffect, useEffectEvent, useRef, useState, type ReactNode } from "react"; |
| 30 | |
| 31 | import { cn } from "~/lib/utils"; |
| 32 | import { readLocalApi } from "~/localApi"; |
| 33 | import { pullRequestEnvironment } from "~/state/pullRequests"; |
| 34 | import { RelativeTime } from "~/components/RelativeTime"; |
| 35 | |
| 36 | import { Button } from "../ui/button"; |
| 37 | import { PullRequestEditButton } from "./PullRequestEditButton"; |
| 38 | import { Collapsible, CollapsiblePanel, CollapsibleTrigger } from "../ui/collapsible"; |
| 39 | import { toastManager } from "../ui/toast"; |
| 40 | import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip"; |
| 41 | import { |
| 42 | buildPullRequestTimeline, |
| 43 | groupPullRequestTimelineConversations, |
| 44 | pullRequestTimelineEventMatches, |
| 45 | type PullRequestTimelineRow, |
| 46 | isPullRequestVerdictStale, |
| 47 | newestPullRequestCommitAt, |
| 48 | pullRequestReviewOutcome, |
| 49 | type PullRequestChatSubject, |
| 50 | type PullRequestReviewOutcome, |
| 51 | type PullRequestTimelineEvent, |
| 52 | type PullRequestTimelineThread, |
| 53 | } from "./pullRequestDetail.logic"; |
| 54 | import { canEditPullRequestComment } from "@t3tools/client-runtime/pull-request-editing"; |
| 55 | import { PullRequestMarkdown } from "./PullRequestMarkdown"; |
| 56 | import { PullRequestMoreReplies } from "./PullRequestMoreReplies"; |
| 57 | import { PullRequestMarkdownEditor } from "./PullRequestMarkdownEditor"; |
| 58 | import { PullRequestReactionBar } from "./PullRequestReactions"; |
| 59 | import { canReactPullRequestComment } from "@t3tools/client-runtime/pull-request-reactions"; |
| 60 | import { PullRequestResolveThreadButton } from "./PullRequestResolveThreadButton"; |
| 61 | import { |
| 62 | PullRequestActorAvatar, |
| 63 | PullRequestDiffStat, |
| 64 | PullRequestMetaLine, |
| 65 | PullRequestReviewOutcomeIcon, |
| 66 | pullRequestReviewOutcomeLabel, |
| 67 | pullRequestReviewOutcomeStaleLabel, |
| 68 | pullRequestReviewOutcomeToneClassName, |
| 69 | } from "./pullRequestPresentation"; |
| 70 | import { PullRequestGlyph } from "./pullRequestIcons"; |
| 71 | import { |
| 72 | type PullRequestCompletionScope, |
| 73 | pullRequestCompletionScope, |
| 74 | } from "./PullRequestMarkdownTextarea"; |
| 75 | import { LegendList, type LegendListRef } from "@legendapp/list/react"; |
| 76 | |
| 77 | import { changeRequestTermKind, type ChangeRequestTermKind } from "./changeRequestTerm"; |
| 78 | |
| 79 | interface ReactionSurface { |
| 80 | revealAnchor?: string | null; |
| 81 | readonly canReact: (event: PullRequestTimelineEvent) => boolean; |
| 82 | /** Absent where there is no composer beside the panel to add a remark to. */ |
| 83 | readonly onAddToChat?: ((comment: PullRequestComment) => void) | undefined; |
| 84 | readonly environmentId: EnvironmentId; |
| 85 | readonly threadRef: ScopedThreadRef | null; |
| 86 | readonly reference: PullRequestRef; |
| 87 | readonly completionScope: PullRequestCompletionScope | null; |
| 88 | readonly onRefresh: () => void; |
| 89 | } |
| 90 | |
| 91 | function TimelineBody({ |
| 92 | body, |
| 93 | markdown, |
| 94 | cwd, |
| 95 | environmentId, |
| 96 | threadRef, |
| 97 | }: { |
| 98 | body: string; |
| 99 | markdown: boolean; |
| 100 | cwd: string; |
| 101 | environmentId: EnvironmentId; |
| 102 | threadRef: ScopedThreadRef | null; |
| 103 | }) { |
| 104 | return ( |
| 105 | <div className="mt-3"> |
| 106 | {markdown ? ( |
| 107 | <PullRequestMarkdown |
| 108 | text={body} |
| 109 | cwd={cwd} |
| 110 | environmentId={environmentId} |
| 111 | threadRef={threadRef} |
| 112 | /> |
| 113 | ) : ( |
| 114 | <p className="whitespace-pre-wrap text-xs text-muted-foreground">{body}</p> |
| 115 | )} |
| 116 | </div> |
| 117 | ); |
| 118 | } |
| 119 | |
| 120 | function ActorName({ actor }: { actor: PullRequestActor | null }) { |
| 121 | return <span className="font-semibold text-foreground">{actor?.login ?? "ghost"}</span>; |
| 122 | } |
| 123 | |
| 124 | function TimelineMarker({ |
| 125 | children, |
| 126 | className, |
| 127 | }: { |
| 128 | children: ReactNode; |
| 129 | className?: string | undefined; |
| 130 | }) { |
| 131 | return ( |
| 132 | <span |
| 133 | className={cn( |
| 134 | "absolute left-0 top-1/2 z-10 flex size-8 -translate-y-1/2 items-center justify-center bg-background", |
| 135 | className, |
| 136 | )} |
| 137 | > |
| 138 | {children} |
| 139 | </span> |
| 140 | ); |
| 141 | } |
| 142 | |
| 143 | function IconMarker({ icon, className }: { icon: ReactNode; className?: string | undefined }) { |
| 144 | return ( |
| 145 | <TimelineMarker className={className}> |
| 146 | <span className="flex size-7 items-center justify-center bg-background text-muted-foreground"> |
| 147 | {icon} |
| 148 | </span> |
| 149 | </TimelineMarker> |
| 150 | ); |
| 151 | } |
| 152 | |
| 153 | function ActorTimelineMarker({ |
| 154 | actors, |
| 155 | className, |
| 156 | fallback, |
| 157 | muted = false, |
| 158 | }: { |
| 159 | actors: ReadonlyArray<PullRequestActor>; |
| 160 | className?: string | undefined; |
| 161 | fallback: ReactNode; |
| 162 | muted?: boolean; |
| 163 | }) { |
| 164 | const actor = actors[0]; |
| 165 | return actor === undefined ? ( |
| 166 | <IconMarker className={className} icon={fallback} /> |
| 167 | ) : ( |
| 168 | <TimelineMarker className={className}> |
| 169 | <PullRequestActorAvatar |
| 170 | actor={actor} |
| 171 | className={cn( |
| 172 | "size-7 bg-muted text-3xs transition-opacity", |
| 173 | muted && "opacity-45 grayscale", |
| 174 | )} |
| 175 | /> |
| 176 | </TimelineMarker> |
| 177 | ); |
| 178 | } |
| 179 | |
| 180 | function friendlyReviewState(value: string): string { |
| 181 | const words = value.toLowerCase().replaceAll("_", " ").replaceAll("-", " "); |
| 182 | return words.replace(/^\w/u, (letter) => letter.toUpperCase()); |
| 183 | } |
| 184 | |
| 185 | function ReviewStateBadge({ state }: { state: string }) { |
| 186 | return ( |
| 187 | <span className="text-3xs font-medium text-muted-foreground">{friendlyReviewState(state)}</span> |
| 188 | ); |
| 189 | } |
| 190 | |
| 191 | function AddToChatButton({ |
| 192 | comment, |
| 193 | reactions, |
| 194 | }: { |
| 195 | comment: PullRequestComment; |
| 196 | reactions: ReactionSurface; |
| 197 | }) { |
| 198 | const gt = useGT(); |
| 199 | const onAddToChat = reactions.onAddToChat; |
| 200 | return onAddToChat === undefined ? null : ( |
| 201 | <Tooltip> |
| 202 | <TooltipTrigger |
| 203 | render={ |
| 204 | <Button |
| 205 | size="icon-xs" |
| 206 | variant="ghost-muted" |
| 207 | className="-mt-1 shrink-0" |
| 208 | aria-label={gt("Add to chat")} |
| 209 | onClick={() => onAddToChat(comment)} |
| 210 | /> |
| 211 | } |
| 212 | > |
| 213 | <MessageSquarePlusIcon className="size-3" /> |
| 214 | </TooltipTrigger> |
| 215 | <TooltipPopup> |
| 216 | <T>Add to chat</T> |
| 217 | </TooltipPopup> |
| 218 | </Tooltip> |
| 219 | ); |
| 220 | } |
| 221 | |
| 222 | function OpenOnHostButton({ url, onOpen }: { url: string | null; onOpen: (url: string) => void }) { |
| 223 | const gt = useGT(); |
| 224 | return url === null ? null : ( |
| 225 | <Button |
| 226 | size="icon-xs" |
| 227 | variant="ghost-muted" |
| 228 | className="-mr-1 shrink-0" |
| 229 | aria-label={gt("Open activity on host")} |
| 230 | onClick={() => onOpen(url)} |
| 231 | > |
| 232 | <ExternalLinkIcon className="size-3" /> |
| 233 | </Button> |
| 234 | ); |
| 235 | } |
| 236 | |
| 237 | function ConversationCard({ |
| 238 | event, |
| 239 | editable, |
| 240 | cwd, |
| 241 | onOpen, |
| 242 | reactions, |
| 243 | showPath = true, |
| 244 | reply = false, |
| 245 | resolveButton = null, |
| 246 | }: { |
| 247 | event: PullRequestTimelineEvent; |
| 248 | editable: PullRequestComment | null; |
| 249 | cwd: string; |
| 250 | onOpen: (url: string) => void; |
| 251 | reactions: ReactionSurface; |
| 252 | showPath?: boolean; |
| 253 | /** A reply goes with its conversation, from the card that heads it. */ |
| 254 | reply?: boolean; |
| 255 | resolveButton?: ReactNode; |
| 256 | }) { |
| 257 | const gt = useGT(); |
| 258 | const canReact = reactions.canReact(event); |
| 259 | const [editing, setEditing] = useState(false); |
| 260 | const [saving, setSaving] = useState(false); |
| 261 | const updateComment = useAtomCommand(pullRequestEnvironment.updateComment, { |
| 262 | reportFailure: false, |
| 263 | }); |
| 264 | |
| 265 | const save = async (body: string) => { |
| 266 | if (editable === null || saving || editable.kind === "review") return; |
| 267 | setSaving(true); |
| 268 | const result = await updateComment({ |
| 269 | environmentId: reactions.environmentId, |
| 270 | input: { ...reactions.reference, commentId: editable.id, kind: editable.kind, body }, |
| 271 | }); |
| 272 | setSaving(false); |
| 273 | if (result._tag === "Failure") { |
| 274 | toastManager.add({ type: "error", title: m(msg("Could not save the comment")) }); |
| 275 | return; |
| 276 | } |
| 277 | setEditing(false); |
| 278 | reactions.onRefresh(); |
| 279 | }; |
| 280 | |
| 281 | return ( |
| 282 | <article |
| 283 | {...(reactions.revealAnchor != null && |
| 284 | pullRequestAnchorMatchesUrl(reactions.revealAnchor, event.url) |
| 285 | ? { |
| 286 | "data-pr-link-target": true, |
| 287 | tabIndex: -1, |
| 288 | // Inset: the collapsible panel clips an outer ring's left and right edges. |
| 289 | className: "group rounded-lg py-2 ring-1 ring-inset ring-primary/40 outline-none", |
| 290 | } |
| 291 | : { className: "group py-2" })} |
| 292 | > |
| 293 | <div className="px-2"> |
| 294 | <div className="flex min-w-0 flex-wrap items-center gap-2"> |
| 295 | <div className="min-w-0 flex-1"> |
| 296 | <h2 className="flex min-w-0 flex-wrap items-center gap-x-1.5 gap-y-1 text-xs"> |
| 297 | {event.kind === "review" ? ( |
| 298 | <T> |
| 299 | <Var> |
| 300 | <ActorName actor={event.actor} /> |
| 301 | </Var>{" "} |
| 302 | <span className="text-muted-foreground">reviewed</span> |
| 303 | </T> |
| 304 | ) : event.kind === "comment" ? ( |
| 305 | <T> |
| 306 | <Var> |
| 307 | <ActorName actor={event.actor} /> |
| 308 | </Var>{" "} |
| 309 | <span className="text-muted-foreground">commented</span> |
| 310 | </T> |
| 311 | ) : ( |
| 312 | <> |
| 313 | <ActorName actor={event.actor} /> |
| 314 | <span className="text-muted-foreground">{event.title}</span> |
| 315 | </> |
| 316 | )} |
| 317 | {event.reviewState ? <ReviewStateBadge state={event.reviewState} /> : null} |
| 318 | </h2> |
| 319 | <PullRequestMetaLine className="mt-1 flex-wrap text-2xs text-muted-foreground"> |
| 320 | <span> |
| 321 | <RelativeTime timestamp={event.at} /> |
| 322 | </span> |
| 323 | {showPath && event.path ? ( |
| 324 | <span className="inline-flex min-w-0 items-center gap-1"> |
| 325 | <FileCode2Icon aria-hidden className="size-3 shrink-0" /> |
| 326 | <span className="truncate">{event.path}</span> |
| 327 | </span> |
| 328 | ) : null} |
| 329 | </PullRequestMetaLine> |
| 330 | </div> |
| 331 | {editable !== null && !editing ? ( |
| 332 | <PullRequestEditButton |
| 333 | aria-label={gt("Edit comment")} |
| 334 | onClick={() => setEditing(true)} |
| 335 | /> |
| 336 | ) : null} |
| 337 | {resolveButton} |
| 338 | {event.body && event.comment && !reply ? ( |
| 339 | <AddToChatButton comment={event.comment} reactions={reactions} /> |
| 340 | ) : null} |
| 341 | {canReact || event.reactions.length > 0 ? ( |
| 342 | <PullRequestReactionBar |
| 343 | className="ml-auto justify-end" |
| 344 | reactions={event.reactions} |
| 345 | canReact={canReact} |
| 346 | subjectId={event.id} |
| 347 | environmentId={reactions.environmentId} |
| 348 | reference={reactions.reference} |
| 349 | onRefresh={reactions.onRefresh} |
| 350 | /> |
| 351 | ) : null} |
| 352 | <OpenOnHostButton url={event.url} onOpen={onOpen} /> |
| 353 | </div> |
| 354 | </div> |
| 355 | {editing && editable !== null ? ( |
| 356 | <div className="px-2 pb-2 pt-3"> |
| 357 | <PullRequestMarkdownEditor |
| 358 | value={editable.body} |
| 359 | cwd={cwd} |
| 360 | environmentId={reactions.environmentId} |
| 361 | completionScope={reactions.completionScope} |
| 362 | threadRef={reactions.threadRef} |
| 363 | label={gt("Edit comment")} |
| 364 | saving={saving} |
| 365 | onSave={(body) => void save(body)} |
| 366 | onCancel={() => setEditing(false)} |
| 367 | /> |
| 368 | </div> |
| 369 | ) : event.body ? ( |
| 370 | <div className="px-2 pb-2"> |
| 371 | <TimelineBody |
| 372 | body={event.body} |
| 373 | markdown={event.markdown} |
| 374 | cwd={cwd} |
| 375 | environmentId={reactions.environmentId} |
| 376 | threadRef={reactions.threadRef} |
| 377 | /> |
| 378 | </div> |
| 379 | ) : null} |
| 380 | </article> |
| 381 | ); |
| 382 | } |
| 383 | |
| 384 | function uniqueConversationActors(events: ReadonlyArray<PullRequestTimelineEvent>) { |
| 385 | const actors = new Map<string, PullRequestActor>(); |
| 386 | for (const event of events) { |
| 387 | const actor = event.actor; |
| 388 | if (actor !== null && !actors.has(actor.login)) actors.set(actor.login, actor); |
| 389 | } |
| 390 | return [...actors.values()]; |
| 391 | } |
| 392 | |
| 393 | function ConversationGroup({ |
| 394 | revealAnchor, |
| 395 | events, |
| 396 | editable, |
| 397 | cwd, |
| 398 | onOpen, |
| 399 | reactions, |
| 400 | }: { |
| 401 | revealAnchor?: string | null; |
| 402 | events: ReadonlyArray<PullRequestTimelineEvent>; |
| 403 | editable: ReadonlyMap<string, PullRequestComment>; |
| 404 | cwd: string; |
| 405 | onOpen: (url: string) => void; |
| 406 | reactions: ReactionSurface; |
| 407 | }) { |
| 408 | const [open, setOpen] = useState( |
| 409 | () => |
| 410 | revealAnchor != null && |
| 411 | events.some((event) => pullRequestAnchorMatchesUrl(revealAnchor, event.url)), |
| 412 | ); |
| 413 | const gt = useGT(); |
| 414 | const actors = uniqueConversationActors(events); |
| 415 | const first = events[0]; |
| 416 | if (first === undefined) return null; |
| 417 | |
| 418 | return ( |
| 419 | <div className="relative mb-5 pl-12 [contain-intrinsic-block-size:48px] [content-visibility:auto]"> |
| 420 | <ActorTimelineMarker |
| 421 | actors={actors} |
| 422 | className="top-6" |
| 423 | fallback={<MessageSquareIcon className="size-3.5" />} |
| 424 | muted={!open} |
| 425 | /> |
| 426 | <Collapsible open={open} onOpenChange={setOpen}> |
| 427 | <div> |
| 428 | <CollapsibleTrigger |
| 429 | className={cn( |
| 430 | "flex w-full min-w-0 items-center gap-3 py-2 text-left transition-opacity hover:opacity-100", |
| 431 | open ? "text-foreground opacity-100" : "text-muted-foreground opacity-55", |
| 432 | )} |
| 433 | > |
| 434 | <span className="min-w-0 flex-1"> |
| 435 | <span className="block text-xs font-semibold"> |
| 436 | {gt("{count, plural, one {{formatted} comment} other {{formatted} comments}}", { |
| 437 | count: events.length, |
| 438 | formatted: events.length.toLocaleString(), |
| 439 | })} |
| 440 | </span> |
| 441 | <span className="block truncate text-3xs text-muted-foreground"> |
| 442 | {gt("{count, plural, one {{formatted} author} other {{formatted} authors}}", { |
| 443 | count: actors.length, |
| 444 | formatted: actors.length.toLocaleString(), |
| 445 | })}{" "} |
| 446 | · <RelativeTime timestamp={first.at} /> |
| 447 | </span> |
| 448 | </span> |
| 449 | <ChevronDownIcon |
| 450 | aria-hidden |
| 451 | className={cn( |
| 452 | "size-3.5 shrink-0 text-muted-foreground transition-transform", |
| 453 | open && "rotate-180", |
| 454 | )} |
| 455 | /> |
| 456 | </CollapsibleTrigger> |
| 457 | <CollapsiblePanel> |
| 458 | {open ? ( |
| 459 | <div className="mt-1 space-y-1"> |
| 460 | {events.map((event) => ( |
| 461 | <ConversationCard |
| 462 | // Host comment ids can collide across pull requests. |
| 463 | key={`${reactions.reference.projectId}#${reactions.reference.number}:${event.id}`} |
| 464 | event={event} |
| 465 | editable={editable.get(event.id) ?? null} |
| 466 | cwd={cwd} |
| 467 | onOpen={onOpen} |
| 468 | reactions={reactions} |
| 469 | /> |
| 470 | ))} |
| 471 | </div> |
| 472 | ) : null} |
| 473 | </CollapsiblePanel> |
| 474 | </div> |
| 475 | </Collapsible> |
| 476 | </div> |
| 477 | ); |
| 478 | } |
| 479 | |
| 480 | function ReviewThreadEvent({ |
| 481 | revealAnchor, |
| 482 | event, |
| 483 | thread, |
| 484 | editable, |
| 485 | cwd, |
| 486 | onOpen, |
| 487 | reactions, |
| 488 | detail, |
| 489 | }: { |
| 490 | revealAnchor?: string | null; |
| 491 | event: PullRequestTimelineEvent; |
| 492 | thread: PullRequestTimelineThread; |
| 493 | detail: PullRequestDetailView; |
| 494 | editable: ReadonlyMap<string, PullRequestComment>; |
| 495 | cwd: string; |
| 496 | onOpen: (url: string) => void; |
| 497 | reactions: ReactionSurface; |
| 498 | }) { |
| 499 | const gt = useGT(); |
| 500 | const dismissed = pullRequestReviewOutcome(event.reviewState) === "dismissed"; |
| 501 | const [expanded, setExpanded] = useState( |
| 502 | () => |
| 503 | (revealAnchor != null && |
| 504 | [event, ...thread.replies].some((item) => |
| 505 | pullRequestAnchorMatchesUrl(revealAnchor, item.url), |
| 506 | )) || |
| 507 | (!thread.isResolved && !dismissed), |
| 508 | ); |
| 509 | const replyCount = thread.replies.length + thread.moreReplies; |
| 510 | |
| 511 | return ( |
| 512 | <div className="group relative mb-5 pl-12 [contain-intrinsic-block-size:48px] [content-visibility:auto]"> |
| 513 | <ActorTimelineMarker |
| 514 | actors={event.actor ? [event.actor] : []} |
| 515 | className="top-4" |
| 516 | fallback={<MessageSquareIcon className="size-3.5" />} |
| 517 | muted={!expanded} |
| 518 | /> |
| 519 | <button |
| 520 | type="button" |
| 521 | className="flex w-full min-w-0 items-center gap-2 px-2 py-2 text-left text-2xs text-muted-foreground hover:text-foreground" |
| 522 | aria-expanded={expanded} |
| 523 | onClick={() => setExpanded((current) => !current)} |
| 524 | > |
| 525 | {thread.path === null ? ( |
| 526 | <span className="min-w-0 truncate"> |
| 527 | <T context="review thread without a file">Overview</T> |
| 528 | </span> |
| 529 | ) : ( |
| 530 | <> |
| 531 | <FileCode2Icon aria-hidden className="size-3 shrink-0" /> |
| 532 | <span className="min-w-0 truncate font-mono"> |
| 533 | {thread.line === null ? thread.path : `${thread.path}:${thread.line}`} |
| 534 | </span> |
| 535 | </> |
| 536 | )} |
| 537 | {thread.isResolved ? ( |
| 538 | <span className="inline-flex shrink-0 items-center gap-1 font-medium text-success-foreground"> |
| 539 | <CircleCheckIcon aria-hidden className="size-3" /> |
| 540 | <T context="review thread state">Resolved</T> |
| 541 | </span> |
| 542 | ) : null} |
| 543 | {dismissed ? ( |
| 544 | <span className="shrink-0 font-medium"> |
| 545 | <T>Review dismissed</T> |
| 546 | </span> |
| 547 | ) : null} |
| 548 | {thread.isOutdated ? ( |
| 549 | <span className="shrink-0 font-medium"> |
| 550 | <T context="review thread state">Outdated</T> |
| 551 | </span> |
| 552 | ) : null} |
| 553 | {expanded ? null : ( |
| 554 | <span className="shrink-0"> |
| 555 | · {event.actor?.login ?? "ghost"} |
| 556 | {replyCount > 0 ? ( |
| 557 | <> |
| 558 | {" "} |
| 559 | ·{" "} |
| 560 | {gt("{count, plural, one {{formatted} reply} other {{formatted} replies}}", { |
| 561 | count: replyCount, |
| 562 | formatted: replyCount.toLocaleString(), |
| 563 | })} |
| 564 | </> |
| 565 | ) : null} |
| 566 | </span> |
| 567 | )} |
| 568 | <ChevronDownIcon |
| 569 | aria-hidden |
| 570 | className={cn("ml-auto size-3.5 shrink-0 transition-transform", expanded && "rotate-180")} |
| 571 | /> |
| 572 | </button> |
| 573 | {expanded ? ( |
| 574 | <> |
| 575 | <ConversationCard |
| 576 | event={event} |
| 577 | editable={editable.get(event.id) ?? null} |
| 578 | cwd={cwd} |
| 579 | onOpen={onOpen} |
| 580 | reactions={reactions} |
| 581 | showPath={false} |
| 582 | resolveButton={ |
| 583 | <PullRequestResolveThreadButton |
| 584 | environmentId={reactions.environmentId} |
| 585 | reference={reactions.reference} |
| 586 | detail={detail} |
| 587 | thread={thread} |
| 588 | onRefresh={reactions.onRefresh} |
| 589 | /> |
| 590 | } |
| 591 | /> |
| 592 | {thread.replies.length > 0 ? ( |
| 593 | <div className="ml-2 space-y-1 border-l border-border/60 pl-3"> |
| 594 | {thread.replies.map((reply) => ( |
| 595 | <ConversationCard |
| 596 | key={reply.id} |
| 597 | event={reply} |
| 598 | editable={editable.get(reply.id) ?? null} |
| 599 | cwd={cwd} |
| 600 | onOpen={onOpen} |
| 601 | reactions={reactions} |
| 602 | showPath={false} |
| 603 | reply |
| 604 | /> |
| 605 | ))} |
| 606 | </div> |
| 607 | ) : null} |
| 608 | </> |
| 609 | ) : null} |
| 610 | {thread.hasMoreReplies ? ( |
| 611 | <div className="px-2"> |
| 612 | <PullRequestMoreReplies {...thread} /> |
| 613 | </div> |
| 614 | ) : null} |
| 615 | </div> |
| 616 | ); |
| 617 | } |
| 618 | |
| 619 | function CommitEvent({ |
| 620 | event, |
| 621 | onOpen, |
| 622 | }: { |
| 623 | event: PullRequestTimelineEvent; |
| 624 | onOpen: (oid: string) => void; |
| 625 | }) { |
| 626 | const gt = useGT(); |
| 627 | return ( |
| 628 | <button |
| 629 | type="button" |
| 630 | className="group relative mb-5 block w-full cursor-pointer rounded-sm pl-12 text-left outline-none [contain-intrinsic-block-size:48px] [content-visibility:auto] focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring" |
| 631 | aria-label={gt("View commit {sha}", { sha: event.id })} |
| 632 | onClick={() => onOpen(event.id)} |
| 633 | > |
| 634 | <ActorTimelineMarker |
| 635 | actors={event.commitAuthors} |
| 636 | fallback={<GitCommitHorizontalIcon className="size-3.5" />} |
| 637 | /> |
| 638 | <div className="flex min-w-0 items-center gap-2.5 py-1.5"> |
| 639 | <div className="min-w-0 flex-1"> |
| 640 | <div className="truncate text-xs font-semibold text-foreground transition-colors group-hover:text-primary"> |
| 641 | {event.body ?? gt("Untitled commit")} |
| 642 | </div> |
| 643 | <div className="mt-1 flex flex-wrap items-center gap-x-2 gap-y-0.5 text-3xs text-muted-foreground"> |
| 644 | <code className="font-mono">{event.id.slice(0, 7)}</code> |
| 645 | <span> |
| 646 | <RelativeTime timestamp={event.at} /> |
| 647 | </span> |
| 648 | </div> |
| 649 | </div> |
| 650 | {event.additions !== null && event.deletions !== null ? ( |
| 651 | <PullRequestDiffStat |
| 652 | additions={event.additions} |
| 653 | deletions={event.deletions} |
| 654 | className="ml-auto shrink-0 font-mono text-3xs" |
| 655 | /> |
| 656 | ) : null} |
| 657 | </div> |
| 658 | </button> |
| 659 | ); |
| 660 | } |
| 661 | |
| 662 | function LifecycleEvent({ |
| 663 | event, |
| 664 | term, |
| 665 | }: { |
| 666 | event: PullRequestTimelineEvent; |
| 667 | term: ChangeRequestTermKind; |
| 668 | }) { |
| 669 | const gt = useGT(); |
| 670 | const presentation = |
| 671 | event.kind === "opened" |
| 672 | ? { |
| 673 | icon: <PullRequestGlyph.pullRequest className="size-3.5" />, |
| 674 | label: gt( |
| 675 | "{term, select, mr {Merge request} cr {Change request} other {Pull request}} opened", |
| 676 | { term }, |
| 677 | ), |
| 678 | } |
| 679 | : event.kind === "merged" |
| 680 | ? { |
| 681 | icon: <PullRequestGlyph.merged className="size-3.5" />, |
| 682 | label: gt( |
| 683 | "{term, select, mr {Merge request} cr {Change request} other {Pull request}} merged", |
| 684 | { term }, |
| 685 | ), |
| 686 | } |
| 687 | : { |
| 688 | icon: <PullRequestGlyph.closed className="size-3.5" />, |
| 689 | label: gt( |
| 690 | "{term, select, mr {Merge request} cr {Change request} other {Pull request}} closed", |
| 691 | { term }, |
| 692 | ), |
| 693 | }; |
| 694 | |
| 695 | return ( |
| 696 | <div className="relative mb-5 pl-12 [contain-intrinsic-block-size:48px] [content-visibility:auto]"> |
| 697 | <IconMarker icon={presentation.icon} /> |
| 698 | <div className="py-1.5 text-xs"> |
| 699 | <div className="flex flex-wrap items-center gap-1.5"> |
| 700 | {event.actor ? <ActorName actor={event.actor} /> : null} |
| 701 | <span className="font-semibold text-foreground">{presentation.label}</span> |
| 702 | </div> |
| 703 | <div className="mt-0.5 text-2xs text-muted-foreground"> |
| 704 | <RelativeTime timestamp={event.at} /> |
| 705 | </div> |
| 706 | </div> |
| 707 | </div> |
| 708 | ); |
| 709 | } |
| 710 | |
| 711 | function staleReviewOutcomeLabel( |
| 712 | outcome: PullRequestReviewOutcome, |
| 713 | gt: ReturnType<typeof useGT>, |
| 714 | ): string { |
| 715 | switch (outcome) { |
| 716 | case "approved": |
| 717 | return gt("Approved, before the latest commits"); |
| 718 | case "changes-requested": |
| 719 | return gt("Changes requested, before the latest commits"); |
| 720 | case "dismissed": |
| 721 | return gt("Review dismissed, before the latest commits"); |
| 722 | } |
| 723 | } |
| 724 | |
| 725 | function ReviewVerdictEvent({ |
| 726 | event, |
| 727 | outcome, |
| 728 | stale, |
| 729 | cwd, |
| 730 | onOpen, |
| 731 | reactions, |
| 732 | }: { |
| 733 | event: PullRequestTimelineEvent; |
| 734 | outcome: PullRequestReviewOutcome; |
| 735 | stale: boolean; |
| 736 | cwd: string; |
| 737 | onOpen: (url: string) => void; |
| 738 | reactions: ReactionSurface; |
| 739 | }) { |
| 740 | const gt = useGT(); |
| 741 | const resolve = useMessages(); |
| 742 | const canReact = reactions.canReact(event); |
| 743 | return ( |
| 744 | <div className="group relative mb-5 pl-12 [contain-intrinsic-block-size:48px] [content-visibility:auto]"> |
| 745 | <ActorTimelineMarker |
| 746 | actors={event.actor ? [event.actor] : []} |
| 747 | className="top-6" |
| 748 | fallback={<PullRequestReviewOutcomeIcon outcome={outcome} />} |
| 749 | /> |
| 750 | <div className="flex min-w-0 flex-wrap items-center gap-2 py-1.5"> |
| 751 | <div className="min-w-0 flex-1"> |
| 752 | <h2 className="flex min-w-0 flex-wrap items-center gap-x-1.5 gap-y-1 text-xs"> |
| 753 | <ActorName actor={event.actor} /> |
| 754 | <Tooltip disabled={!stale}> |
| 755 | <TooltipTrigger |
| 756 | render={ |
| 757 | <span |
| 758 | className={cn( |
| 759 | "font-medium lowercase", |
| 760 | stale |
| 761 | ? "text-muted-foreground opacity-70" |
| 762 | : pullRequestReviewOutcomeToneClassName(outcome), |
| 763 | )} |
| 764 | /> |
| 765 | } |
| 766 | > |
| 767 | {stale ? ( |
| 768 | <> |
| 769 | <span aria-hidden>{pullRequestReviewOutcomeLabel(outcome, resolve)}</span> |
| 770 | <span className="sr-only">{staleReviewOutcomeLabel(outcome, gt)}</span> |
| 771 | </> |
| 772 | ) : ( |
| 773 | pullRequestReviewOutcomeLabel(outcome, resolve) |
| 774 | )} |
| 775 | </TooltipTrigger> |
| 776 | <TooltipPopup>{pullRequestReviewOutcomeStaleLabel(outcome, resolve)}</TooltipPopup> |
| 777 | </Tooltip> |
| 778 | </h2> |
| 779 | <div className="mt-0.5 flex min-w-0 flex-wrap items-center gap-x-2 gap-y-1"> |
| 780 | <PullRequestMetaLine className="flex-wrap text-2xs text-muted-foreground"> |
| 781 | <span> |
| 782 | <RelativeTime timestamp={event.at} /> |
| 783 | </span> |
| 784 | {event.path ? ( |
| 785 | <span className="inline-flex min-w-0 items-center gap-1"> |
| 786 | <FileCode2Icon aria-hidden className="size-3 shrink-0" /> |
| 787 | <span className="truncate">{event.path}</span> |
| 788 | </span> |
| 789 | ) : null} |
| 790 | </PullRequestMetaLine> |
| 791 | </div> |
| 792 | </div> |
| 793 | {event.body && event.comment ? ( |
| 794 | <AddToChatButton comment={event.comment} reactions={reactions} /> |
| 795 | ) : null} |
| 796 | {canReact || event.reactions.length > 0 ? ( |
| 797 | <PullRequestReactionBar |
| 798 | className="ml-auto justify-end" |
| 799 | reactions={event.reactions} |
| 800 | canReact={canReact} |
| 801 | subjectId={event.id} |
| 802 | environmentId={reactions.environmentId} |
| 803 | reference={reactions.reference} |
| 804 | onRefresh={reactions.onRefresh} |
| 805 | /> |
| 806 | ) : null} |
| 807 | <OpenOnHostButton url={event.url} onOpen={onOpen} /> |
| 808 | </div> |
| 809 | {event.body ? ( |
| 810 | <TimelineBody |
| 811 | body={event.body} |
| 812 | markdown={event.markdown} |
| 813 | cwd={cwd} |
| 814 | environmentId={reactions.environmentId} |
| 815 | threadRef={reactions.threadRef} |
| 816 | /> |
| 817 | ) : null} |
| 818 | </div> |
| 819 | ); |
| 820 | } |
| 821 | |
| 822 | export function PullRequestTimelineTab({ |
| 823 | revealUrl, |
| 824 | onRevealSettled, |
| 825 | scrollerRef, |
| 826 | detail, |
| 827 | environmentId, |
| 828 | threadRef = null, |
| 829 | reference, |
| 830 | order, |
| 831 | onOpenCommit, |
| 832 | onAddToChat, |
| 833 | onRefresh, |
| 834 | }: { |
| 835 | /** A clicked link to scroll to and mark, until onRevealSettled reports it shown. */ |
| 836 | revealUrl?: string | undefined; |
| 837 | onRevealSettled?: ((revealUrl: string) => void) | undefined; |
| 838 | scrollerRef: (node: HTMLElement | null) => void; |
| 839 | detail: PullRequestDetailView; |
| 840 | environmentId: EnvironmentId; |
| 841 | threadRef?: ScopedThreadRef | null; |
| 842 | reference: PullRequestRef; |
| 843 | order: "newest" | "oldest"; |
| 844 | onOpenCommit: (oid: string) => void; |
| 845 | onAddToChat?: (subject: PullRequestChatSubject) => void; |
| 846 | onRefresh: () => void; |
| 847 | }) { |
| 848 | const resolve = useMessages(); |
| 849 | const events = buildPullRequestTimeline(detail, resolve); |
| 850 | const location = parsePullRequestAnchor(revealUrl); |
| 851 | const revealAnchor = location?.kind === "timeline" ? location.anchor : null; |
| 852 | // The linked item stays marked and open after it is shown, until another link replaces it. |
| 853 | const [linked, setLinked] = useState<{ readonly anchor: string; readonly count: number } | null>( |
| 854 | () => (revealAnchor === null ? null : { anchor: revealAnchor, count: 1 }), |
| 855 | ); |
| 856 | const [seenRevealUrl, setSeenRevealUrl] = useState(revealUrl); |
| 857 | if (revealUrl !== seenRevealUrl) { |
| 858 | setSeenRevealUrl(revealUrl); |
| 859 | // Each click remounts the linked row, so it opens again even after the user closed it. |
| 860 | if (revealAnchor !== null) setLinked({ anchor: revealAnchor, count: (linked?.count ?? 0) + 1 }); |
| 861 | } |
| 862 | const anchor = linked |
| 863 | ? resolvePullRequestReviewAnchor(linked.anchor, detail.comments, detail.reviewThreads) |
| 864 | : null; |
| 865 | const listRef = useRef<LegendListRef | null>(null); |
| 866 | const matches = (event: PullRequestTimelineEvent) => |
| 867 | anchor !== null && pullRequestTimelineEventMatches(event, anchor); |
| 868 | |
| 869 | const term = changeRequestTermKind(sourceControlClients.get(detail.provider).changeRequest); |
| 870 | const newestCommitAt = newestPullRequestCommitAt(detail.commits); |
| 871 | const reactions: ReactionSurface = { |
| 872 | revealAnchor: anchor, |
| 873 | canReact: (event) => |
| 874 | event.comment !== undefined && canReactPullRequestComment(detail, event.comment), |
| 875 | onAddToChat: onAddToChat && ((comment) => onAddToChat({ kind: "comment", comment })), |
| 876 | environmentId, |
| 877 | threadRef, |
| 878 | reference, |
| 879 | completionScope: pullRequestCompletionScope(detail, environmentId, reference), |
| 880 | onRefresh, |
| 881 | }; |
| 882 | const editable = new Map( |
| 883 | events |
| 884 | .flatMap((event) => [event, ...(event.thread?.replies ?? [])]) |
| 885 | .flatMap((event) => (event.comment ? [event.comment] : [])) |
| 886 | .filter((comment) => canEditPullRequestComment(detail, comment)) |
| 887 | .map((comment) => [comment.id, comment] as const), |
| 888 | ); |
| 889 | const orderedEvents = order === "newest" ? events : events.toReversed(); |
| 890 | const rows = groupPullRequestTimelineConversations(orderedEvents); |
| 891 | const targetIndex = rows.findIndex((row) => |
| 892 | row.kind === "comments" ? row.events.some(matches) : matches(row.event), |
| 893 | ); |
| 894 | const stopHolding = useRef<(() => void) | null>(null); |
| 895 | const reveal = useEffectEvent(() => { |
| 896 | const list = listRef.current; |
| 897 | const url = revealUrl; |
| 898 | if (url === undefined || revealAnchor === null || targetIndex < 0 || !list) return; |
| 899 | stopHolding.current?.(); |
| 900 | void list.scrollToIndex({ index: targetIndex, viewPosition: 0, animated: false }).then(() => { |
| 901 | const scroller = list.getScrollableNode(); |
| 902 | const node = scroller?.querySelector<HTMLElement>("[data-pr-link-target=true]"); |
| 903 | if (scroller && node) stopHolding.current = holdLinkedNodeInView(node, scroller, () => 16); |
| 904 | onRevealSettled?.(url); |
| 905 | }); |
| 906 | }); |
| 907 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- reveals a link destination after activity loading and virtual list mounting |
| 908 | useEffect(() => { |
| 909 | reveal(); |
| 910 | // oxlint-disable-next-line react/exhaustive-effect-dependencies -- runs for each clicked link and again when activity resolves its row |
| 911 | }, [revealUrl, targetIndex]); |
| 912 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- the hold observes DOM outside React and must end with the tab |
| 913 | useEffect(() => () => stopHolding.current?.(), []); |
| 914 | const openOnHost = (url: string) => { |
| 915 | void readLocalApi()?.shell.openExternal(url); |
| 916 | }; |
| 917 | |
| 918 | const rowKey = (row: PullRequestTimelineRow) => { |
| 919 | if (row.kind === "comments") |
| 920 | return `comments:${row.events[0]?.id ?? "empty"}:${row.events.some(matches) ? (linked?.count ?? "") : ""}`; |
| 921 | const event = row.event; |
| 922 | // A thread's resolution or dismissal remounts it, so its disclosure starts in the new state. |
| 923 | return event.thread === undefined |
| 924 | ? `${event.id}:${matches(event) ? (linked?.count ?? "") : ""}` |
| 925 | : `thread:${event.id}:${matches(event) ? (linked?.count ?? "") : ""}:${event.thread.isResolved}:${pullRequestReviewOutcome(event.reviewState) === "dismissed"}`; |
| 926 | }; |
| 927 | const renderRow = ({ item: row }: { item: PullRequestTimelineRow }) => { |
| 928 | if (row.kind === "comments") { |
| 929 | return ( |
| 930 | <ConversationGroup |
| 931 | events={row.events} |
| 932 | revealAnchor={anchor} |
| 933 | editable={editable} |
| 934 | cwd={detail.workspaceRoot} |
| 935 | onOpen={openOnHost} |
| 936 | reactions={reactions} |
| 937 | /> |
| 938 | ); |
| 939 | } |
| 940 | const event = row.event; |
| 941 | if (event.thread !== undefined) { |
| 942 | return ( |
| 943 | <ReviewThreadEvent |
| 944 | event={event} |
| 945 | thread={event.thread} |
| 946 | detail={detail} |
| 947 | revealAnchor={anchor} |
| 948 | editable={editable} |
| 949 | cwd={detail.workspaceRoot} |
| 950 | onOpen={openOnHost} |
| 951 | reactions={reactions} |
| 952 | /> |
| 953 | ); |
| 954 | } |
| 955 | if (event.kind === "commit") return <CommitEvent event={event} onOpen={onOpenCommit} />; |
| 956 | const outcome = pullRequestReviewOutcome(event.reviewState); |
| 957 | if (outcome !== null) { |
| 958 | return ( |
| 959 | <ReviewVerdictEvent |
| 960 | event={event} |
| 961 | outcome={outcome} |
| 962 | stale={isPullRequestVerdictStale(event.at, newestCommitAt)} |
| 963 | cwd={detail.workspaceRoot} |
| 964 | onOpen={openOnHost} |
| 965 | reactions={reactions} |
| 966 | /> |
| 967 | ); |
| 968 | } |
| 969 | return <LifecycleEvent event={event} term={term} />; |
| 970 | }; |
| 971 | |
| 972 | if (events.length === 0) { |
| 973 | return ( |
| 974 | <div ref={scrollerRef} className="scrollbar-gutter-both h-full overflow-y-auto px-4 py-5"> |
| 975 | <div className="flex flex-col items-center justify-center py-16 text-center text-muted-foreground"> |
| 976 | <PullRequestGlyph.pullRequest className="mb-2 size-5" /> |
| 977 | <p className="text-xs"> |
| 978 | <T>No activity yet.</T> |
| 979 | </p> |
| 980 | </div> |
| 981 | </div> |
| 982 | ); |
| 983 | } |
| 984 | |
| 985 | // Virtualized: a large pull request has thousands of events, and mounting every one stalls |
| 986 | // the page. The connector line is drawn by the content container, behind the rows. |
| 987 | return ( |
| 988 | <LegendList<PullRequestTimelineRow> |
| 989 | ref={(list: LegendListRef | null) => { |
| 990 | listRef.current = list; |
| 991 | scrollerRef(list?.getScrollableNode() ?? null); |
| 992 | }} |
| 993 | data={rows} |
| 994 | extraData={{ editable, reactions, newestCommitAt }} |
| 995 | keyExtractor={rowKey} |
| 996 | renderItem={renderRow} |
| 997 | estimatedItemSize={140} |
| 998 | className="scrollbar-gutter-both h-full px-4" |
| 999 | contentContainerClassName="relative mx-auto w-full max-w-3xl before:absolute before:bottom-10 before:left-[15px] before:top-6 before:w-px before:bg-border/45" |
| 1000 | ListHeaderComponent={ |
| 1001 | anchor !== null && targetIndex < 0 && revealUrl === undefined ? ( |
| 1002 | <p role="status" className="py-4 text-xs text-muted-foreground"> |
| 1003 | <T>The linked item is not in the loaded activity.</T> |
| 1004 | </p> |
| 1005 | ) : ( |
| 1006 | <div className="h-5" /> |
| 1007 | ) |
| 1008 | } |
| 1009 | ListFooterComponent={<div className="h-5" />} |
| 1010 | /> |
| 1011 | ); |
| 1012 | } |