-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathHeader.tsx
More file actions
131 lines (120 loc) · 4.32 KB
/
Header.tsx
File metadata and controls
131 lines (120 loc) · 4.32 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
import { useState } from 'react'
import {
TIME_OPTION_VALUES,
TimeOption,
TimeOptions,
} from 'pages/RepoPage/shared/constants'
import { useLocationParams } from 'services/navigation/useLocationParams'
import { useComponentsBackfilled } from 'services/repo'
import { useRepoComponentsSelect } from 'services/repo/useRepoComponentsSelect'
import A from 'ui/A'
import Icon from 'ui/Icon'
import MultiSelect from 'ui/MultiSelect'
import Select from 'ui/Select'
import BranchSelector from './BranchSelector'
type Component = {
name: string
id: string
}
const Header = ({
controlsDisabled,
children,
}: {
controlsDisabled?: boolean
children?: React.ReactNode
}) => {
const [search, setSearch] = useState('')
const { params, updateParams } = useLocationParams({
historicalTrend: '',
components: [] as Component[],
})
const [selectedComponents, setSelectedComponents] = useState<Component[]>(
// @ts-expect-error need to type out useLocationParams
params?.components
)
const { data } = useComponentsBackfilled()
const { data: componentsData, isLoading } = useRepoComponentsSelect({
termId: search,
opts: { suspense: false },
})
const componentNames = componentsData?.components?.map(
(component) => component?.name
)
const value = TimeOptions.find(
// @ts-expect-error need to type out useLocationParams
(item) => item.value === params.historicalTrend
)
const defaultValue = TimeOptions.find(
(option) => option.value === TIME_OPTION_VALUES.LAST_3_MONTHS
)
return (
<div className="flex flex-col justify-end">
<div className="grid w-2/3 divide-y divide-solid divide-ds-gray-secondary sm:w-full sm:grid-cols-2 sm:divide-x sm:divide-y-0 md:grid-cols-4">
<BranchSelector isDisabled={controlsDisabled} />
<div className="flex flex-col justify-between gap-2 p-4 sm:py-0">
<h3 className="text-sm font-semibold text-ds-gray-octonary">
Configured components
</h3>
<p className="flex flex-1 text-xl font-light text-ds-gray-octonary">
{data?.componentsCount}
</p>
<p className="text-xs">
{/* @ts-expect-error - A hasn't been typed yet */}
<A to={{ pageName: 'components' }}>Learn more</A>
</p>
</div>
<div className="flex flex-col gap-2 p-4 sm:py-0">
<h3 className="text-sm font-semibold text-ds-gray-octonary">
Historical trend
</h3>
<Select
// @ts-expect-error Select is not typed
dataMarketing="select-historical-trend"
disabled={controlsDisabled}
ariaName="Select Historical Trend"
items={TimeOptions}
value={value ?? defaultValue}
onChange={(historicalTrend: TimeOption) =>
updateParams({ historicalTrend: historicalTrend.value })
}
renderItem={({ label }: { label: string }) => label}
renderSelected={({ label }: { label: string }) => label}
/>
</div>
<div className="flex flex-col gap-2 p-4 sm:py-0">
<h3 className="text-sm font-semibold text-ds-gray-octonary">
Show by
</h3>
<MultiSelect
// @ts-expect-error something funky with this component
disabled={controlsDisabled}
dataMarketing="components-tab-multi-select"
hook="components-tab-multi-select"
ariaName="Select components to show"
items={componentNames}
selectedItemsOverride={selectedComponents}
resourceName="Component"
isLoading={isLoading}
onChange={(components: Component[]): void => {
setSelectedComponents(components)
updateParams({ components })
}}
onSearch={(termId: string) => setSearch(termId)}
renderSelected={(selectedItems: Component[]) => (
<span className="flex items-center gap-2">
<Icon variant="solid" name="database" />
{selectedItems.length === 0 ? (
'All Components'
) : (
<span>{selectedItems.length} selected components</span>
)}
</span>
)}
/>
</div>
</div>
{children}
</div>
)
}
export default Header