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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 | 1071x 1071x 1071x 1071x 1071x 1071x 1071x 1071x 1071x 1071x 1071x 1x 1071x 5311x 1071x 1071x 1071x 35x 34x 34x 34x 34x 34x 34x 34x 34x 11x 9x 9x 9x 9x 9x 34x 11x 11x 33x 33x 33x 11x 34x 14x 4x 4x 4x 34x 11x 4x 4x 4x 4x 4x 6x 6x 1x 5x 1x 4x 21x 21x 21x 21x 34x 34x 5x 5x 5x 5x 5x 5x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 1x 1x 1x 1x 20x 10x 10x 10x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 39x 39x 39x 39x 5x 9x 9x 11x 11x 14x 37x 37x 37x 37x 37x 24x 13x 10x 10x 10x 34x | /* * 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, { createContext, useContext, useState, useEffect } from 'react' import { isEmpty, isEqual } from 'lodash-es' import produce from 'immer' import { useParams } from 'react-router-dom' import { useToaster } from '@harness/uicore' import { useStrings } from 'framework/strings' import type { AccountPathProps } from '@common/interfaces/RouteInterfaces' import { useGetEnabledFeatureRestrictionDetailByAccountId, useGetFeatureRestrictionDetail, useGetAllFeatureRestrictionMetadata, FeatureRestrictionDetailRequestDTO, ModuleLicenseDTO } from 'services/cd-ng' import type { RestrictionType } from '@common/constants/SubscriptionTypes' import { Editions } from '@common/constants/SubscriptionTypes' import { LICENSE_STATE_VALUES } from 'framework/LicenseStore/licenseStoreUtil' import type { FeatureIdentifier } from './FeatureIdentifier' import type { FeatureDetail, FeatureMetaData, ModuleType, FeatureRequest, FeaturesRequest, FeatureRequestOptions, CheckFeatureReturn } from './featureStoreUtil' type Features = Map<FeatureIdentifier, FeatureDetail> type FeatureMap = Map<FeatureIdentifier, FeatureMetaData> interface GetHighestEditionProps { licenseInformation?: { [key: string]: ModuleLicenseDTO } | Record<string, undefined> licenseState: { [key: string]: LICENSE_STATE_VALUES } } interface GetEditionProps extends GetHighestEditionProps { moduleType: ModuleType isCommunity: boolean } interface GetRestrictionTypeProps extends GetHighestEditionProps { featureRequest?: FeatureRequest isCommunity: boolean } export interface FeaturesContextProps { // features only cache enabled features features: Features featureMap: FeatureMap getEdition: (props: GetEditionProps) => Editions | undefined requestFeatures: (featureRequest: FeatureRequest | FeaturesRequest, options?: FeatureRequestOptions) => void checkFeature: (featureName: FeatureIdentifier) => CheckFeatureReturn requestLimitFeature: (featureRequest: FeatureRequest) => void checkLimitFeature: (featureName: FeatureIdentifier) => CheckFeatureReturn getRestrictionType: (props: GetRestrictionTypeProps) => RestrictionType | undefined getHighestEdition: (props: GetHighestEditionProps) => Editions } const defaultReturn = { enabled: true } export const FeaturesContext = createContext<FeaturesContextProps>({ // features caches features which restrictionType is AVAILABILITY and enabled features: new Map<FeatureIdentifier, FeatureDetail>(), // featureMap caches all feature metadata, featureName: { edition, restrictionType } featureMap: new Map<FeatureIdentifier, FeatureMetaData>(), getEdition: () => { return undefined }, requestFeatures: () => void 0, checkFeature: () => { return defaultReturn }, requestLimitFeature: () => void 0, checkLimitFeature: () => { return defaultReturn }, getRestrictionType: () => { return undefined }, getHighestEdition: () => { return Editions.FREE } }) export function useFeaturesContext(): FeaturesContextProps { return useContext(FeaturesContext) } let pendingAvailRequests: (FeatureRequest | FeaturesRequest)[] = [] let pendingLimitRequests: FeatureRequest[] = [] export function FeaturesProvider(props: React.PropsWithChildren<unknown>): React.ReactElement { const [features, setFeatures] = useState<Features>(new Map<FeatureIdentifier, FeatureDetail>()) const [featureMap, setFeatureMap] = useState<FeatureMap>(new Map<FeatureIdentifier, FeatureMetaData>()) const [hasErr, setHasErr] = useState<boolean>(false) const { showError } = useToaster() const { getString } = useStrings() const { accountId } = useParams<AccountPathProps>() const { data: enabledFeatureList, refetch: getEnabledFeatures, error: gettingEnabledFeaturesError } = useGetEnabledFeatureRestrictionDetailByAccountId({ queryParams: { accountIdentifier: accountId }, lazy: true }) const { data: metadata, error: gettingFeatureMetadataError } = useGetAllFeatureRestrictionMetadata({ queryParams: { accountIdentifier: accountId } }) useEffect(() => { if (!isEmpty(enabledFeatureList)) { const list = enabledFeatureList?.data?.reduce((acc, curr) => { Eif (curr?.name) { acc?.set(curr.name as FeatureIdentifier, { featureName: curr.name as FeatureIdentifier, enabled: !!curr.allowed, moduleType: curr.moduleType }) } return acc }, new Map<FeatureIdentifier, FeatureDetail>()) list && setFeatures(list) } }, [enabledFeatureList]) useEffect(() => { Eif (!isEmpty(metadata)) { const list = metadata?.data?.reduce((acc, curr) => { Eif (curr?.name && curr?.restrictionMetadata) { acc?.set(curr.name as FeatureIdentifier, { moduleType: curr.moduleType as ModuleType, restrictionMetadataMap: curr.restrictionMetadata }) } return acc }, new Map<FeatureIdentifier, FeatureMetaData>()) list && setFeatureMap(list) } }, [metadata]) useEffect(() => { if (gettingEnabledFeaturesError) { // set err flag to true setHasErr(true) showError(gettingEnabledFeaturesError.message || getString('somethingWentWrong')) // reset the queque pendingAvailRequests = [] } // eslint-disable-next-line react-hooks/exhaustive-deps }, [gettingEnabledFeaturesError]) useEffect(() => { Iif (gettingFeatureMetadataError) { showError(gettingFeatureMetadataError.message || getString('somethingWentWrong')) } // eslint-disable-next-line react-hooks/exhaustive-deps }, [gettingFeatureMetadataError]) async function getEnabledFeatureList(featureRequest: FeatureRequest | FeaturesRequest): Promise<void> { // check if this request is already queued Eif (pendingAvailRequests.length === 0) { pendingAvailRequests.push(featureRequest) } else { return } await getEnabledFeatures({}) // reset the queque pendingAvailRequests = [] // reset hasErr setHasErr(false) } // this function is called from `useFeature` hook to cache all enabled AVAILABILITY features async function requestFeatures( featureRequest: FeatureRequest | FeaturesRequest, options?: FeatureRequestOptions ): Promise<void> { const { skipCache = false, skipCondition } = options || {} // exit early if we already fetched features before // disabling this will disable caching, because it will make a fresh request and update in the store if (!skipCache && features.size > 0) { return } // exit early if user has defined a skipCondition and if it returns true if (skipCondition && skipCondition(featureRequest) === true) { return } getEnabledFeatureList(featureRequest) } function checkFeature(featureName: FeatureIdentifier): CheckFeatureReturn { const featureDetail = features.get(featureName) // absence of featureName means feature disabled // api call fails by default set all features to be true const enabled = !!featureDetail?.enabled || hasErr const moduleType = featureDetail?.moduleType return { enabled, featureDetail: { ...featureDetail, featureName, enabled, moduleType, apiFail: hasErr } } } const [featureDetailMap, setFeatureDetailMap] = useState<Map<FeatureIdentifier, FeatureDetail>>( new Map<FeatureIdentifier, FeatureDetail>() ) const { mutate: getFeatureDetails } = useGetFeatureRestrictionDetail({ queryParams: { accountIdentifier: accountId } }) // limit feature request async function requestLimitFeature(featureRequest: FeatureRequest): Promise<void> { const { featureName } = featureRequest // when feature metadata call fails, feature is taken as limit one and comes here Iif (gettingFeatureMetadataError) { return } // check if this request is already queued Eif (!pendingLimitRequests.find(req => isEqual(req, featureRequest))) { pendingLimitRequests.push(featureRequest) } else { return } try { const res = await getFeatureDetails({ name: featureName as FeatureRestrictionDetailRequestDTO['name'] }) // remove the request from queque pendingLimitRequests = pendingLimitRequests.filter(request => request !== featureRequest) const allowed = res?.data?.allowed const restriction = res?.data?.restriction const enabled = !!allowed let limit: number, count: number const apiFail = false const moduleType = res?.data?.moduleType Eif (restriction) { limit = restriction.limit count = restriction.count } setFeatureDetailMap(oldMap => { return produce(oldMap, draft => { // update current feature in the map draft.set(featureName, { featureName, enabled, limit, count, apiFail, moduleType }) }) }) } catch (ex) { showError(ex.data?.message || getString('somethingWentWrong')) // remove the request from queque pendingLimitRequests = pendingLimitRequests.filter(request => request !== featureRequest) setFeatureDetailMap(oldMap => { return produce(oldMap, draft => { // update current feature in the map draft.set(featureName, { featureName, enabled: true, apiFail: true, moduleType: undefined }) }) }) } } function compareEditions(edition1: Editions, edition2: Editions): Editions { if (edition1 === Editions.ENTERPRISE || edition2 === Editions.ENTERPRISE) { return Editions.ENTERPRISE } Eif (edition1 === Editions.TEAM || edition2 === Editions.TEAM) { return Editions.TEAM } return Editions.FREE } // this is to return highest edition from all module licenses: FREE, COMMUNITY < TEAM < ENTERPRISE // if no module licenses, then return FREE as default function getHighestEdition({ licenseInformation, licenseState }: GetHighestEditionProps): Editions { let edition = Editions.FREE const { CI_LICENSE_STATE, CD_LICENSE_STATE, FF_LICENSE_STATE, CCM_LICENSE_STATE } = licenseState Eif (CI_LICENSE_STATE === LICENSE_STATE_VALUES.ACTIVE) { edition = compareEditions(licenseInformation?.['CI']?.edition as Editions, edition) } Eif (FF_LICENSE_STATE === LICENSE_STATE_VALUES.ACTIVE) { edition = compareEditions(licenseInformation?.['CF']?.edition as Editions, edition) } Eif (CD_LICENSE_STATE === LICENSE_STATE_VALUES.ACTIVE) { edition = compareEditions(licenseInformation?.['CD']?.edition as Editions, edition) } Eif (CCM_LICENSE_STATE === LICENSE_STATE_VALUES.ACTIVE) { edition = compareEditions(licenseInformation?.['CE']?.edition as Editions, edition) } return edition } function getEdition({ moduleType, licenseInformation, licenseState, isCommunity }: GetEditionProps): Editions | undefined { // if no license available, reture Free for default Iif (licenseInformation === undefined || isEmpty(licenseInformation)) { return Editions.FREE } Iif (isCommunity) { return Editions.COMMUNITY } const { CI_LICENSE_STATE, CD_LICENSE_STATE, FF_LICENSE_STATE, CCM_LICENSE_STATE } = licenseState switch (moduleType) { case 'CORE': { return getHighestEdition({ licenseInformation, licenseState }) } case 'CI': { if (CI_LICENSE_STATE === LICENSE_STATE_VALUES.ACTIVE) { return (licenseInformation['CI']?.edition as Editions) || Editions.FREE } break } case 'CD': { Eif (CD_LICENSE_STATE === LICENSE_STATE_VALUES.ACTIVE) { return (licenseInformation['CD']?.edition as Editions) || Editions.FREE } break } case 'CF': { Eif (FF_LICENSE_STATE === LICENSE_STATE_VALUES.ACTIVE) { return (licenseInformation['CF']?.edition as Editions) || Editions.FREE } break } case 'CE': { if (CCM_LICENSE_STATE === LICENSE_STATE_VALUES.ACTIVE) { return (licenseInformation['CE']?.edition as Editions) || Editions.FREE } break } } return undefined } // find restrictionType by featureName and edition function getRestrictionType({ featureRequest, licenseInformation, licenseState, isCommunity }: GetRestrictionTypeProps): RestrictionType | undefined { Eif (featureRequest) { const featureMetadata = featureMap.get(featureRequest.featureName) const { moduleType, restrictionMetadataMap } = featureMetadata || {} const edition = getEdition({ moduleType, licenseInformation, licenseState, isCommunity }) if (edition) { return restrictionMetadataMap?.[edition]?.restrictionType as RestrictionType } } return undefined } function checkLimitFeature(featureName: FeatureIdentifier): CheckFeatureReturn { // api call fails by default set feature to be true const featureDetail = featureDetailMap.get(featureName) const enabled = featureDetail?.apiFail || !!featureDetail?.enabled return { enabled, featureDetail } } return ( <FeaturesContext.Provider value={{ features, featureMap, getEdition, requestFeatures, requestLimitFeature, checkLimitFeature, checkFeature, getRestrictionType, getHighestEdition }} > {props.children} </FeaturesContext.Provider> ) } |