2022-04-04 11:22:00 +05:30
|
|
|
import { formatNumber } from '~/locale';
|
|
|
|
import { RUNNER_JOB_COUNT_LIMIT } from './constants';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Formats a job count, limited to a max number
|
|
|
|
*
|
|
|
|
* @param {Number} jobCount
|
|
|
|
* @returns Formatted string
|
|
|
|
*/
|
|
|
|
export const formatJobCount = (jobCount) => {
|
|
|
|
if (typeof jobCount !== 'number') {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
if (jobCount > RUNNER_JOB_COUNT_LIMIT) {
|
|
|
|
return `${formatNumber(RUNNER_JOB_COUNT_LIMIT)}+`;
|
|
|
|
}
|
|
|
|
return formatNumber(jobCount);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a GlTable fields with a given key and label
|
|
|
|
*
|
|
|
|
* @param {Object} options
|
|
|
|
* @returns Field object to add to GlTable fields
|
|
|
|
*/
|
2022-06-21 17:19:12 +05:30
|
|
|
export const tableField = ({ key, label = '', thClasses = [], ...options }) => {
|
2022-04-04 11:22:00 +05:30
|
|
|
return {
|
|
|
|
key,
|
|
|
|
label,
|
2022-07-16 23:28:13 +05:30
|
|
|
thClass: thClasses,
|
2022-04-04 11:22:00 +05:30
|
|
|
tdAttr: {
|
|
|
|
'data-testid': `td-${key}`,
|
|
|
|
},
|
2022-06-21 17:19:12 +05:30
|
|
|
...options,
|
2022-04-04 11:22:00 +05:30
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns variables for a GraphQL query that uses keyset
|
|
|
|
* pagination.
|
|
|
|
*
|
|
|
|
* https://docs.gitlab.com/ee/development/graphql_guide/pagination.html#keyset-pagination
|
|
|
|
*
|
|
|
|
* @param {Object} pagination - Contains before, after, page
|
|
|
|
* @param {Number} pageSize
|
|
|
|
* @returns Variables
|
|
|
|
*/
|
|
|
|
export const getPaginationVariables = (pagination, pageSize = 10) => {
|
|
|
|
const { before, after } = pagination;
|
|
|
|
|
|
|
|
// first + after: Next page
|
|
|
|
// Get the first N items after item X
|
|
|
|
if (after) {
|
|
|
|
return {
|
|
|
|
after,
|
|
|
|
first: pageSize,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// last + before: Prev page
|
|
|
|
// Get the first N items before item X, when you click on Prev
|
|
|
|
if (before) {
|
|
|
|
return {
|
|
|
|
before,
|
|
|
|
last: pageSize,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// first page
|
|
|
|
// Get the first N items
|
|
|
|
return { first: pageSize };
|
|
|
|
};
|
2022-10-11 01:57:18 +05:30
|
|
|
|
|
|
|
/**
|
|
|
|
* Turns a server-provided interval integer represented as a string into an
|
|
|
|
* integer that the frontend can use.
|
|
|
|
*
|
|
|
|
* @param {String} interval - String to convert
|
|
|
|
* @returns Parsed integer
|
|
|
|
*/
|
|
|
|
export const parseInterval = (interval) => {
|
|
|
|
return typeof interval === 'string' ? parseInt(interval, 10) : null;
|
|
|
|
};
|