"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.UserProfilesSelectable = void 0; var _eui = require("@elastic/eui"); var _react = _interopRequireWildcard(require("react")); var _i18n = require("@kbn/i18n"); var _i18nReact = require("@kbn/i18n-react"); var _user_profile = require("./user_profile"); var _user_avatar = require("./user_avatar"); function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } /* * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one * or more contributor license agreements. Licensed under the Elastic License * 2.0 and the Server Side Public License, v 1; you may not use this file except * in compliance with, at your election, the Elastic License 2.0 or the Server * Side Public License, v 1. */ const NULL_OPTION_KEY = 'null'; /** * Props of {@link UserProfilesSelectable} component */ /** * Renders a selectable component given a list of user profiles */ const UserProfilesSelectable = ({ selectedOptions, defaultOptions, options, onChange, onSearchChange, isLoading = false, singleSelection = false, limit, height, loadingMessage, noMatchesMessage, emptyMessage, errorMessage, searchPlaceholder, searchInputId, selectedStatusMessage, limitReachedMessage, nullOptionLabel, defaultOptionsLabel, clearButtonLabel }) => { const [displayedOptions, setDisplayedOptions] = (0, _react.useState)([]); const [searchTerm, setSearchTerm] = (0, _react.useState)(''); const selectedCount = selectedOptions ? selectedOptions.length : 0; const limitReached = limit ? selectedCount >= limit : false; // Resets all displayed options const resetDisplayedOptions = () => { if (options) { setDisplayedOptions(options.map(option => toSelectableOption(option, nullOptionLabel))); return; } setDisplayedOptions([]); updateDisplayedOptions(); }; const ensureSeparator = values => { let index = values.findIndex(option => option.isGroupLabel); if (index === -1) { const length = values.push({ label: defaultOptionsLabel !== null && defaultOptionsLabel !== void 0 ? defaultOptionsLabel : _i18n.i18n.translate('userProfileComponents.userProfilesSelectable.defaultOptionsLabel', { defaultMessage: 'Suggested' }), isGroupLabel: true }); index = length - 1; } return index; }; // Updates displayed options without removing or resorting exiting options const updateDisplayedOptions = () => { if (options) { return; } setDisplayedOptions(values => { // Copy all displayed options const nextOptions = [...values]; // Get any newly added selected options const selectedOptionsToAdd = selectedOptions ? selectedOptions.filter(profile => !nextOptions.find(option => isMatchingOption(option, profile))).map(option => toSelectableOption(option, nullOptionLabel)) : []; // Get any newly added default options const defaultOptionsToAdd = defaultOptions ? defaultOptions.filter(profile => !nextOptions.find(option => isMatchingOption(option, profile)) && !selectedOptionsToAdd.find(option => isMatchingOption(option, profile))).map(option => toSelectableOption(option, nullOptionLabel)) : []; // Merge in any new options and add group separator if necessary if (defaultOptionsToAdd.length) { const separatorIndex = ensureSeparator(nextOptions); nextOptions.splice(separatorIndex, 0, ...selectedOptionsToAdd); nextOptions.push(...defaultOptionsToAdd); } else { nextOptions.push(...selectedOptionsToAdd); } return nextOptions; }); }; // Marks displayed options as checked or unchecked depending on `props.selectedOptions` const updateCheckedStatus = () => { setDisplayedOptions(values => values.map(option => { if (selectedOptions) { var _option$data$data; const match = selectedOptions.find(profile => isMatchingOption(option, profile)); const checked = match === undefined ? undefined : 'on'; const disabled = checked ? false : limitReached; return { ...option, checked, disabled, prepend: option.data ? /*#__PURE__*/_react.default.createElement(_user_avatar.UserAvatar, { user: option.data.user, avatar: (_option$data$data = option.data.data) === null || _option$data$data === void 0 ? void 0 : _option$data$data.avatar, size: "s", isDisabled: disabled }) : undefined }; } return { ...option, checked: undefined, disabled: undefined }; })); }; (0, _react.useEffect)(resetDisplayedOptions, [options]); // eslint-disable-line react-hooks/exhaustive-deps (0, _react.useEffect)(updateDisplayedOptions, [defaultOptions, selectedOptions]); // eslint-disable-line react-hooks/exhaustive-deps (0, _react.useEffect)(updateCheckedStatus, [options, defaultOptions, selectedOptions]); // eslint-disable-line react-hooks/exhaustive-deps return /*#__PURE__*/_react.default.createElement(_eui.EuiSelectable, { options: displayedOptions // @ts-expect-error: Type of `nextOptions` in EuiSelectable does not match what's actually being passed back so need to manually override it , onChange: nextOptions => { if (!onChange) { return; } // Take all selected options from `nextOptions` unless already in `props.selectedOptions` // @ts-expect-error const values = nextOptions.filter(option => { if (option.isGroupLabel || option.checked !== 'on') { return false; } if (selectedOptions && selectedOptions.find(profile => isMatchingOption(option, profile)) !== undefined) { return false; } return true; }).map(option => option.key === NULL_OPTION_KEY ? null : option.data); // Add all options from `props.selectedOptions` unless they have been deselected in `nextOptions` if (selectedOptions && !singleSelection) { selectedOptions.forEach(profile => { const match = nextOptions.find(option => isMatchingOption(option, profile)); if (match === undefined || match.checked === 'on') { if (match && match.key === NULL_OPTION_KEY) { values.unshift(profile); } else { values.push(profile); } } }); } onChange(values); }, style: { maxHeight: height }, singleSelection: singleSelection, searchable: true, searchProps: { placeholder: searchPlaceholder !== null && searchPlaceholder !== void 0 ? searchPlaceholder : _i18n.i18n.translate('userProfileComponents.userProfilesSelectable.searchPlaceholder', { defaultMessage: 'Search' }), value: searchTerm, onChange: value => { setSearchTerm(value); onSearchChange === null || onSearchChange === void 0 ? void 0 : onSearchChange(value); }, isLoading, isClearable: !isLoading, id: searchInputId }, isPreFiltered: true, listProps: { onFocusBadge: false }, loadingMessage: loadingMessage, noMatchesMessage: noMatchesMessage, emptyMessage: emptyMessage, errorMessage: errorMessage, renderOption: (option, searchValue) => { if (option.user) { return /*#__PURE__*/_react.default.createElement(_eui.EuiFlexGroup, { alignItems: "center", justifyContent: "spaceBetween", gutterSize: "s", responsive: false }, /*#__PURE__*/_react.default.createElement(_eui.EuiFlexItem, null, /*#__PURE__*/_react.default.createElement(_eui.EuiHighlight, { search: searchValue }, option.label)), option.user.email && option.user.email !== option.label ? /*#__PURE__*/_react.default.createElement(_eui.EuiFlexItem, { grow: false }, /*#__PURE__*/_react.default.createElement(_eui.EuiTextColor, { color: option.disabled ? 'disabled' : 'subdued' }, searchValue ? /*#__PURE__*/_react.default.createElement(_eui.EuiHighlight, { search: searchValue }, option.user.email) : option.user.email)) : undefined); } return /*#__PURE__*/_react.default.createElement(_eui.EuiHighlight, { search: searchValue }, option.label); } }, (list, search) => /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_eui.EuiPanel, { hasShadow: false, paddingSize: "s" }, search, !singleSelection ? /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_eui.EuiSpacer, { size: "s" }), /*#__PURE__*/_react.default.createElement(_eui.EuiFlexGroup, { alignItems: "center", justifyContent: "spaceBetween", gutterSize: "s", responsive: false }, /*#__PURE__*/_react.default.createElement(_eui.EuiFlexItem, { grow: false }, /*#__PURE__*/_react.default.createElement(_eui.EuiText, { size: "xs", color: "subdued" }, selectedStatusMessage ? selectedStatusMessage(selectedCount) : /*#__PURE__*/_react.default.createElement(_i18nReact.FormattedMessage, { id: "userProfileComponents.userProfilesSelectable.selectedStatusMessage", defaultMessage: "{count, plural, one {# user selected} other {# users selected}}", values: { count: selectedCount } }))), /*#__PURE__*/_react.default.createElement(_eui.EuiFlexItem, { grow: false }, selectedCount ? /*#__PURE__*/_react.default.createElement(_eui.EuiButtonEmpty, { size: "xs", flush: "right", onClick: () => onChange === null || onChange === void 0 ? void 0 : onChange([]), style: { height: '1rem' } }, clearButtonLabel !== null && clearButtonLabel !== void 0 ? clearButtonLabel : /*#__PURE__*/_react.default.createElement(_i18nReact.FormattedMessage, { id: "userProfileComponents.userProfilesSelectable.clearButtonLabel", defaultMessage: "Remove all users" })) : undefined))) : undefined), limit && selectedCount >= limit ? /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_eui.EuiHorizontalRule, { margin: "none" }), /*#__PURE__*/_react.default.createElement(_eui.EuiCallOut, { title: limitReachedMessage ? limitReachedMessage(limit) : /*#__PURE__*/_react.default.createElement(_i18nReact.FormattedMessage, { id: "userProfileComponents.userProfilesSelectable.limitReachedMessage", defaultMessage: "You've selected the maximum of {count, plural, one {# user} other {# users}}", values: { count: limit } }), color: "warning", size: "s" })) : undefined, /*#__PURE__*/_react.default.createElement(_eui.EuiHorizontalRule, { margin: "none" }), list)); }; exports.UserProfilesSelectable = UserProfilesSelectable; function toSelectableOption(userProfile, nullOptionLabel) { if (userProfile) { return { key: userProfile.uid, label: (0, _user_profile.getUserDisplayName)(userProfile.user), data: userProfile }; } return { key: NULL_OPTION_KEY, label: nullOptionLabel !== null && nullOptionLabel !== void 0 ? nullOptionLabel : _i18n.i18n.translate('userProfileComponents.userProfilesSelectable.nullOptionLabel', { defaultMessage: 'No users' }) }; } function isMatchingOption(option, profile) { return option.key === (profile ? profile.uid : NULL_OPTION_KEY); }