@@ -93,6 +93,7 @@ const App: React.FC = () => {
9393 const handleSeedRef = useRef < typeof handleSeed > ( null ! ) ;
9494 const handleNotarizedRef = useRef < typeof handleNotarization > ( null ! ) ;
9595 const handleFinalizedRef = useRef < typeof handleFinalization > ( null ! ) ;
96+ const adjustTimeRef = useRef ( adjustTime ) ;
9697 const isInitializedRef = useRef ( false ) ;
9798 const reconnectTimeoutRef = useRef < NodeJS . Timeout | null > ( null ) ;
9899
@@ -311,7 +312,7 @@ const App: React.FC = () => {
311312 } ) ;
312313 } , [ lastObservedView , adjustTime ] ) ;
313314
314- const handleNotarization = useCallback ( ( notarized : NotarizedJs ) => {
315+ const handleNotarization = useCallback ( ( notarized : NotarizedJs , messageReceivedTime ?: number ) => {
315316 const view = notarized . proof . view ;
316317 setViews ( ( prevViews ) => {
317318 const index = prevViews . findIndex ( ( v ) => v . view === view ) ;
@@ -320,9 +321,10 @@ const App: React.FC = () => {
320321 if ( index !== - 1 && prevViews [ index ] . status === "finalized" ) {
321322 return prevViews ; // No changes needed, preserve finalized state
322323 }
323-
324324 let newViews = [ ...prevViews ] ;
325- const currentTime = adjustTime ( Date . now ( ) ) ;
325+
326+ // If messageReceivedTime is not provided, use the current time
327+ const currentTime = messageReceivedTime || adjustTime ( Date . now ( ) ) ;
326328
327329 // Calculate a reasonable start time using the block timestamp if available
328330 let calculatedStartTime = currentTime ;
@@ -401,12 +403,14 @@ const App: React.FC = () => {
401403 } ) ;
402404 } , [ adjustTime ] ) ;
403405
404- const handleFinalization = useCallback ( ( finalized : FinalizedJs ) => {
406+ const handleFinalization = useCallback ( ( finalized : FinalizedJs , messageReceivedTime ?: number ) => {
405407 const view = finalized . proof . view ;
406408 setViews ( ( prevViews ) => {
407409 const index = prevViews . findIndex ( ( v ) => v . view === view ) ;
408410 let newViews = [ ...prevViews ] ;
409- const currentTime = adjustTime ( Date . now ( ) ) ;
411+
412+ // If messageReceivedTime is not provided, use the current time
413+ const currentTime = messageReceivedTime || adjustTime ( Date . now ( ) ) ;
410414
411415 // Calculate a reasonable start time using the block timestamp if available
412416 let calculatedStartTime = currentTime ;
@@ -516,6 +520,10 @@ const App: React.FC = () => {
516520 handleFinalizedRef . current = handleFinalization ;
517521 } , [ handleFinalization ] ) ;
518522
523+ useEffect ( ( ) => {
524+ adjustTimeRef . current = adjustTime ;
525+ } , [ adjustTime ] ) ;
526+
519527 // WebSocket connection management with fixed single-connection approach
520528 useEffect ( ( ) => {
521529 // If loading, don't start
@@ -573,6 +581,8 @@ const App: React.FC = () => {
573581 } ;
574582
575583 ws . onmessage = ( event ) => {
584+ // Capture timestamp as soon as we receive the message (not when it is verified)
585+ const messageReceivedTime = adjustTimeRef . current ( Date . now ( ) ) ;
576586 const data = new Uint8Array ( event . data ) ;
577587 const kind = data [ 0 ] ;
578588 const payload = data . slice ( 1 ) ;
@@ -584,11 +594,11 @@ const App: React.FC = () => {
584594 break ;
585595 case 1 : // Notarization
586596 const notarized = parse_notarized ( PUBLIC_KEY , payload ) ;
587- if ( notarized ) handleNotarizedRef . current ( notarized ) ;
597+ if ( notarized ) handleNotarizedRef . current ( notarized , messageReceivedTime ) ;
588598 break ;
589599 case 2 : // Finalization
590600 const finalized = parse_finalized ( PUBLIC_KEY , payload ) ;
591- if ( finalized ) handleFinalizedRef . current ( finalized ) ;
601+ if ( finalized ) handleFinalizedRef . current ( finalized , messageReceivedTime ) ;
592602 break ;
593603 }
594604 } ;
0 commit comments