-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathProjectHeader.tsx
More file actions
278 lines (254 loc) · 9.44 KB
/
ProjectHeader.tsx
File metadata and controls
278 lines (254 loc) · 9.44 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// Copyright 2020-2022 SubQuery Pte Ltd authors & contributors
// SPDX-License-Identifier: Apache-2.0
import * as React from 'react';
import { useTranslation } from 'react-i18next';
import { useNavigate, useSearchParams } from 'react-router-dom';
import GetEndpoint from '@components/GetEndpoint';
import { IndexerName } from '@components/IndexerDetails/IndexerName';
import UnsafeWarn from '@components/UnsafeWarn';
import { useAccount } from '@containers/Web3';
import { useConsumerHostServices } from '@hooks/useConsumerHostServices';
import { Manifest } from '@hooks/useGetDeploymentManifest';
import { ProjectDetailsQuery } from '@hooks/useProjectFromQuery';
import { ProjectActionArgv } from '@pages/explorer/Project/type';
import { Tag, Typography } from '@subql/components';
import { ProjectType } from '@subql/network-query';
import { useAsyncMemo } from '@subql/react-hooks';
import { bytesToGb, formatNumber, formatSQT, TOKEN } from '@utils';
import { Button, Tooltip } from 'antd';
import { BigNumber } from 'bignumber.js';
import clsx from 'clsx';
import dayjs from 'dayjs';
import { toSvg } from 'jdenticon';
import { getNetworkNameByChainId } from 'src/const/const';
import { useProjectStore } from 'src/stores/project';
import Detail from '../Detail';
import { Dropdown } from '../Dropdown';
import IPFSImage from '../IPFSImage';
import styles from './ProjectHeader.module.less';
type Props = {
project: ProjectDetailsQuery;
versions?: Record<string, string>;
currentVersion?: string;
onChangeVersion?: (key: string) => void;
isUnsafeDeployment?: boolean;
manifest?: Manifest;
};
const ProjectHeader: React.FC<Props> = ({
project,
versions,
currentVersion,
isUnsafeDeployment,
onChangeVersion,
manifest,
}) => {
const { t } = useTranslation();
const navigate = useNavigate();
const { account } = useAccount();
const { projectDbSize, projectInfo } = useProjectStore();
const { getStatisticQueries, getDominantPriceByDeployment } = useConsumerHostServices({ autoLogin: false });
const [searchParams] = useSearchParams();
const initialOpenModal = React.useMemo(() => {
if (searchParams.get('action') === ProjectActionArgv.CREATE_PLAN) {
return true;
}
return false;
}, [searchParams]);
const createdAtStr = React.useMemo(() => dayjs(project.createdTimestamp).utc(true).fromNow(), [project]);
const updatedAtStr = React.useMemo(() => dayjs(project.updatedTimestamp).utc(true).fromNow(), [project]);
const VersionDropdown = () => {
if (!versions) return <></>;
const menu = Object.entries(versions).map(([key, value]) => {
const deployment = project.deployments.nodes.find((i) => i?.id === key);
const booster =
deployment?.deploymentBoosterSummariesByDeploymentId?.groupedAggregates?.[0]?.keys?.[0] === key
? deployment?.deploymentBoosterSummariesByDeploymentId?.groupedAggregates?.[0]?.sum?.totalAmount || '0'
: '0';
return {
key,
label: (
<Typography style={{ maxWidth: 400 }} className="overflowEllipsis">{`${value} - Boost: ${formatNumber(
formatSQT(booster),
)}`}</Typography>
),
};
});
const handleOnClick = (key: string) => {
onChangeVersion?.(key);
};
return (
<Dropdown
menu={menu}
handleOnClick={handleOnClick}
dropdownContent={currentVersion ? versions[currentVersion] : versions[0]}
styleProps={clsx(styles.dropdown)}
/>
);
};
const networkVal = React.useMemo(() => {
if (project.type === ProjectType.RPC && manifest?.rpcFamily) {
return manifest?.rpcFamily[0];
}
const chainId =
project.type === ProjectType.SUBQUERY ? manifest?.network?.chainId : manifest?.dataSources?.[0]?.network;
if (!chainId) return '-';
return (
getNetworkNameByChainId(chainId, {
projectName: project.metadata?.name,
projectId: project.id,
source: 'ProjectHeader',
}) || chainId
);
}, [project.type, manifest, project.id, project.metadata?.name]);
const isOnwer = React.useMemo(() => account === project.owner, [project.owner, account]);
const dbSize = React.useMemo(() => {
if (!currentVersion)
return {
average: '...',
max: '...',
};
if (!projectInfo[currentVersion])
return {
average: '...',
max: '...',
};
return {
average: `${bytesToGb(projectDbSize[currentVersion || '']?.average)} Gb` || '...',
max: `${bytesToGb(projectDbSize[currentVersion || '']?.max)} Gb` || '...',
};
}, [projectDbSize, currentVersion, projectInfo]);
const yesterdayQueriesCount = useAsyncMemo(async () => {
const today = dayjs();
const yesterday = today.subtract(1, 'day');
const res = await getStatisticQueries({
deployment: [currentVersion || ''],
start_date: yesterday.format('YYYY-MM-DD'),
end_date: today.format('YYYY-MM-DD'),
});
if (BigNumber(res.data.total).isEqualTo(0)) {
return '< 1,000';
}
return formatNumber(res.data.total, 0);
}, [currentVersion]);
const dominantPrice = useAsyncMemo(async () => {
try {
const res = await getDominantPriceByDeployment({
deployment_list: [project.deploymentId],
});
return `${formatSQT(
BigNumber(res.data[0].price || 0)
.multipliedBy(1000)
.toString(),
)} ${TOKEN} / 1,000 requests`;
} catch {
return '...';
}
}, [project.type]);
return (
<div className={styles.container}>
<div className={styles.left}>
<IPFSImage
src={
project.metadata.image ||
`data:image/svg+xml;utf8,${encodeURIComponent(toSvg(project?.metadata.name, 500))}`
}
className={styles.image}
/>
</div>
<div className={styles.inner}>
<div className={styles.upper}>
<div className={styles.titleVersion}>
<Typography
variant="h4"
className={clsx(styles.name, 'overflowEllipsis')}
weight={600}
style={{ marginRight: 8, maxWidth: 500 }}
>
{project.metadata.name}
</Typography>
{isUnsafeDeployment && <UnsafeWarn></UnsafeWarn>}
<VersionDropdown />
<span style={{ flex: 1 }}></span>
<div className={`flex ${styles.groupButton}`}>
{isOnwer ? (
<Button
type="primary"
shape="round"
size="large"
onClick={() => {
navigate(`/projects/create?id=${project.id}`);
}}
>
Edit
</Button>
) : (
''
)}
<GetEndpoint
deploymentId={currentVersion || ''}
project={project}
initialOpen={initialOpenModal}
></GetEndpoint>
</div>
</div>
<IndexerName address={project.owner} size="tiny"></IndexerName>
<div style={{ marginTop: 8, display: 'flex', flexWrap: 'wrap', gap: 8 }}>
{/* in case of someone skip the legal process add more than 2 categories */}
{project.metadata.categories &&
project.metadata.categories.slice(0, 2).map((val) => {
return (
<Button key={val} type="primary" shape="round" className={clsx('staticButton', 'overflowEllipsis')}>
<span className="overflowEllipsis" style={{ maxWidth: 300 }}>
{val}
</span>
</Button>
);
})}
</div>
</div>
<div className={styles.lower}>
<Detail
label="Network"
value={networkVal.length > 10 ? `${networkVal.slice(0, 10)}...` : networkVal}
capitalize
></Detail>
<Detail
label="Type"
value={
{
[ProjectType.LLM]: 'LLM',
[ProjectType.RPC]: 'RPC Endpoint',
[ProjectType.SUBQUERY]: 'Indexed Dataset',
[ProjectType.SQ_DICT]: 'Dictionary',
[ProjectType.SUBGRAPH]: (
<Tag style={{ background: '#6B46EF', color: '#fff', border: '1px solid #DFE3E880' }}>Subgraph</Tag>
),
}[project.type] || ''
}
></Detail>
{currentVersion && (
<Detail label={t('projectHeader.deploymentId')} value={currentVersion} canCopy={true} isTruncate={true} />
)}
<Detail label={t('projectOverview.updatedAt')} value={updatedAtStr} className={styles.column} />
<Detail label={t('projectOverview.createdAt')} value={createdAtStr} className={styles.column} />
{project.type === ProjectType.SUBQUERY ? (
<Detail
label={'DbSize'}
value={
<Tooltip title={`Max: ${dbSize.max}, Average: ${dbSize.average}`}>
<Typography>{dbSize.average}</Typography>
</Tooltip>
}
className={styles.column}
/>
) : (
''
)}
<Detail label={'Queries (Yesterday)'} value={<Typography>{yesterdayQueriesCount.data}</Typography>}></Detail>
<Detail label={'Dominant Price'} value={<Typography>{dominantPrice.data}</Typography>}></Detail>
</div>
</div>
</div>
);
};
export default ProjectHeader;