Files
threshold-ui/addons/core/addon/helpers/time-remaining.js
T
hashicorp-copywrite[bot] b4a83a46d1 [COMPLIANCE] Add Copyright and License Headers (#2027)
Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com>
2023-12-04 09:13:32 -08:00

43 lines
1.1 KiB
JavaScript

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
import Helper from '@ember/component/helper';
import { inject as service } from '@ember/service';
import DurationUnitFormat from 'intl-unofficial-duration-unit-format';
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
const duration = new DurationUnitFormat(this.intl.primaryLocale, {
style: 'timer',
format: `{hours}:{minutes}:{seconds} ${this.intl.t(
'resources.session.remaining',
)}`,
});
// Return the formatted duration
return duration.format(differenceInSeconds);
}
}