-
Notifications
You must be signed in to change notification settings - Fork 245
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: once off timezone mismatch check
- Loading branch information
Showing
3 changed files
with
61 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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,22 +1,73 @@ | ||
import { getTimezoneOffset } from 'date-fns-tz'; | ||
import { useEffect, useMemo } from 'react'; | ||
import { useAuthContext } from '../../contexts/AuthContext'; | ||
import { DEFAULT_TIMEZONE } from '../../lib/timezones'; | ||
import usePersistentContext from '../usePersistentContext'; | ||
import { useActions } from '../useActions'; | ||
import { ActionType } from '../../graphql/actions'; | ||
import { useLogContext } from '../../contexts/LogContext'; | ||
import { LogEvent } from '../../lib/log'; | ||
|
||
export const timezoneMismatchIgnoreKey = 'timezoneMismatchIgnore'; | ||
|
||
export const useStreakTimezoneOk = (): boolean => { | ||
const { user, isLoggedIn } = useAuthContext(); | ||
const [ignoredTimezone] = usePersistentContext(timezoneMismatchIgnoreKey, ''); | ||
const { checkHasCompleted, isActionsFetched, completeAction } = useActions(); | ||
const { logEvent } = useLogContext(); | ||
|
||
const [ignoredTimezone] = usePersistentContext(timezoneMismatchIgnoreKey, ''); | ||
const deviceTimezone = Intl.DateTimeFormat().resolvedOptions().timeZone; | ||
|
||
if (ignoredTimezone === deviceTimezone) { | ||
return true; | ||
} | ||
const isTimezoneOk = useMemo(() => { | ||
if (ignoredTimezone === deviceTimezone) { | ||
return true; | ||
} | ||
|
||
if (!isLoggedIn) { | ||
return true; | ||
} | ||
|
||
return ( | ||
getTimezoneOffset(user?.timezone || DEFAULT_TIMEZONE) === | ||
getTimezoneOffset(Intl.DateTimeFormat().resolvedOptions().timeZone) | ||
); | ||
}, [deviceTimezone, ignoredTimezone, isLoggedIn, user?.timezone]); | ||
|
||
// once off check to see how many users with timezone mismatches we have in the wild | ||
useEffect(() => { | ||
if (isTimezoneOk) { | ||
return; | ||
} | ||
|
||
if (!isActionsFetched) { | ||
return; | ||
} | ||
|
||
if (checkHasCompleted(ActionType.StreakTimezoneMismatch)) { | ||
return; | ||
} | ||
|
||
logEvent({ | ||
event_name: LogEvent.StreakTimezoneMismatch, | ||
extra: JSON.stringify({ | ||
device_timezone: deviceTimezone, | ||
user_timezone: user?.timezone, | ||
timezone_ok: isTimezoneOk, | ||
timezone_ignore: ignoredTimezone, | ||
}), | ||
}); | ||
|
||
completeAction(ActionType.StreakTimezoneMismatch); | ||
}, [ | ||
isTimezoneOk, | ||
ignoredTimezone, | ||
isActionsFetched, | ||
checkHasCompleted, | ||
completeAction, | ||
logEvent, | ||
deviceTimezone, | ||
user?.timezone, | ||
]); | ||
|
||
return isLoggedIn | ||
? getTimezoneOffset(user.timezone || DEFAULT_TIMEZONE) === | ||
getTimezoneOffset(Intl.DateTimeFormat().resolvedOptions().timeZone) | ||
: true; | ||
return isTimezoneOk; | ||
}; |
This file contains 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