@devtron-labs/devtron-fe-common-lib 0.0.60-beta-6 → 0.0.61-beta-1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (65) hide show
  1. package/dist/Common/Api.d.ts +9 -0
  2. package/dist/Common/Constants.d.ts +14 -0
  3. package/dist/Common/DraggableWrapper/types.d.ts +4 -1
  4. package/dist/Common/Helper.d.ts +1 -4
  5. package/dist/Common/Hooks/index.d.ts +1 -0
  6. package/dist/Common/Hooks/useUrlFilters/constants.d.ts +8 -0
  7. package/dist/Common/Hooks/useUrlFilters/index.d.ts +2 -0
  8. package/dist/Common/Hooks/useUrlFilters/types.d.ts +19 -0
  9. package/dist/Common/Hooks/useUrlFilters/useUrlFilters.d.ts +20 -0
  10. package/dist/Common/SearchBar/SearchBar.component.d.ts +1 -1
  11. package/dist/Common/SearchBar/types.d.ts +0 -4
  12. package/dist/Common/SortableTableHeaderCell/SortableTableHeaderCell.d.ts +18 -0
  13. package/dist/Common/SortableTableHeaderCell/index.d.ts +2 -0
  14. package/dist/Common/SortableTableHeaderCell/types.d.ts +25 -0
  15. package/dist/Common/Types.d.ts +16 -3
  16. package/dist/Common/index.d.ts +1 -0
  17. package/dist/Pages/GlobalConfigurations/Authorization/constants.d.ts +15 -0
  18. package/dist/Pages/GlobalConfigurations/Authorization/index.d.ts +1 -0
  19. package/dist/Pages/GlobalConfigurations/index.d.ts +1 -0
  20. package/dist/Pages/index.d.ts +1 -0
  21. package/dist/Shared/Components/BulkSelection/BulkSelection.d.ts +4 -0
  22. package/dist/Shared/Components/BulkSelection/BulkSelectionDropdownItems.d.ts +4 -0
  23. package/dist/Shared/Components/BulkSelection/BulkSelectionProvider.d.ts +4 -0
  24. package/dist/Shared/Components/BulkSelection/constants.d.ts +9 -0
  25. package/dist/Shared/Components/BulkSelection/index.d.ts +4 -0
  26. package/dist/Shared/Components/BulkSelection/types.d.ts +54 -0
  27. package/dist/Shared/Components/index.d.ts +1 -0
  28. package/dist/Shared/index.d.ts +1 -0
  29. package/dist/index.d.ts +3 -1
  30. package/dist/index.js +654 -175
  31. package/package.json +1 -1
  32. package/src/Assets/Icon/ic-arrow-up-down.svg +3 -0
  33. package/src/Assets/Icon/ic-check-all.svg +5 -0
  34. package/src/Assets/Icon/ic-check-square.svg +4 -0
  35. package/src/Assets/Icon/ic-close.svg +9 -0
  36. package/src/Assets/Icon/ic-sort-arrow-down.svg +3 -0
  37. package/src/Common/Api.ts +21 -0
  38. package/src/Common/Constants.ts +17 -1
  39. package/src/Common/DraggableWrapper/types.ts +4 -1
  40. package/src/Common/Helper.tsx +16 -30
  41. package/src/Common/Hooks/index.ts +1 -0
  42. package/src/Common/Hooks/useUrlFilters/constants.ts +9 -0
  43. package/src/Common/Hooks/useUrlFilters/index.ts +2 -0
  44. package/src/Common/Hooks/useUrlFilters/types.ts +21 -0
  45. package/src/Common/Hooks/useUrlFilters/useUrlFilters.ts +121 -0
  46. package/src/Common/SearchBar/SearchBar.component.tsx +0 -2
  47. package/src/Common/SearchBar/types.ts +0 -4
  48. package/src/Common/SortableTableHeaderCell/SortableTableHeaderCell.tsx +44 -0
  49. package/src/Common/SortableTableHeaderCell/index.ts +2 -0
  50. package/src/Common/SortableTableHeaderCell/types.ts +26 -0
  51. package/src/Common/Types.ts +16 -2
  52. package/src/Common/index.ts +1 -0
  53. package/src/Pages/GlobalConfigurations/Authorization/constants.ts +16 -0
  54. package/src/Pages/GlobalConfigurations/Authorization/index.ts +1 -0
  55. package/src/Pages/GlobalConfigurations/index.ts +1 -0
  56. package/src/Pages/index.ts +1 -0
  57. package/src/Shared/Components/BulkSelection/BulkSelection.tsx +77 -0
  58. package/src/Shared/Components/BulkSelection/BulkSelectionDropdownItems.tsx +31 -0
  59. package/src/Shared/Components/BulkSelection/BulkSelectionProvider.tsx +163 -0
  60. package/src/Shared/Components/BulkSelection/constants.tsx +13 -0
  61. package/src/Shared/Components/BulkSelection/index.ts +4 -0
  62. package/src/Shared/Components/BulkSelection/types.tsx +65 -0
  63. package/src/Shared/Components/index.ts +1 -0
  64. package/src/Shared/index.ts +1 -0
  65. package/src/index.ts +3 -1
