@@ -15,7 +15,6 @@ import type { |
15 | 15 | PullRequestDiffSide, |
16 | 16 | PullRequestDiffResult, |
17 | 17 | PullRequestRef, |
18 | | - PullRequestReviewPosition, |
19 | 18 | PullRequestReviewThread, |
20 | 19 | PullRequestThreadCommentsResult, |
21 | 20 | } from "@t3tools/contracts"; |
@@ -45,6 +44,7 @@ import { |
45 | 44 | useRef, |
46 | 45 | useState, |
47 | 46 | type ReactNode, |
| 47 | + type ComponentProps, |
48 | 48 | } from "react"; |
49 | 49 |
|
50 | 50 | import { observeIntersection } from "~/lib/observeIntersection"; |
@@ -150,6 +150,8 @@ import { |
150 | 150 | usePendingReviewComments, |
151 | 151 | usePullRequestReviewStore, |
152 | 152 | type PendingReviewComment, |
| 153 | + type PullRequestDraftAnchor, |
| 154 | + type PullRequestSelectedLines, |
153 | 155 | } from "./pullRequestReviewStore"; |
154 | 156 |
|
155 | 157 | interface ReviewAnnotationGroup { |
@@ -329,14 +331,31 @@ interface MutableAnnotationGroup { |
329 | 331 | draft: boolean; |
330 | 332 | } |
331 | 333 |
|
332 | | -interface DraftAnchor { |
333 | | - readonly fileKey: string; |
334 | | - readonly contentVersion: number; |
335 | | - readonly path: string; |
336 | | - readonly oldPath: string | null; |
337 | | - readonly position: PullRequestReviewPosition; |
338 | | - readonly startPosition?: PullRequestReviewPosition; |
339 | | - readonly range: SelectedLineRange; |
| 334 | +type DraftAnchor = PullRequestDraftAnchor; |
| 335 | + |
| 336 | +// Subscribes to the stored text itself so typing re-renders only the composer. |
| 337 | +function StoredDraftCommentAnnotation({ |
| 338 | + codeViewKey, |
| 339 | + ...props |
| 340 | +}: Omit<ComponentProps<typeof DiffCommentAnnotation>, "text" | "onTextChange"> & { |
| 341 | + readonly codeViewKey: string; |
| 342 | +}) { |
| 343 | + const text = usePullRequestReviewStore( |
| 344 | + (store) => (store.codeViews[codeViewKey] ?? EMPTY_CODE_VIEW).draftText, |
| 345 | + ); |
| 346 | + return ( |
| 347 | + <DiffCommentAnnotation |
| 348 | + {...props} |
| 349 | + text={text} |
| 350 | + onTextChange={(next) => |
| 351 | + usePullRequestReviewStore |
| 352 | + .getState() |
| 353 | + .updateCodeView(codeViewKey, (current) => |
| 354 | + current.draftText === next ? current : { ...current, draftText: next }, |
| 355 | + ) |
| 356 | + } |
| 357 | + /> |
| 358 | + ); |
340 | 359 | } |
341 | 360 |
|
342 | 361 | export interface PullRequestAgentSelectionInput { |
@@ -419,12 +438,21 @@ function PullRequestCodeTab({ |
419 | 438 | Schema.Boolean, |
420 | 439 | ); |
421 | 440 | const [hideTestFiles, setHideTestFiles] = useHideTestFiles(); |
422 | | - const [selectedLines, setSelectedLines] = useState<{ |
423 | | - id: string; |
424 | | - contentVersion: number; |
425 | | - range: SelectedLineRange; |
426 | | - } | null>(null); |
427 | | - const [draft, setDraft] = useState<DraftAnchor | null>(null); |
| 441 | + // The panel unmounts when another right-panel surface opens; the draft outlives it. |
| 442 | + const savedSelectedLines = usePullRequestReviewStore( |
| 443 | + (store) => (store.codeViews[codeViewKey] ?? EMPTY_CODE_VIEW).selectedLines, |
| 444 | + ); |
| 445 | + const savedDraft = usePullRequestReviewStore( |
| 446 | + (store) => (store.codeViews[codeViewKey] ?? EMPTY_CODE_VIEW).draft, |
| 447 | + ); |
| 448 | + const setSelectedLines = (next: PullRequestSelectedLines | null) => |
| 449 | + updateCodeView(codeViewKey, (current) => |
| 450 | + current.selectedLines === next ? current : { ...current, selectedLines: next }, |
| 451 | + ); |
| 452 | + const setDraft = (next: DraftAnchor | null) => |
| 453 | + updateCodeView(codeViewKey, (current) => |
| 454 | + current.draft === next ? current : { ...current, draft: next, draftText: "" }, |
| 455 | + ); |
428 | 456 | const [threadPending, setThreadPending] = useState(false); |
429 | 457 | const [orphansOpen, setOrphansOpen] = useState(false); |
430 | 458 | // Scope slices before reset to prevent using a previous pull request's data or cursor. |
@@ -447,17 +475,14 @@ function PullRequestCodeTab({ |
447 | 475 | const [scrollRestoration, setScrollRestoration] = useState( |
448 | 476 | () => new PullRequestCodeScrollRestoration(readPullRequestCodeView(codeViewKey)), |
449 | 477 | ); |
450 | | - // The panel keeps this mounted across pull requests, so an open composer would otherwise |
451 | | - // survive the switch and attach its comment to whichever one is on screen when it is sent. |
| 478 | + // The panel keeps this mounted across pull requests; the composer is kept per pull request. |
452 | 479 | const [stateScopeKey, setStateScopeKey] = useState(scopeKey); |
453 | 480 | if (stateScopeKey !== scopeKey) { |
454 | 481 | setStateScopeKey(scopeKey); |
455 | 482 | setLastViewedFileKey(null); |
456 | 483 | setScrollRestoration( |
457 | 484 | new PullRequestCodeScrollRestoration(readPullRequestCodeView(codeViewKey)), |
458 | 485 | ); |
459 | | - setDraft(null); |
460 | | - setSelectedLines(null); |
461 | 486 | setVisibleCommitCount(COMMIT_PAGE_SIZE); |
462 | 487 | setOrphansOpen(false); |
463 | 488 | setSliceState({ key: scopeKey, cursor: null, slices: NO_SLICES }); |
@@ -662,19 +687,19 @@ function PullRequestCodeTab({ |
662 | 687 | const hiddenTestFileCount = loadedFiles.length - files.length; |
663 | 688 | // A file on a page not read yet is not gone; only a complete diff can say it is. |
664 | 689 | const diffComplete = loadedSlices.length > 0 && loadedSlices.at(-1)?.nextCursor === null; |
665 | | - if ( |
666 | | - selectedLines !== null && |
667 | | - isPullRequestSelectionStale( |
| 690 | + const selectedLines = |
| 691 | + savedSelectedLines !== null && |
| 692 | + !isPullRequestSelectionStale( |
668 | 693 | loadedFiles, |
669 | | - { ...selectedLines, fileKey: selectedLines.id }, |
| 694 | + { ...savedSelectedLines, fileKey: savedSelectedLines.id }, |
670 | 695 | diffComplete, |
671 | 696 | ) |
672 | | - ) { |
673 | | - setSelectedLines(null); |
674 | | - } |
675 | | - if (draft !== null && isPullRequestSelectionStale(loadedFiles, draft, diffComplete)) { |
676 | | - setDraft(null); |
677 | | - } |
| 697 | + ? savedSelectedLines |
| 698 | + : null; |
| 699 | + const draft = |
| 700 | + savedDraft !== null && !isPullRequestSelectionStale(loadedFiles, savedDraft, diffComplete) |
| 701 | + ? savedDraft |
| 702 | + : null; |
678 | 703 | const changeSelectedLines = (selection: { id: string; range: SelectedLineRange } | null) => { |
679 | 704 | const file = |
680 | 705 | selection === null |
@@ -1159,13 +1184,13 @@ function PullRequestCodeTab({ |
1159 | 1184 | /> |
1160 | 1185 | ))} |
1161 | 1186 | {annotation.metadata.draft && draft && canSelectLines ? ( |
1162 | | - <DiffCommentAnnotation |
| 1187 | + <StoredDraftCommentAnnotation |
| 1188 | + codeViewKey={codeViewKey} |
1163 | 1189 | environmentId={environmentId} |
1164 | 1190 | projectId={reference.projectId} |
1165 | 1191 | ownerKey={JSON.stringify([environmentId, reviewKey, draft])} |
1166 | 1192 | kind="draft" |
1167 | 1193 | rangeLabel={`${draft.path}:${getReviewPositionAnchor(draft.position).line}`} |
1168 | | - text="" |
1169 | 1194 | completionScope={canCommentOnLines ? completionScope : null} |
1170 | 1195 | submitLabel={canCommentOnLines ? "Add to review" : "Add to agent"} |
1171 | 1196 | {...(canCommentOnLines && onAddToAgentSelection |
|