All files / modules/20-rbac/components/RbacThumbnailSelect RbacThumbnailSelect.tsx

100% Statements 19/19
94.44% Branches 17/18
100% Functions 4/4
100% Lines 17/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60              2x 2x       2x 2x               2x 82x 1x         81x                   2x   82x 82x       82x   82x             2x 22x 82x 22x    
/*
 * Copyright 2021 Harness Inc. All rights reserved.
 * Use of this source code is governed by the PolyForm Shield 1.0.0 license
 * that can be found in the licenses directory at the root of this repository, also available at
 * https://polyformproject.org/wp-content/uploads/2020/06/PolyForm-Shield-1.0.0.txt.
 */
 
import React, { ReactElement } from 'react'
import { ThumbnailSelect, ThumbnailSelectProps } from '@wings-software/uicore'
import type { Item } from '@wings-software/uicore/dist/components/ThumbnailSelect/ThumbnailSelect'
import type { PermissionsRequest } from '@rbac/hooks/usePermission'
import type { FeatureProps } from 'framework/featureStore/featureStoreUtil'
import { useFeature } from '@common/hooks/useFeatures'
import { FeatureWarningTooltip } from '@common/components/FeatureWarning/FeatureWarningWithTooltip'
import type { PermissionIdentifier } from '@rbac/interfaces/PermissionIdentifier'
 
export interface RbacFields {
  disabled: boolean
  tooltip?: ReactElement
}
 
const getRbacFieldsForItem = (featureProps?: FeatureProps, featureEnabled?: boolean): RbacFields => {
  if (featureProps?.featureRequest && !featureEnabled) {
    return {
      disabled: true,
      tooltip: <FeatureWarningTooltip featureName={featureProps?.featureRequest.featureName} />
    }
  }
  return {
    disabled: false
  }
}
 
export interface RbacThumbnailItem extends Item {
  permission?: Omit<PermissionsRequest, 'permissions'> & { permission: PermissionIdentifier }
  featureProps?: FeatureProps
}
 
const ApplyRbacOnThumbnailItem = (rbacThumbnailItem: RbacThumbnailItem): Item => {
  // Use the features and permissions hooks to add the 'disabled' and 'tooltip' props to the items
  const { featureProps, ...restItemProps } = rbacThumbnailItem
  const { enabled: featureEnabled } = useFeature({
    featureRequest: featureProps?.featureRequest
  })
 
  const { disabled, tooltip } = getRbacFieldsForItem(featureProps, featureEnabled)
 
  return { ...restItemProps, tooltip, disabled: rbacThumbnailItem.disabled || disabled }
}
 
export interface RbacThumbnailSelectProps extends Omit<ThumbnailSelectProps, 'items'> {
  items: RbacThumbnailItem[]
}
 
export const RbacThumbnailSelect = (props: RbacThumbnailSelectProps) => {
  const { items: itemsWithRbacProps, ...restProps } = props
  const rbacAppliedItems = itemsWithRbacProps.map(itemWithRbacProps => ApplyRbacOnThumbnailItem(itemWithRbacProps))
  return <ThumbnailSelect items={rbacAppliedItems} {...restProps} />
}