vault/ui/app/components/secret-link.js
Chelsea Shaw cc90151c98
UI: add backend to secret link (#26683)
* move secret-link template to be collocated with backing class

* Update secret-link to gracefully handle adding backend to link models

* Update ToolbarSecretLink to pass backend to SecretLink

* Pass backend arg to SecretLink in one-off locations:
- transit-form-show
- secret-list/item
- transform-transformation-item
- transit-actions-layout

* fix database tests and usage of ToolbarSecretLink

* update jsdoc

* update required in jsdocs
2024-04-29 13:38:25 -05:00

58 lines
1.7 KiB
JavaScript

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: BUSL-1.1
*/
import Component from '@glimmer/component';
import { action } from '@ember/object';
import { encodePath } from 'vault/utils/path-encoding-helpers';
import { debug } from '@ember/debug';
/**
* @module SecretLink
*
* @example
* ```js
* <SecretLink
* @mode="edit"
* @backend="backend-path"
* @secret="my/secret path"
* @queryParams={{hash tab="version"}}
* @disabled={{true}}
* />
*
* @param {string} mode - *required* controls the route link. added to the base route vault.cluster.secrets.backend
* @param {string} backend - *required* backend path. Is encoded in the component
* @param {string} secret - secret path. Is encoded in the component
* @param {object} queryParams - params passed to the link
* @param {boolean} disabled - passed to LinkTo to disable link
* @param {CallableFunction} onLinkClick - side effect when link is clicked
*/
export default class SecretLink extends Component {
get link() {
const { mode, secret, backend } = this.args;
if (!backend) {
debug(`Arg "backend" missing from secret-link with mode: ${mode} secret: ${secret}`);
}
const route = `vault.cluster.secrets.backend.${mode}`;
const models = backend ? [encodePath(backend)] : [];
if ((mode !== 'versions' && !secret) || secret === ' ') {
return { route: `${route}-root`, models };
} else {
models.push(encodePath(secret));
return { route, models };
}
}
get query() {
const qp = this.args.queryParams || {};
return qp.isQueryParams ? qp.values : qp;
}
@action
onLinkClick() {
if (this.args.onLinkClick) {
this.args.onLinkClick(...arguments);
}
}
}