@@ -0,0 +1,163 @@
1
+ import { createContext, useContext, useMemo, useState } from 'react'
2
+ import { BULK_SELECTION_CONTEXT_ERROR, SELECT_ALL_ACROSS_PAGES_LOCATOR, getInvalidActionMessage } from './constants'
3
+ import {
4
+ BulkSelectionEvents,
5
+ GetBulkSelectionCheckboxValuesType,
6
+ HandleBulkSelectionType,
7
+ SelectAllDialogStatus,
8
+ UseBulkSelectionProps,
9
+ UseBulkSelectionReturnType,
10
+ } from './types'
11
+ import { CHECKBOX_VALUE, noop } from '../../../Common'
12
+
13
+ // giving type any here since not exporting this context, rather using it through useBulkSelection hook which is typed
14
+ const BulkSelectionContext = createContext<UseBulkSelectionReturnType<any>>({
15
+ selectedIdentifiers: {},
16
+ handleBulkSelection: noop,
17
+ isChecked: false,
18
+ checkboxValue: CHECKBOX_VALUE.CHECKED,
19
+ })
20
+
21
+ export const useBulkSelection = <T,>() => {
22
+ const context = useContext<UseBulkSelectionReturnType<T>>(BulkSelectionContext)
23
+ if (!context) {
24
+ throw new Error(BULK_SELECTION_CONTEXT_ERROR)
25
+ }
26
+ return context
27
+ }
28
+
29
+ export const BulkSelectionProvider = <T,>({
30
+ children,
31
+ identifiers,
32
+ getSelectAllDialogStatus,
33
+ }: UseBulkSelectionProps<T>) => {
34
+ const [selectedIdentifiers, setSelectedIdentifiers] = useState<T>({} as T)
35
+
36
+ const setIdentifiersAfterClear = (newIdentifiers: T, selectedIds: (number | string)[]) => {
37
+ const _newIdentifiers = JSON.parse(JSON.stringify(newIdentifiers))
38
+ selectedIds.forEach((id) => {
39
+ delete _newIdentifiers[id]
40
+ })
41
+ setSelectedIdentifiers(_newIdentifiers)
42
+ }
43
+
44
+ const setIdentifiersAfterPageSelection = (baseObject: T) => {
45
+ const _selectedIdentifiers = JSON.parse(JSON.stringify(selectedIdentifiers))
46
+ // removing bulk selection across pages if present
47
+ if (_selectedIdentifiers[SELECT_ALL_ACROSS_PAGES_LOCATOR]) {
48
+ delete _selectedIdentifiers[SELECT_ALL_ACROSS_PAGES_LOCATOR]
49
+ }
50
+
51
+ setSelectedIdentifiers({
52
+ ..._selectedIdentifiers,
53
+ ...baseObject,
54
+ })
55
+ }
56
+
57
+ const handleBulkSelection = ({ action, data }: HandleBulkSelectionType<T>) => {
58
+ const selectedIds = data?.identifierIds ?? []
59
+ const identifierObject = data?.identifierObject ?? {}
60
+
61
+ switch (action) {
62
+ case BulkSelectionEvents.CLEAR_ALL_SELECTIONS:
63
+ setSelectedIdentifiers({} as T)
64
+ break
65
+
66
+ case BulkSelectionEvents.CLEAR_IDENTIFIERS_AFTER_ACROSS_SELECTION: {
67
+ setIdentifiersAfterClear(identifiers, selectedIds)
68
+ break
69
+ }
70
+
71
+ case BulkSelectionEvents.CLEAR_IDENTIFIERS: {
72
+ setIdentifiersAfterClear(selectedIdentifiers, selectedIds)
73
+ break
74
+ }
75
+
76
+ case BulkSelectionEvents.SELECT_ALL_ACROSS_PAGES: {
77
+ if (selectedIdentifiers[SELECT_ALL_ACROSS_PAGES_LOCATOR]) {
78
+ return
79
+ }
80
+ const selectedIdentifiersLength = Object.keys(selectedIdentifiers).length
81
+ // This is for showing a modal dialog if applicable to confirm if bulk selection is to be applied
82
+ // Do note that the 'getSelectAllDialogStatus' should be called after the length check explicitly
83
+ // otherwise the modal will open each time
84
+ if (selectedIdentifiersLength > 0 && getSelectAllDialogStatus() === SelectAllDialogStatus.OPEN) {
85
+ return
86
+ }
87
+
88
+ setSelectedIdentifiers({
89
+ [SELECT_ALL_ACROSS_PAGES_LOCATOR]: true,
90
+ } as unknown as T)
91
+ break
92
+ }
93
+
94
+ case BulkSelectionEvents.CLEAR_SELECTIONS_AND_SELECT_ALL_ACROSS_PAGES:
95
+ setSelectedIdentifiers({
96
+ [SELECT_ALL_ACROSS_PAGES_LOCATOR]: true,
97
+ } as unknown as T)
98
+ break
99
+
100
+ case BulkSelectionEvents.SELECT_ALL_ON_PAGE: {
101
+ setIdentifiersAfterPageSelection(identifiers)
102
+ break
103
+ }
104
+
105
+ case BulkSelectionEvents.SELECT_IDENTIFIER: {
106
+ setIdentifiersAfterPageSelection(identifierObject as T)
107
+ break
108
+ }
109
+
110
+ default:
111
+ throw new Error(getInvalidActionMessage(action))
112
+ }
113
+ }
114
+
115
+ const getBulkSelectionCheckboxValues = (): GetBulkSelectionCheckboxValuesType => {
116
+ const selectedIdentifiersArray = Object.keys(selectedIdentifiers)
117
+ if (selectedIdentifiersArray.length === 0) {
118
+ return {
119
+ isChecked: false,
120
+ checkboxValue: CHECKBOX_VALUE.CHECKED,
121
+ }
122
+ }
123
+
124
+ // if selectedIdentifiers contains select all across pages locator then it means all are selected
125
+ if (selectedIdentifiers[SELECT_ALL_ACROSS_PAGES_LOCATOR]) {
126
+ return {
127
+ isChecked: true,
128
+ checkboxValue: CHECKBOX_VALUE.BULK_CHECKED,
129
+ }
130
+ }
131
+
132
+ // if all the identifiers are selected then CHECKED else intermediate
133
+ const areAllPresentIdentifiersSelected = Object.keys(identifiers).every(
134
+ (identifierId) => selectedIdentifiers[identifierId],
135
+ )
136
+
137
+ if (areAllPresentIdentifiersSelected) {
138
+ return {
139
+ isChecked: true,
140
+ checkboxValue: CHECKBOX_VALUE.CHECKED,
141
+ }
142
+ }
143
+
144
+ return {
145
+ isChecked: true,
146
+ checkboxValue: CHECKBOX_VALUE.INTERMEDIATE,
147
+ }
148
+ }
149
+
150
+ const { isChecked, checkboxValue } = getBulkSelectionCheckboxValues()
151
+
152
+ const value = useMemo(
153
+ () => ({
154
+ selectedIdentifiers,
155
+ handleBulkSelection,
156
+ isChecked,
157
+ checkboxValue,
158
+ }),
159
+ [selectedIdentifiers, handleBulkSelection, isChecked, checkboxValue],
160
+ )
161
+
162
+ return <BulkSelectionContext.Provider value={value}>{children}</BulkSelectionContext.Provider>
163
+ }
@@ -0,0 +1,13 @@
1
+ import { BulkSelectionEvents } from './types'
2
+
3
+ export const BULK_DROPDOWN_TEST_ID = 'bulk-selection-pop-up-menu' as const
4
+ export const BulkSelectionOptionsLabels = {
5
+ [BulkSelectionEvents.SELECT_ALL_ON_PAGE]: 'All on this page',
6
+ [BulkSelectionEvents.SELECT_ALL_ACROSS_PAGES]: 'All across pages',
7
+ [BulkSelectionEvents.CLEAR_ALL_SELECTIONS]: 'Clear selection',
8
+ } as const
9
+
10
+ // Considering application can not be named as *
11
+ export const SELECT_ALL_ACROSS_PAGES_LOCATOR = '*' as const
12
+ export const getInvalidActionMessage = (action: string) => `Invalid action ${action} passed to useBulkSelection`
13
+ export const BULK_SELECTION_CONTEXT_ERROR = 'useBulkSelection must be used within BulkSelectionContext'
@@ -0,0 +1,4 @@
1
+ export { default as BulkSelection } from './BulkSelection'
2
+ export * from './BulkSelectionProvider'
3
+ export * from './types'
4
+ export * from './constants'
@@ -0,0 +1,65 @@
1
+ import { CHECKBOX_VALUE } from '../../../Common'
2
+
3
+ export enum BulkSelectionEvents {
4
+ CLEAR_ALL_SELECTIONS = 'CLEAR_ALL_SELECTIONS',
5
+ CLEAR_IDENTIFIERS = 'CLEAR_IDENTIFIERS',
6
+ SELECT_ALL_ACROSS_PAGES = 'SELECT_ALL_ACROSS_PAGES',
7
+ SELECT_ALL_ON_PAGE = 'SELECT_ALL_ON_PAGE',
8
+ SELECT_IDENTIFIER = 'SELECT_IDENTIFIER',
9
+ CLEAR_IDENTIFIERS_AFTER_ACROSS_SELECTION = 'CLEAR_IDENTIFIERS_AFTER_ACROSS_SELECTION',
10
+ CLEAR_SELECTIONS_AND_SELECT_ALL_ACROSS_PAGES = 'CLEAR_SELECTIONS_AND_SELECT_ALL_ACROSS_PAGES',
11
+ }
12
+
13
+ export interface HandleBulkSelectionType<T> {
14
+ action: BulkSelectionEvents
15
+ data?: {
16
+ identifierIds?: (number | string)[]
17
+ identifierObject?: T
18
+ }
19
+ }
20
+
21
+ export interface GetBulkSelectionCheckboxValuesType {
22
+ isChecked: boolean
23
+ checkboxValue: CHECKBOX_VALUE
24
+ }
25
+
26
+ export interface UseBulkSelectionReturnType<T> extends GetBulkSelectionCheckboxValuesType {
27
+ selectedIdentifiers: T
28
+ handleBulkSelection: ({ action, data }: HandleBulkSelectionType<T>) => void
29
+ }
30
+
31
+ export interface BulkSelectionProps {
32
+ showPagination: boolean
33
+ }
34
+
35
+ export interface BulkSelectionDropdownItemsType {
36
+ locator: BulkSelectionEvents
37
+ label: string
38
+ isSelected: boolean
39
+ icon: React.FunctionComponent<React.SVGProps<SVGSVGElement>>
40
+ }
41
+
42
+ export interface BulkSelectionDropdownItemsProps<T>
43
+ extends BulkSelectionDropdownItemsType,
44
+ Pick<UseBulkSelectionReturnType<T>, 'handleBulkSelection'> {}
45
+
46
+ export enum SelectAllDialogStatus {
47
+ OPEN = 'OPEN',
48
+ CLOSED = 'CLOSED',
49
+ }
50
+
51
+ // This type is intended to be used with UseBulkSelectionProps, e.g, UseBulkSelectionProps<BulkSelectionIdentifiersType<boolean>>
52
+ export type BulkSelectionIdentifiersType<T> = Record<string | number, T>
53
+
54
+ export interface UseBulkSelectionProps<T> {
55
+ /**
56
+ * Response from API, assuming structure to be array of objects with key and values
57
+ * This will the given ids on current page
58
+ */
59
+ identifiers: T
60
+ /**
61
+ * Act as buffer between select all across pages and select all on page state
62
+ */
63
+ getSelectAllDialogStatus: () => SelectAllDialogStatus
64
+ children?: React.ReactNode
65
+ }
@@ -0,0 +1 @@
1
+ export * from './BulkSelection'
@@ -0,0 +1 @@
1
+ export * from './Components'
package/src/index.ts CHANGED
@@ -1 +1,3 @@
1
- export * from "./Common"
1
+ export * from './Common'
2
+ export * from './Pages'
3
+ export * from './Shared'