-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
feat(usage overview): Introduce util functions and hook #104131
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
+357
−18
Merged
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| import type {DataCategory} from 'sentry/types/core'; | ||
| import {toTitleCase} from 'sentry/utils/string/toTitleCase'; | ||
|
|
||
| import type {AddOn, AddOnCategory, ProductTrial, Subscription} from 'getsentry/types'; | ||
| import { | ||
| checkIsAddOn, | ||
| getActiveProductTrial, | ||
| getBilledCategory, | ||
| getPotentialProductTrial, | ||
| productIsEnabled, | ||
| } from 'getsentry/utils/billing'; | ||
| import {getPlanCategoryName} from 'getsentry/utils/dataCategory'; | ||
|
|
||
| interface ProductBillingMetadata { | ||
| /** | ||
| * The active product trial for the given product, if any. | ||
| * Always null when excludeProductTrials is true. | ||
| */ | ||
| activeProductTrial: ProductTrial | null; | ||
| /** | ||
| * The billed category for the given product. | ||
| * When product is a DataCategory, we just return product. | ||
| * When product is an AddOnCategory, we return the billed category for the | ||
| * add-on. | ||
| */ | ||
| billedCategory: DataCategory | null; | ||
| /** | ||
| * The display name for the given product in title case. | ||
| */ | ||
| displayName: string; | ||
| /** | ||
| * Whether the product is an add-on. | ||
| */ | ||
| isAddOn: boolean; | ||
| /** | ||
| * Whether the product is enabled for the subscription. | ||
| */ | ||
| isEnabled: boolean; | ||
| /** | ||
| * The longest available product trial for the given product, if any. | ||
| * Always null when excludeProductTrials is true. | ||
| */ | ||
| potentialProductTrial: ProductTrial | null; | ||
| /** | ||
| * Whether the usage for the given product has exceeded the limit. | ||
| */ | ||
| usageExceeded: boolean; | ||
| /** | ||
| * The add-on information for the given product from the subscription, | ||
| * if any. | ||
| */ | ||
| addOnInfo?: AddOn; | ||
| } | ||
|
|
||
| const EMPTY_PRODUCT_BILLING_METADATA: ProductBillingMetadata = { | ||
| billedCategory: null, | ||
| displayName: '', | ||
| isAddOn: false, | ||
| isEnabled: false, | ||
| usageExceeded: false, | ||
| activeProductTrial: null, | ||
| potentialProductTrial: null, | ||
| }; | ||
|
|
||
| export function useProductBillingMetadata( | ||
| subscription: Subscription, | ||
| product: DataCategory | AddOnCategory, | ||
| parentProduct?: DataCategory | AddOnCategory, | ||
| excludeProductTrials?: boolean | ||
| ): ProductBillingMetadata { | ||
| const isAddOn = checkIsAddOn(parentProduct ?? product); | ||
| const billedCategory = getBilledCategory(subscription, product); | ||
|
|
||
| if (!billedCategory) { | ||
| return EMPTY_PRODUCT_BILLING_METADATA; | ||
| } | ||
|
|
||
| let displayName = ''; | ||
| let addOnInfo = undefined; | ||
|
|
||
| if (isAddOn) { | ||
| const category = (parentProduct ?? product) as AddOnCategory; | ||
| addOnInfo = subscription.addOns?.[category]; | ||
| if (!addOnInfo) { | ||
| return EMPTY_PRODUCT_BILLING_METADATA; | ||
| } | ||
| displayName = parentProduct | ||
| ? getPlanCategoryName({ | ||
| plan: subscription.planDetails, | ||
| category: billedCategory, | ||
| title: true, | ||
| }) | ||
| : toTitleCase(addOnInfo.productName, {allowInnerUpperCase: true}); | ||
| } else { | ||
| displayName = getPlanCategoryName({ | ||
| plan: subscription.planDetails, | ||
| category: billedCategory, | ||
| title: true, | ||
| }); | ||
| } | ||
|
|
||
| return { | ||
| displayName, | ||
| billedCategory, | ||
| isAddOn, | ||
| isEnabled: productIsEnabled(subscription, parentProduct ?? product), | ||
| addOnInfo, | ||
| usageExceeded: subscription.categories[billedCategory]?.usageExceeded ?? false, | ||
| activeProductTrial: excludeProductTrials | ||
| ? null | ||
| : getActiveProductTrial(subscription.productTrials ?? null, billedCategory), | ||
| potentialProductTrial: excludeProductTrials | ||
| ? null | ||
| : getPotentialProductTrial(subscription.productTrials ?? null, billedCategory), | ||
| }; | ||
| } | ||
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
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.
Bug: Inconsistent parameter usage in billing metadata hook
The
getBilledCategorycall uses onlyproductwhileisAddOnusesparentProduct ?? product. WhenparentProductis an add-on category andproductis a data category,isAddOncorrectly evaluates totrue, butgetBilledCategoryreceives the data category instead of the add-on category. This causesgetBilledCategoryto return the data category unchanged rather than determining the correct billed category for the add-on, leading to incorrect billing metadata.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.
this is intentional as we render different things for add ons and their child categories, see #103983 for full usage