11import { isAtomCommandInterrupted } from "@t3tools/client-runtime/state/runtime" ;
22import type { EnvironmentId , PullRequestRef } from "@t3tools/contracts" ;
3- import { useCallback , useEffect , useMemo , useRef , useState } from "react" ;
3+ import { useEffect , useEffectEvent , useRef , useState } from "react" ;
44
55import { pullRequestEnvironment } from "~/state/pullRequests" ;
66import { useEnvironmentQuery } from "~/state/query" ;
77import { useAtomCommand } from "~/state/use-atom-command" ;
8+ import { useStableCallback } from "~/hooks/useStableCallback" ;
89
910import { toastManager } from "../ui/toast" ;
1011import {
@@ -45,7 +46,7 @@ export function usePullRequestFilesViewed(options: {
4546 enabled ? pullRequestEnvironment . filesViewed ( { environmentId, input : reference } ) : null ,
4647 ) ;
4748 const refresh = query . refresh ;
48- const states = useMemo ( ( ) => toFileViewedStates ( query . data ) , [ query . data ] ) ;
49+ const states = toFileViewedStates ( query . data ) ;
4950 const truncated = query . data ?. truncated === true ;
5051 const error = query . error ;
5152 const [ overlay , setOverlay ] = useState < FileViewedOverlay > ( NO_OVERLAY ) ;
@@ -68,10 +69,11 @@ export function usePullRequestFilesViewed(options: {
6869 // answer that differs from it is the first read that could have seen the write, which is what
6970 // retires the press rather than the host happening to agree with it.
7071 const answeredFrom = useRef < Map < string , FileViewedStates | null > > ( new Map ( ) ) ;
71- const statesRef = useRef ( states ) ;
72- statesRef . current = states ;
72+ // The committed answer, read when a write completes rather than when it was sent.
73+ const committedStates = useRef ( states ) ;
7374
7475 useEffect ( ( ) => {
76+ committedStates . current = states ;
7577 const pending = new Set ( [ ...queued . current . keys ( ) , ...sentBy . current . keys ( ) ] ) ;
7678 const answered = new Set < string > ( ) ;
7779 for ( const [ path , from ] of answeredFrom . current ) {
@@ -82,15 +84,22 @@ export function usePullRequestFilesViewed(options: {
8284 setOverlay ( ( current ) => settleFileViewedOverlay ( current , states , pending , answered ) ) ;
8385 } , [ states ] ) ;
8486
85- const flush = useCallback ( ( ) => {
87+ // Takes the target explicitly so a scope being left still sends its last presses to itself.
88+ const flush = ( target : {
89+ readonly environmentId : EnvironmentId ;
90+ readonly reference : PullRequestRef ;
91+ } ) => {
8692 flushTimer . current = null ;
8793 const batch = toFileViewedBatch ( queued . current ) ;
8894 if ( batch . length === 0 ) return ;
8995 queued . current = new Map ( ) ;
9096 const sentFrom = scope . current ;
9197 const request = ++ requests . current ;
9298 for ( const file of batch ) sentBy . current . set ( file . path , request ) ;
93- void setFilesViewed ( { environmentId, input : { ...reference , files : batch } } ) . then ( ( result ) => {
99+ void setFilesViewed ( {
100+ environmentId : target . environmentId ,
101+ input : { ...target . reference , files : batch } ,
102+ } ) . then ( ( result ) => {
94103 const mine = batch
95104 . map ( ( file ) => file . path )
96105 . filter ( ( path ) => sentBy . current . get ( path ) === request ) ;
@@ -106,26 +115,23 @@ export function usePullRequestFilesViewed(options: {
106115 }
107116 // Answered for from the next read on, whatever it says. A push landing between the write
108117 // and that read comes back as `dismissed`, and the press must not stand over it.
109- for ( const path of mine ) answeredFrom . current . set ( path , statesRef . current ) ;
118+ for ( const path of mine ) answeredFrom . current . set ( path , committedStates . current ) ;
110119 refresh ( ) ;
111120 } ) ;
112- } , [ environmentId , reference , refresh , setFilesViewed ] ) ;
121+ } ;
113122
114- // Read through a ref rather than closed over: `setViewed` is handed to every file header the
115- // viewer draws, and a new identity per render would rebuild all of them.
116- const flushRef = useRef ( flush ) ;
117- flushRef . current = flush ;
123+ const currentTarget = useEffectEvent ( ( ) => ( { environmentId, reference } ) ) ;
124+ const flushTarget = useEffectEvent ( flush ) ;
118125
119126 // Leaving a change request, the environment it lives on, or the page itself records what was
120- // pressed and then drops the rest. The flush kept here is the one bound to the scope being
121- // left, which is what sends those last presses where they were meant to go.
127+ // pressed and then drops the rest, sending those last presses to the scope being left.
122128 useEffect ( ( ) => {
123- const flushScope = flushRef . current ;
129+ const leaving = currentTarget ( ) ;
124130 scope . current = scopeKey ;
125131 return ( ) => {
126132 if ( flushTimer . current !== null ) {
127133 clearTimeout ( flushTimer . current ) ;
128- flushScope ( ) ;
134+ flushTarget ( leaving ) ;
129135 }
130136 queued . current = new Map ( ) ;
131137 sentBy . current = new Map ( ) ;
@@ -134,41 +140,23 @@ export function usePullRequestFilesViewed(options: {
134140 } ;
135141 } , [ scopeKey ] ) ;
136142
137- const refreshRef = useRef ( refresh ) ;
138- refreshRef . current = refresh ;
139- const refreshFromHost = useCallback ( ( ) => refreshRef . current ( ) , [ ] ) ;
140-
141- const setViewed = useCallback ( ( path : string , viewed : boolean ) => {
143+ // Handed to every file header the viewer draws; a new identity would rebuild all of them.
144+ const setViewed = useStableCallback ( ( path : string , viewed : boolean ) => {
142145 setOverlay ( ( current ) => new Map ( current ) . set ( path , viewed ) ) ;
143146 queued . current . set ( path , viewed ) ;
144147 if ( flushTimer . current !== null ) clearTimeout ( flushTimer . current ) ;
145- flushTimer . current = setTimeout ( ( ) => flushRef . current ( ) , FLUSH_DELAY_MS ) ;
146- } , [ ] ) ;
147-
148- const isViewed = useCallback (
149- ( path : string ) => isFileViewed ( path , states , overlay ) ,
150- [ overlay , states ] ,
151- ) ;
152- const isStale = useCallback (
153- ( path : string ) => ! overlay . has ( path ) && isStaleViewedState ( states ?. get ( path ) ) ,
154- [ overlay , states ] ,
155- ) ;
156- const viewedCount = useMemo (
157- ( ) => countViewedFiles ( paths , states , overlay ) ,
158- [ overlay , paths , states ] ,
159- ) ;
148+ const target = { environmentId, reference } ;
149+ flushTimer . current = setTimeout ( ( ) => flush ( target ) , FLUSH_DELAY_MS ) ;
150+ } ) ;
160151
161- return useMemo (
162- ( ) => ( {
163- enabled,
164- isViewed,
165- isStale,
166- setViewed,
167- viewedCount,
168- truncated,
169- error,
170- refresh : refreshFromHost ,
171- } ) ,
172- [ enabled , error , isStale , isViewed , refreshFromHost , setViewed , truncated , viewedCount ] ,
173- ) ;
152+ return {
153+ enabled,
154+ isViewed : ( path : string ) => isFileViewed ( path , states , overlay ) ,
155+ isStale : ( path : string ) => ! overlay . has ( path ) && isStaleViewedState ( states ?. get ( path ) ) ,
156+ setViewed,
157+ viewedCount : countViewedFiles ( paths , states , overlay ) ,
158+ truncated,
159+ error,
160+ refresh,
161+ } ;
174162}
0 commit comments