Skip to content

Commit

Permalink
feat: once off timezone mismatch check
Browse files Browse the repository at this point in the history
  • Loading branch information
capJavert committed Jan 16, 2025
1 parent d9fca98 commit 0305674
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 8 deletions.
1 change: 1 addition & 0 deletions packages/shared/src/graphql/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export enum ActionType {
DigestConfig = 'digest_config',
StreakMilestone = 'streak_milestone',
FetchedSmartTitle = 'fetched_smart_title',
StreakTimezoneMismatch = 'streak_timezone_mismatch',
}

export interface Action {
Expand Down
67 changes: 59 additions & 8 deletions packages/shared/src/hooks/streaks/useStreakTimezoneOk.ts
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;
};
1 change: 1 addition & 0 deletions packages/shared/src/lib/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ export enum LogEvent {
ScheduleStreakReminder = 'schedule streak reminder',
StreakRecover = 'restore streak',
DismissStreakRecover = 'dimiss streaks milestone',
StreakTimezoneMismatch = 'streak timezone mismatch',
// 404 page
View404Page = '404 page',
// Follow Actions - start
Expand Down

0 comments on commit 0305674

Please sign in to comment.