Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -908,7 +908,8 @@
"Wintrust",
"Zürcher",
"CARDFROZEN",
"CARDUNFROZEN"
"CARDUNFROZEN",
"Kolkata"
Comment thread
shubham1206agra marked this conversation as resolved.
Resolved
],
"ignorePaths": [
"src/languages/de.ts",
Expand Down
42 changes: 30 additions & 12 deletions src/hooks/useAutoUpdateTimezone.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,44 @@
import {useEffect} from 'react';
import {useEffect, useEffectEvent} from 'react';
import DateUtils from '@libs/DateUtils';
import {updateAutomaticTimezone} from '@userActions/PersonalDetails';
import ONYXKEYS from '@src/ONYXKEYS';
import {isActingAsDelegateSelector} from '@src/selectors/Account';
import type {SelectedTimezone} from '@src/types/onyx/PersonalDetails';
import {isEmptyObject} from '@src/types/utils/EmptyObject';
import useAppFocusEvent from './useAppFocusEvent';
import useCurrentUserPersonalDetails from './useCurrentUserPersonalDetails';
import useOnyx from './useOnyx';

const useAutoUpdateTimezone = () => {
const [isActingAsDelegate] = useOnyx(ONYXKEYS.ACCOUNT, {selector: isActingAsDelegateSelector});
const currentUserPersonalDetails = useCurrentUserPersonalDetails();
const timezone = currentUserPersonalDetails?.timezone ?? {};
useEffect(() => {
const isTimezonePresent = !isEmptyObject(timezone);
const updateTimezone = () => {
if (isActingAsDelegate) {
// Do not update timezone if user is acting as a delegate, as the timezone should be based on the primary account holder, not the delegate.
return;
}
const currentTimezone = Intl.DateTimeFormat().resolvedOptions().timeZone as SelectedTimezone;
const hasValidCurrentTimezone = typeof currentTimezone === 'string' && currentTimezone.trim().length > 0;
const proposedTimezone = DateUtils.formatToSupportedTimezone({
automatic: true,
selected: currentTimezone,
});

if (hasValidCurrentTimezone && timezone?.automatic && timezone?.selected !== currentTimezone) {
updateAutomaticTimezone(
{
automatic: true,
selected: currentTimezone,
},
currentUserPersonalDetails.accountID,
);
if (hasValidCurrentTimezone && timezone?.automatic && timezone?.selected !== proposedTimezone.selected) {
updateAutomaticTimezone(proposedTimezone, currentUserPersonalDetails.accountID);
}
};
const updateTimezoneEvent = useEffectEvent(updateTimezone);
useAppFocusEvent(updateTimezone);
Comment thread
shubham1206agra marked this conversation as resolved.
Resolved
Comment thread
shubham1206agra marked this conversation as resolved.
Resolved
useEffect(() => {
if (!isTimezonePresent) {
// If we don't have a timezone, we can't update it, so we should wait until we do before attempting to update it.
return;
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [timezone?.automatic, timezone?.selected]);
updateTimezoneEvent();
}, [isTimezonePresent]);
};

export default useAutoUpdateTimezone;
15 changes: 15 additions & 0 deletions tests/unit/useAutoUpdateTimezoneTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,21 @@ describe('useAutoUpdateTimezone', () => {
);
});

it('does not call update when selected timezone matches system timezone (with backwards compatibility)', () => {
setSystemTimezone('Asia/Calcutta');
mockUseCurrentUserPersonalDetails.mockReturnValue({
accountID: 1,
timezone: {
automatic: true,
selected: 'Asia/Kolkata',
},
} as unknown as ReturnType<typeof useCurrentUserPersonalDetails>);

render(<TestComponent />);

expect(updateAutomaticTimezoneSpy).not.toHaveBeenCalled();
});

it('does not call update when selected matches system timezone', () => {
mockUseCurrentUserPersonalDetails.mockReturnValue({
accountID: 1,
Expand Down
Loading