summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/ref/components/ref_results_section.vue
blob: 4fa2a92ff03eda59ee0d48c626f94eff7b0c78e4 (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
<script>
import { GlDropdownSectionHeader, GlDropdownItem, GlBadge, GlIcon } from '@gitlab/ui';
import { s__ } from '~/locale';

export default {
  name: 'RefResultsSection',
  components: {
    GlDropdownSectionHeader,
    GlDropdownItem,
    GlBadge,
    GlIcon,
  },
  props: {
    showHeader: {
      type: Boolean,
      required: false,
      default: true,
    },

    sectionTitle: {
      type: String,
      required: true,
    },

    totalCount: {
      type: Number,
      required: true,
    },

    /**
     * An array of object that have the following properties:
     *
     * - name (String, required): The name of the ref that will be displayed
     * - value (String, optional): The value that will be selected when the ref
     *   is selected. If not provided, `name` will be used as the value.
     *   For example, commits use the short SHA for `name`
     *   and long SHA for `value`.
     * - subtitle (String, optional): Text to render underneath the name.
     *   For example, used to render the commit's title underneath its SHA.
     * - default (Boolean, optional): Whether or not to render a "default"
     *   indicator next to the item. Used to indicate
     *   the project's default branch.
     *
     */
    items: {
      type: Array,
      required: true,
      validator: (items) => Array.isArray(items) && items.every((item) => item.name),
    },

    /**
     * The currently selected ref.
     * Used to render a check mark by the selected item.
     * */
    selectedRef: {
      type: String,
      required: false,
      default: '',
    },

    /**
     * An error object that indicates that an error
     * occurred while fetching items for this section
     */
    error: {
      type: Error,
      required: false,
      default: null,
    },

    /** The message to display if an error occurs */
    errorMessage: {
      type: String,
      required: false,
      default: '',
    },
  },
  computed: {
    totalCountText() {
      return this.totalCount > 999 ? s__('TotalRefCountIndicator|1000+') : `${this.totalCount}`;
    },
  },
  methods: {
    showCheck(item) {
      return item.name === this.selectedRef || item.value === this.selectedRef;
    },
  },
};
</script>

<template>
  <div>
    <gl-dropdown-section-header v-if="showHeader">
      <div class="gl-display-flex align-items-center" data-testid="section-header">
        <span class="gl-mr-2 gl-mb-1">{{ sectionTitle }}</span>
        <gl-badge variant="neutral">{{ totalCountText }}</gl-badge>
      </div>
    </gl-dropdown-section-header>
    <template v-if="error">
      <div class="gl-display-flex align-items-start text-danger gl-ml-4 gl-mr-4 gl-mb-3">
        <gl-icon name="error" class="gl-mr-2 gl-mt-2 gl-flex-shrink-0" />
        <span>{{ errorMessage }}</span>
      </div>
    </template>
    <template v-else>
      <gl-dropdown-item
        v-for="item in items"
        :key="item.name"
        @click="$emit('selected', item.value || item.name)"
      >
        <div class="gl-display-flex align-items-start">
          <gl-icon
            name="mobile-issue-close"
            class="gl-mr-2 gl-flex-shrink-0"
            :class="{ 'gl-visibility-hidden': !showCheck(item) }"
          />

          <div class="gl-flex-grow-1 gl-display-flex gl-flex-direction-column">
            <span class="gl-font-monospace">{{ item.name }}</span>
            <span class="gl-text-gray-400">{{ item.subtitle }}</span>
          </div>

          <gl-badge v-if="item.default" size="sm" variant="info">{{
            s__('DefaultBranchLabel|default')
          }}</gl-badge>
        </div>
      </gl-dropdown-item>
    </template>
  </div>
</template>