@@ -15,7 +15,6 @@ import type {
1515 PullRequestDiffSide ,
1616 PullRequestDiffResult ,
1717 PullRequestRef ,
18- PullRequestReviewPosition ,
1918 PullRequestReviewThread ,
2019 PullRequestThreadCommentsResult ,
2120} from "@t3tools/contracts" ;
@@ -45,6 +44,7 @@ import {
4544 useRef ,
4645 useState ,
4746 type ReactNode ,
47+ type ComponentProps ,
4848} from "react" ;
4949
5050import { observeIntersection } from "~/lib/observeIntersection" ;
@@ -150,6 +150,8 @@ import {
150150 usePendingReviewComments ,
151151 usePullRequestReviewStore ,
152152 type PendingReviewComment ,
153+ type PullRequestDraftAnchor ,
154+ type PullRequestSelectedLines ,
153155} from "./pullRequestReviewStore" ;
154156
155157interface ReviewAnnotationGroup {
@@ -329,14 +331,31 @@ interface MutableAnnotationGroup {
329331 draft : boolean ;
330332}
331333
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+ ) ;
340359}
341360
342361export interface PullRequestAgentSelectionInput {
@@ -419,12 +438,21 @@ function PullRequestCodeTab({
419438 Schema . Boolean ,
420439 ) ;
421440 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+ ) ;
428456 const [ threadPending , setThreadPending ] = useState ( false ) ;
429457 const [ orphansOpen , setOrphansOpen ] = useState ( false ) ;
430458 // Scope slices before reset to prevent using a previous pull request's data or cursor.
@@ -447,17 +475,14 @@ function PullRequestCodeTab({
447475 const [ scrollRestoration , setScrollRestoration ] = useState (
448476 ( ) => new PullRequestCodeScrollRestoration ( readPullRequestCodeView ( codeViewKey ) ) ,
449477 ) ;
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.
452479 const [ stateScopeKey , setStateScopeKey ] = useState ( scopeKey ) ;
453480 if ( stateScopeKey !== scopeKey ) {
454481 setStateScopeKey ( scopeKey ) ;
455482 setLastViewedFileKey ( null ) ;
456483 setScrollRestoration (
457484 new PullRequestCodeScrollRestoration ( readPullRequestCodeView ( codeViewKey ) ) ,
458485 ) ;
459- setDraft ( null ) ;
460- setSelectedLines ( null ) ;
461486 setVisibleCommitCount ( COMMIT_PAGE_SIZE ) ;
462487 setOrphansOpen ( false ) ;
463488 setSliceState ( { key : scopeKey , cursor : null , slices : NO_SLICES } ) ;
@@ -662,19 +687,19 @@ function PullRequestCodeTab({
662687 const hiddenTestFileCount = loadedFiles . length - files . length ;
663688 // A file on a page not read yet is not gone; only a complete diff can say it is.
664689 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 (
668693 loadedFiles ,
669- { ...selectedLines , fileKey : selectedLines . id } ,
694+ { ...savedSelectedLines , fileKey : savedSelectedLines . id } ,
670695 diffComplete ,
671696 )
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 ;
678703 const changeSelectedLines = ( selection : { id : string ; range : SelectedLineRange } | null ) => {
679704 const file =
680705 selection === null
@@ -1159,13 +1184,13 @@ function PullRequestCodeTab({
11591184 />
11601185 ) ) }
11611186 { annotation . metadata . draft && draft && canSelectLines ? (
1162- < DiffCommentAnnotation
1187+ < StoredDraftCommentAnnotation
1188+ codeViewKey = { codeViewKey }
11631189 environmentId = { environmentId }
11641190 projectId = { reference . projectId }
11651191 ownerKey = { JSON . stringify ( [ environmentId , reviewKey , draft ] ) }
11661192 kind = "draft"
11671193 rangeLabel = { `${ draft . path } :${ getReviewPositionAnchor ( draft . position ) . line } ` }
1168- text = ""
11691194 completionScope = { canCommentOnLines ? completionScope : null }
11701195 submitLabel = { canCommentOnLines ? "Add to review" : "Add to agent" }
11711196 { ...( canCommentOnLines && onAddToAgentSelection
0 commit comments