Skip to content

Commit

Permalink
feat: move timezone ignore setting to api (#4072)
Browse files Browse the repository at this point in the history
  • Loading branch information
capJavert authored Jan 16, 2025
1 parent 5c4129d commit ed994f1
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,15 @@ import {
} from '../../../lib/timezones';
import { SimpleTooltip } from '../../tooltips';
import { isTesting } from '../../../lib/constants';
import {
timezoneMismatchIgnoreKey,
useStreakTimezoneOk,
} from '../../../hooks/streaks/useStreakTimezoneOk';
import { useStreakTimezoneOk } from '../../../hooks/streaks/useStreakTimezoneOk';
import { usePrompt } from '../../../hooks/usePrompt';
import usePersistentContext from '../../../hooks/usePersistentContext';
import { useLogContext } from '../../../contexts/LogContext';
import {
LogEvent,
StreakTimezonePromptAction,
TargetId,
} from '../../../lib/log';
import { useSettingsContext } from '../../../contexts/SettingsContext';

const getStreak = ({
value,
Expand Down Expand Up @@ -106,6 +103,7 @@ export function ReadingStreakPopup({
fullWidth,
}: ReadingStreakPopupProps): ReactElement {
const router = useRouter();
const { flags, updateFlag } = useSettingsContext();
const isMobile = useViewSize(ViewSize.MobileL);
const { user } = useAuthContext();
const { completeAction } = useActions();
Expand All @@ -117,8 +115,6 @@ export function ReadingStreakPopup({
const [showStreakConfig, toggleShowStreakConfig] = useToggle(false);
const isTimezoneOk = useStreakTimezoneOk();
const { showPrompt } = usePrompt();
const [timezoneMismatchIgnore, setTimezoneMismatchIgnore] =
usePersistentContext(timezoneMismatchIgnoreKey, '');
const { logEvent } = useLogContext();

const streaks = useMemo(() => {
Expand Down Expand Up @@ -210,7 +206,7 @@ export function ReadingStreakPopup({
device_timezone: deviceTimezone,
user_timezone: user?.timezone,
timezone_ok: isTimezoneOk,
timezone_ignore: timezoneMismatchIgnore,
timezone_ignore: flags?.timezoneMismatchIgnore,
};

logEvent({
Expand Down Expand Up @@ -253,7 +249,7 @@ export function ReadingStreakPopup({
});

if (!promptResult) {
setTimezoneMismatchIgnore(deviceTimezone);
updateFlag('timezoneMismatchIgnore', deviceTimezone);

return;
}
Expand Down
7 changes: 5 additions & 2 deletions packages/shared/src/contexts/SettingsContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ export interface SettingsContextData extends Omit<RemoteSettings, 'theme'> {
toggleAutoDismissNotifications: () => Promise<void>;
loadedSettings: boolean;
updateCustomLinks: (links: string[]) => Promise<unknown>;
updateFlag: (flag: keyof SettingsFlags, value: boolean) => Promise<unknown>;
updateFlag: (
flag: keyof SettingsFlags,
value: string | boolean,
) => Promise<unknown>;
syncSettings: (bootUserId?: string) => Promise<unknown>;
onToggleHeaderPlacement(): Promise<unknown>;
setOnboardingChecklistView: (value: ChecklistViewState) => Promise<unknown>;
Expand Down Expand Up @@ -267,7 +270,7 @@ export const SettingsContextProvider = ({
...settings,
onboardingChecklistView: value,
}),
updateFlag: (flag: keyof SettingsFlags, value: boolean) =>
updateFlag: (flag: keyof SettingsFlags, value: string | boolean) =>
setSettings({
...settings,
flags: {
Expand Down
1 change: 1 addition & 0 deletions packages/shared/src/graphql/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export type SettingsFlags = {
sidebarResourcesExpanded: boolean;
sidebarBookmarksExpanded: boolean;
clickbaitShieldEnabled: boolean;
timezoneMismatchIgnore?: string;
};

export enum SidebarSettingsFlags {
Expand Down
19 changes: 11 additions & 8 deletions packages/shared/src/hooks/streaks/useStreakTimezoneOk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,22 @@ 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';
import { useSettingsContext } from '../../contexts/SettingsContext';

export const useStreakTimezoneOk = (): boolean => {
const { user, isLoggedIn } = useAuthContext();
const { checkHasCompleted, isActionsFetched, completeAction } = useActions();
const { logEvent } = useLogContext();
const { flags } = useSettingsContext();

const [ignoredTimezone] = usePersistentContext(timezoneMismatchIgnoreKey, '');
const deviceTimezone = Intl.DateTimeFormat().resolvedOptions().timeZone;

const isTimezoneOk = useMemo(() => {
if (ignoredTimezone === deviceTimezone) {
if (flags?.timezoneMismatchIgnore === deviceTimezone) {
return true;
}

Expand All @@ -31,7 +29,12 @@ export const useStreakTimezoneOk = (): boolean => {
getTimezoneOffset(user?.timezone || DEFAULT_TIMEZONE) ===
getTimezoneOffset(Intl.DateTimeFormat().resolvedOptions().timeZone)
);
}, [deviceTimezone, ignoredTimezone, isLoggedIn, user?.timezone]);
}, [
deviceTimezone,
flags?.timezoneMismatchIgnore,
isLoggedIn,
user?.timezone,
]);

// once off check to see how many users with timezone mismatches we have in the wild
useEffect(() => {
Expand All @@ -53,14 +56,14 @@ export const useStreakTimezoneOk = (): boolean => {
device_timezone: deviceTimezone,
user_timezone: user?.timezone,
timezone_ok: isTimezoneOk,
timezone_ignore: ignoredTimezone,
timezone_ignore: flags?.timezoneMismatchIgnore,
}),
});

completeAction(ActionType.StreakTimezoneMismatch);
}, [
isTimezoneOk,
ignoredTimezone,
flags?.timezoneMismatchIgnore,
isActionsFetched,
checkHasCompleted,
completeAction,
Expand Down

0 comments on commit ed994f1

Please sign in to comment.