-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathRegistrySearch.tsx
More file actions
229 lines (214 loc) · 6.69 KB
/
RegistrySearch.tsx
File metadata and controls
229 lines (214 loc) · 6.69 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import React, {
useState,
useRef,
forwardRef,
useImperativeHandle,
} from "react";
import {
EuiText,
EuiFieldSearch,
EuiSpacer,
EuiHorizontalRule,
EuiPanel,
EuiFlexGroup,
EuiFlexItem,
EuiBadge,
EuiTitle,
} from "@elastic/eui";
import EuiCustomLink from "./EuiCustomLink";
import { css } from "@emotion/react";
const searchResultsStyles = {
searchResults: {
marginTop: "8px",
},
categoryGroup: {
marginBottom: "8px",
},
searchResultItem: {
padding: "8px 0",
borderBottom: "1px solid #eee",
},
searchResultItemLast: {
padding: "8px 0",
borderBottom: "none",
},
itemDescription: {
fontSize: "0.85em",
color: "#666",
marginTop: "4px",
},
};
interface RegistrySearchProps {
categories: {
name: string;
data: any[];
getLink: (item: any) => string;
}[];
}
export interface RegistrySearchRef {
focusSearchInput: () => void;
}
const getItemType = (item: any, category: string): string | undefined => {
if (category === "Features" && "valueType" in item) {
return item.valueType;
}
if (category === "Feature Views" && "type" in item) {
return item.type;
}
return undefined;
};
const RegistrySearch = forwardRef<RegistrySearchRef, RegistrySearchProps>(
({ categories }, ref) => {
const [searchText, setSearchText] = useState("");
const inputRef = useRef<HTMLInputElement | null>(null);
const focusSearchInput = () => {
if (inputRef.current) {
inputRef.current.focus();
}
};
useImperativeHandle(
ref,
() => ({
focusSearchInput,
}),
[focusSearchInput],
);
const searchResults = categories.map(({ name, data, getLink }) => {
const filteredItems = searchText
? data.filter((item) => {
const itemName =
"name" in item
? String(item.name)
: "spec" in item && item.spec && "name" in item.spec
? String(item.spec.name ?? "Unknown")
: "Unknown";
return itemName.toLowerCase().includes(searchText.toLowerCase());
})
: [];
const items = filteredItems.map((item) => {
const itemName =
"name" in item
? String(item.name)
: "spec" in item && item.spec && "name" in item.spec
? String(item.spec.name ?? "Unknown")
: "Unknown";
return {
name: itemName,
link: getLink(item),
description:
"spec" in item && item.spec && "description" in item.spec
? String(item.spec.description || "")
: "",
type: getItemType(item, name),
projectId: "projectId" in item ? String(item.projectId) : undefined,
};
});
return {
title: name,
items,
};
});
return (
<>
<EuiFieldSearch
placeholder="Search across Feature Views, Features, Entities, etc."
value={searchText}
onChange={(e) => setSearchText(e.target.value)}
isClearable
fullWidth
inputRef={(node) => {
inputRef.current = node;
}}
aria-label="Search registry"
compressed
append={
<EuiText size="xs" color="subdued">
<span style={{ whiteSpace: "nowrap" }}>⌘K</span>
</EuiText>
}
/>
<EuiSpacer size="s" />
{searchText && (
<div style={searchResultsStyles.searchResults}>
<EuiText>
<h4>Search Results</h4>
</EuiText>
<EuiSpacer size="xs" />
{searchResults.filter((result) => result.items.length > 0).length >
0 ? (
searchResults
.filter((result) => result.items.length > 0)
.map((result) => (
<div
key={result.title}
style={searchResultsStyles.categoryGroup}
>
<EuiPanel hasBorder={true} paddingSize="m">
<EuiTitle size="xs">
<h3>
{result.title} ({result.items.length})
</h3>
</EuiTitle>
<EuiHorizontalRule margin="xs" />
{result.items.map((item, idx) => (
<div
key={item.name}
style={
idx === result.items.length - 1
? searchResultsStyles.searchResultItemLast
: searchResultsStyles.searchResultItem
}
>
<EuiFlexGroup>
<EuiFlexItem>
<EuiCustomLink
to={item.link}
onClick={() => setSearchText("")}
>
<strong>{item.name}</strong>
</EuiCustomLink>
{item.description && (
<div
style={searchResultsStyles.itemDescription}
>
{item.description}
</div>
)}
{item.projectId && (
<div
style={{
fontSize: "0.85em",
color: "#69707D",
marginTop: "4px",
}}
>
Project: {item.projectId}
</div>
)}
</EuiFlexItem>
{item.type && (
<EuiFlexItem grow={false}>
<EuiBadge>{item.type}</EuiBadge>
</EuiFlexItem>
)}
</EuiFlexGroup>
</div>
))}
</EuiPanel>
<EuiSpacer size="m" />
</div>
))
) : (
<EuiPanel hasBorder={true} paddingSize="m" color="subdued">
<EuiText textAlign="center">
<p>No matches found for "{searchText}"</p>
</EuiText>
</EuiPanel>
)}
</div>
)}
</>
);
},
);
export default RegistrySearch;