summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/runner/utils.js
blob: 6e4c8c45e7b3a98bb1db816f8150a428957fa2da (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
import { formatNumber } from '~/locale';
import { DEFAULT_TH_CLASSES } from '~/lib/utils/constants';
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
 */
export const tableField = ({ key, label = '', thClasses = [] }) => {
  return {
    key,
    label,
    thClass: [DEFAULT_TH_CLASSES, ...thClasses],
    tdAttr: {
      'data-testid': `td-${key}`,
    },
  };
};

/**
 * 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 };
};