Skip to content

Commit fa7e228

Browse files
committed
Merge branch 'main' into feat/aws-code-commit
2 parents a4cd612 + 82dedc0 commit fa7e228

File tree

17 files changed

+490
-25
lines changed

17 files changed

+490
-25
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@devtron-labs/devtron-fe-common-lib",
3-
"version": "0.0.94-beta-2",
3+
"version": "0.0.95",
44
"description": "Supporting common component library",
55
"type": "module",
66
"main": "dist/index.js",

src/Common/Helper.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -738,7 +738,6 @@ export const handleRelativeDateSorting = (dateStringA, dateStringB, sortOrder) =
738738
/**
739739
* Returns a stringified YAML with default indentation & line width
740740
*/
741-
742741
export const YAMLStringify = (obj: object | unknown, option?: object) =>
743742
YAML.stringify(obj, { indent: 2, lineWidth: 0, ...option })
744743

src/Common/SortableTableHeaderCell/SortableTableHeaderCell.tsx

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import { ReactComponent as SortIcon } from '../../Assets/Icon/ic-arrow-up-down.svg'
1818
import { ReactComponent as SortArrowDown } from '../../Assets/Icon/ic-sort-arrow-down.svg'
1919
import { SortingOrder } from '../Constants'
20+
import { noop } from '../Helper'
2021
import { SortableTableHeaderCellProps } from './types'
2122

2223
/**
@@ -39,22 +40,35 @@ const SortableTableHeaderCell = ({
3940
sortOrder,
4041
title,
4142
disabled,
42-
}: SortableTableHeaderCellProps) => (
43-
<button
44-
type="button"
45-
className="dc__transparent p-0 bcn-0 cn-7 flex dc__content-start dc__gap-4 cursor"
46-
onClick={triggerSorting}
47-
disabled={disabled}
48-
>
49-
<span className="dc__uppercase dc__ellipsis-right">{title}</span>
50-
{isSorted ? (
51-
<SortArrowDown
52-
className={`icon-dim-12 mw-12 scn-7 ${sortOrder === SortingOrder.DESC ? 'dc__flip-180' : ''}`}
53-
/>
54-
) : (
55-
<SortIcon className="icon-dim-12 mw-12 scn-7" />
56-
)}
57-
</button>
58-
)
43+
isSortable = true,
44+
}: SortableTableHeaderCellProps) => {
45+
const renderSortIcon = () => {
46+
if (!isSortable) {
47+
return null
48+
}
49+
50+
if (isSorted) {
51+
return (
52+
<SortArrowDown
53+
className={`icon-dim-12 mw-12 scn-7 dc__transition--transform ${sortOrder === SortingOrder.DESC ? 'dc__flip-180' : ''}`}
54+
/>
55+
)
56+
}
57+
58+
return <SortIcon className="icon-dim-12 mw-12 scn-7" />
59+
}
60+
61+
return (
62+
<button
63+
type="button"
64+
className="dc__transparent p-0 bcn-0 cn-7 flex dc__content-start dc__gap-4 dc__select-text"
65+
onClick={isSortable ? triggerSorting : noop}
66+
disabled={disabled}
67+
>
68+
<span className="dc__uppercase dc__ellipsis-right">{title}</span>
69+
{renderSortIcon()}
70+
</button>
71+
)
72+
}
5973

6074
export default SortableTableHeaderCell

src/Common/SortableTableHeaderCell/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,9 @@ export interface SortableTableHeaderCellProps {
3939
* If true, the cell is disabled
4040
*/
4141
disabled: boolean
42+
/**
43+
* If false, the cell acts like normal table header cell
44+
* @default true
45+
*/
46+
isSortable?: boolean
4247
}

src/Shared/API/APIQueuing.ts

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright (c) 2024. Devtron Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import { PromiseAllStatusType, ApiQueuingWithBatchResponseItem, BatchConfigType } from '../types'
18+
19+
const eachCall = (batchConfig, functionCalls, resolve, reject, shouldRejectOnError) => {
20+
const callIndex = batchConfig.lastIndex
21+
Promise.resolve(functionCalls[callIndex]())
22+
.then((result) => {
23+
// eslint-disable-next-line no-param-reassign
24+
batchConfig.results[callIndex] = { status: PromiseAllStatusType.FULFILLED, value: result }
25+
})
26+
.catch((error) => {
27+
// eslint-disable-next-line no-param-reassign
28+
batchConfig.results[callIndex] = { status: PromiseAllStatusType.REJECTED, reason: error }
29+
})
30+
.finally(() => {
31+
if (shouldRejectOnError && batchConfig.results[callIndex].status === PromiseAllStatusType.REJECTED) {
32+
reject(batchConfig.results[callIndex].reason)
33+
return
34+
}
35+
36+
// eslint-disable-next-line no-plusplus, no-param-reassign
37+
batchConfig.completedCalls++
38+
if (batchConfig.lastIndex < functionCalls.length) {
39+
eachCall(batchConfig, functionCalls, resolve, reject, shouldRejectOnError)
40+
// eslint-disable-next-line no-plusplus, no-param-reassign
41+
batchConfig.lastIndex++
42+
} else if (batchConfig.completedCalls === functionCalls.length) {
43+
resolve(batchConfig.results)
44+
}
45+
})
46+
}
47+
48+
/**
49+
* Executes a batch of function calls concurrently with queuing.
50+
* @param functionCalls The array of function calls returning promise to be executed.
51+
* @param batchSize The maximum number of function calls to be executed concurrently. Defaults to the value of `window._env_.API_BATCH_SIZE`.
52+
* @param shouldRejectOnError If set to true, the promise will reject if any of the function calls rejects, i.e, acts like Promise.all else Promise.allSettled . Defaults to false.
53+
* @returns A promise that resolves to a array of objects containing the status and value of the batch execution.
54+
*/
55+
const ApiQueuingWithBatch = <T>(
56+
functionCalls,
57+
httpProtocol: string,
58+
shouldRejectOnError: boolean = false,
59+
batchSize: number = window._env_.API_BATCH_SIZE,
60+
): Promise<ApiQueuingWithBatchResponseItem<T>[]> => {
61+
if (!batchSize || batchSize <= 0) {
62+
// eslint-disable-next-line no-param-reassign
63+
batchSize = ['http/0.9', 'http/1.0', 'http/1.1'].indexOf(httpProtocol) !== -1 ? 5 : 30
64+
}
65+
66+
return new Promise((resolve, reject) => {
67+
if (functionCalls.length === 0) {
68+
resolve([])
69+
}
70+
const batchConfig: BatchConfigType = {
71+
lastIndex: 0,
72+
concurrentCount: batchSize,
73+
results: functionCalls.map(() => null),
74+
completedCalls: 0,
75+
}
76+
for (
77+
let index = 0;
78+
index < batchConfig.concurrentCount && index < functionCalls.length;
79+
index++, batchConfig.lastIndex++
80+
) {
81+
eachCall(batchConfig, functionCalls, resolve, reject, shouldRejectOnError)
82+
}
83+
})
84+
}
85+
86+
export default ApiQueuingWithBatch

