Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
Size: Mime:
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import { useState, useMemo, Fragment, useEffect } from 'react';
import { useSelector } from 'react-redux';
import { Cross, Tick } from '@scaleflex/icons';
import { Label, Notification } from '@scaleflex/ui/core';
import Styled from './GalleryRoleSelect.styled';
import { findGalleryRoleIndexById, generateCamelCaseName, generatePrettyName, getGalleryRoleLabel } from './GalleryRoleSelect.utils';
import { useExplorerI18n } from '../../hooks';
import { selectAccessRoles, selectAccessRolesPermissions } from '../../slices/common.slice';

/**
 * @param {object} props
 * @param {string} props.value - Role ID
 * @param {function} props.onChange
 * @param {boolean} [props.showPermissionsInfo=true]
 * @param {number} [props.selectorPaddingX=17]
 * @param {boolean} [props.disabled=false]
 * @param {boolean} [props.readOnly=false]
 * @param {boolean} [props.defaultValueFirstItem=false]
 * @param {number} [props.minRoleId] - Use to specify which roles could be selected
 * @param {boolean} [props.showNotification=false]
 * @returns
 */
import { jsx as _jsx } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
import { Fragment as _Fragment } from "react/jsx-runtime";
var GalleryRoleSelect = function GalleryRoleSelect(_ref) {
  var value = _ref.value,
    onChange = _ref.onChange,
    _ref$showPermissionsI = _ref.showPermissionsInfo,
    showPermissionsInfo = _ref$showPermissionsI === void 0 ? true : _ref$showPermissionsI,
    _ref$selectorPaddingX = _ref.selectorPaddingX,
    selectorPaddingX = _ref$selectorPaddingX === void 0 ? 17 : _ref$selectorPaddingX,
    _ref$disabled = _ref.disabled,
    disabled = _ref$disabled === void 0 ? false : _ref$disabled,
    _ref$readOnly = _ref.readOnly,
    readOnly = _ref$readOnly === void 0 ? false : _ref$readOnly,
    _ref$defaultValueFirs = _ref.defaultValueFirstItem,
    defaultValueFirstItem = _ref$defaultValueFirs === void 0 ? false : _ref$defaultValueFirs,
    minRoleId = _ref.minRoleId,
    _ref$showNotification = _ref.showNotification,
    showNotification = _ref$showNotification === void 0 ? false : _ref$showNotification;
  var accessRoles = useSelector(selectAccessRoles);
  var accessRolesPermissions = useSelector(selectAccessRolesPermissions);
  var i18n = useExplorerI18n(function (_ref2) {
    var i18n = _ref2.i18n;
    return i18n;
  });
  var filteredAccessRoles = useMemo(function () {
    return (accessRoles || []).filter(function (role) {
      return !(role !== null && role !== void 0 && role.default_gallery_role_only);
    });
  }, [accessRoles]);
  var _useState = useState(),
    _useState2 = _slicedToArray(_useState, 2),
    hoveredRole = _useState2[0],
    setHoveredRole = _useState2[1];
  var _useState3 = useState(0),
    _useState4 = _slicedToArray(_useState3, 2),
    containerWidth = _useState4[0],
    setContainerWidth = _useState4[1];
  var activeAccessRoleIndex = useMemo(function () {
    return findGalleryRoleIndexById(value, filteredAccessRoles);
  }, [value, filteredAccessRoles]);
  var activeRole = filteredAccessRoles === null || filteredAccessRoles === void 0 ? void 0 : filteredAccessRoles[activeAccessRoleIndex];
  var actualRole = hoveredRole || activeRole;
  var actualAccessRoleIndex = useMemo(function () {
    return findGalleryRoleIndexById(actualRole === null || actualRole === void 0 ? void 0 : actualRole.id, filteredAccessRoles);
  }, [actualRole, filteredAccessRoles]);
  var itemLabelWidth = useMemo(function () {
    return Math.max(75, Math.round(containerWidth / ((filteredAccessRoles === null || filteredAccessRoles === void 0 ? void 0 : filteredAccessRoles.length) || 0)) + 20);
  }, [containerWidth, filteredAccessRoles]);
  var isNotificationVisible = showPermissionsInfo && actualRole && showNotification && isFinite(minRoleId) && actualRole.id <= minRoleId;
  var getPermLabel = function getPermLabel(permId, label) {
    var i18nKey = generateCamelCaseName(permId, 'galleryRolePerm');
    var defaultValue = label || generatePrettyName(permId);
    return i18n(i18nKey) || i18n(i18nKey, defaultValue) || defaultValue;
  };
  var renderPermissionIcon = function renderPermissionIcon(permission) {
    var isActive = ((actualRole === null || actualRole === void 0 ? void 0 : actualRole.permissions) || []).includes(permission);
    var IconComponent = isActive ? Tick : Cross;
    return /*#__PURE__*/_jsx(Styled.PermissionItemIcon, {
      active: isActive,
      children: /*#__PURE__*/_jsx(IconComponent, {
        size: 5
      })
    });
  };
  var renderPermissions = function renderPermissions() {
    return (accessRolesPermissions || []).map(function (_ref3) {
      var id = _ref3.id,
        name = _ref3.name;
      return /*#__PURE__*/_jsxs(Styled.PermissionItem, {
        children: [renderPermissionIcon(id), /*#__PURE__*/_jsx(Styled.PermissionItemLabel, {
          children: getPermLabel(id, name)
        })]
      }, id);
    });
  };
  useEffect(function () {
    if (defaultValueFirstItem && !value && filteredAccessRoles.length) {
      var availableRoles = isFinite(minRoleId) ? filteredAccessRoles.filter(function (_role) {
        return _role.id > minRoleId;
      }) : filteredAccessRoles;
      if (availableRoles) {
        onChange(availableRoles === null || availableRoles === void 0 ? void 0 : availableRoles[0]);
      }
    }
  }, [defaultValueFirstItem, value, filteredAccessRoles]);
  return /*#__PURE__*/_jsxs(Styled.Container, {
    onMouseLeave: readOnly ? undefined : function () {
      return setHoveredRole(null);
    },
    ref: function ref(node) {
      if (node) {
        setContainerWidth(node.getBoundingClientRect().width);
      }
    },
    disabled: disabled,
    children: [/*#__PURE__*/_jsx(Label, {
      children: i18n('accessTabGalleryRoleLabel')
    }), /*#__PURE__*/_jsx(Styled.Selector, {
      selectorPaddingX: selectorPaddingX,
      children: filteredAccessRoles.map(function (role, index) {
        var isRoleDisabled = isFinite(minRoleId) && role.id <= minRoleId;
        return /*#__PURE__*/_jsxs(Fragment, {
          children: [index > 0 && /*#__PURE__*/_jsx(Styled.SelectorSeparator, {
            active: index <= actualAccessRoleIndex,
            disabled: isRoleDisabled
          }), /*#__PURE__*/_jsx(Styled.SelectorDot, {
            active: index <= actualAccessRoleIndex,
            selected: index === activeAccessRoleIndex,
            $label: getGalleryRoleLabel(role.name, i18n),
            onMouseEnter: readOnly ? undefined : function () {
              return setHoveredRole(role);
            },
            onClick: disabled || isRoleDisabled ? undefined : function () {
              return onChange(role);
            },
            itemLabelWidth: itemLabelWidth,
            odd: !(index % 2),
            readOnly: readOnly,
            disabled: isRoleDisabled
          })]
        }, role.id);
      })
    }), showPermissionsInfo && actualRole && /*#__PURE__*/_jsxs(_Fragment, {
      children: [isNotificationVisible && /*#__PURE__*/_jsx(Notification, {
        message: i18n('explorerGalleryRoleLowerRoleWarning'),
        status: "warning"
      }), /*#__PURE__*/_jsxs(Styled.Permissions, {
        noMarginTop: isNotificationVisible,
        children: [/*#__PURE__*/_jsx(Styled.PermissionsLabel, {
          children: i18n('accessTabRolePermissionLabel', {
            role: getGalleryRoleLabel(actualRole === null || actualRole === void 0 ? void 0 : actualRole.name, i18n)
          })
        }), /*#__PURE__*/_jsx(Styled.PermissionsContainer, {
          children: renderPermissions()
        })]
      })]
    })]
  });
};
export default GalleryRoleSelect;