@atlaskit/global-search
Version:
A cross-product search component (batteries included)
86 lines (74 loc) • 4.55 kB
JavaScript
import _defineProperty from "@babel/runtime/helpers/defineProperty";
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
import { addQueryParam } from '../../util/url-utils';
var CONFLUENCE_SEARCH_SESSION_ID_PARAM_NAME = 'search_id';
var JIRA_SEARCH_SESSION_ID_PARAM_NAME = 'searchSessionId';
/**
* Apply the given function to the specified keys in the supplied ResultMap
* @param resultMapperFn function to map results
* @param keysToMap the keys of the given ResultType to map over
* @param results the GenericResultMap to apply resultMapperFn to
*/
var mapJiraResultMap = function mapJiraResultMap(resultMapperFn, keysToMap, results) {
var objectKeys = Object.keys(results);
var nonMapped = objectKeys.filter(function (key) {
return !keysToMap.includes(key);
}).reduce(function (accum, key) {
return _objectSpread(_objectSpread({}, accum), {}, _defineProperty({}, key, results[key]));
}, {});
return Object.keys(results).filter(function (key) {
return keysToMap.includes(key);
}).reduce(function (accum, resultType) {
return _objectSpread(_objectSpread({}, accum), {}, _defineProperty({}, resultType, results[resultType].map(resultMapperFn)));
}, _objectSpread({}, nonMapped));
};
/**
* Same as mapGenericResultMap but for ConfluenceResultMaps. These maps contain more data than
* @param resultMapperFn function to map results
* @param keysToMap the keys of the given ResultType to map over
* @param results the GenericResultMap to apply resultMapperFn to
*/
var mapConfluenceResultMap = function mapConfluenceResultMap(resultMapperFn, keysToMap, results) {
var objectKeys = Object.keys(results);
return objectKeys.filter(function (key) {
return keysToMap.includes(key);
}).reduce(function (accum, resultType) {
//It's currently impossible to type this due items being an union of arrays
//see https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-3.html#improved-behavior-for-calling-union-types
//@ts-ignore
var items = results[resultType].items.map(resultMapperFn);
return _objectSpread(_objectSpread({}, accum), {}, _defineProperty({}, resultType, _objectSpread(_objectSpread({}, results[resultType]), {}, {
items: items
})));
}, _objectSpread({}, results));
};
var attachSearchSessionIdToResult = function attachSearchSessionIdToResult(searchSessionId, searchSessionIdParamName) {
return function (result) {
var href = result.href;
href = addQueryParam(href, searchSessionIdParamName, searchSessionId);
return _objectSpread(_objectSpread({}, result), {}, {
href: href.toString()
});
};
};
export var attachConfluenceContextIdentifiers = function attachConfluenceContextIdentifiers(searchSessionId, results) {
return mapConfluenceResultMap(attachSearchSessionIdToResult(searchSessionId, CONFLUENCE_SEARCH_SESSION_ID_PARAM_NAME), ['objects', 'spaces'], results);
};
export var attachJiraContextIdentifiers = function attachJiraContextIdentifiers(searchSessionId, results) {
var attachSearchSessionId = attachSearchSessionIdToResult(searchSessionId, JIRA_SEARCH_SESSION_ID_PARAM_NAME);
var attachJiraContext = function attachJiraContext(result) {
var href = result.href;
if (result.containerId) {
href = addQueryParam(href, 'searchContainerId', result.containerId);
}
href = addQueryParam(href, 'searchContentType', result.contentType.replace('jira-', ''));
href = addQueryParam(href, 'searchObjectId', result.resultId);
return _objectSpread(_objectSpread({}, result), {}, {
href: href
});
};
return mapJiraResultMap(function (r) {
return attachJiraContext(attachSearchSessionId(r));
}, ['objects', 'containers'], results);
};