diff options
Diffstat (limited to 'app/assets/javascripts/search/store')
-rw-r--r-- | app/assets/javascripts/search/store/actions.js | 45 | ||||
-rw-r--r-- | app/assets/javascripts/search/store/constants.js | 7 | ||||
-rw-r--r-- | app/assets/javascripts/search/store/getters.js | 9 | ||||
-rw-r--r-- | app/assets/javascripts/search/store/index.js | 2 | ||||
-rw-r--r-- | app/assets/javascripts/search/store/mutation_types.js | 2 | ||||
-rw-r--r-- | app/assets/javascripts/search/store/mutations.js | 3 | ||||
-rw-r--r-- | app/assets/javascripts/search/store/state.js | 6 | ||||
-rw-r--r-- | app/assets/javascripts/search/store/utils.js | 80 |
8 files changed, 152 insertions, 2 deletions
diff --git a/app/assets/javascripts/search/store/actions.js b/app/assets/javascripts/search/store/actions.js index 0c3f273fec7..b53557c0ec5 100644 --- a/app/assets/javascripts/search/store/actions.js +++ b/app/assets/javascripts/search/store/actions.js @@ -2,11 +2,13 @@ import Api from '~/api'; import createFlash from '~/flash'; import { visitUrl, setUrlParams } from '~/lib/utils/url_utility'; import { __ } from '~/locale'; +import { GROUPS_LOCAL_STORAGE_KEY, PROJECTS_LOCAL_STORAGE_KEY } from './constants'; import * as types from './mutation_types'; +import { loadDataFromLS, setFrequentItemToLS, mergeById } from './utils'; export const fetchGroups = ({ commit }, search) => { commit(types.REQUEST_GROUPS); - Api.groups(search) + Api.groups(search, { order_by: 'similarity' }) .then((data) => { commit(types.RECEIVE_GROUPS_SUCCESS, data); }) @@ -30,7 +32,12 @@ export const fetchProjects = ({ commit, state }, search) => { if (groupId) { // TODO (https://gitlab.com/gitlab-org/gitlab/-/issues/323331): For errors `createFlash` is called twice; in `callback` and in `Api.groupProjects` - Api.groupProjects(groupId, search, {}, callback); + Api.groupProjects( + groupId, + search, + { order_by: 'similarity', with_shared: false, include_subgroups: true }, + callback, + ); } else { // The .catch() is due to the API method not handling a rejection properly Api.projects(search, { order_by: 'id' }, callback).catch(() => { @@ -39,6 +46,40 @@ export const fetchProjects = ({ commit, state }, search) => { } }; +export const loadFrequentGroups = async ({ commit }) => { + const data = loadDataFromLS(GROUPS_LOCAL_STORAGE_KEY); + commit(types.LOAD_FREQUENT_ITEMS, { key: GROUPS_LOCAL_STORAGE_KEY, data }); + + const promises = data.map((d) => Api.group(d.id)); + try { + const inflatedData = mergeById(await Promise.all(promises), data); + commit(types.LOAD_FREQUENT_ITEMS, { key: GROUPS_LOCAL_STORAGE_KEY, data: inflatedData }); + } catch { + createFlash({ message: __('There was a problem fetching recent groups.') }); + } +}; + +export const loadFrequentProjects = async ({ commit }) => { + const data = loadDataFromLS(PROJECTS_LOCAL_STORAGE_KEY); + commit(types.LOAD_FREQUENT_ITEMS, { key: PROJECTS_LOCAL_STORAGE_KEY, data }); + + const promises = data.map((d) => Api.project(d.id).then((res) => res.data)); + try { + const inflatedData = mergeById(await Promise.all(promises), data); + commit(types.LOAD_FREQUENT_ITEMS, { key: PROJECTS_LOCAL_STORAGE_KEY, data: inflatedData }); + } catch { + createFlash({ message: __('There was a problem fetching recent projects.') }); + } +}; + +export const setFrequentGroup = ({ state }, item) => { + setFrequentItemToLS(GROUPS_LOCAL_STORAGE_KEY, state.frequentItems, item); +}; + +export const setFrequentProject = ({ state }, item) => { + setFrequentItemToLS(PROJECTS_LOCAL_STORAGE_KEY, state.frequentItems, item); +}; + export const setQuery = ({ commit }, { key, value }) => { commit(types.SET_QUERY, { key, value }); }; diff --git a/app/assets/javascripts/search/store/constants.js b/app/assets/javascripts/search/store/constants.js new file mode 100644 index 00000000000..3abf7cac6ba --- /dev/null +++ b/app/assets/javascripts/search/store/constants.js @@ -0,0 +1,7 @@ +export const MAX_FREQUENT_ITEMS = 5; + +export const MAX_FREQUENCY = 5; + +export const GROUPS_LOCAL_STORAGE_KEY = 'global-search-frequent-groups'; + +export const PROJECTS_LOCAL_STORAGE_KEY = 'global-search-frequent-projects'; diff --git a/app/assets/javascripts/search/store/getters.js b/app/assets/javascripts/search/store/getters.js new file mode 100644 index 00000000000..650af5fa55a --- /dev/null +++ b/app/assets/javascripts/search/store/getters.js @@ -0,0 +1,9 @@ +import { GROUPS_LOCAL_STORAGE_KEY, PROJECTS_LOCAL_STORAGE_KEY } from './constants'; + +export const frequentGroups = (state) => { + return state.frequentItems[GROUPS_LOCAL_STORAGE_KEY]; +}; + +export const frequentProjects = (state) => { + return state.frequentItems[PROJECTS_LOCAL_STORAGE_KEY]; +}; diff --git a/app/assets/javascripts/search/store/index.js b/app/assets/javascripts/search/store/index.js index 1923c8b96ab..4fa88822722 100644 --- a/app/assets/javascripts/search/store/index.js +++ b/app/assets/javascripts/search/store/index.js @@ -1,6 +1,7 @@ import Vue from 'vue'; import Vuex from 'vuex'; import * as actions from './actions'; +import * as getters from './getters'; import mutations from './mutations'; import createState from './state'; @@ -8,6 +9,7 @@ Vue.use(Vuex); export const getStoreConfig = ({ query }) => ({ actions, + getters, mutations, state: createState({ query }), }); diff --git a/app/assets/javascripts/search/store/mutation_types.js b/app/assets/javascripts/search/store/mutation_types.js index a6430b53c4f..5c1c29dc738 100644 --- a/app/assets/javascripts/search/store/mutation_types.js +++ b/app/assets/javascripts/search/store/mutation_types.js @@ -7,3 +7,5 @@ export const RECEIVE_PROJECTS_SUCCESS = 'RECEIVE_PROJECTS_SUCCESS'; export const RECEIVE_PROJECTS_ERROR = 'RECEIVE_PROJECTS_ERROR'; export const SET_QUERY = 'SET_QUERY'; + +export const LOAD_FREQUENT_ITEMS = 'LOAD_FREQUENT_ITEMS'; diff --git a/app/assets/javascripts/search/store/mutations.js b/app/assets/javascripts/search/store/mutations.js index 91d7cf66c8f..63156a89738 100644 --- a/app/assets/javascripts/search/store/mutations.js +++ b/app/assets/javascripts/search/store/mutations.js @@ -26,4 +26,7 @@ export default { [types.SET_QUERY](state, { key, value }) { state.query[key] = value; }, + [types.LOAD_FREQUENT_ITEMS](state, { key, data }) { + state.frequentItems[key] = data; + }, }; diff --git a/app/assets/javascripts/search/store/state.js b/app/assets/javascripts/search/store/state.js index 9a0d61d0b93..5b1429ccc97 100644 --- a/app/assets/javascripts/search/store/state.js +++ b/app/assets/javascripts/search/store/state.js @@ -1,8 +1,14 @@ +import { GROUPS_LOCAL_STORAGE_KEY, PROJECTS_LOCAL_STORAGE_KEY } from './constants'; + const createState = ({ query }) => ({ query, groups: [], fetchingGroups: false, projects: [], fetchingProjects: false, + frequentItems: { + [GROUPS_LOCAL_STORAGE_KEY]: [], + [PROJECTS_LOCAL_STORAGE_KEY]: [], + }, }); export default createState; diff --git a/app/assets/javascripts/search/store/utils.js b/app/assets/javascripts/search/store/utils.js new file mode 100644 index 00000000000..60c09221ca9 --- /dev/null +++ b/app/assets/javascripts/search/store/utils.js @@ -0,0 +1,80 @@ +import AccessorUtilities from '../../lib/utils/accessor'; +import { MAX_FREQUENT_ITEMS, MAX_FREQUENCY } from './constants'; + +function extractKeys(object, keyList) { + return Object.fromEntries(keyList.map((key) => [key, object[key]])); +} + +export const loadDataFromLS = (key) => { + if (!AccessorUtilities.isLocalStorageAccessSafe()) { + return []; + } + + try { + return JSON.parse(localStorage.getItem(key)) || []; + } catch { + // The LS got in a bad state, let's wipe it + localStorage.removeItem(key); + return []; + } +}; + +export const setFrequentItemToLS = (key, data, itemData) => { + if (!AccessorUtilities.isLocalStorageAccessSafe()) { + return; + } + + const keyList = [ + 'id', + 'avatar_url', + 'name', + 'full_name', + 'name_with_namespace', + 'frequency', + 'lastUsed', + ]; + + try { + const frequentItems = data[key].map((obj) => extractKeys(obj, keyList)); + const item = extractKeys(itemData, keyList); + const existingItemIndex = frequentItems.findIndex((i) => i.id === item.id); + + if (existingItemIndex >= 0) { + // Up the frequency (Max 5) + const currentFrequency = frequentItems[existingItemIndex].frequency; + frequentItems[existingItemIndex].frequency = Math.min(currentFrequency + 1, MAX_FREQUENCY); + frequentItems[existingItemIndex].lastUsed = new Date().getTime(); + } else { + // Only store a max of 5 items + if (frequentItems.length >= MAX_FREQUENT_ITEMS) { + frequentItems.pop(); + } + + frequentItems.push({ ...item, frequency: 1, lastUsed: new Date().getTime() }); + } + + // Sort by frequency and lastUsed + frequentItems.sort((a, b) => { + if (a.frequency > b.frequency) { + return -1; + } else if (a.frequency < b.frequency) { + return 1; + } + return b.lastUsed - a.lastUsed; + }); + + // Note we do not need to commit a mutation here as immediately after this we refresh the page to + // update the search results. + localStorage.setItem(key, JSON.stringify(frequentItems)); + } catch { + // The LS got in a bad state, let's wipe it + localStorage.removeItem(key); + } +}; + +export const mergeById = (inflatedData, storedData) => { + return inflatedData.map((data) => { + const stored = storedData?.find((d) => d.id === data.id) || {}; + return { ...stored, ...data }; + }); +}; |