-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathClusterSelector.tsx
More file actions
203 lines (192 loc) · 5.78 KB
/
Copy pathClusterSelector.tsx
File metadata and controls
203 lines (192 loc) · 5.78 KB
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
import {
ClusterIcon,
ComboBox,
FillLevel,
Input2,
ListBoxFooter,
ListBoxFooterPlus,
ListBoxItem,
ManagementClusterIcon,
SearchIcon,
VirtualClusterIcon,
} from '@pluralsh/design-system'
import { useThrottle } from 'components/hooks/useThrottle'
import { FillLevelDiv } from 'components/utils/FillLevelDiv'
import { ClusterProviderIcon } from 'components/utils/Provider'
import { ClusterTinyFragment, useClusterSelectorQuery } from 'generated/graphql'
import isEmpty from 'lodash/isEmpty'
import { ComponentPropsWithRef, useCallback, useMemo, useState } from 'react'
import { useTheme } from 'styled-components'
import { useProjectId } from '../../contexts/ProjectsContext'
import { withCurrentCluster } from '../../kubernetes/clusterSelection'
import { useFetchPaginatedData } from '../../utils/table/useFetchPaginatedData'
import { ClusterUpgradeChip } from '../clusters/ClusterUpgradeButton'
export default function ClusterSelector({
onClusterChange,
clusterId,
allowDeselect,
hideTitleContent = false,
showUpgrades = false,
placeholder = 'Filter by cluster',
deselectLabel = 'Show all clusters',
inputProps,
fillLevel = 1,
...props
}: {
onClusterChange: (cluster: ClusterTinyFragment | null) => void
clusterId: Nullable<string>
allowDeselect: boolean
hideTitleContent?: boolean
showUpgrades?: boolean
placeholder?: string
deselectLabel?: string
inputProps?: ComponentPropsWithRef<typeof Input2>
fillLevel?: FillLevel
} & Omit<ComponentPropsWithRef<typeof ComboBox>, 'children'>) {
const theme = useTheme()
const projectId = useProjectId()
const [inputValue, setInputValue] = useState('')
const throttledInput = useThrottle(inputValue, 100)
const [clusterSelectIsOpen, setClusterSelectIsOpen] = useState(false)
const { data, loading, pageInfo, fetchNextPage } = useFetchPaginatedData(
{
queryHook: useClusterSelectorQuery,
keyPath: ['clusters'],
errorPolicy: 'ignore',
},
{
q: throttledInput || undefined,
currentClusterId: clusterId || undefined,
projectId,
}
)
const clusters = useMemo(
() =>
withCurrentCluster(
data?.clusters?.edges?.flatMap((e) => (e?.node ? e.node : [])) || [],
data?.cluster
),
[data?.cluster, data?.clusters?.edges]
)
const findCluster = useCallback(
(clusterId: Nullable<string>) => {
if (!clusterId) {
return null
}
if (data?.cluster && data?.cluster.id === clusterId) {
return data.cluster
}
return clusters?.find((cluster) => cluster?.id === clusterId)
},
[clusters, data?.cluster]
)
const selectedCluster = useMemo(
() => findCluster(clusterId),
[clusterId, findCluster]
)
return (
<FillLevelDiv fillLevel={fillLevel}>
<ComboBox
inputProps={{
placeholder: selectedCluster ? selectedCluster.name : placeholder,
...inputProps,
}}
titleContent={
!hideTitleContent ? (
<div css={{ display: 'flex', gap: theme.spacing.xsmall }}>
{data?.cluster?.self ? (
<ManagementClusterIcon />
) : data?.cluster?.virtual ? (
<VirtualClusterIcon fullColor={false} />
) : (
<ClusterIcon />
)}
</div>
) : undefined
}
startIcon={
clusterSelectIsOpen || !selectedCluster ? (
<SearchIcon />
) : (
<ClusterProviderIcon
cluster={selectedCluster}
size={16}
/>
)
}
endIcon={
showUpgrades ? (
<ClusterUpgradeChip cluster={selectedCluster} />
) : undefined
}
inputValue={inputValue}
onInputChange={setInputValue}
loading={clusterSelectIsOpen && data && loading}
isOpen={clusterSelectIsOpen}
onOpenChange={(isOpen) => {
setClusterSelectIsOpen(isOpen)
}}
dropdownFooter={
!data ? (
<ListBoxFooter>Loading</ListBoxFooter>
) : isEmpty(clusters) ? (
<ListBoxFooter>No results</ListBoxFooter>
) : pageInfo?.hasNextPage ? (
<ListBoxFooterPlus>Show more</ListBoxFooterPlus>
) : undefined
}
onFooterClick={() => {
if (pageInfo?.hasNextPage) {
fetchNextPage()
} else {
setClusterSelectIsOpen(false)
}
}}
{...(clusterId && allowDeselect
? {
dropdownFooterFixed: (
<ListBoxFooterPlus
onClick={() => {
setInputValue('')
setClusterSelectIsOpen(false)
onClusterChange?.(null)
}}
leftContent={<ClusterIcon />}
>
{deselectLabel}
</ListBoxFooterPlus>
),
}
: {})}
selectedKey={clusterId || ''}
onSelectionChange={(key) => {
const newCluster = findCluster(key as string) ?? null
if (newCluster || allowDeselect) {
onClusterChange?.(newCluster)
}
setInputValue('')
}}
{...props}
>
{clusters.map((cluster) => (
<ListBoxItem
key={cluster?.id}
label={cluster?.name}
textValue={cluster?.name}
leftContent={
<ClusterProviderIcon
cluster={cluster}
size={16}
/>
}
rightContent={
showUpgrades && cluster.id !== selectedCluster?.id ? (
<ClusterUpgradeChip cluster={cluster} />
) : undefined
}
/>
))}
</ComboBox>
</FillLevelDiv>
)
}