-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathProjectSelect.tsx
More file actions
163 lines (152 loc) · 4.58 KB
/
ProjectSelect.tsx
File metadata and controls
163 lines (152 loc) · 4.58 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
/**
@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 { BAISelect, BAISelectProps } from 'backend.ai-ui';
import _ from 'lodash';
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[];
fetchKey?: string;
}
const ProjectSelect: React.FC<ProjectSelectProps> = ({
onSelectProject,
domain,
disableDefaultFilter,
lockedProjectTypes,
fetchKey,
...selectProps
}) => {
const { t } = useTranslation();
const [currentUser] = useCurrentUserInfo();
const baiClient = useSuspendedBackendaiClient();
const blockList = baiClient?._config?.blockList ?? null;
const [value, setValue] = useControllableState_deprecated(selectProps);
const userRole = useCurrentUserRole();
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: 'store-and-network',
fetchKey: fetchKey,
},
);
// 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
? []
: (_.chain(accessibleProjects)
.filter((p) => lockedProjectTypes.includes(p?.type ?? ''))
.map('id')
.compact()
.value() 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 = _.chain(accessibleProjects)
.groupBy('type')
.map((value, key) => {
return {
label: getLabel(key),
title: key,
options: _.chain(value)
.sortBy('name')
.map((project) => {
return {
label: project?.name,
value: project?.id,
projectId: project?.id,
projectResourcePolicy: project?.resource_policy,
projectName: project?.name,
disabled: lockedProjectIds.includes(project?.id ?? ''),
};
})
.value(),
};
})
.value();
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
}
/>
);
};
export default ProjectSelect;