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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | 396x 396x 396x 396x 396x 396x 396x 396x 396x 2870x 2870x 2870x 2870x 2811x 2811x 5x 2806x 12x 2794x 2870x 59x 2811x 2811x 2811x 396x | /*
* Copyright 2022 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 { Button as CoreButton, ButtonProps as CoreButtonProps } from '@harness/uicore'
import { PopoverInteractionKind, Classes } from '@blueprintjs/core'
import RBACTooltip from '@rbac/components/RBACTooltip/RBACTooltip'
import { usePermission, PermissionsRequest } from '@rbac/hooks/usePermission'
import { useFeatures } from '@common/hooks/useFeatures'
import type { PermissionIdentifier } from '@rbac/interfaces/PermissionIdentifier'
import type { FeaturesProps } from 'framework/featureStore/featureStoreUtil'
import FeatureTooltip from '@common/components/FeatureWarning/FeatureTooltip'
import { getPermissionRequestFromProps } from '@rbac/utils/utils'
export interface ButtonProps extends CoreButtonProps {
permission?: Omit<PermissionsRequest, 'permissions'> & { permission: PermissionIdentifier }
featuresProps?: FeaturesProps & { warningMessage?: string }
}
export interface BtnProps {
disabled: boolean
tooltip?: ReactElement
darkTheme?: boolean
}
const RbacButton: React.FC<ButtonProps> = ({
permission: permissionRequest,
featuresProps,
tooltipProps,
...restProps
}) => {
const [canDoAction] = usePermission(getPermissionRequestFromProps(permissionRequest), [permissionRequest])
const { featuresRequest } = featuresProps || {}
const { features } = useFeatures({
featuresRequest
})
const featureDisabled = [...features.values()].find(feature => !feature.enabled) !== undefined
function getBtnProps(): BtnProps {
// if permission check override the priorirty
Iif (featuresProps?.isPermissionPrioritized && permissionRequest && !canDoAction) {
return {
disabled: true,
tooltip: (
<RBACTooltip
permission={permissionRequest.permission}
resourceType={permissionRequest.resource.resourceType}
resourceScope={permissionRequest.resourceScope}
/>
)
}
}
// feature check by default take priority
if (featuresProps?.featuresRequest && featureDisabled) {
return {
darkTheme: true,
disabled: true,
tooltip: <FeatureTooltip features={features} warningMessage={featuresProps.warningMessage} />
}
}
// permission check
if (permissionRequest && !canDoAction) {
return {
disabled: true,
tooltip: (
<RBACTooltip
permission={permissionRequest.permission}
resourceType={permissionRequest.resource.resourceType}
resourceScope={permissionRequest.resourceScope}
/>
)
}
}
return {
disabled: false
}
}
if (!featuresProps?.featuresRequest && !permissionRequest) {
return <CoreButton {...restProps} tooltipProps={tooltipProps} />
}
const btnProps = getBtnProps()
const { disabled, tooltip, darkTheme } = btnProps
return (
<CoreButton
{...restProps}
disabled={restProps.disabled || disabled}
tooltip={disabled ? tooltip : restProps.tooltip ? restProps.tooltip : undefined}
tooltipProps={
disabled
? {
hoverCloseDelay: 50,
className: darkTheme ? Classes.DARK : undefined,
interactionKind: featureDisabled ? PopoverInteractionKind.HOVER : PopoverInteractionKind.HOVER_TARGET_ONLY
}
: tooltipProps
}
/>
)
}
export default RbacButton
|