UNPKG

@atlaskit/global-search

Version:

A cross-product search component (batteries included)

135 lines (124 loc) 5.14 kB
import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray"; import _slicedToArray from "@babel/runtime/helpers/slicedToArray"; import { ResultType, AnalyticsType, ContentType } from '../../model/Result'; import { take, getJiraAdvancedSearchUrl, JiraEntityTypes } from '../SearchResultsUtil'; import { messages } from '../../messages'; import { attachJiraContextIdentifiers } from '../common/contextIdentifiersHelper'; import { getJiraMaxObjects } from '../../util/experiment-utils'; var DEFAULT_MAX_OBJECTS = 8; var MAX_CONTAINERS = 6; var MAX_PEOPLE = 3; export var MAX_RECENT_RESULTS_TO_SHOW = 3; var DEFAULT_JIRA_RESULTS_MAP = { objects: [], containers: [], people: [] }; var isEmpty = function isEmpty() { var arr = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []; return !arr.length; }; var hasNoResults = function hasNoResults() { var objects = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []; var poeple = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : []; var containers = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : []; return isEmpty(objects) && isEmpty(poeple) && isEmpty(containers); }; var sliceResults = function sliceResults(resultsMap, abTest) { var _ref = resultsMap ? resultsMap : DEFAULT_JIRA_RESULTS_MAP, objects = _ref.objects, containers = _ref.containers, people = _ref.people; var _map = [{ items: objects, count: getJiraMaxObjects(abTest, DEFAULT_MAX_OBJECTS) }, { items: people, count: MAX_PEOPLE }, { items: containers, count: MAX_CONTAINERS }].map(function (_ref2) { var items = _ref2.items, count = _ref2.count; return take(items, count); }), _map2 = _slicedToArray(_map, 3), objectsToDisplay = _map2[0], peopleToDisplay = _map2[1], containersToDisplay = _map2[2]; return { objectsToDisplay: objectsToDisplay, containersToDisplay: containersToDisplay, peopleToDisplay: peopleToDisplay }; }; export var mapRecentResultsToUIGroups = function mapRecentResultsToUIGroups(recentlyViewedObjects, searchSessionId, features, appPermission) { var withSessionId = recentlyViewedObjects !== null ? attachJiraContextIdentifiers(searchSessionId, recentlyViewedObjects) : recentlyViewedObjects; var _sliceResults = sliceResults(withSessionId, features.abTest), objectsToDisplay = _sliceResults.objectsToDisplay, peopleToDisplay = _sliceResults.peopleToDisplay, containersToDisplay = _sliceResults.containersToDisplay; return [{ items: objectsToDisplay, key: 'issues', title: messages.jira_recent_issues_heading, totalSize: objectsToDisplay.length, showTotalSize: false // Jira doesn't support search extensions yet }, { items: containersToDisplay, key: 'containers', title: appPermission && !appPermission.hasSoftwareAccess ? messages.jira_recent_core_containers : messages.jira_recent_containers, totalSize: containersToDisplay.length, showTotalSize: false }, { items: peopleToDisplay, key: 'people', title: messages.jira_recent_people_heading, totalSize: peopleToDisplay.length, showTotalSize: false }]; }; export var mapSearchResultsToUIGroups = function mapSearchResultsToUIGroups(searchResultsObjects, searchSessionId, features, appPermission, query) { var withSessionId = searchResultsObjects !== null ? attachJiraContextIdentifiers(searchSessionId, searchResultsObjects) : searchResultsObjects; var _sliceResults2 = sliceResults(withSessionId, features.abTest), objectsToDisplay = _sliceResults2.objectsToDisplay, peopleToDisplay = _sliceResults2.peopleToDisplay, containersToDisplay = _sliceResults2.containersToDisplay; return [{ items: objectsToDisplay, key: 'issues', title: messages.jira_search_result_issues_heading, showTotalSize: false, // Jira doesn't support search extensions yet totalSize: objectsToDisplay.length }].concat(_toConsumableArray(!hasNoResults(objectsToDisplay, peopleToDisplay, containersToDisplay) ? [{ items: [{ resultType: ResultType.JiraIssueAdvancedSearch, resultId: 'search-jira', name: 'jira', href: getJiraAdvancedSearchUrl({ entityType: JiraEntityTypes.Issues, query: query }), analyticsType: AnalyticsType.LinkPostQueryAdvancedSearchJira, contentType: ContentType.JiraIssue }], key: 'issue-advanced', title: isEmpty(objectsToDisplay) ? messages.jira_search_result_issues_heading : undefined, showTotalSize: false, totalSize: 1 }] : []), [{ items: containersToDisplay, key: 'containers', title: appPermission && !appPermission.hasSoftwareAccess ? messages.jira_search_result_core_containers_heading : messages.jira_search_result_containers_heading, showTotalSize: false, totalSize: containersToDisplay.length }, { items: peopleToDisplay, key: 'people', title: messages.jira_search_result_people_heading, showTotalSize: false, totalSize: peopleToDisplay.length }]); };