From 736622fbfa5b0973006d1af96df0a54b065fbe61 Mon Sep 17 00:00:00 2001 From: Chris Bongers Date: Thu, 12 Nov 2026 16:14:35 +0200 Subject: [PATCH 1/2] feat(streak): one-tap reminder opt-out from the push /notifications/streak-reminder/off is the target of the "Turn off streak reminders" button on the streak pushes. For a signed-in reader it removes the streak_reminder digest row, as the settings switch does, and also mutes the in-app streak_reminder flag so no path sends another one, then toasts and lands on /settings/notifications where the same row turns it back on. A signed-out reader goes through the usual protected-page login with after_auth pointing back at the link. The mute waits for both the reminder row and the notification flags to load, since the flags mutation replaces the whole set from client state. Co-Authored-By: Claude Fable 5.1 --- .../src/hooks/streaks/useStreakReminder.ts | 44 ++++- packages/shared/src/lib/log.ts | 1 + .../__tests__/StreakReminderOffPage.spec.tsx | 165 ++++++++++++++++++ .../notifications/streak-reminder/off.tsx | 60 +++++++ 4 files changed, 264 insertions(+), 6 deletions(-) create mode 100644 packages/webapp/__tests__/StreakReminderOffPage.spec.tsx create mode 100644 packages/webapp/pages/notifications/streak-reminder/off.tsx diff --git a/packages/shared/src/hooks/streaks/useStreakReminder.ts b/packages/shared/src/hooks/streaks/useStreakReminder.ts index 9124428db70..7db5a76c398 100644 --- a/packages/shared/src/hooks/streaks/useStreakReminder.ts +++ b/packages/shared/src/hooks/streaks/useStreakReminder.ts @@ -15,18 +15,27 @@ export const STREAK_REMINDER_HOUR = 20; interface UseStreakReminder { isSubscribed: boolean; + /** Both the reminder row and the notification flags have answered. */ + isReady: boolean; toggleStreakReminder: () => void; + turnOffStreakReminder: () => Promise; } +const toggleLogExtra = JSON.stringify({ + channel: NotificationChannel.Web, + category: NotificationCategory.StreakReminder, +}); + export const useStreakReminder = (): UseStreakReminder => { const { user } = useAuthContext(); const { logEvent } = useLogContext(); - const { notificationSettings, setNotificationStatus } = + const { notificationSettings, setNotificationStatus, isLoadingPreferences } = useNotificationSettings(); const { getPersonalizedDigest, subscribePersonalizedDigest, unsubscribePersonalizedDigest, + isSettled, } = usePersonalizedDigest(); const isMuted = @@ -36,16 +45,34 @@ export const useStreakReminder = (): UseStreakReminder => { !isMuted && !!getPersonalizedDigest(UserPersonalizedDigestType.StreakReminder); + // The one-tap opt-out from the push: removes the reminder row like the + // settings switch and also mutes the in-app flag, so nothing sends another + const turnOffStreakReminder = async () => { + logEvent({ + event_name: LogEvent.DisableNotification, + extra: toggleLogExtra, + }); + + if (notificationSettings && !isMuted) { + setNotificationStatus( + NotificationType.StreakReminder, + 'inApp', + NotificationPreferenceStatus.Muted, + ); + } + + await unsubscribePersonalizedDigest({ + type: UserPersonalizedDigestType.StreakReminder, + }); + }; + const toggleStreakReminder = () => { const value = !isSubscribed; logEvent({ event_name: value ? LogEvent.EnableNotification : LogEvent.DisableNotification, - extra: JSON.stringify({ - channel: NotificationChannel.Web, - category: NotificationCategory.StreakReminder, - }), + extra: toggleLogExtra, }); if (!value) { @@ -78,5 +105,10 @@ export const useStreakReminder = (): UseStreakReminder => { }); }; - return { isSubscribed, toggleStreakReminder }; + return { + isSubscribed, + isReady: isSettled && !isLoadingPreferences, + toggleStreakReminder, + turnOffStreakReminder, + }; }; diff --git a/packages/shared/src/lib/log.ts b/packages/shared/src/lib/log.ts index 055c08b7d44..a94525459f2 100644 --- a/packages/shared/src/lib/log.ts +++ b/packages/shared/src/lib/log.ts @@ -783,6 +783,7 @@ export enum TargetType { StreaksMilestone = 'streaks milestone', StreakRecover = 'streak restore', StreakFreezePurchase = 'streak freeze purchase', + StreakReminderOptOut = 'streak reminder opt out', PromotionCard = 'promotion_card', PromotionalBanner = 'promotion_banner', MarketingCtaPopover = 'promotion_popover', diff --git a/packages/webapp/__tests__/StreakReminderOffPage.spec.tsx b/packages/webapp/__tests__/StreakReminderOffPage.spec.tsx new file mode 100644 index 00000000000..5d19fa490f9 --- /dev/null +++ b/packages/webapp/__tests__/StreakReminderOffPage.spec.tsx @@ -0,0 +1,165 @@ +import React from 'react'; +import nock from 'nock'; +import type { RenderResult } from '@testing-library/react'; +import { render, waitFor } from '@testing-library/react'; +import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; +import type { NextRouter } from 'next/router'; +import { useRouter } from 'next/router'; +import { AuthContextProvider } from '@dailydotdev/shared/src/contexts/AuthContext'; +import { getLogContextStatic } from '@dailydotdev/shared/src/contexts/LogContext'; +import { mockGraphQL } from '@dailydotdev/shared/__tests__/helpers/graphql'; +import { waitForNock } from '@dailydotdev/shared/__tests__/helpers/utilities'; +import loggedUser from '@dailydotdev/shared/__tests__/fixture/loggedUser'; +import type { LoggedUser } from '@dailydotdev/shared/src/lib/user'; +import { + GET_NOTIFICATION_SETTINGS, + GET_PERSONALIZED_DIGEST_SETTINGS, + UNSUBSCRIBE_PERSONALIZED_DIGEST_MUTATION, + UPDATE_NOTIFICATION_SETTINGS_MUTATION, + UserPersonalizedDigestType, +} from '@dailydotdev/shared/src/graphql/users'; +import { NotificationPreferenceStatus } from '@dailydotdev/shared/src/graphql/notifications'; +import { NotificationType } from '@dailydotdev/shared/src/components/notifications/utils'; +import { TOAST_NOTIF_KEY } from '@dailydotdev/shared/src/hooks/useToastNotification'; +import type { ToastNotification } from '@dailydotdev/shared/src/hooks/useToastNotification'; +import { LogEvent, TargetType } from '@dailydotdev/shared/src/lib/log'; +import { AFTER_AUTH_PARAM } from '@dailydotdev/shared/src/components/auth/common'; +import StreakReminderOffPage, { + streakReminderOffToast, + streakReminderSettingsPath, +} from '../pages/notifications/streak-reminder/off'; + +const LogContext = getLogContextStatic(); +const path = '/notifications/streak-reminder/off'; +const routerReplace = jest.fn(); +const logEvent = jest.fn(); +let client: QueryClient; + +const notificationSettings = { + [NotificationType.StreakReminder]: { + email: NotificationPreferenceStatus.Muted, + inApp: NotificationPreferenceStatus.Subscribed, + }, + [NotificationType.StreakResetRestore]: { + email: NotificationPreferenceStatus.Subscribed, + inApp: NotificationPreferenceStatus.Subscribed, + }, +}; + +beforeEach(() => { + nock.cleanAll(); + jest.clearAllMocks(); + client = new QueryClient(); + jest.mocked(useRouter).mockImplementation( + () => + ({ + pathname: path, + asPath: path, + query: {}, + replace: routerReplace, + push: jest.fn(), + isReady: true, + }) as unknown as NextRouter, + ); +}); + +const mockSettingsQueries = () => { + mockGraphQL({ + request: { query: GET_PERSONALIZED_DIGEST_SETTINGS, variables: {} }, + result: { + data: { + personalizedDigest: [ + { + preferredDay: 3, + preferredHour: 20, + type: UserPersonalizedDigestType.StreakReminder, + flags: { sendType: 'workdays' }, + deliveryPaused: false, + }, + ], + }, + }, + }); + mockGraphQL({ + request: { query: GET_NOTIFICATION_SETTINGS }, + result: { data: { notificationSettings } }, + }); +}; + +const renderPage = (user?: LoggedUser): RenderResult => + render( + + + + + + + , + ); + +it('should remove the reminder, mute the flag, toast and land on settings', async () => { + mockSettingsQueries(); + mockGraphQL({ + request: { + query: UPDATE_NOTIFICATION_SETTINGS_MUTATION, + variables: { + notificationFlags: { + ...notificationSettings, + [NotificationType.StreakReminder]: { + email: NotificationPreferenceStatus.Muted, + inApp: NotificationPreferenceStatus.Muted, + }, + }, + }, + }, + result: { data: { updateNotificationSettings: { _: true } } }, + }); + mockGraphQL({ + request: { + query: UNSUBSCRIBE_PERSONALIZED_DIGEST_MUTATION, + variables: { type: UserPersonalizedDigestType.StreakReminder }, + }, + result: { data: { unsubscribePersonalizedDigest: { _: true } } }, + }); + + renderPage(loggedUser); + + await waitForNock(); + await waitFor(() => + expect(routerReplace).toHaveBeenCalledWith(streakReminderSettingsPath), + ); + expect(routerReplace).toHaveBeenCalledTimes(1); + expect(client.getQueryData(TOAST_NOTIF_KEY)?.message).toBe( + streakReminderOffToast, + ); + expect(logEvent).toHaveBeenCalledWith({ + event_name: LogEvent.Click, + target_type: TargetType.StreakReminderOptOut, + extra: JSON.stringify({ origin: 'push' }), + }); +}); + +it('should send a signed-out reader to login and back to this link', async () => { + renderPage(); + + await waitFor(() => expect(routerReplace).toHaveBeenCalledTimes(1)); + const [redirect] = routerReplace.mock.calls[0]; + expect(redirect).toContain('onboarding?'); + expect( + new URL(redirect, 'http://localhost').searchParams.get(AFTER_AUTH_PARAM), + ).toBe(path); + expect(logEvent).not.toHaveBeenCalled(); + expect(client.getQueryData(TOAST_NOTIF_KEY)).toBeFalsy(); +}); diff --git a/packages/webapp/pages/notifications/streak-reminder/off.tsx b/packages/webapp/pages/notifications/streak-reminder/off.tsx new file mode 100644 index 00000000000..f0a8020ac73 --- /dev/null +++ b/packages/webapp/pages/notifications/streak-reminder/off.tsx @@ -0,0 +1,60 @@ +import type { ReactElement } from 'react'; +import React, { useEffect, useRef } from 'react'; +import { useRouter } from 'next/router'; +import type { NextSeoProps } from 'next-seo'; +import { useAuthContext } from '@dailydotdev/shared/src/contexts/AuthContext'; +import { useLogContext } from '@dailydotdev/shared/src/contexts/LogContext'; +import { useStreakReminder } from '@dailydotdev/shared/src/hooks/streaks/useStreakReminder'; +import { useToastNotification } from '@dailydotdev/shared/src/hooks/useToastNotification'; +import { LogEvent, TargetType } from '@dailydotdev/shared/src/lib/log'; +import ProtectedPage from '../../../components/ProtectedPage'; +import { getLayout } from '../../../components/layouts/MainLayout'; +import { noindexSeoProps } from '../../../next-seo'; + +export const streakReminderOffToast = + 'Streak reminders are off. You can turn them back on in Settings.'; +export const streakReminderOffFailedToast = + 'Could not turn off streak reminders. You can turn them off in Settings.'; +export const streakReminderSettingsPath = '/settings/notifications'; + +const seo: NextSeoProps = { ...noindexSeoProps, title: 'Streak reminders' }; + +// Target of the "Turn off streak reminders" button on the streak pushes. One +// tap mutes the reminder for a signed-in reader and lands on the settings +// row that turns it back on. +const StreakReminderOffPage = (): ReactElement => { + const router = useRouter(); + const { user } = useAuthContext(); + const { logEvent } = useLogContext(); + const { displayToast } = useToastNotification(); + const { isReady, turnOffStreakReminder } = useStreakReminder(); + const started = useRef(false); + + useEffect(() => { + if (!user || !isReady || started.current) { + return; + } + + started.current = true; + turnOffStreakReminder() + .then(() => { + logEvent({ + event_name: LogEvent.Click, + target_type: TargetType.StreakReminderOptOut, + extra: JSON.stringify({ origin: 'push' }), + }); + displayToast(streakReminderOffToast); + }) + .catch(() => displayToast(streakReminderOffFailedToast)) + .finally(() => router.replace(streakReminderSettingsPath)); + // @NOTE see https://dailydotdev.atlassian.net/l/cp/dK9h1zoM + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [user, isReady]); + + return {null}; +}; + +StreakReminderOffPage.getLayout = getLayout; +StreakReminderOffPage.layoutProps = { seo }; + +export default StreakReminderOffPage; From ea94f5c45e977d88806afda6994eea4d79e0b1a0 Mon Sep 17 00:00:00 2001 From: Chris Bongers Date: Mon, 16 Nov 2026 11:12:32 +0200 Subject: [PATCH 2/2] fix(streak): drop the in-app mute from the streak reminder opt-out page The one-tap opt-out now only deletes the reminder row, like the settings switch. The API stamps the opt-out so the row is not recreated, and the mute was a second source of truth that a later re-enable never cleared. Co-Authored-By: Claude Opus 5.5 --- .../src/hooks/streaks/useStreakReminder.ts | 16 ++----- .../__tests__/StreakReminderOffPage.spec.tsx | 44 +++++++++++-------- .../notifications/streak-reminder/off.tsx | 2 +- 3 files changed, 30 insertions(+), 32 deletions(-) diff --git a/packages/shared/src/hooks/streaks/useStreakReminder.ts b/packages/shared/src/hooks/streaks/useStreakReminder.ts index 7db5a76c398..ae2eae93a17 100644 --- a/packages/shared/src/hooks/streaks/useStreakReminder.ts +++ b/packages/shared/src/hooks/streaks/useStreakReminder.ts @@ -15,7 +15,7 @@ export const STREAK_REMINDER_HOUR = 20; interface UseStreakReminder { isSubscribed: boolean; - /** Both the reminder row and the notification flags have answered. */ + /** The reminder row query has answered. */ isReady: boolean; toggleStreakReminder: () => void; turnOffStreakReminder: () => Promise; @@ -29,7 +29,7 @@ const toggleLogExtra = JSON.stringify({ export const useStreakReminder = (): UseStreakReminder => { const { user } = useAuthContext(); const { logEvent } = useLogContext(); - const { notificationSettings, setNotificationStatus, isLoadingPreferences } = + const { notificationSettings, setNotificationStatus } = useNotificationSettings(); const { getPersonalizedDigest, @@ -46,21 +46,13 @@ export const useStreakReminder = (): UseStreakReminder => { !!getPersonalizedDigest(UserPersonalizedDigestType.StreakReminder); // The one-tap opt-out from the push: removes the reminder row like the - // settings switch and also mutes the in-app flag, so nothing sends another + // settings switch. The API stamps the opt-out so the row is not recreated. const turnOffStreakReminder = async () => { logEvent({ event_name: LogEvent.DisableNotification, extra: toggleLogExtra, }); - if (notificationSettings && !isMuted) { - setNotificationStatus( - NotificationType.StreakReminder, - 'inApp', - NotificationPreferenceStatus.Muted, - ); - } - await unsubscribePersonalizedDigest({ type: UserPersonalizedDigestType.StreakReminder, }); @@ -107,7 +99,7 @@ export const useStreakReminder = (): UseStreakReminder => { return { isSubscribed, - isReady: isSettled && !isLoadingPreferences, + isReady: isSettled, toggleStreakReminder, turnOffStreakReminder, }; diff --git a/packages/webapp/__tests__/StreakReminderOffPage.spec.tsx b/packages/webapp/__tests__/StreakReminderOffPage.spec.tsx index 5d19fa490f9..d8e8623a9fb 100644 --- a/packages/webapp/__tests__/StreakReminderOffPage.spec.tsx +++ b/packages/webapp/__tests__/StreakReminderOffPage.spec.tsx @@ -15,7 +15,6 @@ import { GET_NOTIFICATION_SETTINGS, GET_PERSONALIZED_DIGEST_SETTINGS, UNSUBSCRIBE_PERSONALIZED_DIGEST_MUTATION, - UPDATE_NOTIFICATION_SETTINGS_MUTATION, UserPersonalizedDigestType, } from '@dailydotdev/shared/src/graphql/users'; import { NotificationPreferenceStatus } from '@dailydotdev/shared/src/graphql/notifications'; @@ -25,6 +24,7 @@ import type { ToastNotification } from '@dailydotdev/shared/src/hooks/useToastNo import { LogEvent, TargetType } from '@dailydotdev/shared/src/lib/log'; import { AFTER_AUTH_PARAM } from '@dailydotdev/shared/src/components/auth/common'; import StreakReminderOffPage, { + streakReminderOffFailedToast, streakReminderOffToast, streakReminderSettingsPath, } from '../pages/notifications/streak-reminder/off'; @@ -109,31 +109,19 @@ const renderPage = (user?: LoggedUser): RenderResult => , ); -it('should remove the reminder, mute the flag, toast and land on settings', async () => { - mockSettingsQueries(); - mockGraphQL({ - request: { - query: UPDATE_NOTIFICATION_SETTINGS_MUTATION, - variables: { - notificationFlags: { - ...notificationSettings, - [NotificationType.StreakReminder]: { - email: NotificationPreferenceStatus.Muted, - inApp: NotificationPreferenceStatus.Muted, - }, - }, - }, - }, - result: { data: { updateNotificationSettings: { _: true } } }, - }); +const mockUnsubscribe = (result: Parameters[0]['result']) => mockGraphQL({ request: { query: UNSUBSCRIBE_PERSONALIZED_DIGEST_MUTATION, variables: { type: UserPersonalizedDigestType.StreakReminder }, }, - result: { data: { unsubscribePersonalizedDigest: { _: true } } }, + result, }); +it('should remove the reminder, toast and land on settings', async () => { + mockSettingsQueries(); + mockUnsubscribe({ data: { unsubscribePersonalizedDigest: { _: true } } }); + renderPage(loggedUser); await waitForNock(); @@ -151,6 +139,24 @@ it('should remove the reminder, mute the flag, toast and land on settings', asyn }); }); +it('should toast the failure when the reminder could not be removed', async () => { + mockSettingsQueries(); + mockUnsubscribe({ errors: [{ message: 'Unexpected error' }] }); + + renderPage(loggedUser); + + await waitForNock(); + await waitFor(() => + expect(routerReplace).toHaveBeenCalledWith(streakReminderSettingsPath), + ); + expect(client.getQueryData(TOAST_NOTIF_KEY)?.message).toBe( + streakReminderOffFailedToast, + ); + expect(logEvent).not.toHaveBeenCalledWith( + expect.objectContaining({ target_type: TargetType.StreakReminderOptOut }), + ); +}); + it('should send a signed-out reader to login and back to this link', async () => { renderPage(); diff --git a/packages/webapp/pages/notifications/streak-reminder/off.tsx b/packages/webapp/pages/notifications/streak-reminder/off.tsx index f0a8020ac73..07d0333d94e 100644 --- a/packages/webapp/pages/notifications/streak-reminder/off.tsx +++ b/packages/webapp/pages/notifications/streak-reminder/off.tsx @@ -20,7 +20,7 @@ export const streakReminderSettingsPath = '/settings/notifications'; const seo: NextSeoProps = { ...noindexSeoProps, title: 'Streak reminders' }; // Target of the "Turn off streak reminders" button on the streak pushes. One -// tap mutes the reminder for a signed-in reader and lands on the settings +// tap removes the reminder for a signed-in reader and lands on the settings // row that turns it back on. const StreakReminderOffPage = (): ReactElement => { const router = useRouter();