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

utils.js « command_palette « 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: 32abbbfd3c2738695e6e8f73b9d13c299c8675b5 (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
import { isNil, omitBy } from 'lodash';
import { objectToQuery, joinPaths } from '~/lib/utils/url_utility';
import { TRACKING_UNKNOWN_ID } from '~/super_sidebar/constants';
import {
  SEARCH_SCOPE,
  GLOBAL_COMMANDS_GROUP_TITLE,
  TRACKING_CLICK_COMMAND_PALETTE_ITEM,
} from './constants';

export const commandMapper = ({ name, items }) => {
  // TODO: we filter out invite_members for now, because it is complicated to add the invite members modal here
  // and is out of scope for the basic command palette items. If it proves to be useful, we can add it later.
  return {
    name: name || GLOBAL_COMMANDS_GROUP_TITLE,
    items: items.filter(({ component }) => component !== 'invite_members'),
  };
};

export const linksReducer = (acc, menuItem) => {
  const trackingAttrs = ({ id, title }) => {
    return {
      extraAttrs: {
        'data-track-action': TRACKING_CLICK_COMMAND_PALETTE_ITEM,
        'data-track-label': id || TRACKING_UNKNOWN_ID,
        ...(id
          ? {}
          : {
              'data-track-extra': JSON.stringify({ title }),
            }),
      },
    };
  };

  acc.push({
    text: menuItem.title,
    keywords: menuItem.title,
    icon: menuItem.icon,
    href: menuItem.link,
    ...trackingAttrs(menuItem),
  });
  if (menuItem.items?.length) {
    const items = menuItem.items.map((item) => ({
      keywords: item.title,
      text: [menuItem.title, item.title].join(' > '),
      href: item.link,
      icon: menuItem.icon,
      ...trackingAttrs(item),
    }));

    /* eslint-disable-next-line no-param-reassign */
    acc = [...acc, ...items];
  }
  return acc;
};

export const fileMapper = (projectBlobPath, file) => {
  return {
    icon: 'doc-code',
    text: file,
    href: joinPaths(projectBlobPath, file),
    extraAttrs: {
      'data-track-action': TRACKING_CLICK_COMMAND_PALETTE_ITEM,
      'data-track-label': 'file',
    },
  };
};

export const autocompleteQuery = ({ path, searchTerm, handle, projectId }) => {
  const query = omitBy(
    {
      term: searchTerm,
      project_id: projectId,
      filter: 'search',
      scope: SEARCH_SCOPE[handle],
    },
    isNil,
  );

  return `${path}?${objectToQuery(query)}`;
};