summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/frequent_items/components/app.vue
blob: a4e883c96b59e316de937664e252bc00cd078dd1 (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
173
<script>
import { GlLoadingIcon, GlButton, GlIcon, GlTooltipDirective } from '@gitlab/ui';
import AccessorUtilities from '~/lib/utils/accessor';
import {
  mapVuexModuleState,
  mapVuexModuleActions,
  mapVuexModuleGetters,
} from '~/lib/utils/vuex_module_mappers';
import { FREQUENT_ITEMS, STORAGE_KEY } from '../constants';
import eventHub from '../event_hub';
import { isMobile, updateExistingFrequentItem, sanitizeItem } from '../utils';
import FrequentItemsList from './frequent_items_list.vue';
import frequentItemsMixin from './frequent_items_mixin';
import FrequentItemsSearchInput from './frequent_items_search_input.vue';

export default {
  components: {
    FrequentItemsSearchInput,
    FrequentItemsList,
    GlLoadingIcon,
    GlButton,
    GlIcon,
  },
  directives: {
    GlTooltip: GlTooltipDirective,
  },
  mixins: [frequentItemsMixin],
  inject: ['vuexModule'],
  props: {
    currentUserName: {
      type: String,
      required: true,
    },
    currentItem: {
      type: Object,
      required: true,
    },
    searchClass: {
      type: String,
      required: false,
      default: '',
    },
  },
  computed: {
    ...mapVuexModuleState((vm) => vm.vuexModule, [
      'searchQuery',
      'isLoadingItems',
      'isItemsListEditable',
      'isFetchFailed',
      'isItemRemovalFailed',
      'items',
    ]),
    ...mapVuexModuleGetters((vm) => vm.vuexModule, ['hasSearchQuery']),
    translations() {
      return this.getTranslations(['loadingMessage', 'header', 'headerEditToggle']);
    },
  },
  created() {
    const { namespace, currentUserName, currentItem } = this;
    const storageKey = `${currentUserName}/${STORAGE_KEY[namespace]}`;

    this.setNamespace(namespace);
    this.setStorageKey(storageKey);

    if (currentItem.id) {
      this.logItemAccess(storageKey, currentItem);
    }

    eventHub.$on(`${this.namespace}-dropdownOpen`, this.dropdownOpenHandler);

    // As we init it through requestIdleCallback it could be that the dropdown is already open
    const namespaceDropdown = document.getElementById(`nav-${this.namespace}-dropdown`);
    if (namespaceDropdown && namespaceDropdown.classList.contains('show')) {
      this.dropdownOpenHandler();
    }
  },
  beforeDestroy() {
    eventHub.$off(`${this.namespace}-dropdownOpen`, this.dropdownOpenHandler);
  },
  methods: {
    ...mapVuexModuleActions((vm) => vm.vuexModule, [
      'setNamespace',
      'setStorageKey',
      'toggleItemsListEditablity',
      'fetchFrequentItems',
    ]),
    dropdownOpenHandler() {
      if (this.searchQuery === '' || isMobile()) {
        this.fetchFrequentItems();
      }
    },
    logItemAccess(storageKey, unsanitizedItem) {
      const item = sanitizeItem(unsanitizedItem);

      if (!AccessorUtilities.canUseLocalStorage()) {
        return false;
      }

      // Check if there's any frequent items list set
      const storedRawItems = localStorage.getItem(storageKey);
      const storedFrequentItems = storedRawItems
        ? JSON.parse(storedRawItems)
        : [{ ...item, frequency: 1 }]; // No frequent items list set, set one up.

      // Check if item already exists in list
      const itemMatchIndex = storedFrequentItems.findIndex(
        (frequentItem) => frequentItem.id === item.id,
      );

      if (itemMatchIndex > -1) {
        storedFrequentItems[itemMatchIndex] = updateExistingFrequentItem(
          storedFrequentItems[itemMatchIndex],
          item,
        );
      } else {
        if (storedFrequentItems.length === FREQUENT_ITEMS.MAX_COUNT) {
          storedFrequentItems.shift();
        }

        storedFrequentItems.push({ ...item, frequency: 1 });
      }

      return localStorage.setItem(storageKey, JSON.stringify(storedFrequentItems));
    },
  },
};
</script>

<template>
  <div class="gl-display-flex gl-flex-direction-column gl-flex-align-items-stretch gl-h-full">
    <frequent-items-search-input
      :namespace="namespace"
      :class="searchClass"
      data-testid="frequent-items-search-input"
    />
    <gl-loading-icon
      v-if="isLoadingItems"
      :label="translations.loadingMessage"
      size="lg"
      class="loading-animation prepend-top-20"
      data-testid="loading"
    />
    <div
      v-if="!isLoadingItems && !hasSearchQuery"
      class="section-header gl-display-flex"
      data-testid="header"
    >
      <span class="gl-flex-grow-1">{{ translations.header }}</span>
      <gl-button
        v-if="items.length"
        v-gl-tooltip.left
        size="small"
        category="tertiary"
        :aria-label="translations.headerEditToggle"
        :title="translations.headerEditToggle"
        :class="{ 'gl-bg-gray-100!': isItemsListEditable }"
        class="gl-p-2!"
        @click="toggleItemsListEditablity"
      >
        <gl-icon name="pencil" :class="{ 'gl-text-gray-900!': isItemsListEditable }" />
      </gl-button>
    </div>
    <frequent-items-list
      v-if="!isLoadingItems"
      :items="items"
      :namespace="namespace"
      :has-search-query="hasSearchQuery"
      :is-fetch-failed="isFetchFailed"
      :is-item-removal-failed="isItemRemovalFailed"
      :matcher="searchQuery"
    />
  </div>
</template>