summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/runner/components/runner_filtered_search_bar.vue
blob: e14b3b17fa89b2228c1c509f0ed86b2f66272271 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<script>
import { cloneDeep } from 'lodash';
import { formatNumber, sprintf, __, s__ } from '~/locale';
import { OPERATOR_IS_ONLY } from '~/vue_shared/components/filtered_search_bar/constants';
import FilteredSearch from '~/vue_shared/components/filtered_search_bar/filtered_search_bar_root.vue';
import BaseToken from '~/vue_shared/components/filtered_search_bar/tokens/base_token.vue';
import {
  STATUS_ACTIVE,
  STATUS_PAUSED,
  STATUS_ONLINE,
  STATUS_OFFLINE,
  STATUS_NOT_CONNECTED,
  INSTANCE_TYPE,
  GROUP_TYPE,
  PROJECT_TYPE,
  CREATED_DESC,
  CREATED_ASC,
  CONTACTED_DESC,
  CONTACTED_ASC,
  PARAM_KEY_STATUS,
  PARAM_KEY_RUNNER_TYPE,
  PARAM_KEY_TAG,
} from '../constants';
import TagToken from './search_tokens/tag_token.vue';

const sortOptions = [
  {
    id: 1,
    title: __('Created date'),
    sortDirection: {
      descending: CREATED_DESC,
      ascending: CREATED_ASC,
    },
  },
  {
    id: 2,
    title: __('Last contact'),
    sortDirection: {
      descending: CONTACTED_DESC,
      ascending: CONTACTED_ASC,
    },
  },
];

export default {
  components: {
    FilteredSearch,
  },
  props: {
    value: {
      type: Object,
      required: true,
      validator(val) {
        return Array.isArray(val?.filters) && typeof val?.sort === 'string';
      },
    },
    namespace: {
      type: String,
      required: true,
    },
    activeRunnersCount: {
      type: Number,
      required: true,
    },
  },
  data() {
    // filtered_search_bar_root.vue may mutate the inital
    // filters. Use `cloneDeep` to prevent those mutations
    //  from affecting this component
    const { filters, sort } = cloneDeep(this.value);
    return {
      initialFilterValue: filters,
      initialSortBy: sort,
    };
  },
  computed: {
    searchTokens() {
      return [
        {
          icon: 'status',
          title: __('Status'),
          type: PARAM_KEY_STATUS,
          token: BaseToken,
          unique: true,
          options: [
            { value: STATUS_ACTIVE, title: s__('Runners|Active') },
            { value: STATUS_PAUSED, title: s__('Runners|Paused') },
            { value: STATUS_ONLINE, title: s__('Runners|Online') },
            { value: STATUS_OFFLINE, title: s__('Runners|Offline') },

            // Added extra quotes in this title to avoid splitting this value:
            // see: https://gitlab.com/gitlab-org/gitlab-ui/-/issues/1438
            { value: STATUS_NOT_CONNECTED, title: `"${s__('Runners|Not connected')}"` },
          ],
          // TODO In principle we could support more complex search rules,
          // this can be added to a separate issue.
          operators: OPERATOR_IS_ONLY,
        },

        {
          icon: 'file-tree',
          title: __('Type'),
          type: PARAM_KEY_RUNNER_TYPE,
          token: BaseToken,
          unique: true,
          options: [
            { value: INSTANCE_TYPE, title: s__('Runners|instance') },
            { value: GROUP_TYPE, title: s__('Runners|group') },
            { value: PROJECT_TYPE, title: s__('Runners|project') },
          ],
          // TODO We should support more complex search rules,
          // search for multiple states (OR) or have NOT operators
          operators: OPERATOR_IS_ONLY,
        },

        {
          icon: 'tag',
          title: s__('Runners|Tags'),
          type: PARAM_KEY_TAG,
          token: TagToken,
          recentTokenValuesStorageKey: `${this.namespace}-recent-tags`,
          operators: OPERATOR_IS_ONLY,
        },
      ];
    },
    activeRunnersMessage() {
      return sprintf(__('Runners currently online: %{active_runners_count}'), {
        active_runners_count: formatNumber(this.activeRunnersCount),
      });
    },
  },
  methods: {
    onFilter(filters) {
      const { sort } = this.value;

      this.$emit('input', {
        filters,
        sort,
        pagination: { page: 1 },
      });
    },
    onSort(sort) {
      const { filters } = this.value;

      this.$emit('input', {
        filters,
        sort,
        pagination: { page: 1 },
      });
    },
  },
  sortOptions,
};
</script>
<template>
  <div>
    <filtered-search
      v-bind="$attrs"
      :namespace="namespace"
      recent-searches-storage-key="runners-search"
      :sort-options="$options.sortOptions"
      :initial-filter-value="initialFilterValue"
      :initial-sort-by="initialSortBy"
      :tokens="searchTokens"
      :search-input-placeholder="__('Search or filter results...')"
      data-testid="runners-filtered-search"
      @onFilter="onFilter"
      @onSort="onSort"
    />
    <div class="gl-text-right" data-testid="active-runners-message">{{ activeRunnersMessage }}</div>
  </div>
</template>