UNPKG

@atlaskit/editor-plugin-mentions

Version:

Mentions plugin for @atlaskit/editor-core

189 lines (184 loc) 7.24 kB
/* InviteItemWithEmailDomain.tsx generated by @compiled/babel-plugin v0.39.1 */ import _extends from "@babel/runtime/helpers/extends"; import _slicedToArray from "@babel/runtime/helpers/slicedToArray"; import "./InviteItemWithEmailDomain.compiled.css"; import { ax, ix } from "@compiled/react/runtime"; import React, { useCallback, useEffect, useRef, useState } from 'react'; import { FormattedMessage, injectIntl } from 'react-intl'; import { mentionMessages as messages } from '@atlaskit/editor-common/messages'; import EmailIcon from '@atlaskit/icon/core/email'; import StatusErrorIcon from '@atlaskit/icon/core/status-error'; import { isValidEmail } from '@atlaskit/user-picker'; var mentionItemStyle = null; var mentionItemSelectedStyle = null; var style = { byline: "_19pkv77o", rowStyle: "_1reo15vq _18m915vq _4cvr1h6o _1e0c1txw _2lx2vrvc _1n261g80 _ca0q12x7 _n3td12x7 _19bvutpp _u5f3utpp _1bto1l2s _s7n4nkob", avatar: "_16jlidpf _1o9zkb7n _i0dl1wug _1e0c1txw _4cvr1h6o _1bah1h6o _kqswh2mm _1bsb14no _4t3i14no", nameSection: "_16jlkb7n _1o9zkb7n _i0dlf1ug _1ul9idpf _18u0utpp _syazazsu", capitalize: "_1p1d1dk0" }; var VALID_OPTION = 'VALID'; var POTENTIAL_OPTION = 'POTENTIAL'; // eslint-disable-next-line require-unicode-regexp var COMPLETE_EMAIL_REGEX = /^[^\s@]+@[^\s@]+\.[^\s@]+$/i; var ERROR_DELAY_MS = 750; var getInviteOption = function getInviteOption(inputValue, suggestedEmailDomain) { if (inputValue.includes(' ') && inputValue.includes('@')) { return inputValue; } var isEmail = inputValue && [VALID_OPTION, POTENTIAL_OPTION].includes(isValidEmail(inputValue)); if (isEmail || !suggestedEmailDomain) { return inputValue; } return "".concat(inputValue.toLocaleLowerCase(), "@").concat(suggestedEmailDomain); }; var getIsEmailValid = function getIsEmailValid(inputValue) { if (!inputValue || inputValue.length === 0) { return false; } if (inputValue.includes(' ')) { return false; } if (inputValue.includes('@') && !COMPLETE_EMAIL_REGEX.test(inputValue)) { return false; } return true; }; export var INVITE_ITEM_DESCRIPTION = { id: 'invite-teammate' }; var InviteItemWithEmailDomain = function InviteItemWithEmailDomain(_ref) { var productName = _ref.productName, onMount = _ref.onMount, onMouseEnter = _ref.onMouseEnter, onSelection = _ref.onSelection, selected = _ref.selected, userRole = _ref.userRole, _ref$query = _ref.query, query = _ref$query === void 0 ? '' : _ref$query, emailDomain = _ref.emailDomain, intl = _ref.intl; var _useState = useState(false), _useState2 = _slicedToArray(_useState, 2), showErrorIcon = _useState2[0], setShowErrorIcon = _useState2[1]; var timeoutRef = useRef(); var possibleEmail = getInviteOption(query, emailDomain); var isEmailValid = getIsEmailValid(query); // Use debounced error state for icon and byline display var shouldShowError = !isEmailValid && showErrorIcon; // Use debounced error state for byline: show invalid message only after delay var isValidForByline = isEmailValid || !shouldShowError; var getByline = function getByline() { if (!isValidForByline) { return intl.formatMessage(messages.inviteTeammateInvalidEmail); } if (userRole === 'admin') { return /*#__PURE__*/React.createElement(FormattedMessage // Ignored via go/ees005 // eslint-disable-next-line react/jsx-props-no-spreading , _extends({}, messages.inviteItemTitle, { // eslint-disable-next-line @atlassian/perf-linting/no-unstable-inline-props -- Ignored via go/ees017 (to be fixed) values: { userRole: userRole || 'basic', productName: /*#__PURE__*/React.createElement("span", { "data-testid": "capitalized-message", className: ax([style.capitalize]) }, productName) } })); } return intl.formatMessage(messages.sendInvite); }; var onSelected = useCallback( // Ignored via go/ees005 // eslint-disable-next-line @typescript-eslint/no-explicit-any function (event) { if (onSelection) { // For mouse events, only handle left click if ('button' in event && event.button !== 0) { return; } // For keyboard events, only handle Enter and Space if ('key' in event && event.key !== 'Enter' && event.key !== ' ') { return; } event.preventDefault(); onSelection(INVITE_ITEM_DESCRIPTION, event); } }, [onSelection]); var onItemMouseEnter = useCallback( // Ignored via go/ees005 // eslint-disable-next-line @typescript-eslint/no-explicit-any function (event) { if (onMouseEnter) { onMouseEnter(INVITE_ITEM_DESCRIPTION, event); } }, [onMouseEnter]); var onItemFocus = useCallback( // Ignored via go/ees005 // eslint-disable-next-line @typescript-eslint/no-explicit-any function (event) { if (onMouseEnter) { onMouseEnter(INVITE_ITEM_DESCRIPTION, event); } }, [onMouseEnter]); useEffect(function () { if (onMount) { onMount(); } }, [onMount]); // Debounce error icon display: only show error after user stops typing invalid input useEffect(function () { // Clear existing timeout if (timeoutRef.current) { clearTimeout(timeoutRef.current); timeoutRef.current = undefined; } // Reset error icon immediately if input becomes valid or empty if (isEmailValid || query.length === 0) { setShowErrorIcon(false); } else { // Debounce: only show error icon after delay if input remains invalid timeoutRef.current = setTimeout(function () { setShowErrorIcon(true); }, ERROR_DELAY_MS); } // Cleanup timeout on unmount or when query changes return function () { if (timeoutRef.current) { clearTimeout(timeoutRef.current); } }; }, [query, isEmailValid]); var displayName = query && emailDomain ? possibleEmail : undefined; return displayName && /*#__PURE__*/React.createElement("div", { role: "button", tabIndex: 0, onMouseDown: onSelected, onKeyDown: onSelected, onMouseEnter: onItemMouseEnter, onFocus: onItemFocus, "data-id": INVITE_ITEM_DESCRIPTION.id, className: ax(["_1reo15vq _18m915vq _bfhk1j28 _1e0c1ule _2mzuglyw _80omtlke", selected && "_bfhk1dpa"]) }, /*#__PURE__*/React.createElement("div", { className: ax([style.rowStyle]) }, /*#__PURE__*/React.createElement("span", { className: ax([style.avatar]) }, shouldShowError ? /*#__PURE__*/React.createElement(StatusErrorIcon, { label: "Error", color: "var(--ds-icon-danger, #C9372C)" }) : /*#__PURE__*/React.createElement(EmailIcon, { label: "Email", color: "var(--ds-icon-subtle, #505258)" })), /*#__PURE__*/React.createElement("div", { "data-testid": "name-section", className: ax([style.nameSection]) }, /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", null, displayName), /*#__PURE__*/React.createElement("div", { className: ax([style.byline]) }, getByline()))))); }; // eslint-disable-next-line @typescript-eslint/ban-types var _default_1 = injectIntl(InviteItemWithEmailDomain); export default _default_1;