summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/frequent_items/components/frequent_items_list_item.vue
blob: 056dedf87572395d3aef16be65c0a8e8089b93aa (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
<script>
import { GlButton, GlTooltipDirective, GlIcon } from '@gitlab/ui';
import SafeHtml from '~/vue_shared/directives/safe_html';
import highlight from '~/lib/utils/highlight';
import { truncateNamespace } from '~/lib/utils/text_utility';
import { mapVuexModuleState, mapVuexModuleActions } from '~/lib/utils/vuex_module_mappers';
import Tracking from '~/tracking';
import ProjectAvatar from '~/vue_shared/components/project_avatar.vue';

const trackingMixin = Tracking.mixin();

export default {
  components: {
    GlIcon,
    GlButton,
    ProjectAvatar,
  },
  directives: {
    SafeHtml,
    GlTooltip: GlTooltipDirective,
  },
  mixins: [trackingMixin],
  inject: ['vuexModule'],
  props: {
    matcher: {
      type: String,
      required: false,
      default: '',
    },
    itemId: {
      type: Number,
      required: true,
    },
    itemName: {
      type: String,
      required: true,
    },
    namespace: {
      type: String,
      required: false,
      default: '',
    },
    webUrl: {
      type: String,
      required: true,
    },
    avatarUrl: {
      required: true,
      validator(value) {
        return value === null || typeof value === 'string';
      },
    },
  },
  computed: {
    ...mapVuexModuleState((vm) => vm.vuexModule, ['dropdownType', 'isItemsListEditable']),
    truncatedNamespace() {
      return truncateNamespace(this.namespace);
    },
    highlightedItemName() {
      return highlight(this.itemName, this.matcher);
    },
    itemTrackingLabel() {
      return `${this.dropdownType}_dropdown_frequent_items_list_item`;
    },
  },
  methods: {
    removeFrequentItemTracked(item) {
      this.track('click_button', {
        label: `${this.dropdownType}_dropdown_remove_frequent_item`,
        property: 'navigation_top',
      });
      this.removeFrequentItem(item);
    },
    ...mapVuexModuleActions((vm) => vm.vuexModule, ['removeFrequentItem']),
  },
};
</script>

<template>
  <li class="frequent-items-list-item-container gl-relative">
    <gl-button
      category="tertiary"
      :href="webUrl"
      class="gl-text-left gl-w-full"
      button-text-classes="gl-display-flex gl-w-full"
      data-testid="frequent-item-link"
      @click="track('click_link', { label: itemTrackingLabel, property: 'navigation_top' })"
    >
      <div class="gl-flex-grow-1">
        <project-avatar
          class="gl-float-left gl-mr-3"
          :project-avatar-url="avatarUrl"
          :project-id="itemId"
          :project-name="itemName"
          aria-hidden="true"
        />
        <div
          data-testid="frequent-items-item-metadata-container"
          class="frequent-items-item-metadata-container"
        >
          <div
            v-safe-html="highlightedItemName"
            data-testid="frequent-items-item-title"
            :title="itemName"
            class="frequent-items-item-title"
          ></div>
          <div
            v-if="namespace"
            data-testid="frequent-items-item-namespace"
            :title="namespace"
            class="frequent-items-item-namespace"
          >
            {{ truncatedNamespace }}
          </div>
        </div>
      </div>
    </gl-button>
    <gl-button
      v-if="isItemsListEditable"
      v-gl-tooltip.left
      size="small"
      category="tertiary"
      :aria-label="__('Remove')"
      :title="__('Remove')"
      class="gl-align-self-center gl-p-1! gl-absolute! gl-w-auto! gl-right-4 gl-top-half gl-translate-y-n50"
      data-testid="item-remove"
      @click.stop.prevent="removeFrequentItemTracked(itemId)"
    >
      <gl-icon name="close" />
    </gl-button>
  </li>
</template>