@@ -27,6 +27,7 @@ import {
2727import { useAtomRefresh } from "@effect/atom-react" ;
2828import * as Schema from "effect/Schema" ;
2929import { useCallback , useEffect , useMemo , useRef , useState , type ReactNode } from "react" ;
30+ import { useShallow } from "zustand/react/shallow" ;
3031
3132import { useLocalStorage } from "~/hooks/useLocalStorage" ;
3233import { useClientSettings , useUpdateClientSettings } from "~/hooks/useSettings" ;
@@ -151,6 +152,47 @@ interface DraftAnchor {
151152 readonly range : SelectedLineRange ;
152153}
153154
155+ /**
156+ * The draft card subscribes to the stored text itself so a keystroke re-renders this one editor
157+ * instead of the code tab, whose renderAnnotation feeds the viewer's portal memoization.
158+ */
159+ function LineDraftAnnotation ( {
160+ lineDraftKey,
161+ anchor,
162+ fileMissing,
163+ secondaryAction,
164+ onCancel,
165+ onComment,
166+ } : {
167+ readonly lineDraftKey : string ;
168+ readonly anchor : DraftAnchor ;
169+ readonly fileMissing : boolean ;
170+ readonly secondaryAction :
171+ | { readonly label : string ; readonly onAction : ( text : string ) => void }
172+ | undefined ;
173+ readonly onCancel : ( ) => void ;
174+ readonly onComment : ( text : string ) => void ;
175+ } ) {
176+ const text = usePullRequestReviewStore ( ( store ) => store . lineDrafts [ lineDraftKey ] ?. text ?? "" ) ;
177+ return (
178+ < DiffCommentAnnotation
179+ kind = "draft"
180+ rangeLabel = { `${ anchor . path } :${ getReviewPositionAnchor ( anchor . position ) . line } ` }
181+ text = { text }
182+ onTextChange = { ( next ) => {
183+ const store = usePullRequestReviewStore . getState ( ) ;
184+ const current = store . lineDrafts [ lineDraftKey ] ;
185+ if ( current ) store . setLineDraft ( lineDraftKey , { ...current , text : next } ) ;
186+ } }
187+ submitLabel = "Add to review"
188+ submitDisabled = { fileMissing }
189+ { ...( secondaryAction ? { secondaryAction } : { } ) }
190+ onCancel = { onCancel }
191+ onComment = { onComment }
192+ />
193+ ) ;
194+ }
195+
154196/** A range of the diff and the reader's request for the agent. */
155197export interface PullRequestAgentSelectionInput {
156198 /** The marked lines, already in the shape the composer draws and the agent reads. */
@@ -261,21 +303,25 @@ function PullRequestCodeTab({
261303 // everything below is keyed by both.
262304 const scopeKey = commit === null ? referenceKey : `${ referenceKey } @${ commit } ` ;
263305 const lineDraftKey = JSON . stringify ( [ environmentId , scopeKey ] ) ;
264- const draft = usePullRequestReviewStore ( ( store ) => store . lineDrafts [ lineDraftKey ] ?? null ) ;
306+ // Anchor fields only: the stored draft gets a new identity on every keystroke, and the
307+ // callbacks below feed the viewer's portal memoization, so subscribing to `text` here would
308+ // remount every visible file header on each character typed.
309+ const draft = usePullRequestReviewStore (
310+ useShallow ( ( store ) => {
311+ const current = store . lineDrafts [ lineDraftKey ] ;
312+ if ( ! current ) return null ;
313+ const { text : _text , ...anchor } = current ;
314+ return anchor ;
315+ } ) ,
316+ ) ;
265317 const setLineDraft = usePullRequestReviewStore ( ( store ) => store . setLineDraft ) ;
266318 const setDraft = useCallback (
267319 ( next : DraftAnchor | null ) => {
268320 setLineDraft ( lineDraftKey , next === null ? null : { ...next , text : "" } ) ;
269321 } ,
270322 [ lineDraftKey , setLineDraft ] ,
271323 ) ;
272- const setDraftText = useCallback (
273- ( text : string ) => {
274- const current = usePullRequestReviewStore . getState ( ) . lineDrafts [ lineDraftKey ] ;
275- if ( current ) setLineDraft ( lineDraftKey , { ...current , text } ) ;
276- } ,
277- [ lineDraftKey , setLineDraft ] ,
278- ) ;
324+
279325 // The editor is keyed separately; resetting the viewer must not discard its draft.
280326 useEffect ( ( ) => {
281327 setSelectedLines ( null ) ;
@@ -1017,24 +1063,21 @@ function PullRequestCodeTab({
10171063 />
10181064 ) ) }
10191065 { annotation . metadata . draft && draft ? (
1020- < DiffCommentAnnotation
1021- kind = "draft"
1022- rangeLabel = { `${ draft . path } :${ getReviewPositionAnchor ( draft . position ) . line } ` }
1023- text = { draft . text }
1024- onTextChange = { setDraftText }
1025- submitLabel = "Add to review"
1026- submitDisabled = { ! files . some ( ( file ) => buildFileDiffRenderKey ( file ) === draft . fileKey ) }
1027- { ...( onAddToAgentSelection
1028- ? {
1029- secondaryAction : {
1066+ < LineDraftAnnotation
1067+ lineDraftKey = { lineDraftKey }
1068+ anchor = { draft }
1069+ fileMissing = { ! files . some ( ( file ) => buildFileDiffRenderKey ( file ) === draft . fileKey ) }
1070+ secondaryAction = {
1071+ onAddToAgentSelection
1072+ ? {
10301073 label : "Add to agent" ,
10311074 onAction : ( text : string ) =>
10321075 finishSelection ( draft , text , ( comment ) =>
10331076 onAddToAgentSelection ( { comment, request : text } ) ,
10341077 ) ,
1035- } ,
1036- }
1037- : { } ) }
1078+ }
1079+ : undefined
1080+ }
10381081 onCancel = { ( ) => {
10391082 setDraft ( null ) ;
10401083 setSelectedLines ( null ) ;
@@ -1065,7 +1108,6 @@ function PullRequestCodeTab({
10651108 renderThreadCard ,
10661109 reviewKey ,
10671110 setDraft ,
1068- setDraftText ,
10691111 ] ,
10701112 ) ;
10711113
0 commit comments