-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathProjectSelect.tsx
More file actions
202 lines (189 loc) · 6.2 KB
/
ProjectSelect.tsx
File metadata and controls
202 lines (189 loc) · 6.2 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
/**
@license
Copyright (c) 2015-2026 Lablup Inc. All rights reserved.
*/
import { ProjectSelectorQuery } from '../__generated__/ProjectSelectorQuery.graphql';
import { useSuspendedBackendaiClient } from '../hooks';
import { useCurrentUserInfo, useCurrentUserRole } from '../hooks/backendai';
import useControllableState_deprecated from '../hooks/useControllableState';
import { useCurrentUserProjectRoles } from '../hooks/useCurrentUserProjectRoles';
import { InfoCircleOutlined } from '@ant-design/icons';
import { theme, Tooltip } from 'antd';
import { BAIFlex, BAISelect, BAISelectProps } from 'backend.ai-ui';
import * as _ from 'lodash-es';
import { ShieldUser } from 'lucide-react';
import React, { useEffect, useEffectEvent } from 'react';
import { useTranslation } from 'react-i18next';
import { graphql, useLazyLoadQuery } from 'react-relay';
type ProjectInfo = {
label: React.ReactNode;
value: string | number;
projectId: string;
projectResourcePolicy: any; // Replace 'any' with the actual type
projectName: string;
};
export interface ProjectSelectProps extends BAISelectProps {
onSelectProject?: (projectInfo: ProjectInfo) => void;
domain: string;
autoSelectDefault?: boolean;
disableDefaultFilter?: boolean;
lockedProjectTypes?: string[];
}
const ProjectSelect: React.FC<ProjectSelectProps> = ({
onSelectProject,
domain,
disableDefaultFilter,
lockedProjectTypes,
...selectProps
}) => {
const { t } = useTranslation();
const { token } = theme.useToken();
const [currentUser] = useCurrentUserInfo();
const baiClient = useSuspendedBackendaiClient();
const blockList = baiClient?._config?.blockList ?? null;
const [value, setValue] = useControllableState_deprecated(selectProps);
const userRole = useCurrentUserRole();
const { projectAdminIds } = useCurrentUserProjectRoles();
const { groups, user } = useLazyLoadQuery<ProjectSelectorQuery>(
graphql`
query ProjectSelectorQuery(
$domain_name: String
$email: String
$type: [String]
) {
groups(domain_name: $domain_name, is_active: true, type: $type) {
id
is_active
name
resource_policy
type
}
user(email: $email) {
groups {
id
name
}
}
}
`,
{
domain_name: domain,
email: currentUser.email,
type:
(userRole === 'admin' || userRole === 'superadmin') &&
_.includes(blockList, 'model-store')
? ['GENERAL']
: ['GENERAL', 'MODEL_STORE'],
},
{
fetchPolicy: 'network-only',
},
);
// temporary filtering groups by accessible groups according to user query
const accessibleProjects = disableDefaultFilter
? groups
: groups?.filter((project) =>
user?.groups?.map((group) => group?.id).includes(project?.id),
);
const lockedProjectIds = !lockedProjectTypes?.length
? []
: (_.compact(
_.map(
_.filter(accessibleProjects, (p) =>
lockedProjectTypes.includes(p?.type ?? ''),
),
'id',
),
) as string[]);
// Auto-select locked projects when they become available
const autoSelectLockedProjects = useEffectEvent(() => {
if (lockedProjectIds.length > 0) {
const currentVal = _.isArray(value) ? (value as string[]) : [];
const missing = lockedProjectIds.filter((id) => !currentVal.includes(id));
if (missing.length > 0) {
setValue([...currentVal, ...missing]);
}
}
});
const lockedProjectIdsKey = lockedProjectIds.join(',');
useEffect(() => {
autoSelectLockedProjects();
}, [lockedProjectIdsKey]);
const getLabel = (key: string) =>
({
GENERAL: t('general.General'),
MODEL_STORE: t('data.ModelStore'),
})[key] || key;
const groupOptions = _.map(
_.groupBy(accessibleProjects, 'type'),
(value, key) => {
return {
label: getLabel(key),
title: key,
options: _.map(_.sortBy(value, 'name'), (project) => {
const showBadge =
!!project?.id && projectAdminIds.includes(project.id);
return {
label: showBadge ? (
<BAIFlex gap={token.marginXS} align="center">
<span>{project?.name}</span>
<Tooltip title={t('projectSelect.ProjectAdminBadge')}>
<ShieldUser />
</Tooltip>
</BAIFlex>
) : (
project?.name
),
value: project?.id,
projectId: project?.id,
projectResourcePolicy: project?.resource_policy,
projectName: project?.name,
disabled: lockedProjectIds.includes(project?.id ?? ''),
};
}),
};
},
);
const showNoProjectError =
!accessibleProjects?.length &&
!selectProps.disabled &&
!selectProps.loading;
const noAccessibleProjectsMessage = t('projectSelect.NoAccessibleProjects');
return (
<BAISelect
onChange={(value, option) => {
setValue(value);
onSelectProject?.(option as ProjectInfo);
}}
placeholder={t('storageHost.quotaSettings.SelectProject')}
popupMatchSelectWidth={false}
{...selectProps}
value={value}
showSearch={{
optionFilterProp: 'projectName',
}}
options={
_.size(groupOptions) > 1 ? groupOptions : groupOptions[0]?.options
}
status={showNoProjectError ? 'error' : selectProps.status}
// Surface the empty-state reason on the focusable control itself,
// not only on the non-focusable suffix icon. `tooltip` wraps the
// whole BAISelect in an antd Tooltip (hover over the entire control,
// not just the tiny icon), and `aria-label` gives keyboard /
// screen-reader users a persistent accessible description that does
// not depend on the tooltip being open.
tooltip={
showNoProjectError ? noAccessibleProjectsMessage : selectProps.tooltip
}
aria-label={
showNoProjectError
? noAccessibleProjectsMessage
: selectProps['aria-label']
}
suffixIcon={
showNoProjectError ? <InfoCircleOutlined /> : selectProps.suffixIcon
}
/>
);
};
export default ProjectSelect;