|
| 1 | +/** |
| 2 | + * Copyright (c) HashiCorp, Inc. |
| 3 | + * SPDX-License-Identifier: BUSL-1.1 |
| 4 | + */ |
| 5 | + |
| 6 | +import Controller from '@ember/controller'; |
| 7 | +import { tracked } from '@glimmer/tracking'; |
| 8 | +import { action } from '@ember/object'; |
| 9 | +import { service } from '@ember/service'; |
| 10 | +import { STATUSES_APP_TOKEN } from 'api/models/app-token'; |
| 11 | + |
| 12 | +export default class ScopesScopeAppTokensIndexController extends Controller { |
| 13 | + // =services |
| 14 | + |
| 15 | + @service intl; |
| 16 | + |
| 17 | + // =attributes |
| 18 | + |
| 19 | + queryParams = [ |
| 20 | + 'search', |
| 21 | + 'page', |
| 22 | + 'pageSize', |
| 23 | + 'sortAttribute', |
| 24 | + 'sortDirection', |
| 25 | + { statuses: { type: 'array' } }, |
| 26 | + ]; |
| 27 | + |
| 28 | + @tracked search; |
| 29 | + @tracked page = 1; |
| 30 | + @tracked pageSize = 10; |
| 31 | + @tracked sortAttribute; |
| 32 | + @tracked sortDirection; |
| 33 | + @tracked statuses = []; |
| 34 | + |
| 35 | + /** |
| 36 | + * Status options for filtering |
| 37 | + */ |
| 38 | + get statusOptions() { |
| 39 | + return STATUSES_APP_TOKEN.map((status) => ({ |
| 40 | + id: status, |
| 41 | + name: this.intl.t(`resources.app-token.status.${status}`), |
| 42 | + })); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Returns status badge configuration for app tokens |
| 47 | + * @param {string} status |
| 48 | + * @returns {object} |
| 49 | + */ |
| 50 | + @action |
| 51 | + getStatusBadge(status) { |
| 52 | + const statusConfig = { |
| 53 | + active: { color: 'success' }, |
| 54 | + expired: { color: 'critical' }, |
| 55 | + revoked: { color: 'critical' }, |
| 56 | + stale: { color: 'critical' }, |
| 57 | + unknown: { color: 'neutral' }, |
| 58 | + }; |
| 59 | + |
| 60 | + const config = statusConfig[status] || { color: 'neutral' }; |
| 61 | + return { |
| 62 | + text: this.intl.t(`resources.app-token.status.${status}`), |
| 63 | + color: config.color, |
| 64 | + }; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Formats date for tooltip display |
| 69 | + * @param {Date} date |
| 70 | + * @returns {string} |
| 71 | + */ |
| 72 | + @action |
| 73 | + formatTooltipDate(date) { |
| 74 | + return date.toLocaleDateString('en-US', { |
| 75 | + year: 'numeric', |
| 76 | + month: 'short', |
| 77 | + day: 'numeric', |
| 78 | + hour: 'numeric', |
| 79 | + minute: '2-digit', |
| 80 | + hour12: true, |
| 81 | + }); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Returns object of filters to be used for displaying selected filters |
| 86 | + * @returns {object} |
| 87 | + */ |
| 88 | + get filters() { |
| 89 | + return { |
| 90 | + allFilters: { |
| 91 | + statuses: this.statusOptions, |
| 92 | + }, |
| 93 | + selectedFilters: { |
| 94 | + statuses: this.statuses, |
| 95 | + }, |
| 96 | + }; |
| 97 | + } |
| 98 | + |
| 99 | + // =actions |
| 100 | + |
| 101 | + /** |
| 102 | + * Handles input on each keystroke and the search queryParam |
| 103 | + * @param {object} event |
| 104 | + */ |
| 105 | + @action |
| 106 | + handleSearchInput(event) { |
| 107 | + const { value } = event.target; |
| 108 | + this.search = value; |
| 109 | + this.page = 1; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Sets the selected items for the given paramKey and sets the page to 1 |
| 114 | + * @param {string} paramKey |
| 115 | + * @param {[string]} selectedItems |
| 116 | + */ |
| 117 | + @action |
| 118 | + applyFilter(paramKey, selectedItems) { |
| 119 | + this[paramKey] = [...selectedItems]; |
| 120 | + this.page = 1; |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Sets sort values and sets page to 1 |
| 125 | + * @param {string} sortBy |
| 126 | + * @param {string} sortOrder |
| 127 | + */ |
| 128 | + @action |
| 129 | + onSort(sortBy, sortOrder) { |
| 130 | + this.sortAttribute = sortBy; |
| 131 | + this.sortDirection = sortOrder; |
| 132 | + this.page = 1; |
| 133 | + } |
| 134 | +} |
0 commit comments