-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Introduce free trial countdown pop-up #84049
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dukenv0307
wants to merge
29
commits into
Expensify:main
Choose a base branch
from
dukenv0307:fix/84022
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+626
−0
Open
Changes from 10 commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
106994b
Introduce free trial countdown pop-up
dukenv0307 f21e45e
add tests
dukenv0307 905d611
update code
dukenv0307 af73253
early return if loading state
dukenv0307 c98fb52
add translations
dukenv0307 ae8600c
update changes
dukenv0307 f3794e2
Merge branch 'main' into fix/84022
dukenv0307 25295e1
Merge branch 'main' into fix/84022
dukenv0307 f44c7f2
update height
dukenv0307 a5aee0d
update style
dukenv0307 9ddf5ba
resolve the conflict
dukenv0307 130e3e2
update nvp key
dukenv0307 ab2b357
Merge branch 'main' into fix/84022
dukenv0307 6d18fcf
add shouldShowProactiveAppReviewModal check in useTrialPaymentReminder
dukenv0307 72790af
Merge branch 'main' into fix/84022
dukenv0307 2771a93
fix issue modal open when create the new workspace
dukenv0307 9b68afb
lint fix
dukenv0307 3f1c0d4
resolve conflict
dukenv0307 646f429
Suppress AppReview modal when another modal is visible
dukenv0307 bfa0e34
Merge branch 'main' into fix/84022
dukenv0307 7713e7f
update shouldShowModal
dukenv0307 192ad78
format
dukenv0307 cbb41d6
move logic to modal manager
dukenv0307 e5aadf3
Merge branch 'main' into fix/84022
dukenv0307 34c8614
fix comments
dukenv0307 bd248d5
spell
dukenv0307 a271c37
Merge branch 'main' into fix/84022
dukenv0307 23c2589
Merge branch 'main' into fix/84022
dukenv0307 fda035f
move buttons
dukenv0307 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| import React from 'react'; | ||
| import {View} from 'react-native'; | ||
| import {useMemoizedLazyIllustrations} from '@hooks/useLazyAsset'; | ||
| import useLocalize from '@hooks/useLocalize'; | ||
| import useResponsiveLayout from '@hooks/useResponsiveLayout'; | ||
| import useThemeStyles from '@hooks/useThemeStyles'; | ||
| import type {CountdownTime, TrialReminderVariant} from '@hooks/useTrialPaymentReminder'; | ||
| import colors from '@styles/theme/colors'; | ||
| import CONST from '@src/CONST'; | ||
| import Button from './Button'; | ||
| import ImageSVG from './ImageSVG'; | ||
| import Modal from './Modal'; | ||
| import Text from './Text'; | ||
|
|
||
| type TrialPaymentReminderModalProps = { | ||
| /** Whether the modal is visible */ | ||
| isVisible: boolean; | ||
|
|
||
| /** The variant of the modal to display */ | ||
| variant: TrialReminderVariant; | ||
|
|
||
| /** Number of days remaining for 'nearEnd' variant */ | ||
| daysRemaining?: number; | ||
|
|
||
| /** Countdown time for 'countdown' variant */ | ||
| countdownTime?: CountdownTime; | ||
|
|
||
| /** Called when user presses Close */ | ||
| onClose: () => void; | ||
|
|
||
| /** Called when user presses Add payment card */ | ||
| onAddPaymentCard: () => void; | ||
| }; | ||
|
|
||
| function padZero(num: number): string { | ||
| return num.toString().padStart(2, '0'); | ||
| } | ||
|
|
||
| function TrialPaymentReminderModal({isVisible, variant, daysRemaining, countdownTime, onClose, onAddPaymentCard}: TrialPaymentReminderModalProps) { | ||
| const {shouldUseNarrowLayout} = useResponsiveLayout(); | ||
| const styles = useThemeStyles(); | ||
| const illustrations = useMemoizedLazyIllustrations(['ArmWithCardPos']); | ||
| const {translate} = useLocalize(); | ||
|
|
||
| return ( | ||
| <Modal | ||
| onClose={onClose} | ||
| onBackdropPress={() => {}} | ||
| isVisible={isVisible} | ||
| type={shouldUseNarrowLayout ? CONST.MODAL.MODAL_TYPE.BOTTOM_DOCKED : CONST.MODAL.MODAL_TYPE.CONFIRM} | ||
| innerContainerStyle={styles.pv0} | ||
| > | ||
| <View style={[styles.alignItemsCenter, styles.wAuto, {backgroundColor: colors.blue800, height: CONST.CONFIRM_CONTENT_SVG_SIZE.HEIGHT}, styles.pb7]}> | ||
| <ImageSVG | ||
| src={illustrations.ArmWithCardPos} | ||
| contentFit="contain" | ||
| /> | ||
| </View> | ||
| <View style={[styles.m5]}> | ||
| {variant === CONST.TRIAL_REMINDER_VARIANT.NEAR_END && daysRemaining !== undefined && ( | ||
| <Text style={[styles.textSuccess, styles.textStrong, styles.mb2]}>{translate('trialPaymentReminder.trialEndsInDays', {days: daysRemaining})}</Text> | ||
| )} | ||
| {variant === CONST.TRIAL_REMINDER_VARIANT.COUNTDOWN && !!countdownTime && ( | ||
| <Text style={[styles.textSuccess, styles.textStrong, styles.mb2]}> | ||
| {translate('trialPaymentReminder.trialEndsCountdown', { | ||
| hours: padZero(countdownTime.hours), | ||
| minutes: padZero(countdownTime.minutes), | ||
| seconds: padZero(countdownTime.seconds), | ||
| })} | ||
| </Text> | ||
| )} | ||
|
|
||
| <Text style={[styles.textHeadlineH1, styles.mb3]}>{translate('trialPaymentReminder.title')}</Text> | ||
| <Text style={[styles.textSupporting]}>{translate('trialPaymentReminder.subtitle')}</Text> | ||
|
|
||
| <Button | ||
| style={[styles.mt5]} | ||
| onPress={onClose} | ||
| text={translate('trialPaymentReminder.closeButton')} | ||
| large | ||
| /> | ||
| <Button | ||
| success | ||
| style={[styles.mt3]} | ||
| onPress={onAddPaymentCard} | ||
| pressOnEnter | ||
| text={translate('trialPaymentReminder.addPaymentCardButton')} | ||
| large | ||
| /> | ||
| </View> | ||
| </Modal> | ||
| ); | ||
| } | ||
|
|
||
| export default TrialPaymentReminderModal; | ||
|
dukenv0307 marked this conversation as resolved.
|
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import React, {useCallback} from 'react'; | ||
| import useTrialPaymentReminder from '@hooks/useTrialPaymentReminder'; | ||
| import Navigation from '@libs/Navigation/Navigation'; | ||
| import ROUTES from '@src/ROUTES'; | ||
| import TrialPaymentReminderModal from './TrialPaymentReminderModal'; | ||
|
|
||
| function TrialPaymentReminderModalManager() { | ||
| const {shouldShowModal, currentVariation, countdownTime, dismiss} = useTrialPaymentReminder(); | ||
|
|
||
| const handleClose = useCallback(() => { | ||
| dismiss(); | ||
| }, [dismiss]); | ||
|
|
||
| const handleAddPaymentCard = useCallback(() => { | ||
| dismiss(); | ||
| Navigation.navigate(ROUTES.SETTINGS_SUBSCRIPTION_ADD_PAYMENT_CARD); | ||
| }, [dismiss]); | ||
|
|
||
| if (!currentVariation) { | ||
| return null; | ||
| } | ||
|
|
||
| return ( | ||
| <TrialPaymentReminderModal | ||
| isVisible={shouldShowModal} | ||
| variant={currentVariation.variant} | ||
| daysRemaining={currentVariation.daysRemaining} | ||
| countdownTime={countdownTime} | ||
| onClose={handleClose} | ||
| onAddPaymentCard={handleAddPaymentCard} | ||
| /> | ||
| ); | ||
| } | ||
|
|
||
| export default TrialPaymentReminderModalManager; |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,194 @@ | ||
| import {useCallback, useEffect, useMemo, useRef, useState} from 'react'; | ||
| import type {ValueOf} from 'type-fest'; | ||
| import {calculateRemainingTrialSeconds, calculateTrialDayNumber, doesUserHavePaymentCardAdded, isUserOnFreeTrial} from '@libs/SubscriptionUtils'; | ||
| import {setNameValuePair} from '@userActions/User'; | ||
| import CONST from '@src/CONST'; | ||
| import ONYXKEYS from '@src/ONYXKEYS'; | ||
| import isLoadingOnyxValue from '@src/types/utils/isLoadingOnyxValue'; | ||
| import useOnyx from './useOnyx'; | ||
|
|
||
| const FIVE_MINUTES_MS = 5 * 60 * 1000; | ||
| const TWENTY_FOUR_HOURS_IN_SECONDS = 24 * 60 * 60; | ||
|
|
||
| /** Waiting for trial data to load */ | ||
| const DELAY_LOADING = 'loading'; | ||
| /** No trial yet, waiting for workspace creation */ | ||
| const DELAY_WAITING = 'waiting'; | ||
| /** Trial just started, 5-minute timer running */ | ||
| const DELAY_TIMING = 'timing'; | ||
| /** Ready to show modal */ | ||
| const DELAY_NO_DELAY = 'no_delay'; | ||
|
|
||
| const DELAY_STATE = { | ||
| LOADING: DELAY_LOADING, | ||
| WAITING: DELAY_WAITING, | ||
| TIMING: DELAY_TIMING, | ||
| NO_DELAY: DELAY_NO_DELAY, | ||
| } as const; | ||
|
|
||
| type TrialReminderVariant = ValueOf<typeof CONST.TRIAL_REMINDER_VARIANT>; | ||
| type DelayState = ValueOf<typeof DELAY_STATE>; | ||
|
|
||
| type TrialReminderVariation = { | ||
| id: string; | ||
| variant: TrialReminderVariant; | ||
| daysRemaining?: number; | ||
| }; | ||
|
|
||
| type CountdownTime = { | ||
| hours: number; | ||
| minutes: number; | ||
| seconds: number; | ||
| }; | ||
|
|
||
| const TRIAL_REMINDER_VARIATIONS = [ | ||
| {id: 'day1', dayOfTrial: 1, variant: CONST.TRIAL_REMINDER_VARIANT.BASIC}, | ||
| {id: 'day2', dayOfTrial: 2, variant: CONST.TRIAL_REMINDER_VARIANT.BASIC}, | ||
| {id: 'day3', dayOfTrial: 3, variant: CONST.TRIAL_REMINDER_VARIANT.BASIC}, | ||
| {id: 'day7', dayOfTrial: 7, variant: CONST.TRIAL_REMINDER_VARIANT.BASIC}, | ||
| {id: 'day15', dayOfTrial: 15, variant: CONST.TRIAL_REMINDER_VARIANT.BASIC}, | ||
| {id: 'day28', dayOfTrial: 28, variant: CONST.TRIAL_REMINDER_VARIANT.NEAR_END}, | ||
| {id: 'day29', dayOfTrial: 29, variant: CONST.TRIAL_REMINDER_VARIANT.NEAR_END}, | ||
| {id: 'last24h', dayOfTrial: -1, variant: CONST.TRIAL_REMINDER_VARIANT.COUNTDOWN}, | ||
| ] as const; | ||
|
|
||
| function computeCurrentVariation(firstDayFreeTrial: string | undefined, lastDayFreeTrial: string | undefined): TrialReminderVariation | null { | ||
| if (!isUserOnFreeTrial(firstDayFreeTrial, lastDayFreeTrial)) { | ||
| return null; | ||
| } | ||
|
|
||
| const remainingSeconds = calculateRemainingTrialSeconds(lastDayFreeTrial); | ||
|
|
||
| // Last 24 hours takes priority | ||
| if (remainingSeconds <= TWENTY_FOUR_HOURS_IN_SECONDS && remainingSeconds > 0) { | ||
| return { | ||
| id: 'last24h', | ||
| variant: CONST.TRIAL_REMINDER_VARIANT.COUNTDOWN, | ||
| }; | ||
| } | ||
|
|
||
| const currentTrialDay = calculateTrialDayNumber(firstDayFreeTrial); | ||
| if (currentTrialDay <= 0) { | ||
| return null; | ||
| } | ||
|
|
||
| for (let i = TRIAL_REMINDER_VARIATIONS.length - 1; i >= 0; i--) { | ||
| const variation = TRIAL_REMINDER_VARIATIONS[i]; | ||
| if (variation.dayOfTrial > 0 && variation.dayOfTrial <= currentTrialDay) { | ||
|
Comment on lines
+93
to
+110
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @dukenv0307 I looked at the proposal for BE. We should store a timestamp each time the NVP is dismissed and calculate via the timestamp vs the window |
||
| if (variation.variant === CONST.TRIAL_REMINDER_VARIANT.NEAR_END) { | ||
| const daysRemaining = Math.ceil(remainingSeconds / TWENTY_FOUR_HOURS_IN_SECONDS); | ||
| return { | ||
| id: variation.id, | ||
| variant: variation.variant, | ||
| daysRemaining, | ||
| }; | ||
| } | ||
| return { | ||
| id: variation.id, | ||
| variant: variation.variant, | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| function useTrialPaymentReminder() { | ||
| const [firstDayFreeTrial, firstDayFreeTrialResult] = useOnyx(ONYXKEYS.NVP_FIRST_DAY_FREE_TRIAL); | ||
| const [lastDayFreeTrial] = useOnyx(ONYXKEYS.NVP_LAST_DAY_FREE_TRIAL); | ||
| const [billingFundID] = useOnyx(ONYXKEYS.NVP_BILLING_FUND_ID); | ||
| const [dismissedVariation, dismissedVariationResult] = useOnyx(ONYXKEYS.NVP_TRIAL_PAYMENT_REMINDER_DISMISSED); | ||
|
|
||
| const [delayState, setDelayState] = useState<DelayState>(DELAY_STATE.LOADING); | ||
|
|
||
| useEffect(() => { | ||
| if (isLoadingOnyxValue(firstDayFreeTrialResult)) { | ||
| return; | ||
| } | ||
|
|
||
| if (delayState === DELAY_STATE.LOADING) { | ||
| setDelayState(firstDayFreeTrial ? DELAY_STATE.NO_DELAY : DELAY_STATE.WAITING); | ||
| return; | ||
| } | ||
|
|
||
| if (delayState === DELAY_STATE.WAITING && firstDayFreeTrial) { | ||
| setDelayState(DELAY_STATE.TIMING); | ||
| } | ||
| }, [delayState, firstDayFreeTrial, firstDayFreeTrialResult]); | ||
|
|
||
| // Run 5-minute timer when in 'timing' state | ||
| useEffect(() => { | ||
| if (delayState !== DELAY_STATE.TIMING) { | ||
| return; | ||
| } | ||
| const timer = setTimeout(() => setDelayState(DELAY_STATE.NO_DELAY), FIVE_MINUTES_MS); | ||
| return () => clearTimeout(timer); | ||
| }, [delayState]); | ||
|
|
||
| const remainingSecondsRef = useRef(0); | ||
|
|
||
| const [countdownTime, setCountdownTime] = useState<CountdownTime>({hours: 0, minutes: 0, seconds: 0}); | ||
|
|
||
| const currentVariation = useMemo(() => computeCurrentVariation(firstDayFreeTrial, lastDayFreeTrial), [firstDayFreeTrial, lastDayFreeTrial]); | ||
|
|
||
| // Run countdown timer when variant is 'countdown' | ||
| useEffect(() => { | ||
| if (currentVariation?.variant !== CONST.TRIAL_REMINDER_VARIANT.COUNTDOWN) { | ||
| return; | ||
| } | ||
|
|
||
| remainingSecondsRef.current = calculateRemainingTrialSeconds(lastDayFreeTrial); | ||
|
|
||
| const updateCountdown = () => { | ||
| remainingSecondsRef.current -= 1; | ||
| const secs = remainingSecondsRef.current; | ||
| if (secs <= 0) { | ||
| setCountdownTime({hours: 0, minutes: 0, seconds: 0}); | ||
| return; | ||
| } | ||
| setCountdownTime({ | ||
| hours: Math.floor(secs / 3600), | ||
| minutes: Math.floor((secs % 3600) / 60), | ||
| seconds: Math.floor(secs % 60), | ||
| }); | ||
| }; | ||
|
|
||
| updateCountdown(); | ||
| const interval = setInterval(updateCountdown, 1000); | ||
| return () => clearInterval(interval); | ||
| }, [currentVariation?.variant, lastDayFreeTrial]); | ||
|
|
||
| const shouldShowModal = useMemo(() => { | ||
| if (!isUserOnFreeTrial(firstDayFreeTrial, lastDayFreeTrial)) { | ||
| return false; | ||
| } | ||
| if (doesUserHavePaymentCardAdded(billingFundID)) { | ||
| return false; | ||
| } | ||
| if (delayState !== DELAY_STATE.NO_DELAY) { | ||
| return false; | ||
| } | ||
| if (isLoadingOnyxValue(dismissedVariationResult)) { | ||
| return false; | ||
| } | ||
| if (!currentVariation) { | ||
| return false; | ||
| } | ||
| if (dismissedVariation === currentVariation.id) { | ||
| return false; | ||
| } | ||
| return true; | ||
| }, [firstDayFreeTrial, lastDayFreeTrial, billingFundID, delayState, currentVariation, dismissedVariation, dismissedVariationResult]); | ||
|
|
||
| const dismiss = useCallback(() => { | ||
| if (!currentVariation) { | ||
| return; | ||
| } | ||
| setNameValuePair(ONYXKEYS.NVP_TRIAL_PAYMENT_REMINDER_DISMISSED, currentVariation.id, dismissedVariation ?? ''); | ||
| }, [currentVariation, dismissedVariation]); | ||
|
|
||
| return {shouldShowModal, currentVariation, countdownTime, dismiss}; | ||
| } | ||
|
|
||
| export default useTrialPaymentReminder; | ||
| export type {TrialReminderVariant, CountdownTime}; | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just confirming, same color for both dark/light mode?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@grgia Yes, I think it's correct cc @shawnborton
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cc @Expensify/design - should the green button always be at the bottom in this case? Also this might be a good excuse to use our ghost button for the Close button and put it at the bottom?
Otherwise yes, the colors in the themes look correct to me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm maybe so? Our regular "confirmation dialog" has the primary action on top, but I'm not sure if we use a different placement for these types of modals. Either of you have any other examples of this type?

I was thinking this could be nice, but I'm not sure I love how it looks in practice. What do you think?

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, this doesn't feel great to me.
But yeah, I tend to think that the green button should be above the primary button when we are showing it in a modal.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@shawnborton @dannymcclain Is it ok?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That looks good to me 👍