@@ -137,8 +137,19 @@ export function ComposerEditor({
137137 const [ , forceNativeEventRender ] = useState ( 0 ) ;
138138 // Start history empty so restored drafts or recycled views receive a non-echo controlled payload.
139139 const nativeEventSnapshotsRef = useRef < ComposerNativeEventSnapshot [ ] > ( [ ] ) ;
140- const [ initialConfirmedTokens ] = useState ( ( ) => collectComposerInlineTokens ( props . value ) ) ;
141- const confirmedTokensRef = useRef ( initialConfirmedTokens ) ;
140+ // Tokens advance from the previous committed value; render-phase state keeps
141+ // an abandoned render from leaking a trailing token into the next one.
142+ const [ confirmedTokens , setConfirmedTokens ] = useState ( ( ) => ( {
143+ value : props . value ,
144+ tokens : collectComposerInlineTokens ( props . value ) ,
145+ } ) ) ;
146+ let tokens = confirmedTokens . tokens ;
147+ if ( confirmedTokens . value !== props . value ) {
148+ tokens = collectComposerInlineTokens ( props . value , {
149+ preserveTrailingFrom : confirmedTokens . tokens ,
150+ } ) ;
151+ setConfirmedTokens ( { value : props . value , tokens } ) ;
152+ }
142153 const theme = useUniwindTheme ( ) ;
143154 const handlePaste = useNativePaste ( ( uris ) => onPasteImages ?.( uris ) ) ;
144155
@@ -156,12 +167,6 @@ export function ComposerEditor({
156167 const skillLabels = useMemo ( ( ) => composerSkillLabels ( skills ) , [ skills ] ) ;
157168 const chipTextColor = createNativeComposerTheme ( theme ) . text ;
158169 const tokensJson = useMemo ( ( ) => {
159- /* oxlint-disable react/refs -- Native controlled edits must reconcile against the last acknowledged native event before sending the next payload. */
160- const tokens = collectComposerInlineTokens ( props . value , {
161- preserveTrailingFrom : confirmedTokensRef . current ,
162- } ) ;
163- confirmedTokensRef . current = tokens ;
164- /* oxlint-enable react/refs */
165170 return JSON . stringify (
166171 composerContextEditorTokens ( props . value , tokens , skillLabels ) . map ( ( token ) => {
167172 const record =
@@ -194,7 +199,7 @@ export function ComposerEditor({
194199 } ;
195200 } ) ,
196201 ) ;
197- } , [ props . value , props . context , skillLabels , theme , chipTextColor , confirmedTokensRef ] ) ;
202+ } , [ props . value , tokens , props . context , skillLabels , theme , chipTextColor ] ) ;
198203 // Every render resolves against the snapshot history, so a render whose
199204 // (value, selection) lags the acknowledged native state is stamped behind
200205 // the native revision and rejected by the editor instead of re-applying a
0 commit comments