1- import { useEffect , useState } from "react" ;
1+ import { useEffect , useRef , useState } from "react" ;
22import type { AssistantCitationRequest , AssistantCitationTarget } from "./AssistantCitationSource" ;
33import type { CitationPhase } from "./citationProcedure" ;
44
@@ -15,10 +15,12 @@ const CITATION_SCROLL_KEYS = new Set([
1515/** One activation of a citation; virtual row remounts and listeners share it. */
1616interface CitationNavigation {
1717 readonly target : AssistantCitationTarget ;
18- /** Takes over from the live follow for this activation; the cleanup retires it. */
19- readonly activate : ( ) => ( ) => void ;
18+ /** Takes over from the live follow for this activation. */
19+ readonly activate : ( ) => void ;
20+ /** Stops this activation for good once another one (or none) replaces it. */
21+ readonly retire : ( ) => void ;
2022 /** Listens for user navigation that stops this activation's pending scroll. */
21- readonly attachDismissal : ( viewport : HTMLElement ) => ( ) => void ;
23+ readonly attachDismissal : ( viewport : HTMLElement | null ) => ( ) => void ;
2224}
2325
2426interface CitationSettlement {
@@ -70,12 +72,12 @@ export function useAssistantCitationTarget({
7072 const activate = ( ) => {
7173 // The follow must yield before the citation row starts scrolling.
7274 if ( ! done ) onManualNavigation ( ) ;
73- return ( ) => {
74- activationRef . current . dismissed = true ;
75- activationRef . current . cancelScroll ?.( ) ;
76- } ;
7775 } ;
78- const attachDismissal = ( citationViewport : HTMLElement ) => {
76+ const retire = ( ) => {
77+ activationRef . current . dismissed = true ;
78+ activationRef . current . cancelScroll ?.( ) ;
79+ } ;
80+ const attachDismissal = ( citationViewport : HTMLElement | null ) => {
7981 // User navigation stops a pending citation scroll; Escape dismisses it outright.
8082 const dismiss = ( onlyPending : boolean ) => {
8183 const activation = activationRef . current ;
@@ -92,7 +94,7 @@ export function useAssistantCitationTarget({
9294 } else if (
9395 CITATION_SCROLL_KEYS . has ( event . key ) &&
9496 event . target instanceof Node &&
95- citationViewport . contains ( event . target )
97+ citationViewport ? .contains ( event . target )
9698 ) {
9799 dismiss ( true ) ;
98100 }
@@ -103,25 +105,31 @@ export function useAssistantCitationTarget({
103105 const onNavigation = ( ) => dismiss ( true ) ;
104106 document . addEventListener ( "keydown" , onKeyDown ) ;
105107 document . addEventListener ( "pointerdown" , onNavigation , true ) ;
106- citationViewport . addEventListener ( "wheel" , onWheel , { passive : true } ) ;
107- citationViewport . addEventListener ( "touchmove" , onNavigation , { passive : true } ) ;
108+ citationViewport ? .addEventListener ( "wheel" , onWheel , { passive : true } ) ;
109+ citationViewport ? .addEventListener ( "touchmove" , onNavigation , { passive : true } ) ;
108110 return ( ) => {
109111 document . removeEventListener ( "keydown" , onKeyDown ) ;
110112 document . removeEventListener ( "pointerdown" , onNavigation , true ) ;
111- citationViewport . removeEventListener ( "wheel" , onWheel ) ;
112- citationViewport . removeEventListener ( "touchmove" , onNavigation ) ;
113+ citationViewport ? .removeEventListener ( "wheel" , onWheel ) ;
114+ citationViewport ? .removeEventListener ( "touchmove" , onNavigation ) ;
113115 } ;
114116 } ;
115- return { target, activate, attachDismissal } ;
117+ return { target, activate, retire , attachDismissal } ;
116118 }
117119
118- useEffect ( ( ) => current ?. activate ( ) , [ current ] ) ;
119- // The viewport mounts after the first render and the activation changes with
120- // the route, so the listeners follow both.
120+ // Retiring is one-way, so it runs when the activation is replaced rather than
121+ // in a cleanup: StrictMode and <Activity> replay cleanups on a live activation.
122+ // Unmounting needs no retire; the rows observing it unmount with the list.
123+ const activatedRef = useRef < CitationNavigation | null > ( null ) ;
121124 useEffect ( ( ) => {
122- if ( ! current || ! viewport ) return ;
123- return current . attachDismissal ( viewport ) ;
124- } , [ current , viewport ] ) ;
125+ if ( activatedRef . current === current ) return ;
126+ activatedRef . current ?. retire ( ) ;
127+ activatedRef . current = current ;
128+ current ?. activate ( ) ;
129+ } , [ current ] ) ;
130+ // Escape and pointerdown dismiss before the viewport mounts; the viewport
131+ // listeners join once it exists.
132+ useEffect ( ( ) => current ?. attachDismissal ( viewport ) , [ current , viewport ] ) ;
125133
126134 const settled = current !== null && settlement ?. target === current . target ? settlement : null ;
127135 const target =
0 commit comments