@@ -25,6 +25,9 @@ import "../styles/PlanPage.css"
2525// Create API service instance
2626const apiService = new APIService ( ) ;
2727
28+ // Module-level flag to ensure reload redirect only fires once per actual page reload
29+ let hasHandledReload = false ;
30+
2831/**
2932 * Page component for displaying a specific plan
3033 * Accessible via the route /plan/{plan_id}
@@ -460,8 +463,36 @@ const PlanPage: React.FC = () => {
460463 } ) ;
461464
462465 return ( ) => unsubscribe ( ) ;
463- } , [ scrollToBottom , planData , processAgentMessage ] ) ; //onPlanReceived, scrollToBottom
466+ } , [ scrollToBottom , planData , processAgentMessage ] ) ; //onPlanReceived, scrollToBottom
467+
468+ // Detect page reload and navigate to Home (only if plan is not yet approved)
469+ useEffect ( ( ) => {
470+ if ( hasHandledReload ) return ;
471+ if ( loading ) return ; // Wait until plan data is loaded before deciding
472+ if ( ! showApprovalButtons ) return ; // Plan already approved, stay on page
473+ const navEntries = performance . getEntriesByType ( "navigation" ) as PerformanceNavigationTiming [ ] ;
474+ if ( navEntries . length > 0 && navEntries [ 0 ] . type === "reload" ) {
475+ hasHandledReload = true ;
476+ navigate ( "/" , { replace : true } ) ;
477+ }
478+ } , [ navigate , showApprovalButtons , loading ] ) ;
479+
480+ // Warn user before page refresh/close (only when plan is pending approval)
481+ useEffect ( ( ) => {
482+ const handleBeforeUnload = ( e : BeforeUnloadEvent ) => {
483+ e . preventDefault ( ) ;
484+ e . returnValue = "" ;
485+ } ;
464486
487+ if ( planId && ! errorLoading && showApprovalButtons ) {
488+ window . addEventListener ( "beforeunload" , handleBeforeUnload ) ;
489+ }
490+
491+ return ( ) => {
492+ window . removeEventListener ( "beforeunload" , handleBeforeUnload ) ;
493+ } ;
494+ } , [ planId , errorLoading , showApprovalButtons ] ) ;
495+ console . log ( 'Niraj Chaudhari 99' ) ;
465496 // Loading message rotation effect
466497 useEffect ( ( ) => {
467498 let interval : NodeJS . Timeout ;
0 commit comments