2020-02-20 04:09:13 -05:00
|
|
|
import { take } from 'lodash';
|
2019-12-23 10:07:48 -05:00
|
|
|
import { GlBreakpointInstance as bp } from '@gitlab/ui/dist/utils';
|
2020-01-30 16:08:47 -05:00
|
|
|
import sanitize from 'sanitize-html';
|
2018-07-06 09:40:11 -04:00
|
|
|
import { FREQUENT_ITEMS, HOUR_IN_MS } from './constants';
|
|
|
|
|
2019-12-23 10:07:48 -05:00
|
|
|
export const isMobile = () => ['md', 'sm', 'xs'].includes(bp.getBreakpointSize());
|
2018-07-06 09:40:11 -04:00
|
|
|
|
|
|
|
export const getTopFrequentItems = items => {
|
|
|
|
if (!items) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
const frequentItemsCount = isMobile()
|
|
|
|
? FREQUENT_ITEMS.LIST_COUNT_MOBILE
|
|
|
|
: FREQUENT_ITEMS.LIST_COUNT_DESKTOP;
|
|
|
|
|
|
|
|
const frequentItems = items.filter(item => item.frequency >= FREQUENT_ITEMS.ELIGIBLE_FREQUENCY);
|
|
|
|
|
|
|
|
if (!frequentItems || frequentItems.length === 0) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
frequentItems.sort((itemA, itemB) => {
|
|
|
|
// Sort all frequent items in decending order of frequency
|
|
|
|
// and then by lastAccessedOn with recent most first
|
|
|
|
if (itemA.frequency !== itemB.frequency) {
|
|
|
|
return itemB.frequency - itemA.frequency;
|
|
|
|
} else if (itemA.lastAccessedOn !== itemB.lastAccessedOn) {
|
|
|
|
return itemB.lastAccessedOn - itemA.lastAccessedOn;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
});
|
|
|
|
|
2020-02-20 04:09:13 -05:00
|
|
|
return take(frequentItems, frequentItemsCount);
|
2018-07-06 09:40:11 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
export const updateExistingFrequentItem = (frequentItem, item) => {
|
|
|
|
const accessedOverHourAgo =
|
|
|
|
Math.abs(item.lastAccessedOn - frequentItem.lastAccessedOn) / HOUR_IN_MS > 1;
|
|
|
|
|
|
|
|
return {
|
|
|
|
...item,
|
|
|
|
frequency: accessedOverHourAgo ? frequentItem.frequency + 1 : frequentItem.frequency,
|
|
|
|
lastAccessedOn: accessedOverHourAgo ? Date.now() : frequentItem.lastAccessedOn,
|
|
|
|
};
|
|
|
|
};
|
2020-01-30 16:08:47 -05:00
|
|
|
|
2020-03-26 14:08:03 -04:00
|
|
|
export const sanitizeItem = item => {
|
|
|
|
// Only sanitize if the key exists on the item
|
|
|
|
const maybeSanitize = key => {
|
|
|
|
if (!Object.prototype.hasOwnProperty.call(item, key)) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
return { [key]: sanitize(item[key].toString(), { allowedTags: [] }) };
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
...item,
|
|
|
|
...maybeSanitize('name'),
|
|
|
|
...maybeSanitize('namespace'),
|
|
|
|
};
|
|
|
|
};
|