vault/ui/app/components/secret-engine/ttl-picker-v2.ts
Vault Automation 91eabbd0db
[Feature][UI]: General Settings Follow Up Items (#8965) (#9262)
* UI: VAULT-39172 VAULT-38567 general settings followup (#8910)

* Add unsaved changes fields

* Set up default values for TTL and update general-settings

* Add form error state

* Ass TODO cmment

* Move actions back!

* Update unsaved changes state

* Address comments and add TODOs

* UI: VAULT-39264 Lease Duration TTL picker (#9080)

* Update default and max ttl to show correct default

* Query sys/internal endpoint for ttl values

* WIP ttl-picker-v2

* Intialize values and check for if ttl value is unset

* Use ttlKey instead of name

* Set name to be ttlKey

* Show validation for ttl picker

* Fix validation bugs

* Remove lease duration files

* Add copyright headers

* Initalize only when its a custom value

* Update ttl-picker to not have a dropdown

* Validate field before converting to secs

* [UI] Fix styling and update version card component (#9214)

* Fix styling and update version card component

* Update unsaved changes

* Code cleanup

* More code cleanup!

* Add helper function

* Remove query for lease duration

* Fix outstanding issues

* Captialize unsaved changes

* Update util name

* Remove action helper

* [UI]: General Settings design feedback updates (#9257)

* Small refactor based on design feedback

* More refactoring!

* Rename variables so it makes more sense!

* Remove unused modal fields

Co-authored-by: Kianna <30884335+kiannaquach@users.noreply.github.com>
2025-09-10 15:27:45 -07:00

114 lines
3.3 KiB
TypeScript

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: BUSL-1.1
*/
import Component from '@glimmer/component';
import { action } from '@ember/object';
import { service } from '@ember/service';
import { tracked } from '@glimmer/tracking';
import { convertFromSeconds, durationToSeconds, largestUnitFromSeconds } from 'core/utils/duration-utils';
import { CUSTOM, SYSTEM_DEFAULT } from './page/general-settings';
import type FlashMessageService from 'vault/services/flash-messages';
import type SecretsEngineResource from 'vault/resources/secrets/engine';
import type ApiService from 'vault/services/api';
import type RouterService from '@ember/routing/router-service';
import type { HTMLElementEvent } from 'vault/forms';
/**
* @module TtlPickerV2 handles the display of the ttl picker fo the lease duration card in general settings.
*
* @example
* <SecretEngine::TtlPickerV2
@model={{this.model}}
@isDefaultTtlPicker={{boolean}}
/>
*
* @param {object} model - A model contains a secret engine resource, lease config from the sys/internal endpoint.
* @param {boolean} isDefaultTtlPicker - isDefaultTtlPicker is a boolean that determines if the picker is default or max ttl.
*/
interface Args {
model: {
secretsEngine: SecretsEngineResource;
};
ttlKey: 'default_lease_ttl' | 'max_lease_ttl';
}
export default class TtlPickerV2 extends Component<Args> {
systemDefaultTtl = 0;
systemDefault = SYSTEM_DEFAULT;
custom = CUSTOM;
@service declare readonly flashMessages: FlashMessageService;
@service declare readonly api: ApiService;
@service declare readonly router: RouterService;
@tracked selectedUnit = 's';
@tracked time = '';
@tracked errorMessage = '';
constructor(owner: unknown, args: Args) {
super(owner, args);
this.initializeTtl();
}
initializeTtl() {
const ttlValue = this.args?.model?.secretsEngine?.config[this.args.ttlKey];
let seconds = 0;
if (typeof ttlValue === 'number') {
// if the passed value is a number, assume unit is seconds
seconds = ttlValue;
} else {
const parseDuration = durationToSeconds(ttlValue || '');
// if parsing fails leave it empty
if (parseDuration === null) return;
seconds = parseDuration;
}
const unit = largestUnitFromSeconds(seconds);
const time = convertFromSeconds(seconds, unit);
this.time = time.toString() || '';
this.selectedUnit = unit;
}
get unitOptions() {
return [
{ label: 'seconds', value: 's' },
{ label: 'minutes', value: 'm' },
{ label: 'hours', value: 'h' },
{ label: 'days', value: 'd' },
];
}
get formField() {
return {
label: this.args?.ttlKey === 'default_lease_ttl' ? 'Time-to-live (TTL)' : 'Maximum Time-to-live (TTL)',
helperText:
this.args?.ttlKey === 'default_lease_ttl'
? 'Standard expiry deadline.'
: 'Maximum possible extension for expiry.',
};
}
@action
setTtlTime(event: HTMLElementEvent<HTMLInputElement>) {
this.errorMessage = '';
if (isNaN(Number(event.target.value))) {
this.errorMessage = 'Only use numbers for this setting.';
return;
}
this.time = event.target.value;
this.args.model.secretsEngine.config[this.args.ttlKey] = event.target.value;
}
@action
setUnit(event: HTMLElementEvent<HTMLSelectElement>) {
this.selectedUnit = event.target.value;
}
}