1- import { useEffect , useRef , useState } from 'react' ;
1+ import { useEffect , useState } from 'react' ;
22import { useSelector , useDispatch } from 'react-redux' ;
33import axios from 'axios' ;
44import { ENDPOINTS } from '~/utils/URL' ;
@@ -11,163 +11,19 @@ function PermissionWatcher() {
1111 const dispatch = useDispatch ( ) ;
1212 const { isAuthenticated, forceLogoutAt } = useSelector ( state => state . auth || { } ) ;
1313 const userProfile = useSelector ( state => state . userProfile ) ;
14- const isAcknowledged = userProfile ?. permissions ?. isAcknowledged === true ;
14+ const isAcknowledged = userProfile ?. permissions ?. isAcknowledged !== false ;
1515 const [ isAckLoading , setIsAckLoading ] = useState ( false ) ;
1616 // Get seconds remaining until force logout
1717 const secondsRemaining = useCountdown ( forceLogoutAt ) ;
1818
19- // Track previous acknowledgement state across this auth session
20- const prevIsAcknowledgedRef = useRef ( null ) ;
21- const isInitialStateRef = useRef ( true ) ;
22-
23- // Debug: Log when isAcknowledged changes
24- useEffect ( ( ) => {
25- console . log (
26- 'isAcknowledged changed to:' ,
27- isAcknowledged ,
28- 'isInitialState:' ,
29- isInitialStateRef . current ,
30- ) ;
31- if ( isAcknowledged === true && isInitialStateRef . current ) {
32- console . trace ( 'Stack trace for isAcknowledged change to true' ) ;
33- // If we're in initial state and isAcknowledged becomes true,
34- // it means something else is overriding our state
35- // We need to preserve the isAcknowledged: false state
36- console . log ( 'Overriding isAcknowledged back to false to preserve banner state' ) ;
37- // We can't directly modify the Redux state here, so we'll handle this differently
38- }
39- } , [ isAcknowledged ] ) ;
40-
41- // Start the force logout countdown only when acknowledgement flips from true -> false
42- // during an active session. First login with unacknowledged permissions only shows banner.
19+ // Start the force logout countdown when conditions are met
4320 useEffect ( ( ) => {
44- if ( ! isAuthenticated ) {
45- prevIsAcknowledgedRef . current = null ;
46- isInitialStateRef . current = true ; // Reset initial state on logout
47- return ;
48- }
49-
50- // Initialize on first authenticated render; do not trigger countdown on first load
51- if ( prevIsAcknowledgedRef . current === null ) {
52- // Set to the current value to detect transitions
53- prevIsAcknowledgedRef . current = isAcknowledged ;
54- isInitialStateRef . current = true ;
55- console . log (
56- 'PermissionWatcher: Initial state - isAcknowledged:' ,
57- isAcknowledged ,
58- 'prevIsAcknowledgedRef set to:' ,
59- isAcknowledged ,
60- ) ;
61- return ;
62- }
63-
64- const wasAcknowledged = prevIsAcknowledgedRef . current === true ;
65- const nowUnacknowledged = isAcknowledged === false ;
66-
67- // Debug: Log the transition
68- console . log (
69- 'PermissionWatcher Debug - wasAcknowledged:' ,
70- wasAcknowledged ,
71- 'nowUnacknowledged:' ,
72- nowUnacknowledged ,
73- 'forceLogoutAt:' ,
74- forceLogoutAt ,
75- 'isInitialState:' ,
76- isInitialStateRef . current ,
77- ) ;
78-
79- // Only start countdown if:
80- // 1. We transition from acknowledged to unacknowledged
81- // 2. We're NOT in initial state (meaning this is a change during active session)
82- // 3. No force logout is already in progress
83- if ( wasAcknowledged && nowUnacknowledged && ! isInitialStateRef . current && ! forceLogoutAt ) {
84- console . log ( 'Starting force logout countdown due to permission change during active session' ) ;
21+ if ( isAuthenticated && ! isAcknowledged && ! forceLogoutAt ) {
22+ // eslint-disable-next-line no-console
23+ console . log ( 'Starting force logout countdown due to unacknowledged permission changes' ) ;
8524 dispatch ( startForceLogout ( 20000 ) ) ; // 20 seconds countdown
8625 }
87-
88- // Update the ref for next comparison
89- prevIsAcknowledgedRef . current = isAcknowledged ;
90-
91- // Mark that we're no longer in initial state after first update
92- if ( isInitialStateRef . current ) {
93- isInitialStateRef . current = false ;
94- }
9526 } , [ isAuthenticated , isAcknowledged , forceLogoutAt , dispatch ] ) ;
96-
97- // Poll for permission changes every 10 seconds to detect changes made by other users
98- // Start polling after a delay to allow initial page load to complete
99- useEffect ( ( ) => {
100- console . log (
101- 'PermissionWatcher useEffect - isAuthenticated:' ,
102- isAuthenticated ,
103- 'userProfile._id:' ,
104- userProfile ?. _id ,
105- ) ;
106- if ( ! isAuthenticated || ! userProfile ?. _id ) {
107- console . log ( 'PermissionWatcher: Not starting polling - missing auth or userProfile._id' ) ;
108- return ;
109- }
110-
111- let pollInterval ;
112-
113- // Start polling after 10 seconds delay to avoid interfering with initial load
114- const startDelay = setTimeout ( ( ) => {
115- console . log ( 'Starting permission polling...' ) ;
116- pollInterval = setInterval ( async ( ) => {
117- // Only poll if no force logout is in progress
118- if ( ! forceLogoutAt ) {
119- console . log ( 'Polling for permission changes...' ) ;
120- try {
121- const response = await axios . get ( ENDPOINTS . USER_PROFILE ( userProfile . _id ) ) ;
122- const newIsAcknowledged = response . data ?. permissions ?. isAcknowledged === true ;
123- console . log (
124- 'Polling response - isAcknowledged:' ,
125- newIsAcknowledged ,
126- 'prevIsAcknowledgedRef:' ,
127- prevIsAcknowledgedRef . current ,
128- 'isInitialState:' ,
129- isInitialStateRef . current ,
130- ) ;
131-
132- // Check for transition from true to false during polling
133- // Only trigger logout if we're NOT in initial state (meaning this is a change during active session)
134- if (
135- prevIsAcknowledgedRef . current === true &&
136- newIsAcknowledged === false &&
137- ! isInitialStateRef . current
138- ) {
139- console . log ( 'Permission change detected during polling - starting countdown' ) ;
140- dispatch ( startForceLogout ( 20000 ) ) ;
141- }
142-
143- // Skip other polling logic if we're in initial state (first login with isAcknowledged: false)
144- if ( isInitialStateRef . current && isAcknowledged === false ) {
145- console . log (
146- 'Skipping other polling logic - in initial state with isAcknowledged: false' ,
147- ) ;
148- return ;
149- }
150-
151- // Update the ref and fetch profile
152- prevIsAcknowledgedRef . current = newIsAcknowledged ;
153- dispatch ( getUserProfile ( userProfile . _id ) ) ;
154-
155- // Mark that we're no longer in initial state
156- isInitialStateRef . current = false ;
157- } catch ( error ) {
158- console . error ( 'Polling error:' , error ) ;
159- }
160- }
161- } , 10000 ) ; // Poll every 10 seconds
162- } , 10000 ) ;
163-
164- return ( ) => {
165- clearTimeout ( startDelay ) ;
166- if ( pollInterval ) {
167- clearInterval ( pollInterval ) ;
168- }
169- } ;
170- } , [ isAuthenticated , userProfile ?. _id , forceLogoutAt , dispatch ] ) ;
17127 // Handle acknowledgment of permission changes
17228 const handleAcknowledge = async ( ) => {
17329 try {
0 commit comments