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