src/Shared/API/index.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* Copyright (c) 2024. Devtron Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
export { default as ApiQueuingWithBatch } from './APIQueuing'
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Copyright (c) 2024 Devtron Inc.
3+
* All rights reserved.
4+
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
import { useMemo } from 'react'
19+
import ReactSelect from 'react-select'
20+
import { GenericSectionErrorState } from '../GenericSectionErrorState'
21+
import { commonSelectStyles, GroupHeading, LoadingIndicator } from '../ReactSelect'
22+
import { ClearIndicator, useAsync } from '../../../Common'
23+
import { ENVIRONMENT_SELECTOR_TEXT } from './constants'
24+
import { getEnvironmentsGroupedByCluster } from './service'
25+
import { EnvironmentSelectorProps } from './types'
26+
import { getSelectedOptions } from './utils'
27+
28+
const EnvironmentSelector = <T,>({
29+
handleEnvironmentChange,
30+
selectedEnvironmentsMap,
31+
isMulti,
32+
placeholder = ENVIRONMENT_SELECTOR_TEXT.DEFAULT_PLACEHOLDER,
33+
processOptions,
34+
styles,
35+
isClearable,
36+
autoFocus,
37+
}: EnvironmentSelectorProps<T>) => {
38+
const [areOptionsLoading, allEnvironmentOptions, optionsError, reloadOptions] = useAsync(
39+
getEnvironmentsGroupedByCluster,
40+
)
41+
const options = useMemo(() => {
42+
if (!allEnvironmentOptions) {
43+
return []
44+
}
45+
46+
return processOptions?.(allEnvironmentOptions) || allEnvironmentOptions
47+
}, [allEnvironmentOptions, processOptions])
48+
49+
const value = useMemo(() => {
50+
const selectedOptions = getSelectedOptions<T>(selectedEnvironmentsMap, options)
51+
if (isMulti) {
52+
return selectedOptions
53+
}
54+
55+
return selectedOptions?.[0] || null
56+
}, [selectedEnvironmentsMap, options, isMulti])
57+
58+
const renderNoEnvironmentMessage = () => {
59+
if (optionsError) {
60+
return <GenericSectionErrorState withBorder reload={reloadOptions} />
61+
}
62+
63+
return (
64+
<p className="m-0 cn-7 fs-13 fw-4 lh-20 py-6 px-8">{ENVIRONMENT_SELECTOR_TEXT.NO_ENVIRONMENTS_AVAILABLE}</p>
65+
)
66+
}
67+
68+
return (
69+
<ReactSelect
70+
isMulti={isMulti}
71+
options={options}
72+
value={value}
73+
onChange={handleEnvironmentChange}
74+
isSearchable
75+
isClearable={isClearable}
76+
isLoading={areOptionsLoading}
77+
placeholder={placeholder}
78+
components={{
79+
IndicatorSeparator: null,
80+
LoadingIndicator,
81+
NoOptionsMessage: renderNoEnvironmentMessage,
82+
GroupHeading,
83+
ClearIndicator,
84+
}}
85+
styles={styles ?? commonSelectStyles}
86+
backspaceRemovesValue={false}
87+
autoFocus={autoFocus}
88+
/>
89+
)
90+
}
91+
92+
export default EnvironmentSelector
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright (c) 2024 Devtron Inc.
3+
* All rights reserved.
4+
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
export const ENVIRONMENT_SELECTOR_TEXT = {
19+
DEFAULT_PLACEHOLDER: 'Select Environment',
20+
GROUP_HEADING_PREFIX: 'Cluster:',
21+
NO_ENVIRONMENTS_AVAILABLE: 'No environments available',
22+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* Copyright (c) 2024 Devtron Inc.
3+
* All rights reserved.
4+
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
export { default as EnvironmentSelector } from './EnvironmentSelector.component'

0 commit comments

Comments
 (0)