-
Notifications
You must be signed in to change notification settings - Fork 3.7k
fix: prevent infinite timezone update loop for delegate sessions #84232
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
+34
−3
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,26 +1,57 @@ | ||
| import {useEffect} from 'react'; | ||
| import {useEffect, useRef} from 'react'; | ||
| import {updateAutomaticTimezone} from '@userActions/PersonalDetails'; | ||
| import {isActingAsDelegateSelector} from '@selectors/Account'; | ||
| import type {SelectedTimezone} from '@src/types/onyx/PersonalDetails'; | ||
| import useCurrentUserPersonalDetails from './useCurrentUserPersonalDetails'; | ||
| import useOnyx from './useOnyx'; | ||
| import ONYXKEYS from '@src/ONYXKEYS'; | ||
|
|
||
| const THROTTLE_INTERVAL_MS = 60 * 60 * 1000; // 1 hour | ||
|
|
||
| const useAutoUpdateTimezone = () => { | ||
| const currentUserPersonalDetails = useCurrentUserPersonalDetails(); | ||
| const timezone = currentUserPersonalDetails?.timezone ?? {}; | ||
| const accountID = currentUserPersonalDetails?.accountID; | ||
|
|
||
| // Use useOnyx to get the Account data (contains delegatedAccess) | ||
| const [account] = useOnyx(ONYXKEYS.ACCOUNT); | ||
| const isDelegate = isActingAsDelegateSelector(account); | ||
|
|
||
| // Use useRef to store per-account timestamps | ||
| const lastUpdateTimestamps = useRef<Record<number, number>>({}); | ||
|
|
||
| useEffect(() => { | ||
| // Skip auto-timezone updates for copilot/delegate sessions | ||
| if (isDelegate) { | ||
| return; | ||
| } | ||
|
|
||
| const currentTime = Date.now(); | ||
|
|
||
| // Get the last update timestamp for this specific account | ||
| const lastUpdateTimestamp = lastUpdateTimestamps.current[accountID] ?? 0; | ||
|
|
||
| // Throttle timezone updates to once per hour (per account) | ||
| if (currentTime - lastUpdateTimestamp < THROTTLE_INTERVAL_MS) { | ||
| return; | ||
| } | ||
|
|
||
| const currentTimezone = Intl.DateTimeFormat().resolvedOptions().timeZone as SelectedTimezone; | ||
| const hasValidCurrentTimezone = typeof currentTimezone === 'string' && currentTimezone.trim().length > 0; | ||
|
|
||
| if (hasValidCurrentTimezone && timezone?.automatic && timezone?.selected !== currentTimezone) { | ||
| // Update the timestamp for this specific account | ||
| lastUpdateTimestamps.current[accountID] = currentTime; | ||
| updateAutomaticTimezone( | ||
| { | ||
| automatic: true, | ||
| selected: currentTimezone, | ||
| }, | ||
| currentUserPersonalDetails.accountID, | ||
| accountID, | ||
| ); | ||
| } | ||
| // eslint-disable-next-line react-hooks/exhaustive-deps | ||
| }, [timezone?.automatic, timezone?.selected]); | ||
| }, [timezone?.automatic, timezone?.selected, isDelegate, accountID]); | ||
| }; | ||
|
|
||
| export default useAutoUpdateTimezone; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❌ CLEAN-REACT-PATTERNS-2 (docs)
currentUserPersonalDetails?.accountaccesses a property that does not exist on thePersonalDetailstype (nor onCurrentUserPersonalDetails). This will always beundefined, soisActingAsDelegateSelector(account)will always returnfalse, making the delegate check completely non-functional.The
isActingAsDelegateSelectorexpectsOnyxEntry<Account>from theONYXKEYS.ACCOUNTOnyx key, not a field from personal details. The established pattern used elsewhere in the codebase (e.g.,ProductTrainingContext/index.tsx:62,AddNewCardPage.tsx:50) is:Replace lines 12-13 with:
And update the references from
isDelegatetoisActingAsDelegate(or rename as preferred).Please rate this suggestion with 👍 or 👎 to help us improve! Reactions are used to monitor reviewer efficiency.