Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export function ProductTrialAlert(props: ProductTrialAlertProps) {
{t('Learn More')}
</LinkButton>
);
} else if (daysLeft > 0 && daysLeft <= 7 && trial.isStarted) {
} else if (daysLeft >= 0 && daysLeft <= 7 && trial.isStarted) {
alertHeader = t('%s Trial', titleCase(getProductName(trial.category)));
if (isPaid && categoryUsesOnDemand) {
alertText = t(
Expand Down
8 changes: 2 additions & 6 deletions static/gsApp/components/productTrial/productTrialTag.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import moment from 'moment-timezone';

import {Tag, type TagProps} from '@sentry/scraps/badge';

import {IconBusiness} from 'sentry/icons';
Expand All @@ -21,9 +19,9 @@ export function ProductTrialTag({
variant,
showTrialEnded = false,
}: ProductTrialTagProps) {
const now = moment();
const daysLeft = -1 * getDaysSinceDate(trial.endDate ?? '');

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: When trial.endDate is null or undefined, the component incorrectly calculates the days remaining as NaN and renders "NaN days left" to the user.
Severity: MEDIUM

Suggested Fix

Add a guard clause at the beginning of the component to handle cases where trial.endDate is null or undefined. If endDate is not present, return null early to prevent the getDaysSinceDate calculation from executing with invalid input and producing NaN.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent. Verify if this is a real issue. If it is, propose a fix; if not, explain why it's
not valid.

Location: static/gsApp/components/productTrial/productTrialTag.tsx#L22

Potential issue: The `ProductTrial` type definition allows the `endDate` field to be
optional. When `trial.endDate` is `null` or `undefined`, the code passes an empty string
to `getDaysSinceDate` due to the `?? ''` nullish coalescing operator. The
`getDaysSinceDate` function, when called with an empty string, uses `moment('')` which
results in an invalid date, causing the function to return `NaN`. Consequently, the
`daysLeft` variable becomes `NaN`. All subsequent conditional checks on `daysLeft`
(e.g., `daysLeft < 0`) evaluate to false, leading the component to render the
user-facing text "NaN days left". This bug is introduced by the pull request's
refactoring, which moves the calculation before the necessary safety checks.

Also affects:

  • static/app/utils/getDaysSinceDate.tsx
  • static/gsApp/types/index.tsx:268
  • static/gsApp/components/productTrial/productTrialAlert.tsx:57

Did we get this right? 👍 / 👎 to inform future reviews.


if (moment(trial.endDate).add(1, 'days').isBefore(now)) {
if (daysLeft < 0) {
if (!showTrialEnded) {
return null;
}
Expand All @@ -43,8 +41,6 @@ export function ProductTrialTag({
);
}

const daysLeft = -1 * getDaysSinceDate(trial.endDate ?? '');

return (
<Tag icon={<IconClock />} variant={variant ?? (daysLeft <= 7 ? 'warning' : 'info')}>
{t('%d days left', daysLeft)}
Expand Down
Loading