2023-03-15 16:00:52 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) HashiCorp, Inc.
|
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
|
|
|
*/
|
|
|
|
|
2023-02-14 17:00:24 +00:00
|
|
|
import Component from '@glimmer/component';
|
|
|
|
import { action } from '@ember/object';
|
|
|
|
import { tracked } from '@glimmer/tracking';
|
|
|
|
import { inject as service } from '@ember/service';
|
|
|
|
import isAfter from 'date-fns/isAfter';
|
|
|
|
import differenceInDays from 'date-fns/differenceInDays';
|
|
|
|
import localStorage from 'vault/lib/local-storage';
|
2023-03-22 18:19:11 +00:00
|
|
|
import timestamp from 'core/utils/timestamp';
|
2023-02-14 17:00:24 +00:00
|
|
|
|
2021-06-03 20:30:26 +00:00
|
|
|
/**
|
|
|
|
* @module LicenseBanners
|
|
|
|
* LicenseBanners components are used to display Vault-specific license expiry messages
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* ```js
|
|
|
|
* <LicenseBanners @expiry={expiryDate} />
|
|
|
|
* ```
|
|
|
|
* @param {string} expiry - RFC3339 date timestamp
|
|
|
|
*/
|
|
|
|
|
|
|
|
export default class LicenseBanners extends Component {
|
2023-02-14 17:00:24 +00:00
|
|
|
@service version;
|
|
|
|
|
|
|
|
@tracked warningDismissed;
|
|
|
|
@tracked expiredDismissed;
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super(...arguments);
|
2023-06-09 18:03:58 +00:00
|
|
|
// reset and show a previously dismissed license banner if:
|
|
|
|
// the version has been updated or the license has been updated (indicated by a change in the expiry date).
|
|
|
|
const bannerType = localStorage.getItem(this.dismissedBannerKey); // returns either warning or expired
|
|
|
|
|
|
|
|
this.updateDismissType(bannerType);
|
2023-02-14 17:00:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get currentVersion() {
|
|
|
|
return this.version.version;
|
|
|
|
}
|
|
|
|
|
2023-06-09 18:03:58 +00:00
|
|
|
get dismissedBannerKey() {
|
|
|
|
return `dismiss-license-banner-${this.currentVersion}-${this.args.expiry}`;
|
|
|
|
}
|
|
|
|
|
2021-06-03 20:30:26 +00:00
|
|
|
get licenseExpired() {
|
|
|
|
if (!this.args.expiry) return false;
|
2023-03-22 18:19:11 +00:00
|
|
|
return isAfter(timestamp.now(), new Date(this.args.expiry));
|
2021-06-03 20:30:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get licenseExpiringInDays() {
|
|
|
|
// Anything more than 30 does not render a warning
|
|
|
|
if (!this.args.expiry) return 99;
|
2023-03-22 18:19:11 +00:00
|
|
|
return differenceInDays(new Date(this.args.expiry), timestamp.now());
|
2021-06-03 20:30:26 +00:00
|
|
|
}
|
2023-02-14 17:00:24 +00:00
|
|
|
|
|
|
|
@action
|
|
|
|
dismissBanner(dismissAction) {
|
|
|
|
// if a client's version changed their old localStorage key will still exists.
|
2023-06-09 18:03:58 +00:00
|
|
|
localStorage.cleanupStorage('dismiss-license-banner', this.dismissedBannerKey);
|
2023-02-14 17:00:24 +00:00
|
|
|
// updates localStorage and then updates the template by calling updateDismissType
|
2023-06-09 18:03:58 +00:00
|
|
|
localStorage.setItem(this.dismissedBannerKey, dismissAction);
|
2023-02-14 17:00:24 +00:00
|
|
|
this.updateDismissType(dismissAction);
|
|
|
|
}
|
|
|
|
|
|
|
|
updateDismissType(dismissType) {
|
|
|
|
// updates tracked properties to update template
|
|
|
|
if (dismissType === 'warning') {
|
|
|
|
this.warningDismissed = true;
|
|
|
|
} else if (dismissType === 'expired') {
|
|
|
|
this.expiredDismissed = true;
|
|
|
|
}
|
|
|
|
}
|
2021-06-03 20:30:26 +00:00
|
|
|
}
|