Welcome to mirror list, hosted at ThFree Co, Russian Federation.

utils.js « global_search « components « super_sidebar « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2c369cbdf5f11e76436cfb3d0483ff85f8236c70 (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
import { pickBy } from 'lodash';
import { slugify, truncateNamespace } from '~/lib/utils/text_utility';
import {
  GROUPS_CATEGORY,
  PROJECTS_CATEGORY,
  MERGE_REQUEST_CATEGORY,
  ISSUES_CATEGORY,
  RECENT_EPICS_CATEGORY,
} from '~/vue_shared/global_search/constants';
import { TRACKING_CLICK_COMMAND_PALETTE_ITEM } from './command_palette/constants';
import { LARGE_AVATAR_PX, SMALL_AVATAR_PX } from './constants';

const getTruncatedNamespace = (string) => {
  if (string.split(' / ').length > 2) {
    return truncateNamespace(string);
  }

  return string;
};
const getAvatarSize = (category) => {
  if (category === GROUPS_CATEGORY || category === PROJECTS_CATEGORY) {
    return LARGE_AVATAR_PX;
  }

  return SMALL_AVATAR_PX;
};

const getEntityId = (item, searchContext) => {
  switch (item.category) {
    case GROUPS_CATEGORY:
    case RECENT_EPICS_CATEGORY:
      return item.group_id || item.id || searchContext?.group?.id;
    case PROJECTS_CATEGORY:
    case ISSUES_CATEGORY:
    case MERGE_REQUEST_CATEGORY:
      return item.project_id || item.id || searchContext?.project?.id;
    default:
      return item.id;
  }
};
const getEntityName = (item, searchContext) => {
  switch (item.category) {
    case GROUPS_CATEGORY:
    case RECENT_EPICS_CATEGORY:
      return item.group_name || item.value || item.label || searchContext?.group?.name;
    case PROJECTS_CATEGORY:
    case ISSUES_CATEGORY:
    case MERGE_REQUEST_CATEGORY:
      return item.project_name || item.value || item.label || searchContext?.project?.name;
    default:
      return item.label;
  }
};

export const getFormattedItem = (item, searchContext) => {
  const { id, category, value, label, url: href, avatar_url } = item;
  let namespace;
  const text = value || label;
  if (value) {
    namespace = getTruncatedNamespace(label);
  }
  const avatarSize = getAvatarSize(category);
  const entityId = getEntityId(item, searchContext);
  const entityName = getEntityName(item, searchContext);
  const trackingLabel = slugify(category ?? '');
  const trackingAttrs = trackingLabel
    ? {
        extraAttrs: {
          'data-track-action': TRACKING_CLICK_COMMAND_PALETTE_ITEM,
          'data-track-label': slugify(category, '_'),
        },
      }
    : {};

  return pickBy(
    {
      id,
      category,
      value,
      label,
      text,
      href,
      avatar_url,
      avatar_size: avatarSize,
      namespace,
      entity_id: entityId,
      entity_name: entityName,
      ...trackingAttrs,
    },
    (val) => val !== undefined,
  );
};