Skip to content

Commit a5fb4da

Browse files
fix(misc): Fix getExactDuration behaviour when given a precision parameter (#103958)
This PR fixes a bug visible on some [alert pages](https://sentry.sentry.io/issues/6827154726/open-periods/?project=4507946704961536&referrer=event-or-group-header). <img width="196" height="95" alt="image" src="https://github.com/user-attachments/assets/da0907d2-14fe-4374-ad93-a411e408629f" /> The predicate in the ternary fails to return true when it's supposed to because it's matching the plural and singular version of a word. e.g. in the above case, it doesn't match `minutes` and `minute` so it recurses and appends a "0 minutes". Instead of comparing `minSuffix` and `suffix` we now just compare `precision` (the minimum unit) and the unit of the block.
1 parent 74b88cb commit a5fb4da

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

static/app/utils/duration/getExactDuration.spec.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ describe('getExactDuration', () => {
2929

3030
it('should abbreviate label', () => {
3131
expect(getExactDuration(234235435, true)).toBe('387wk 2d 1hr 23min 55s');
32+
expect(getExactDuration(61, true, 'minutes')).toBe('1min');
3233
});
3334

3435
it('should pin/truncate to the min suffix precision if provided', () => {
@@ -41,5 +42,6 @@ describe('getExactDuration', () => {
4142
expect(getExactDuration(234235435.2, false, 'seconds')).toBe(
4243
'387 weeks 2 days 1 hour 23 minutes 55 seconds'
4344
);
45+
expect(getExactDuration(61, false, 'minutes')).toBe('1 minute');
4446
});
4547
});

static/app/utils/duration/getExactDuration.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,31 +43,31 @@ export function getExactDuration(
4343
const {quotient, remainder} = divideBy(WEEK);
4444
const suffix = abbr ? t('wk') : ` ${tn('week', 'weeks', quotient)}`;
4545

46-
return `${quotient}${suffix} ${minSuffix === suffix ? '' : convertDuration(remainder / 1000, abbr)}`;
46+
return `${quotient}${suffix} ${precision === 'weeks' ? '' : convertDuration(remainder / 1000, abbr)}`;
4747
}
4848
if (value >= DAY || (value && minSuffix === ' days')) {
4949
const {quotient, remainder} = divideBy(DAY);
5050
const suffix = abbr ? t('d') : ` ${tn('day', 'days', quotient)}`;
5151

52-
return `${quotient}${suffix} ${minSuffix === suffix ? '' : convertDuration(remainder / 1000, abbr)}`;
52+
return `${quotient}${suffix} ${precision === 'days' ? '' : convertDuration(remainder / 1000, abbr)}`;
5353
}
5454
if (value >= HOUR || (value && minSuffix === ' hours')) {
5555
const {quotient, remainder} = divideBy(HOUR);
5656
const suffix = abbr ? t('hr') : ` ${tn('hour', 'hours', quotient)}`;
5757

58-
return `${quotient}${suffix} ${minSuffix === suffix ? '' : convertDuration(remainder / 1000, abbr)}`;
58+
return `${quotient}${suffix} ${precision === 'hours' ? '' : convertDuration(remainder / 1000, abbr)}`;
5959
}
6060
if (value >= MINUTE || (value && minSuffix === ' minutes')) {
6161
const {quotient, remainder} = divideBy(MINUTE);
6262
const suffix = abbr ? t('min') : ` ${tn('minute', 'minutes', quotient)}`;
6363

64-
return `${quotient}${suffix} ${minSuffix === suffix ? '' : convertDuration(remainder / 1000, abbr)}`;
64+
return `${quotient}${suffix} ${precision === 'minutes' ? '' : convertDuration(remainder / 1000, abbr)}`;
6565
}
6666
if (value >= SECOND || (value && minSuffix === ' seconds')) {
6767
const {quotient, remainder} = divideBy(SECOND);
6868
const suffix = abbr ? t('s') : ` ${tn('second', 'seconds', quotient)}`;
6969

70-
return `${quotient}${suffix} ${minSuffix === suffix ? '' : convertDuration(remainder / 1000, abbr)}`;
70+
return `${quotient}${suffix} ${precision === 'seconds' ? '' : convertDuration(remainder / 1000, abbr)}`;
7171
}
7272

7373
if (value === 0) {

0 commit comments

Comments
 (0)