summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/monitoring/components/dashboards_dropdown.vue
blob: 1238996154d186f695c7228527902e5b58ccc709 (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
<script>
import {
  GlIcon,
  GlDropdown,
  GlDropdownItem,
  GlDropdownSectionHeader,
  GlDropdownDivider,
  GlSearchBoxByType,
  GlModalDirective,
} from '@gitlab/ui';
import { mapState, mapGetters } from 'vuex';

const events = {
  selectDashboard: 'selectDashboard',
};

export default {
  components: {
    GlIcon,
    GlDropdown,
    GlDropdownItem,
    GlDropdownSectionHeader,
    GlDropdownDivider,
    GlSearchBoxByType,
  },
  directives: {
    GlModal: GlModalDirective,
  },
  props: {
    defaultBranch: {
      type: String,
      required: true,
    },
  },
  data() {
    return {
      searchTerm: '',
    };
  },
  computed: {
    ...mapState('monitoringDashboard', ['allDashboards']),
    ...mapGetters('monitoringDashboard', ['selectedDashboard']),
    selectedDashboardText() {
      return this.selectedDashboard?.display_name;
    },
    selectedDashboardPath() {
      return this.selectedDashboard?.path;
    },

    filteredDashboards() {
      return this.allDashboards.filter(({ display_name = '' }) =>
        display_name.toLowerCase().includes(this.searchTerm.toLowerCase()),
      );
    },
    shouldShowNoMsgContainer() {
      return this.filteredDashboards.length === 0;
    },
    starredDashboards() {
      return this.filteredDashboards.filter(({ starred }) => starred);
    },
    nonStarredDashboards() {
      return this.filteredDashboards.filter(({ starred }) => !starred);
    },
  },
  methods: {
    dashboardDisplayName(dashboard) {
      return dashboard.display_name || dashboard.path || '';
    },
    selectDashboard(dashboard) {
      this.$emit(events.selectDashboard, dashboard);
    },
  },
};
</script>
<template>
  <gl-dropdown
    toggle-class="dropdown-menu-toggle"
    menu-class="monitor-dashboard-dropdown-menu"
    :text="selectedDashboardText"
  >
    <div class="d-flex flex-column overflow-hidden">
      <gl-dropdown-section-header>{{ __('Dashboard') }}</gl-dropdown-section-header>
      <gl-search-box-by-type ref="monitorDashboardsDropdownSearch" v-model="searchTerm" />

      <div class="flex-fill overflow-auto">
        <gl-dropdown-item
          v-for="dashboard in starredDashboards"
          :key="dashboard.path"
          :is-check-item="true"
          :is-checked="dashboard.path === selectedDashboardPath"
          @click="selectDashboard(dashboard)"
        >
          <div class="gl-display-flex">
            <span class="gl-flex-grow-1 gl-min-w-0 gl-overflow-hidden gl-overflow-wrap-break">
              {{ dashboardDisplayName(dashboard) }}
            </span>
            <gl-icon class="text-muted gl-flex-shrink-0 gl-ml-3 gl-align-self-center" name="star" />
          </div>
        </gl-dropdown-item>
        <gl-dropdown-divider
          v-if="starredDashboards.length && nonStarredDashboards.length"
          ref="starredListDivider"
        />

        <gl-dropdown-item
          v-for="dashboard in nonStarredDashboards"
          :key="dashboard.path"
          :is-check-item="true"
          :is-checked="dashboard.path === selectedDashboardPath"
          @click="selectDashboard(dashboard)"
        >
          <span class="gl-overflow-hidden gl-overflow-wrap-break">
            {{ dashboardDisplayName(dashboard) }}
          </span>
        </gl-dropdown-item>
      </div>

      <div
        v-show="shouldShowNoMsgContainer"
        ref="monitorDashboardsDropdownMsg"
        class="text-secondary no-matches-message"
      >
        {{ __('No matching results') }}
      </div>
    </div>
  </gl-dropdown>
</template>