-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathStorageStatusPanelCard.tsx
More file actions
196 lines (188 loc) · 6.09 KB
/
StorageStatusPanelCard.tsx
File metadata and controls
196 lines (188 loc) · 6.09 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
import { useSuspendedBackendaiClient } from '../hooks';
import { useVFolderInvitations } from '../hooks/backendai';
import { useSuspenseTanQuery } from '../hooks/reactQueryAlias';
import { useCurrentProjectValue } from '../hooks/useCurrentProject';
import BAICard, { BAICardProps } from './BAICard';
import BAIPanelItem from './BAIPanelItem';
import { StorageStatusPanelCardQuery } from './__generated__/StorageStatusPanelCardQuery.graphql';
import { Badge, Col, Row, theme, Tooltip, Typography } from 'antd';
import { createStyles } from 'antd-style';
import graphql from 'babel-plugin-relay/macro';
import _ from 'lodash';
import React, { useDeferredValue } from 'react';
import { useTranslation } from 'react-i18next';
import { useLazyLoadQuery } from 'react-relay';
const useStyles = createStyles(({ css, token }) => ({
invitationTooltip: css`
.ant-tooltip-arrow {
right: -${token.sizeXS}px;
bottom: ${token.size}px;
}
.ant-tooltip-content {
left: ${token.size}px;
bottom: ${token.size}px;
}
`,
}));
interface StorageStatusPanelProps extends BAICardProps {
fetchKey?: string;
onRequestBadgeClick?: () => void;
}
const StorageStatusPanelCard: React.FC<StorageStatusPanelProps> = ({
fetchKey,
onRequestBadgeClick,
...cardProps
}) => {
const { t } = useTranslation();
const { token } = theme.useToken();
const { styles } = useStyles();
const baiClient = useSuspendedBackendaiClient();
const currentProject = useCurrentProjectValue();
const deferredFetchKey = useDeferredValue(fetchKey);
const [{ count }] = useVFolderInvitations(deferredFetchKey);
const isExcludedCount = (status: string) => {
return _.includes(
['delete-ongoing', 'delete-complete', 'delete-error'],
status,
);
};
const { data: vfolders } = useSuspenseTanQuery({
queryKey: ['vfolders', { deferredFetchKey, id: currentProject?.id }],
queryFn: () => {
return baiClient.vfolder.list(currentProject?.id);
},
});
// FIXME: vfolder_node query does not provide a information about the vfolder's owner.
// So, even if we use fragment, we still need to filter the vfolders by each conditions in client side.
const createdCount = vfolders?.filter(
(item: any) =>
item.is_owner &&
item.ownership_type === 'user' &&
!isExcludedCount(item.status),
).length;
const projectCount = vfolders?.filter(
(item: any) =>
item.ownership_type === 'group' && !isExcludedCount(item.status),
).length;
const invitedCount = vfolders?.filter(
(item: any) =>
!item.is_owner &&
item.ownership_type === 'user' &&
!isExcludedCount(item.status),
).length;
const { user_resource_policy, project_resource_policy } =
useLazyLoadQuery<StorageStatusPanelCardQuery>(
graphql`
query StorageStatusPanelCardQuery($name: String!) {
user_resource_policy {
max_vfolder_count
}
project_resource_policy(name: $name) {
max_vfolder_count
}
}
`,
{
name: currentProject?.name,
},
);
return (
<>
<BAICard {...cardProps} title={t('data.StorageStatus')}>
<Row gutter={[24, 16]}>
<Col
span={8}
style={{
borderRight: `1px solid ${token.colorBorderSecondary}`,
justifyItems: 'center',
}}
>
<BAIPanelItem
title={t('data.MyFolders')}
value={createdCount}
unit={
user_resource_policy?.max_vfolder_count
? `/ ${user_resource_policy?.max_vfolder_count}`
: undefined
}
/>
</Col>
<Col
span={8}
style={{
borderRight: `1px solid ${token.colorBorderSecondary}`,
justifyItems: 'center',
}}
>
<BAIPanelItem
title={t('data.ProjectFolders')}
value={projectCount}
unit={
project_resource_policy?.max_vfolder_count
? `/ ${project_resource_policy?.max_vfolder_count}`
: undefined
}
/>
</Col>
<Col
span={8}
style={{
justifyItems: 'center',
}}
>
<BAIPanelItem
title={
count > 0 ? (
// Add <a></a> to make tooltip clickable
// eslint-disable-next-line
<a
onClick={() => {
onRequestBadgeClick?.();
}}
>
<Tooltip
title={
count > 0
? t('data.InvitedFoldersTooltip', {
count: count,
})
: null
}
rootClassName={styles.invitationTooltip}
placement="topRight"
>
<Badge
count={count > 0 ? `+${count}` : null}
offset={[0, -`${token.sizeXS}`]}
>
<Typography.Title level={5} style={{ margin: 0 }}>
{t('data.InvitedFolders')}
</Typography.Title>
</Badge>
</Tooltip>
</a>
) : (
<Typography.Title level={5} style={{ margin: 0 }}>
{t('data.InvitedFolders')}
</Typography.Title>
)
}
value={
<Typography.Text
strong
style={{
fontSize: token.fontSizeHeading1,
color: token.Layout?.headerBg,
}}
>
{invitedCount}
</Typography.Text>
}
/>
</Col>
</Row>
</BAICard>
</>
);
};
export default StorageStatusPanelCard;