Skip to content

Commit 6060512

Browse files
committed
feat: make pluginList prop type generic
1 parent 47427e8 commit 6060512

File tree

5 files changed

+100
-44
lines changed

5 files changed

+100
-44
lines changed

src/Shared/Components/FilterChips/FilterChips.component.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,14 @@ const FilterChip = ({
3434
const valueToDisplay = getFormattedValue(label, value) ?? value
3535

3636
return (
37-
labelToDisplay &&
3837
valueToDisplay && (
3938
<div className="flexbox flex-align-center br-4 dc__border dc__bg-n50 pl-6 pr-6 pt-2 pb-2 dc__user-select-none h-24 dc__gap-6 fs-12 lh-20 cn-9 fw-4 dc__ellipsis-right">
40-
<span className="fw-6 dc__capitalize">{labelToDisplay}</span>
41-
<span className="dc__divider h-24" />
39+
{labelToDisplay && (
40+
<>
41+
<span className="fw-6 dc__capitalize">{labelToDisplay}</span>
42+
<span className="dc__divider h-24" />
43+
</>
44+
)}
4245
<span className="dc__ellipsis-right">{valueToDisplay}</span>
4346
<button
4447
type="button"

src/Shared/Components/FilterChips/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export interface FilterChipProps {
2020
/**
2121
* Filter label
2222
*/
23-
label: string
23+
label?: string
2424
/**
2525
* Corresponding value of the filter
2626
*/

src/Shared/Components/Plugin/PluginListContainer.tsx

Lines changed: 65 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,65 @@
1-
import { useEffect, useMemo } from 'react'
1+
import { useEffect, useMemo, useState } from 'react'
22
import { useParams } from 'react-router'
33
import { APIResponseHandler } from '../APIResponseHandler'
44
import PluginTagSelect from './PluginTagSelect'
5+
import PluginList from './PluginList'
6+
import { FilterChips } from '../FilterChips'
57
import { SearchBar, useAsync } from '../../../Common'
68
import { getAvailablePluginTags, getPluginStoreData } from './service'
7-
import { PluginListParamsType, PluginListContainerProps, PluginListProps } from './types'
9+
import {
10+
PluginListParamsType,
11+
PluginListContainerProps,
12+
PluginListProps,
13+
PluginListFiltersType,
14+
PluginListItemType,
15+
} from './types'
816
import { ReactComponent as ICCross } from '../../../Assets/Icon/ic-cross.svg'
917
import { ReactComponent as ICVisibility } from '../../../Assets/Icon/ic-visibility-on.svg'
10-
import PluginList from './PluginList'
18+
import { DEFAULT_PLUGIN_LIST_FILTERS } from './constants'
1119

1220
const PluginListContainer = ({
13-
pluginList,
14-
handlePluginListUpdate,
15-
filters,
16-
handleUpdateFilters,
17-
isSelectable,
1821
availableTags,
1922
handleUpdateAvailableTags,
2023
pluginDataStore,
2124
handlePluginDataStoreUpdate,
22-
totalCount,
23-
handleUpdateTotalCount,
24-
persistFilters,
25-
selectedPluginsMap,
2625
handlePluginSelection,
26+
// Selected plugins Section
27+
isSelectable,
28+
selectedPluginsMap,
29+
// Plugin list Section
30+
persistFilters,
31+
parentPluginList,
32+
handleParentPluginListUpdate,
33+
parentTotalCount,
34+
handleParentTotalCount,
35+
parentFilters,
36+
handleUpdateParentFilters,
2737
}: Readonly<PluginListContainerProps>) => {
28-
const { searchKey, selectedTags, showSelectedPlugins } = filters
2938
const { appId } = useParams<PluginListParamsType>()
3039

40+
const [pluginList, setPluginList] = useState<PluginListItemType[]>(parentPluginList || [])
41+
const [totalCount, setTotalCount] = useState<number>(parentTotalCount || 0)
42+
const [filters, setFilters] = useState<PluginListFiltersType>(
43+
parentFilters || structuredClone(DEFAULT_PLUGIN_LIST_FILTERS),
44+
)
45+
46+
const handlePluginListUpdate = (updatedPluginList: PluginListItemType[]) => {
47+
setPluginList(updatedPluginList)
48+
handleParentPluginListUpdate?.(updatedPluginList)
49+
}
50+
51+
const handleUpdateTotalCount = (updatedTotalCount: number) => {
52+
setTotalCount(updatedTotalCount)
53+
handleParentTotalCount?.(updatedTotalCount)
54+
}
55+
56+
const handleUpdateFilters = (updatedFilters: PluginListFiltersType) => {
57+
setFilters(updatedFilters)
58+
handleUpdateParentFilters?.(updatedFilters)
59+
}
60+
61+
const { searchKey, selectedTags, showSelectedPlugins } = filters || {}
62+
3163
const pluginDataDependency = persistFilters ? [pluginList] : [searchKey, appId, selectedTags]
3264
// TODO: Add abortController as well
3365
const [isLoadingPluginData, pluginData, pluginDataError, reloadPluginData] = useAsync(
@@ -36,7 +68,7 @@ const PluginListContainer = ({
3668
searchKey,
3769
selectedTags,
3870
offset: 0,
39-
appId: appId ? +appId : undefined,
71+
appId: appId ? +appId : null,
4072
}),
4173
pluginDataDependency,
4274
persistFilters ? !pluginList.length : true,
@@ -99,16 +131,6 @@ const PluginListContainer = ({
99131
})
100132
}
101133

102-
useEffect(
103-
// TODO: Test this persistFilters value on unmount
104-
() => () => {
105-
if (!persistFilters) {
106-
handleClearFilters()
107-
}
108-
},
109-
[],
110-
)
111-
112134
const handleSearch = (searchText: string) => {
113135
handleUpdateFilters({
114136
...filters,
@@ -145,6 +167,13 @@ const PluginListContainer = ({
145167
})
146168
}
147169

170+
const handleRemoveSelectedTag = (filterConfig: Pick<PluginListFiltersType, 'selectedTags'>) => {
171+
handleUpdateFilters({
172+
...filters,
173+
selectedTags: filterConfig.selectedTags,
174+
})
175+
}
176+
148177
// TODO: Be remember to turn showSelectedPluginFilter as false if count becomes 0
149178
const showSelectedPluginFilter = useMemo(
150179
() => isSelectable && selectedPluginsMap && Object.keys(selectedPluginsMap).length > 0,
@@ -191,6 +220,18 @@ const PluginListContainer = ({
191220
)}
192221
</div>
193222

223+
{!!selectedTags.length && (
224+
<FilterChips<Pick<PluginListFiltersType, 'selectedTags'>>
225+
filterConfig={{
226+
selectedTags,
227+
}}
228+
onRemoveFilter={handleRemoveSelectedTag}
229+
clearFilters={handleClearFilters}
230+
className="p-0 w-100 pt-4"
231+
clearButtonClassName="dc__no-background-imp dc__no-border-imp dc__tab-focus"
232+
/>
233+
)}
234+
194235
<APIResponseHandler
195236
isLoading={isLoadingPluginData}
196237
error={pluginDataError}

src/Shared/Components/Plugin/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ export * from './types'
22
export * from './service'
33
export * from './utils'
44
export * from './constants'
5-
export * from './PluginListContainer'
5+
export { default as PluginListContainer } from './PluginListContainer'

src/Shared/Components/Plugin/types.ts

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -84,23 +84,38 @@ export interface PluginDataStoreType {
8484

8585
// TODO: Deprecate this type
8686
export interface PluginDetailType extends DetailedPluginVersionType {}
87+
export type PluginListItemType = Pick<PluginDetailType, 'parentPluginId'>
8788

8889
interface BasePluginListContainerProps {
89-
filters: PluginListFiltersType
90-
handleUpdateFilters: (filters: PluginListFiltersType) => void
9190
availableTags: string[]
9291
handleUpdateAvailableTags: (tags: string[]) => void
9392
handlePluginSelection: (parentPluginId: PluginDetailType['parentPluginId']) => void
9493
pluginDataStore: PluginDataStoreType
9594
handlePluginDataStoreUpdate: (pluginStore: PluginDataStoreType) => void
96-
totalCount: number
97-
handleUpdateTotalCount: (totalCount: number) => void
98-
persistFilters?: boolean
99-
pluginList: Pick<PluginDetailType, 'parentPluginId'>[]
100-
handlePluginListUpdate: (pluginList: Pick<PluginDetailType, 'parentPluginId'>[]) => void
10195
}
10296

97+
type PluginListType =
98+
| {
99+
persistFilters: true
100+
parentPluginList: PluginListItemType[]
101+
handleParentPluginListUpdate: (pluginList: PluginListItemType[]) => void
102+
parentTotalCount: number
103+
handleParentTotalCount: (totalCount: number) => void
104+
parentFilters: PluginListFiltersType
105+
handleUpdateParentFilters: (filters: PluginListFiltersType) => void
106+
}
107+
| {
108+
persistFilters: false
109+
parentPluginList?: never
110+
handleParentPluginListUpdate?: never
111+
parentTotalCount?: never
112+
handleParentTotalCount?: never
113+
parentFilters?: never
114+
handleUpdateParentFilters?: never
115+
}
116+
103117
export type PluginListContainerProps = BasePluginListContainerProps &
118+
PluginListType &
104119
(
105120
| {
106121
/**
@@ -140,7 +155,7 @@ export interface PluginListParamsType {
140155
}
141156

142157
export interface PluginTagSelectProps extends Pick<BasePluginListContainerProps, 'availableTags'> {
143-
selectedTags: BasePluginListContainerProps['filters']['selectedTags']
158+
selectedTags: PluginListContainerProps['parentFilters']['selectedTags']
144159
isLoading: boolean
145160
hasError: boolean
146161
reloadTags: () => void
@@ -150,16 +165,13 @@ export interface PluginTagSelectProps extends Pick<BasePluginListContainerProps,
150165
export interface PluginListProps
151166
extends Pick<
152167
PluginListContainerProps,
153-
| 'pluginDataStore'
154-
| 'pluginList'
155-
| 'totalCount'
156-
| 'filters'
157-
| 'handlePluginSelection'
158-
| 'selectedPluginsMap'
159-
| 'isSelectable'
168+
'pluginDataStore' | 'handlePluginSelection' | 'selectedPluginsMap' | 'isSelectable'
160169
> {
161170
handleDataUpdateForPluginResponse: (pluginResponse: Awaited<ReturnType<typeof getPluginStoreData>>) => void
162171
handleClearFilters: () => void
172+
pluginList: PluginListContainerProps['parentPluginList']
173+
totalCount: PluginListContainerProps['parentTotalCount']
174+
filters: PluginListContainerProps['parentFilters']
163175
}
164176

165177
export interface PluginCardProps

0 commit comments

Comments
 (0)