Merge branch '15020-collection-with-the-same-content-adress'
[arvados-workbench2.git] / src / views-components / data-explorer / renderers.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import * as React from 'react';
6 import { Grid, Typography, withStyles, Tooltip, IconButton, Checkbox } from '@material-ui/core';
7 import { FavoriteStar, PublicFavoriteStar } from '../favorite-star/favorite-star';
8 import { ResourceKind, TrashableResource } from '~/models/resource';
9 import { ProjectIcon, CollectionIcon, ProcessIcon, DefaultIcon, WorkflowIcon, ShareIcon } from '~/components/icon/icon';
10 import { formatDate, formatFileSize } from '~/common/formatters';
11 import { resourceLabel } from '~/common/labels';
12 import { connect, DispatchProp } from 'react-redux';
13 import { RootState } from '~/store/store';
14 import { getResource } from '~/store/resources/resources';
15 import { GroupContentsResource } from '~/services/groups-service/groups-service';
16 import { getProcess, Process, getProcessStatus, getProcessStatusColor } from '~/store/processes/process';
17 import { ArvadosTheme } from '~/common/custom-theme';
18 import { compose, Dispatch } from 'redux';
19 import { WorkflowResource } from '~/models/workflow';
20 import { ResourceStatus } from '~/views/workflow-panel/workflow-panel-view';
21 import { getUuidPrefix, openRunProcess } from '~/store/workflow-panel/workflow-panel-actions';
22 import { getResourceData } from "~/store/resources-data/resources-data";
23 import { openSharingDialog } from '~/store/sharing-dialog/sharing-dialog-actions';
24 import { UserResource } from '~/models/user';
25 import { toggleIsActive, toggleIsAdmin } from '~/store/users/users-actions';
26 import { LinkResource } from '~/models/link';
27 import { navigateTo } from '~/store/navigation/navigation-action';
28 import { withResourceData } from '~/views-components/data-explorer/with-resources';
29
30 const renderName = (dispatch: Dispatch, item: { name: string; uuid: string, kind: string }) =>
31     <Grid container alignItems="center" wrap="nowrap" spacing={16}>
32         <Grid item>
33             {renderIcon(item.kind)}
34         </Grid>
35         <Grid item>
36             <Typography color="primary" style={{ width: 'auto', cursor: 'pointer' }} onClick={() => dispatch<any>(navigateTo(item.uuid))}>
37                 {item.name}
38             </Typography>
39         </Grid>
40         <Grid item>
41             <Typography variant="caption">
42                 <FavoriteStar resourceUuid={item.uuid} />
43                 <PublicFavoriteStar resourceUuid={item.uuid} />
44             </Typography>
45         </Grid>
46     </Grid>;
47
48 export const ResourceName = connect(
49     (state: RootState, props: { uuid: string }) => {
50         const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
51         return resource || { name: '', uuid: '', kind: '' };
52     })((resource: { name: string; uuid: string, kind: string } & DispatchProp<any>) => renderName(resource.dispatch, resource));
53
54 const renderIcon = (kind: string) => {
55     switch (kind) {
56         case ResourceKind.PROJECT:
57             return <ProjectIcon />;
58         case ResourceKind.COLLECTION:
59             return <CollectionIcon />;
60         case ResourceKind.PROCESS:
61             return <ProcessIcon />;
62         case ResourceKind.WORKFLOW:
63             return <WorkflowIcon />;
64         default:
65             return <DefaultIcon />;
66     }
67 };
68
69 const renderDate = (date?: string) => {
70     return <Typography noWrap style={{ minWidth: '100px' }}>{formatDate(date)}</Typography>;
71 };
72
73 const renderWorkflowName = (item: { name: string; uuid: string, kind: string, ownerUuid: string }) =>
74     <Grid container alignItems="center" wrap="nowrap" spacing={16}>
75         <Grid item>
76             {renderIcon(item.kind)}
77         </Grid>
78         <Grid item>
79             <Typography color="primary" style={{ width: '100px' }}>
80                 {item.name}
81             </Typography>
82         </Grid>
83     </Grid>;
84
85 export const ResourceWorkflowName = connect(
86     (state: RootState, props: { uuid: string }) => {
87         const resource = getResource<WorkflowResource>(props.uuid)(state.resources);
88         return resource || { name: '', uuid: '', kind: '', ownerUuid: '' };
89     })(renderWorkflowName);
90
91 const getPublicUuid = (uuidPrefix: string) => {
92     return `${uuidPrefix}-tpzed-anonymouspublic`;
93 };
94
95 const resourceShare = (dispatch: Dispatch, uuidPrefix: string, ownerUuid?: string, uuid?: string) => {
96     const isPublic = ownerUuid === getPublicUuid(uuidPrefix);
97     return (
98         <div>
99             {!isPublic && uuid &&
100                 <Tooltip title="Share">
101                     <IconButton onClick={() => dispatch<any>(openSharingDialog(uuid))}>
102                         <ShareIcon />
103                     </IconButton>
104                 </Tooltip>
105             }
106         </div>
107     );
108 };
109
110 export const ResourceShare = connect(
111     (state: RootState, props: { uuid: string }) => {
112         const resource = getResource<WorkflowResource>(props.uuid)(state.resources);
113         const uuidPrefix = getUuidPrefix(state);
114         return {
115             uuid: resource ? resource.uuid : '',
116             ownerUuid: resource ? resource.ownerUuid : '',
117             uuidPrefix
118         };
119     })((props: { ownerUuid?: string, uuidPrefix: string, uuid?: string } & DispatchProp<any>) =>
120         resourceShare(props.dispatch, props.uuidPrefix, props.ownerUuid, props.uuid));
121
122 const renderFirstName = (item: { firstName: string }) => {
123     return <Typography noWrap>{item.firstName}</Typography>;
124 };
125
126 // User Resources
127 export const ResourceFirstName = connect(
128     (state: RootState, props: { uuid: string }) => {
129         const resource = getResource<UserResource>(props.uuid)(state.resources);
130         return resource || { firstName: '' };
131     })(renderFirstName);
132
133 const renderLastName = (item: { lastName: string }) =>
134     <Typography noWrap>{item.lastName}</Typography>;
135
136 export const ResourceLastName = connect(
137     (state: RootState, props: { uuid: string }) => {
138         const resource = getResource<UserResource>(props.uuid)(state.resources);
139         return resource || { lastName: '' };
140     })(renderLastName);
141
142 const renderUuid = (item: { uuid: string }) =>
143     <Typography noWrap>{item.uuid}</Typography>;
144
145 export const ResourceUuid = connect(
146     (state: RootState, props: { uuid: string }) => {
147         const resource = getResource<UserResource>(props.uuid)(state.resources);
148         return resource || { uuid: '' };
149     })(renderUuid);
150
151 const renderEmail = (item: { email: string }) =>
152     <Typography noWrap>{item.email}</Typography>;
153
154 export const ResourceEmail = connect(
155     (state: RootState, props: { uuid: string }) => {
156         const resource = getResource<UserResource>(props.uuid)(state.resources);
157         return resource || { email: '' };
158     })(renderEmail);
159
160 const renderIsActive = (props: { uuid: string, isActive: boolean, toggleIsActive: (uuid: string) => void }) =>
161     <Checkbox
162         color="primary"
163         checked={props.isActive}
164         onClick={() => props.toggleIsActive(props.uuid)} />;
165
166 export const ResourceIsActive = connect(
167     (state: RootState, props: { uuid: string }) => {
168         const resource = getResource<UserResource>(props.uuid)(state.resources);
169         return resource || { isActive: false };
170     }, { toggleIsActive }
171 )(renderIsActive);
172
173 const renderIsAdmin = (props: { uuid: string, isAdmin: boolean, toggleIsAdmin: (uuid: string) => void }) =>
174     <Checkbox
175         color="primary"
176         checked={props.isAdmin}
177         onClick={() => props.toggleIsAdmin(props.uuid)} />;
178
179 export const ResourceIsAdmin = connect(
180     (state: RootState, props: { uuid: string }) => {
181         const resource = getResource<UserResource>(props.uuid)(state.resources);
182         return resource || { isAdmin: false };
183     }, { toggleIsAdmin }
184 )(renderIsAdmin);
185
186 const renderUsername = (item: { username: string }) =>
187     <Typography noWrap>{item.username}</Typography>;
188
189 export const ResourceUsername = connect(
190     (state: RootState, props: { uuid: string }) => {
191         const resource = getResource<UserResource>(props.uuid)(state.resources);
192         return resource || { username: '' };
193     })(renderUsername);
194
195 // Common methods
196 const renderCommonData = (data: string) =>
197     <Typography noWrap>{data}</Typography>;
198
199 const renderCommonDate = (date: string) =>
200     <Typography noWrap>{formatDate(date)}</Typography>;
201
202 export const CommonUuid = withResourceData('uuid', renderCommonData);
203
204 // Api Client Authorizations
205 export const TokenApiClientId = withResourceData('apiClientId', renderCommonData);
206
207 export const TokenApiToken = withResourceData('apiToken', renderCommonData);
208
209 export const TokenCreatedByIpAddress = withResourceData('createdByIpAddress', renderCommonDate);
210
211 export const TokenDefaultOwnerUuid = withResourceData('defaultOwnerUuid', renderCommonData);
212
213 export const TokenExpiresAt = withResourceData('expiresAt', renderCommonDate);
214
215 export const TokenLastUsedAt = withResourceData('lastUsedAt', renderCommonDate);
216
217 export const TokenLastUsedByIpAddress = withResourceData('lastUsedByIpAddress', renderCommonData);
218
219 export const TokenScopes = withResourceData('scopes', renderCommonData);
220
221 export const TokenUserId = withResourceData('userId', renderCommonData);
222
223 // Compute Node Resources
224 const renderNodeInfo = (data: string) => {
225     return <Typography>{JSON.stringify(data, null, 4)}</Typography>;
226 };
227
228 const clusterColors = [
229     ['#f44336', '#fff'],
230     ['#2196f3', '#fff'],
231     ['#009688', '#fff'],
232     ['#cddc39', '#fff'],
233     ['#ff9800', '#fff']
234 ];
235
236 export const ResourceCluster = (props: { uuid: string }) => {
237     const CLUSTER_ID_LENGTH = 5;
238     const pos = props.uuid.indexOf('-');
239     const clusterId = pos >= CLUSTER_ID_LENGTH ? props.uuid.substr(0, pos) : '';
240     const ci = pos >= CLUSTER_ID_LENGTH ? (props.uuid.charCodeAt(0) + props.uuid.charCodeAt(1)) % clusterColors.length : 0;
241     return <Typography>
242         <div style={{
243             backgroundColor: clusterColors[ci][0],
244             color: clusterColors[ci][1],
245             padding: "2px 7px",
246             borderRadius: 3
247         }}>{clusterId}</div>
248     </Typography>;
249 };
250
251 export const ComputeNodeInfo = withResourceData('info', renderNodeInfo);
252
253 export const ComputeNodeDomain = withResourceData('domain', renderCommonData);
254
255 export const ComputeNodeFirstPingAt = withResourceData('firstPingAt', renderCommonDate);
256
257 export const ComputeNodeHostname = withResourceData('hostname', renderCommonData);
258
259 export const ComputeNodeIpAddress = withResourceData('ipAddress', renderCommonData);
260
261 export const ComputeNodeJobUuid = withResourceData('jobUuid', renderCommonData);
262
263 export const ComputeNodeLastPingAt = withResourceData('lastPingAt', renderCommonDate);
264
265 // Links Resources
266 const renderLinkName = (item: { name: string }) =>
267     <Typography noWrap>{item.name || '(none)'}</Typography>;
268
269 export const ResourceLinkName = connect(
270     (state: RootState, props: { uuid: string }) => {
271         const resource = getResource<LinkResource>(props.uuid)(state.resources);
272         return resource || { name: '' };
273     })(renderLinkName);
274
275 const renderLinkClass = (item: { linkClass: string }) =>
276     <Typography noWrap>{item.linkClass}</Typography>;
277
278 export const ResourceLinkClass = connect(
279     (state: RootState, props: { uuid: string }) => {
280         const resource = getResource<LinkResource>(props.uuid)(state.resources);
281         return resource || { linkClass: '' };
282     })(renderLinkClass);
283
284 const renderLinkTail = (dispatch: Dispatch, item: { uuid: string, tailUuid: string, tailKind: string }) => {
285     const currentLabel = resourceLabel(item.tailKind);
286     const isUnknow = currentLabel === "Unknown";
287     return (<div>
288         {!isUnknow ? (
289             renderLink(dispatch, item.tailUuid, currentLabel)
290         ) : (
291                 <Typography noWrap color="default">
292                     {item.tailUuid}
293                 </Typography>
294             )}
295     </div>);
296 };
297
298 const renderLink = (dispatch: Dispatch, uuid: string, label: string) =>
299     <Typography noWrap color="primary" style={{ 'cursor': 'pointer' }} onClick={() => dispatch<any>(navigateTo(uuid))}>
300         {label}: {uuid}
301     </Typography>;
302
303 export const ResourceLinkTail = connect(
304     (state: RootState, props: { uuid: string }) => {
305         const resource = getResource<LinkResource>(props.uuid)(state.resources);
306         return {
307             item: resource || { uuid: '', tailUuid: '', tailKind: ResourceKind.NONE }
308         };
309     })((props: { item: any } & DispatchProp<any>) =>
310         renderLinkTail(props.dispatch, props.item));
311
312 const renderLinkHead = (dispatch: Dispatch, item: { uuid: string, headUuid: string, headKind: ResourceKind }) =>
313     renderLink(dispatch, item.headUuid, resourceLabel(item.headKind));
314
315 export const ResourceLinkHead = connect(
316     (state: RootState, props: { uuid: string }) => {
317         const resource = getResource<LinkResource>(props.uuid)(state.resources);
318         return {
319             item: resource || { uuid: '', headUuid: '', headKind: ResourceKind.NONE }
320         };
321     })((props: { item: any } & DispatchProp<any>) =>
322         renderLinkHead(props.dispatch, props.item));
323
324 export const ResourceLinkUuid = connect(
325     (state: RootState, props: { uuid: string }) => {
326         const resource = getResource<LinkResource>(props.uuid)(state.resources);
327         return resource || { uuid: '' };
328     })(renderUuid);
329
330 // Process Resources
331 const resourceRunProcess = (dispatch: Dispatch, uuid: string) => {
332     return (
333         <div>
334             {uuid &&
335                 <Tooltip title="Run process">
336                     <IconButton onClick={() => dispatch<any>(openRunProcess(uuid))}>
337                         <ProcessIcon />
338                     </IconButton>
339                 </Tooltip>}
340         </div>
341     );
342 };
343
344 export const ResourceRunProcess = connect(
345     (state: RootState, props: { uuid: string }) => {
346         const resource = getResource<WorkflowResource>(props.uuid)(state.resources);
347         return {
348             uuid: resource ? resource.uuid : ''
349         };
350     })((props: { uuid: string } & DispatchProp<any>) =>
351         resourceRunProcess(props.dispatch, props.uuid));
352
353 const renderWorkflowStatus = (uuidPrefix: string, ownerUuid?: string) => {
354     if (ownerUuid === getPublicUuid(uuidPrefix)) {
355         return renderStatus(ResourceStatus.PUBLIC);
356     } else {
357         return renderStatus(ResourceStatus.PRIVATE);
358     }
359 };
360
361 const renderStatus = (status: string) =>
362     <Typography noWrap style={{ width: '60px' }}>{status}</Typography>;
363
364 export const ResourceWorkflowStatus = connect(
365     (state: RootState, props: { uuid: string }) => {
366         const resource = getResource<WorkflowResource>(props.uuid)(state.resources);
367         const uuidPrefix = getUuidPrefix(state);
368         return {
369             ownerUuid: resource ? resource.ownerUuid : '',
370             uuidPrefix
371         };
372     })((props: { ownerUuid?: string, uuidPrefix: string }) => renderWorkflowStatus(props.uuidPrefix, props.ownerUuid));
373
374 export const ResourceLastModifiedDate = connect(
375     (state: RootState, props: { uuid: string }) => {
376         const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
377         return { date: resource ? resource.modifiedAt : '' };
378     })((props: { date: string }) => renderDate(props.date));
379
380 export const ResourceTrashDate = connect(
381     (state: RootState, props: { uuid: string }) => {
382         const resource = getResource<TrashableResource>(props.uuid)(state.resources);
383         return { date: resource ? resource.trashAt : '' };
384     })((props: { date: string }) => renderDate(props.date));
385
386 export const ResourceDeleteDate = connect(
387     (state: RootState, props: { uuid: string }) => {
388         const resource = getResource<TrashableResource>(props.uuid)(state.resources);
389         return { date: resource ? resource.deleteAt : '' };
390     })((props: { date: string }) => renderDate(props.date));
391
392 export const renderFileSize = (fileSize?: number) =>
393     <Typography noWrap style={{ minWidth: '45px' }}>
394         {formatFileSize(fileSize)}
395     </Typography>;
396
397 export const ResourceFileSize = connect(
398     (state: RootState, props: { uuid: string }) => {
399         const resource = getResourceData(props.uuid)(state.resourcesData);
400         return { fileSize: resource ? resource.fileSize : 0 };
401     })((props: { fileSize?: number }) => renderFileSize(props.fileSize));
402
403 const renderOwner = (owner: string) =>
404     <Typography noWrap>
405         {owner}
406     </Typography>;
407
408 export const ResourceOwner = connect(
409     (state: RootState, props: { uuid: string }) => {
410         const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
411         return { owner: resource ? resource.ownerUuid : '' };
412     })((props: { owner: string }) => renderOwner(props.owner));
413
414 export const ResourceOwnerName = connect(
415     (state: RootState, props: { uuid: string }) => {
416         const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
417         const ownerNameState = state.ownerName;
418         const ownerName = ownerNameState.find(it => it.uuid === resource!.ownerUuid);
419         return { owner: ownerName ? ownerName!.name : resource!.ownerUuid };
420     })((props: { owner: string }) => renderOwner(props.owner));
421
422 const renderType = (type: string) =>
423     <Typography noWrap>
424         {resourceLabel(type)}
425     </Typography>;
426
427 export const ResourceType = connect(
428     (state: RootState, props: { uuid: string }) => {
429         const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
430         return { type: resource ? resource.kind : '' };
431     })((props: { type: string }) => renderType(props.type));
432
433 export const ProcessStatus = compose(
434     connect((state: RootState, props: { uuid: string }) => {
435         return { process: getProcess(props.uuid)(state.resources) };
436     }),
437     withStyles({}, { withTheme: true }))
438     ((props: { process?: Process, theme: ArvadosTheme }) => {
439         const status = props.process ? getProcessStatus(props.process) : "-";
440         return <Typography
441             noWrap
442             align="center"
443             style={{ color: getProcessStatusColor(status, props.theme) }} >
444             {status}
445         </Typography>;
446     });