forked from kubeflow/hub
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModelRegistrySelector.tsx
More file actions
199 lines (186 loc) · 6.26 KB
/
ModelRegistrySelector.tsx
File metadata and controls
199 lines (186 loc) · 6.26 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
import * as React from 'react';
import {
Bullseye,
Button,
DescriptionList,
DescriptionListDescription,
DescriptionListGroup,
DescriptionListTerm,
Flex,
FlexItem,
Icon,
Popover,
PopoverPosition,
Tooltip,
} from '@patternfly/react-core';
import text from '@patternfly/react-styles/css/utilities/Text/text';
import truncateStyles from '@patternfly/react-styles/css/components/Truncate/truncate';
import { InfoCircleIcon, BlueprintIcon } from '@patternfly/react-icons';
import { useBrowserStorage } from '~/shared/components/browserStorage';
import { ModelRegistrySelectorContext } from '~/app/context/ModelRegistrySelectorContext';
import { ModelRegistry } from '~/app/types';
import SimpleSelect, { SimpleSelectOption } from '~/shared/components/SimpleSelect';
import WhosMyAdministrator from '~/shared/components/WhosMyAdministrator';
import KubeflowDocs from '~/shared/components/KubeflowDocs';
import { useThemeContext } from '~/app/ThemeContext';
const MODEL_REGISTRY_FAVORITE_STORAGE_KEY = 'kubeflow.dashboard.model.registry.favorite';
type ModelRegistrySelectorProps = {
modelRegistry: string;
onSelection: (modelRegistry: string) => void;
primary?: boolean;
isFullWidth?: boolean;
};
const ModelRegistrySelector: React.FC<ModelRegistrySelectorProps> = ({
modelRegistry,
onSelection,
primary,
isFullWidth,
}) => {
const { modelRegistries, updatePreferredModelRegistry } = React.useContext(
ModelRegistrySelectorContext,
);
const { isMUITheme } = useThemeContext();
const selection = modelRegistries.find((mr) => mr.name === modelRegistry);
const [favorites, setFavorites] = useBrowserStorage<string[]>(
MODEL_REGISTRY_FAVORITE_STORAGE_KEY,
[],
);
const selectionDisplayName = selection ? selection.displayName : modelRegistry;
const toggleLabel = modelRegistries.length === 0 ? 'No model registries' : selectionDisplayName;
const getMRSelectDescription = (mr: ModelRegistry) => {
const desc = mr.description || '';
if (!desc) {
return;
}
const tooltipContent = (
<DescriptionList>
<DescriptionListGroup>
<DescriptionListTerm>{`${mr.displayName} description`}</DescriptionListTerm>
<DescriptionListDescription>{desc}</DescriptionListDescription>
</DescriptionListGroup>
</DescriptionList>
);
return (
<Tooltip content={tooltipContent} isContentLeftAligned>
<span className={truncateStyles.truncate}>
<span className={truncateStyles.truncateStart}>{desc}</span>
</span>
</Tooltip>
);
};
const allOptions: SimpleSelectOption[] = modelRegistries.map((mr) => ({
key: mr.name,
label: mr.name,
dropdownLabel: mr.displayName,
description: getMRSelectDescription(mr),
isFavorited: favorites.includes(mr.name),
}));
const favoriteOptions = (favIds: string[]) =>
allOptions.filter((option) => favIds.includes(option.key));
const selector = (
<SimpleSelect
isScrollable
placeholder="Select a model registry"
dataTestId="model-registry-selector-dropdown"
toggleProps={{ id: 'download-steps-logs-toggle' }}
toggleLabel={toggleLabel}
aria-label="Model registry toggle"
previewDescription={false}
onChange={(key) => {
updatePreferredModelRegistry(modelRegistries.find((obj) => obj.name === key));
onSelection(key);
}}
isFullWidth={isFullWidth}
maxMenuHeight="300px"
popperProps={{ maxWidth: '400px' }}
value={selection?.name}
groupedOptions={[
...(favorites.length > 0
? [
{
key: 'favorites-group',
label: 'Favorites',
options: favoriteOptions(favorites),
},
]
: []),
{
key: 'all',
label: 'All model registries',
options: allOptions,
},
]}
onActionClick={(event: React.MouseEvent, value: string, actionId: string) => {
event.stopPropagation();
if (actionId === 'fav') {
const isFavorited = favorites.includes(value);
if (isFavorited) {
setFavorites(favorites.filter((id) => id !== value));
} else {
setFavorites([...favorites, value]);
}
}
}}
/>
);
if (primary) {
return selector;
}
return (
<Flex spaceItems={{ default: 'spaceItemsLg' }} alignItems={{ default: 'alignItemsCenter' }}>
<FlexItem>
<Icon>
<BlueprintIcon />
</Icon>
</FlexItem>
<FlexItem>
<Bullseye>Model registry</Bullseye>
</FlexItem>
<FlexItem>{selector}</FlexItem>
{selection && (
<FlexItem>
<Popover
aria-label="Model registry description popover"
data-testid="mr-details-popover"
position="right"
headerContent={`${selection.displayName} details`}
bodyContent={
<DescriptionList>
<DescriptionListGroup>
<DescriptionListTerm>Description</DescriptionListTerm>
<DescriptionListDescription
className={!selection.description ? text.textColorDisabled : ''}
>
{selection.description || 'No description'}
</DescriptionListDescription>
</DescriptionListGroup>
</DescriptionList>
}
>
<Button variant="link" icon={<InfoCircleIcon />} data-testid="view-details-button">
View details
</Button>
</Popover>
</FlexItem>
)}
<FlexItem align={{ default: 'alignRight' }}>
{isMUITheme ? (
<KubeflowDocs
buttonLabel="Need another registry?"
linkTestId="model-registry-help-button"
/>
) : (
<WhosMyAdministrator
buttonLabel="Need another registry?"
headerContent="Need another registry?"
leadText="To request access to a new or existing model registry, contact your administrator."
contentTestId="model-registry-help-content"
linkTestId="model-registry-help-button"
popoverPosition={PopoverPosition.left}
/>
)}
</FlexItem>
</Flex>
);
};
export default ModelRegistrySelector;