2023-12-04 09:13:32 -08:00
|
|
|
/**
|
|
|
|
|
* Copyright (c) HashiCorp, Inc.
|
2024-05-31 11:29:24 -07:00
|
|
|
* SPDX-License-Identifier: BUSL-1.1
|
2023-12-04 09:13:32 -08:00
|
|
|
*/
|
|
|
|
|
|
2023-11-28 17:42:22 -05:00
|
|
|
import Helper from '@ember/component/helper';
|
|
|
|
|
import { inject as service } from '@ember/service';
|
2024-09-13 09:43:08 -05:00
|
|
|
import { DurationFormat } from '@formatjs/intl-durationformat';
|
|
|
|
|
import { secondsToDuration } from 'core/utils/seconds-to-duration';
|
2023-11-28 17:42:22 -05:00
|
|
|
|
|
|
|
|
export default class extends Helper {
|
|
|
|
|
// =services
|
|
|
|
|
@service clockTick;
|
|
|
|
|
@service intl;
|
|
|
|
|
|
|
|
|
|
compute([expiration_time]) {
|
|
|
|
|
if (!expiration_time) return;
|
|
|
|
|
|
|
|
|
|
// Parse the expiration time
|
|
|
|
|
const expirationTime = new Date(expiration_time);
|
|
|
|
|
|
|
|
|
|
// Get the curent time
|
|
|
|
|
const currentTime = this.clockTick.now;
|
|
|
|
|
|
|
|
|
|
// Calculate the difference in milleseconds
|
|
|
|
|
const difference = expirationTime - currentTime;
|
|
|
|
|
|
|
|
|
|
// Calculate the difference in seconds if the difference is positive
|
|
|
|
|
const differenceInSeconds =
|
|
|
|
|
difference > 0 ? Math.floor(difference / 1000) : 0;
|
|
|
|
|
|
|
|
|
|
// Format the duration
|
2024-09-13 09:43:08 -05:00
|
|
|
const duration = new DurationFormat(this.intl.primaryLocale, {
|
|
|
|
|
style: 'digital',
|
2023-11-28 17:42:22 -05:00
|
|
|
});
|
2024-09-13 09:43:08 -05:00
|
|
|
const formattedTime = duration.format(
|
|
|
|
|
secondsToDuration(differenceInSeconds),
|
|
|
|
|
);
|
2023-11-28 17:42:22 -05:00
|
|
|
|
2024-09-13 09:43:08 -05:00
|
|
|
return `${formattedTime} ${this.intl.t('resources.session.remaining')}`;
|
2023-11-28 17:42:22 -05:00
|
|
|
}
|
|
|
|
|
}
|