-
-
Notifications
You must be signed in to change notification settings - Fork 11.8k
Added multiple-active-subscriptions members warning and filter #28232
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
59eb759
Added multiple subscriptions member filter
kevinansfield ddfccfb
Updated styles
peterzimon 98d563c
Updated test change
peterzimon bc433ac
Changed multiple subscriptions filter to a hidden filter predicate
kevinansfield fcd3335
✨ Added a warning for members with multiple active Stripe subscriptions
kevinansfield 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
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
49 changes: 49 additions & 0 deletions
49
apps/posts/src/views/members/components/multiple-active-subscriptions-banner.tsx
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,49 @@ | ||
| import {Banner} from '@tryghost/shade/components'; | ||
| import {formatNumber} from '@tryghost/shade/utils'; | ||
| import {useMultipleActiveSubscriptionsBanner} from '../hooks/use-multiple-active-subscriptions-banner'; | ||
|
|
||
| interface MultipleActiveSubscriptionsBannerProps { | ||
| nql?: string; | ||
| search: string; | ||
| } | ||
|
|
||
| const MultipleActiveSubscriptionsBanner = ({ | ||
| nql, | ||
| search | ||
| }: MultipleActiveSubscriptionsBannerProps) => { | ||
| const banner = useMultipleActiveSubscriptionsBanner({ | ||
| nql, | ||
| search | ||
| }); | ||
|
|
||
| if (!banner.shouldShow) { | ||
| return null; | ||
| } | ||
|
|
||
| return ( | ||
| <Banner | ||
| role="status" | ||
| variant="warning" | ||
| {...(banner.canDismiss ? { | ||
| dismissible: true as const, | ||
| onDismiss: banner.handleDismiss | ||
| } : { | ||
| dismissible: false as const | ||
| })} | ||
| > | ||
| <div className="flex flex-col items-baseline gap-3 pr-8 sm:flex-row"> | ||
| We found {formatNumber(banner.count)} {banner.count === 1 ? 'member' : 'members'} with more than one active paid subscription.{' '} | ||
| <div className="flex items-baseline gap-3"> | ||
| {banner.canDismiss && ( | ||
| <button className="nowrap font-semibold !underline" type="button" onClick={banner.handleViewMembers}> | ||
| View members | ||
| </button> | ||
| )} | ||
| <a className="nowrap font-semibold underline" href="https://ghost.org/help/duplicate-subscription-warning/" rel="noopener noreferrer" target="_blank">Learn more</a> | ||
| </div> | ||
| </div> | ||
| </Banner> | ||
| ); | ||
| }; | ||
|
|
||
| export default MultipleActiveSubscriptionsBanner; |
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
148 changes: 148 additions & 0 deletions
148
apps/posts/src/views/members/hooks/use-multiple-active-subscriptions-banner.ts
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,148 @@ | ||
| import { | ||
| MULTIPLE_ACTIVE_STRIPE_CUSTOMERS_FILTER, | ||
| buildUserWithDismissedMultipleActiveSubscriptionsBanner, | ||
| getMultipleActiveSubscriptionsBannerPreference, | ||
| isMultipleActiveSubscriptionsFilter | ||
| } from '../multiple-active-subscriptions'; | ||
| import {buildMembersUrl} from '../member-route'; | ||
| import {canManageMembers, useEditUser} from '@tryghost/admin-x-framework/api/users'; | ||
| import {toast} from 'sonner'; | ||
| import {useBrowseMembers} from '@tryghost/admin-x-framework/api/members'; | ||
| import {useCallback, useEffect, useMemo, useState} from 'react'; | ||
| import {useCurrentUser} from '@tryghost/admin-x-framework/api/current-user'; | ||
| import {useNavigate} from 'react-router'; | ||
|
|
||
| interface UseMultipleActiveSubscriptionsBannerOptions { | ||
| nql?: string; | ||
| search: string; | ||
| } | ||
|
|
||
| /** | ||
| * Drives the banner warning that some members have active subscriptions across | ||
| * multiple Stripe customers. Dismissal is stored per-user as the member count | ||
| * at dismissal time, so the banner stays hidden until the count grows beyond | ||
| * what the user last acknowledged. | ||
| */ | ||
| export function useMultipleActiveSubscriptionsBanner({ | ||
| nql, | ||
| search | ||
| }: UseMultipleActiveSubscriptionsBannerOptions) { | ||
| const navigate = useNavigate(); | ||
| const {data: currentUser} = useCurrentUser(); | ||
| const {mutateAsync: editUser, isLoading: isDismissing} = useEditUser(); | ||
| const [optimisticDismissedCount, setOptimisticDismissedCount] = useState<number | null>(null); | ||
|
|
||
| const canManageMemberList = currentUser ? canManageMembers(currentUser) : false; | ||
| const isViewingFilter = isMultipleActiveSubscriptionsFilter(nql); | ||
| // Only relevant on the unfiltered member list or when viewing the | ||
| // affected members themselves — any other filter/search hides the banner. | ||
| const shouldConsiderBanner = !search && (!nql || isViewingFilter); | ||
|
|
||
| // Count-only query: we just need pagination.total, not the members. | ||
| const { | ||
| data | ||
| } = useBrowseMembers({ | ||
| searchParams: { | ||
| filter: MULTIPLE_ACTIVE_STRIPE_CUSTOMERS_FILTER, | ||
| limit: '1', | ||
| fields: 'id', | ||
| order: 'id' | ||
| }, | ||
| defaultErrorHandler: false, | ||
| enabled: canManageMemberList && shouldConsiderBanner, | ||
| refetchOnMount: 'always', | ||
| staleTime: 0 | ||
| }); | ||
|
|
||
| const count = data?.meta?.pagination?.total ?? 0; | ||
| const preference = useMemo(() => { | ||
| return getMultipleActiveSubscriptionsBannerPreference(currentUser?.accessibility); | ||
| }, [currentUser?.accessibility]); | ||
| const dismissedCount = optimisticDismissedCount ?? preference.dismissedCount ?? 0; | ||
| // While viewing the filtered list the banner explains what's being shown, | ||
| // so it can't be dismissed and ignores any previous dismissal. | ||
| const canDismiss = !isViewingFilter; | ||
| const shouldShow = shouldConsiderBanner | ||
| && ( | ||
| isViewingFilter | ||
| || count > dismissedCount | ||
| ); | ||
|
|
||
| // When the member count shrinks below the stored dismissal count, lower the | ||
| // stored count to match — otherwise fixing some members would leave enough | ||
| // headroom for new occurrences to go unnoticed until the old high-water | ||
| // mark is passed again. | ||
| useEffect(() => { | ||
| const storedDismissedCount = preference.dismissedCount; | ||
|
|
||
| if ( | ||
| !currentUser | ||
| || optimisticDismissedCount !== null | ||
| || isDismissing | ||
| || storedDismissedCount === undefined | ||
| || data === undefined | ||
| || count >= storedDismissedCount | ||
| ) { | ||
| return; | ||
| } | ||
|
|
||
| setOptimisticDismissedCount(count); | ||
|
|
||
| editUser(buildUserWithDismissedMultipleActiveSubscriptionsBanner( | ||
| currentUser, | ||
| count, | ||
| preference.dismissedAt ?? new Date().toISOString() | ||
| )).then(() => { | ||
| setOptimisticDismissedCount(null); | ||
| }).catch((error) => { | ||
| setOptimisticDismissedCount(null); | ||
| // This keeps the preference in sync opportunistically; failing to sync should not interrupt the member list. | ||
| // eslint-disable-next-line no-console | ||
| console.log('Unable to update multiple active subscriptions banner dismissed count', error); | ||
| }); | ||
| }, [ | ||
| count, | ||
| currentUser, | ||
| data, | ||
| editUser, | ||
| isDismissing, | ||
| optimisticDismissedCount, | ||
| preference.dismissedAt, | ||
| preference.dismissedCount | ||
| ]); | ||
|
|
||
| // Hides the banner immediately via optimistic state, then persists the | ||
| // current count to the user's accessibility preferences. | ||
| const handleDismiss = useCallback(() => { | ||
| if (!currentUser || isDismissing) { | ||
| return; | ||
| } | ||
|
|
||
| const previousDismissedCount = optimisticDismissedCount; | ||
|
|
||
| setOptimisticDismissedCount(count); | ||
|
|
||
| editUser(buildUserWithDismissedMultipleActiveSubscriptionsBanner( | ||
| currentUser, | ||
| count, | ||
| new Date().toISOString() | ||
| )).then(() => { | ||
| setOptimisticDismissedCount(null); | ||
| }).catch(() => { | ||
| setOptimisticDismissedCount(previousDismissedCount); | ||
| toast.error('Unable to dismiss notification. Please try again.'); | ||
| }); | ||
| }, [count, currentUser, editUser, isDismissing, optimisticDismissedCount]); | ||
|
|
||
| const handleViewMembers = useCallback(() => { | ||
| navigate(buildMembersUrl({filter: MULTIPLE_ACTIVE_STRIPE_CUSTOMERS_FILTER})); | ||
| }, [navigate]); | ||
|
|
||
| return { | ||
| canDismiss, | ||
| count, | ||
| handleDismiss, | ||
| handleViewMembers, | ||
| shouldShow | ||
| }; | ||
| } | ||
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
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.
Would it make more sense to have something tied to a date rather than the count? Or should it just be if you dismiss the banner, then it's gone forever?
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.
The dismiss/re-show behaviour was discussed a fair bit. The behaviour we landed on for this initial version is that any time the count increases from the last known value we re-show the banner, although the data model should support time-based dismissals in future if we decide that's needed.