18123: Disable delete group link if user lacks permission.
[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 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 { Resource, ResourceKind, TrashableResource } from 'models/resource';
9 import { ProjectIcon, FilterGroupIcon, CollectionIcon, ProcessIcon, DefaultIcon, ShareIcon, CollectionOldVersionIcon, WorkflowIcon, RemoveIcon, RenameIcon } from 'components/icon/icon';
10 import { formatDate, formatFileSize, formatTime } 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, filterResources } from 'store/resources/resources';
15 import { GroupContentsResource } from 'services/groups-service/groups-service';
16 import { getProcess, Process, getProcessStatus, getProcessStatusColor, getProcessRuntime } 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 as WorkflowStatus } from 'views/workflow-panel/workflow-panel-view';
21 import { getUuidPrefix, openRunProcess } from 'store/workflow-panel/workflow-panel-actions';
22 import { openSharingDialog } from 'store/sharing-dialog/sharing-dialog-actions';
23 import { getUserFullname, getUserDisplayName, User, UserResource } from 'models/user';
24 import { toggleIsActive, toggleIsAdmin } from 'store/users/users-actions';
25 import { LinkClass, LinkResource } from 'models/link';
26 import { navigateTo, navigateToGroupDetails } from 'store/navigation/navigation-action';
27 import { withResourceData } from 'views-components/data-explorer/with-resources';
28 import { CollectionResource } from 'models/collection';
29 import { IllegalNamingWarning } from 'components/warning/warning';
30 import { loadResource } from 'store/resources/resources-actions';
31 import { GroupClass, GroupResource } from 'models/group';
32 import { openRemoveGroupMemberDialog } from 'store/group-details-panel/group-details-panel-actions';
33 import { setMemberIsHidden } from 'store/group-details-panel/group-details-panel-actions';
34 import { formatPermissionLevel } from 'views-components/sharing-dialog/permission-select';
35 import { PermissionLevel } from 'models/permission';
36 import { openPermissionEditContextMenu } from 'store/context-menu/context-menu-actions';
37 import { getUserUuid } from 'common/getuser';
38
39 const renderName = (dispatch: Dispatch, item: GroupContentsResource) => {
40
41     const navFunc = ("groupClass" in item && item.groupClass === GroupClass.ROLE ? navigateToGroupDetails : navigateTo);
42     return <Grid container alignItems="center" wrap="nowrap" spacing={16}>
43         <Grid item>
44             {renderIcon(item)}
45         </Grid>
46         <Grid item>
47             <Typography color="primary" style={{ width: 'auto', cursor: 'pointer' }} onClick={() => dispatch<any>(navFunc(item.uuid))}>
48                 {item.kind === ResourceKind.PROJECT || item.kind === ResourceKind.COLLECTION
49                     ? <IllegalNamingWarning name={item.name} />
50                     : null}
51                 {item.name}
52             </Typography>
53         </Grid>
54         <Grid item>
55             <Typography variant="caption">
56                 <FavoriteStar resourceUuid={item.uuid} />
57                 <PublicFavoriteStar resourceUuid={item.uuid} />
58             </Typography>
59         </Grid>
60     </Grid>;
61 };
62
63 export const ResourceName = connect(
64     (state: RootState, props: { uuid: string }) => {
65         const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
66         return resource;
67     })((resource: GroupContentsResource & DispatchProp<any>) => renderName(resource.dispatch, resource));
68
69 const renderIcon = (item: GroupContentsResource) => {
70     switch (item.kind) {
71         case ResourceKind.PROJECT:
72             if (item.groupClass === GroupClass.FILTER) {
73                 return <FilterGroupIcon />;
74             }
75             return <ProjectIcon />;
76         case ResourceKind.COLLECTION:
77             if (item.uuid === item.currentVersionUuid) {
78                 return <CollectionIcon />;
79             }
80             return <CollectionOldVersionIcon />;
81         case ResourceKind.PROCESS:
82             return <ProcessIcon />;
83         case ResourceKind.WORKFLOW:
84             return <WorkflowIcon />;
85         default:
86             return <DefaultIcon />;
87     }
88 };
89
90 const renderDate = (date?: string) => {
91     return <Typography noWrap style={{ minWidth: '100px' }}>{formatDate(date)}</Typography>;
92 };
93
94 const renderWorkflowName = (item: WorkflowResource) =>
95     <Grid container alignItems="center" wrap="nowrap" spacing={16}>
96         <Grid item>
97             {renderIcon(item)}
98         </Grid>
99         <Grid item>
100             <Typography color="primary" style={{ width: '100px' }}>
101                 {item.name}
102             </Typography>
103         </Grid>
104     </Grid>;
105
106 export const ResourceWorkflowName = connect(
107     (state: RootState, props: { uuid: string }) => {
108         const resource = getResource<WorkflowResource>(props.uuid)(state.resources);
109         return resource;
110     })(renderWorkflowName);
111
112 const getPublicUuid = (uuidPrefix: string) => {
113     return `${uuidPrefix}-tpzed-anonymouspublic`;
114 };
115
116 const resourceShare = (dispatch: Dispatch, uuidPrefix: string, ownerUuid?: string, uuid?: string) => {
117     const isPublic = ownerUuid === getPublicUuid(uuidPrefix);
118     return (
119         <div>
120             {!isPublic && uuid &&
121                 <Tooltip title="Share">
122                     <IconButton onClick={() => dispatch<any>(openSharingDialog(uuid))}>
123                         <ShareIcon />
124                     </IconButton>
125                 </Tooltip>
126             }
127         </div>
128     );
129 };
130
131 export const ResourceShare = connect(
132     (state: RootState, props: { uuid: string }) => {
133         const resource = getResource<WorkflowResource>(props.uuid)(state.resources);
134         const uuidPrefix = getUuidPrefix(state);
135         return {
136             uuid: resource ? resource.uuid : '',
137             ownerUuid: resource ? resource.ownerUuid : '',
138             uuidPrefix
139         };
140     })((props: { ownerUuid?: string, uuidPrefix: string, uuid?: string } & DispatchProp<any>) =>
141         resourceShare(props.dispatch, props.uuidPrefix, props.ownerUuid, props.uuid));
142
143 // User Resources
144 const renderFirstName = (item: { firstName: string }) => {
145     return <Typography noWrap>{item.firstName}</Typography>;
146 };
147
148 export const ResourceFirstName = connect(
149     (state: RootState, props: { uuid: string }) => {
150         const resource = getResource<UserResource>(props.uuid)(state.resources);
151         return resource || { firstName: '' };
152     })(renderFirstName);
153
154 const renderLastName = (item: { lastName: string }) =>
155     <Typography noWrap>{item.lastName}</Typography>;
156
157 export const ResourceLastName = connect(
158     (state: RootState, props: { uuid: string }) => {
159         const resource = getResource<UserResource>(props.uuid)(state.resources);
160         return resource || { lastName: '' };
161     })(renderLastName);
162
163 const renderFullName = (item: { firstName: string, lastName: string }) =>
164     <Typography noWrap>{(item.firstName + " " + item.lastName).trim()}</Typography>;
165
166 export const ResourceFullName = connect(
167     (state: RootState, props: { uuid: string }) => {
168         const resource = getResource<UserResource>(props.uuid)(state.resources);
169         return resource || { firstName: '', lastName: '' };
170     })(renderFullName);
171
172
173 const renderUuid = (item: { uuid: string }) =>
174     <Typography noWrap>{item.uuid}</Typography>;
175
176 export const ResourceUuid = connect(
177     (state: RootState, props: { uuid: string }) => {
178         const resource = getResource<UserResource>(props.uuid)(state.resources);
179         return resource || { uuid: '' };
180     })(renderUuid);
181
182 const renderEmail = (item: { email: string }) =>
183     <Typography noWrap>{item.email}</Typography>;
184
185 export const ResourceEmail = connect(
186     (state: RootState, props: { uuid: string }) => {
187         const resource = getResource<UserResource>(props.uuid)(state.resources);
188         return resource || { email: '' };
189     })(renderEmail);
190
191 const renderIsActive = (props: { uuid: string, kind: ResourceKind, isActive: boolean, toggleIsActive: (uuid: string) => void }) => {
192     if (props.kind === ResourceKind.USER) {
193         return <Checkbox
194             color="primary"
195             checked={props.isActive}
196             onClick={() => props.toggleIsActive(props.uuid)} />;
197     } else {
198         return <Typography />;
199     }
200 }
201
202 export const ResourceIsActive = connect(
203     (state: RootState, props: { uuid: string }) => {
204         const resource = getResource<UserResource>(props.uuid)(state.resources);
205         return resource || { isActive: false, kind: ResourceKind.NONE };
206     }, { toggleIsActive }
207 )(renderIsActive);
208
209 export const ResourceLinkTailIsActive = connect(
210     (state: RootState, props: { uuid: string }) => {
211         const link = getResource<LinkResource>(props.uuid)(state.resources);
212         const tailResource = getResource<UserResource>(link?.tailUuid || '')(state.resources);
213
214         return tailResource || { isActive: false, kind: ResourceKind.NONE };
215     }, { toggleIsActive }
216 )(renderIsActive);
217
218 const renderIsHidden = (props: { memberLinkUuid: string, permissionLinkUuid: string, hidden: boolean, setMemberIsHidden: (memberLinkUuid: string, permissionLinkUuid: string, hide: boolean) => void }) => {
219     if (props.memberLinkUuid) {
220         return <Checkbox
221                 color="primary"
222                 checked={props.hidden}
223                 onClick={() => props.setMemberIsHidden(props.memberLinkUuid, props.permissionLinkUuid, !props.hidden)} />;
224     } else {
225         return <Typography />;
226     }
227 }
228
229 export const ResourceLinkTailIsHidden = connect(
230     (state: RootState, props: { uuid: string }) => {
231         const link = getResource<LinkResource>(props.uuid)(state.resources);
232         const member = getResource<Resource>(link?.tailUuid || '')(state.resources);
233         const group = getResource<GroupResource>(link?.headUuid || '')(state.resources);
234         const permissions = filterResources((resource: LinkResource) => {
235             return resource.linkClass === LinkClass.PERMISSION
236                 && resource.headUuid === link?.tailUuid
237                 && resource.tailUuid === group?.uuid
238                 && resource.name === PermissionLevel.CAN_READ;
239         })(state.resources);
240
241         const permissionLinkUuid = permissions.length > 0 ? permissions[0].uuid : '';
242         const isVisible = link && group && permissions.length > 0;
243
244         return member?.kind === ResourceKind.USER
245             ? { memberLinkUuid: link?.uuid, permissionLinkUuid, hidden: !isVisible }
246             : { memberLinkUuid: '', permissionLinkUuid: '', hidden: false};
247     }, { setMemberIsHidden }
248 )(renderIsHidden);
249
250 const renderIsAdmin = (props: { uuid: string, isAdmin: boolean, toggleIsAdmin: (uuid: string) => void }) =>
251     <Checkbox
252         color="primary"
253         checked={props.isAdmin}
254         onClick={() => props.toggleIsAdmin(props.uuid)} />;
255
256 export const ResourceIsAdmin = connect(
257     (state: RootState, props: { uuid: string }) => {
258         const resource = getResource<UserResource>(props.uuid)(state.resources);
259         return resource || { isAdmin: false };
260     }, { toggleIsAdmin }
261 )(renderIsAdmin);
262
263 const renderUsername = (item: { username: string }) =>
264     <Typography noWrap>{item.username}</Typography>;
265
266 export const ResourceUsername = connect(
267     (state: RootState, props: { uuid: string }) => {
268         const resource = getResource<UserResource>(props.uuid)(state.resources);
269         return resource || { username: '' };
270     })(renderUsername);
271
272 // Common methods
273 const renderCommonData = (data: string) =>
274     <Typography noWrap>{data}</Typography>;
275
276 const renderCommonDate = (date: string) =>
277     <Typography noWrap>{formatDate(date)}</Typography>;
278
279 export const CommonUuid = withResourceData('uuid', renderCommonData);
280
281 // Api Client Authorizations
282 export const TokenApiClientId = withResourceData('apiClientId', renderCommonData);
283
284 export const TokenApiToken = withResourceData('apiToken', renderCommonData);
285
286 export const TokenCreatedByIpAddress = withResourceData('createdByIpAddress', renderCommonDate);
287
288 export const TokenDefaultOwnerUuid = withResourceData('defaultOwnerUuid', renderCommonData);
289
290 export const TokenExpiresAt = withResourceData('expiresAt', renderCommonDate);
291
292 export const TokenLastUsedAt = withResourceData('lastUsedAt', renderCommonDate);
293
294 export const TokenLastUsedByIpAddress = withResourceData('lastUsedByIpAddress', renderCommonData);
295
296 export const TokenScopes = withResourceData('scopes', renderCommonData);
297
298 export const TokenUserId = withResourceData('userId', renderCommonData);
299
300 const clusterColors = [
301     ['#f44336', '#fff'],
302     ['#2196f3', '#fff'],
303     ['#009688', '#fff'],
304     ['#cddc39', '#fff'],
305     ['#ff9800', '#fff']
306 ];
307
308 export const ResourceCluster = (props: { uuid: string }) => {
309     const CLUSTER_ID_LENGTH = 5;
310     const pos = props.uuid.length > CLUSTER_ID_LENGTH ? props.uuid.indexOf('-') : 5;
311     const clusterId = pos >= CLUSTER_ID_LENGTH ? props.uuid.substr(0, pos) : '';
312     const ci = pos >= CLUSTER_ID_LENGTH ? (((((
313         (props.uuid.charCodeAt(0) * props.uuid.charCodeAt(1))
314         + props.uuid.charCodeAt(2))
315         * props.uuid.charCodeAt(3))
316         + props.uuid.charCodeAt(4))) % clusterColors.length) : 0;
317     return <span style={{
318         backgroundColor: clusterColors[ci][0],
319         color: clusterColors[ci][1],
320         padding: "2px 7px",
321         borderRadius: 3
322     }}>{clusterId}</span>;
323 };
324
325 // Links Resources
326 const renderLinkName = (item: { name: string }) =>
327     <Typography noWrap>{item.name || '(none)'}</Typography>;
328
329 export const ResourceLinkName = connect(
330     (state: RootState, props: { uuid: string }) => {
331         const resource = getResource<LinkResource>(props.uuid)(state.resources);
332         return resource || { name: '' };
333     })(renderLinkName);
334
335 const renderLinkClass = (item: { linkClass: string }) =>
336     <Typography noWrap>{item.linkClass}</Typography>;
337
338 export const ResourceLinkClass = connect(
339     (state: RootState, props: { uuid: string }) => {
340         const resource = getResource<LinkResource>(props.uuid)(state.resources);
341         return resource || { linkClass: '' };
342     })(renderLinkClass);
343
344 const getResourceDisplayName = (resource: Resource): string => {
345     if ((resource as UserResource).kind === ResourceKind.USER
346           && typeof (resource as UserResource).firstName !== 'undefined') {
347         // We can be sure the resource is UserResource
348         return getUserDisplayName(resource as UserResource);
349     } else {
350         return (resource as GroupContentsResource).name;
351     }
352 }
353
354 const renderResourceLink = (dispatch: Dispatch, item: Resource) => {
355     var displayName = getResourceDisplayName(item);
356
357     return <Typography noWrap color="primary" style={{ 'cursor': 'pointer' }} onClick={() => dispatch<any>(navigateTo(item.uuid))}>
358         {resourceLabel(item.kind)}: {displayName || item.uuid}
359     </Typography>;
360 };
361
362 export const ResourceLinkTail = connect(
363     (state: RootState, props: { uuid: string }) => {
364         const resource = getResource<LinkResource>(props.uuid)(state.resources);
365         const tailResource = getResource<Resource>(resource?.tailUuid || '')(state.resources);
366
367         return {
368             item: tailResource || { uuid: resource?.tailUuid || '', kind: resource?.tailKind || ResourceKind.NONE }
369         };
370     })((props: { item: Resource } & DispatchProp<any>) =>
371         renderResourceLink(props.dispatch, props.item));
372
373 export const ResourceLinkHead = connect(
374     (state: RootState, props: { uuid: string }) => {
375         const resource = getResource<LinkResource>(props.uuid)(state.resources);
376         const headResource = getResource<Resource>(resource?.headUuid || '')(state.resources);
377
378         return {
379             item: headResource || { uuid: resource?.headUuid || '', kind: resource?.headKind || ResourceKind.NONE }
380         };
381     })((props: { item: Resource } & DispatchProp<any>) =>
382         renderResourceLink(props.dispatch, props.item));
383
384 export const ResourceLinkUuid = connect(
385     (state: RootState, props: { uuid: string }) => {
386         const resource = getResource<LinkResource>(props.uuid)(state.resources);
387         return resource || { uuid: '' };
388     })(renderUuid);
389
390 export const ResourceLinkHeadUuid = connect(
391     (state: RootState, props: { uuid: string }) => {
392         const link = getResource<LinkResource>(props.uuid)(state.resources);
393         const headResource = getResource<Resource>(link?.headUuid || '')(state.resources);
394
395         return headResource || { uuid: '' };
396     })(renderUuid);
397
398 export const ResourceLinkTailUuid = connect(
399     (state: RootState, props: { uuid: string }) => {
400         const link = getResource<LinkResource>(props.uuid)(state.resources);
401         const tailResource = getResource<Resource>(link?.tailUuid || '')(state.resources);
402
403         return tailResource || { uuid: '' };
404     })(renderUuid);
405
406 const renderLinkDelete = (dispatch: Dispatch, item: LinkResource, canManage: boolean) => {
407     if (item.uuid) {
408         return canManage ?
409             <Typography noWrap>
410                 <IconButton data-cy="resource-delete-button" onClick={() => dispatch<any>(openRemoveGroupMemberDialog(item.uuid))}>
411                     <RemoveIcon />
412                 </IconButton>
413             </Typography> :
414             <Typography noWrap>
415                 <IconButton disabled data-cy="resource-delete-button">
416                     <RemoveIcon />
417                 </IconButton>
418             </Typography>;
419     } else {
420       return <Typography noWrap></Typography>;
421     }
422 }
423
424 export const ResourceLinkDelete = connect(
425     (state: RootState, props: { uuid: string }) => {
426         const link = getResource<LinkResource>(props.uuid)(state.resources);
427         return {
428             item: link || { uuid: '', kind: ResourceKind.NONE },
429             canManage: link && getResourceLinkCanManage(state, link),
430         };
431     })((props: { item: LinkResource, canManage: boolean } & DispatchProp<any>) =>
432       renderLinkDelete(props.dispatch, props.item, props.canManage));
433
434 export const ResourceLinkTailEmail = connect(
435     (state: RootState, props: { uuid: string }) => {
436         const link = getResource<LinkResource>(props.uuid)(state.resources);
437         const resource = getResource<UserResource>(link?.tailUuid || '')(state.resources);
438
439         return resource || { email: '' };
440     })(renderEmail);
441
442 export const ResourceLinkTailUsername = connect(
443     (state: RootState, props: { uuid: string }) => {
444         const link = getResource<LinkResource>(props.uuid)(state.resources);
445         const resource = getResource<UserResource>(link?.tailUuid || '')(state.resources);
446
447         return resource || { username: '' };
448     })(renderUsername);
449
450 const renderPermissionLevel = (dispatch: Dispatch, link: LinkResource, canManage: boolean) => {
451     return <Typography noWrap>
452         {formatPermissionLevel(link.name as PermissionLevel)}
453         {canManage ?
454             <IconButton onClick={(event) => dispatch<any>(openPermissionEditContextMenu(event, link))}>
455                 <RenameIcon />
456             </IconButton> :
457             ''
458         }
459     </Typography>;
460 }
461
462 export const ResourceLinkHeadPermissionLevel = connect(
463     (state: RootState, props: { uuid: string }) => {
464         const link = getResource<LinkResource>(props.uuid)(state.resources);
465
466         return {
467             link: link || { uuid: '', name: '', kind: ResourceKind.NONE },
468             canManage: link && getResourceLinkCanManage(state, link),
469         };
470     })((props: { link: LinkResource, canManage: boolean } & DispatchProp<any>) =>
471         renderPermissionLevel(props.dispatch, props.link, props.canManage));
472
473 export const ResourceLinkTailPermissionLevel = connect(
474     (state: RootState, props: { uuid: string }) => {
475         const link = getResource<LinkResource>(props.uuid)(state.resources);
476
477         return {
478             link: link || { uuid: '', name: '', kind: ResourceKind.NONE },
479             canManage: link && getResourceLinkCanManage(state, link),
480         };
481     })((props: { link: LinkResource, canManage: boolean } & DispatchProp<any>) =>
482         renderPermissionLevel(props.dispatch, props.link, props.canManage));
483
484 const getResourceLinkCanManage = (state: RootState, link: LinkResource) => {
485     const headResource = getResource<Resource>(link.headUuid)(state.resources);
486     // const tailResource = getResource<Resource>(link.tailUuid)(state.resources);
487     const userUuid = getUserUuid(state);
488
489     if (headResource && headResource.kind === ResourceKind.GROUP) {
490         return userUuid ? (headResource as GroupResource).writableBy?.includes(userUuid) : false;
491     } else {
492         // true for now
493         return true;
494     }
495 }
496
497 // Process Resources
498 const resourceRunProcess = (dispatch: Dispatch, uuid: string) => {
499     return (
500         <div>
501             {uuid &&
502                 <Tooltip title="Run process">
503                     <IconButton onClick={() => dispatch<any>(openRunProcess(uuid))}>
504                         <ProcessIcon />
505                     </IconButton>
506                 </Tooltip>}
507         </div>
508     );
509 };
510
511 export const ResourceRunProcess = connect(
512     (state: RootState, props: { uuid: string }) => {
513         const resource = getResource<WorkflowResource>(props.uuid)(state.resources);
514         return {
515             uuid: resource ? resource.uuid : ''
516         };
517     })((props: { uuid: string } & DispatchProp<any>) =>
518         resourceRunProcess(props.dispatch, props.uuid));
519
520 const renderWorkflowStatus = (uuidPrefix: string, ownerUuid?: string) => {
521     if (ownerUuid === getPublicUuid(uuidPrefix)) {
522         return renderStatus(WorkflowStatus.PUBLIC);
523     } else {
524         return renderStatus(WorkflowStatus.PRIVATE);
525     }
526 };
527
528 const renderStatus = (status: string) =>
529     <Typography noWrap style={{ width: '60px' }}>{status}</Typography>;
530
531 export const ResourceWorkflowStatus = connect(
532     (state: RootState, props: { uuid: string }) => {
533         const resource = getResource<WorkflowResource>(props.uuid)(state.resources);
534         const uuidPrefix = getUuidPrefix(state);
535         return {
536             ownerUuid: resource ? resource.ownerUuid : '',
537             uuidPrefix
538         };
539     })((props: { ownerUuid?: string, uuidPrefix: string }) => renderWorkflowStatus(props.uuidPrefix, props.ownerUuid));
540
541 export const ResourceLastModifiedDate = connect(
542     (state: RootState, props: { uuid: string }) => {
543         const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
544         return { date: resource ? resource.modifiedAt : '' };
545     })((props: { date: string }) => renderDate(props.date));
546
547 export const ResourceCreatedAtDate = connect(
548     (state: RootState, props: { uuid: string }) => {
549         const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
550         return { date: resource ? resource.createdAt : '' };
551     })((props: { date: string }) => renderDate(props.date));
552
553 export const ResourceTrashDate = connect(
554     (state: RootState, props: { uuid: string }) => {
555         const resource = getResource<TrashableResource>(props.uuid)(state.resources);
556         return { date: resource ? resource.trashAt : '' };
557     })((props: { date: string }) => renderDate(props.date));
558
559 export const ResourceDeleteDate = connect(
560     (state: RootState, props: { uuid: string }) => {
561         const resource = getResource<TrashableResource>(props.uuid)(state.resources);
562         return { date: resource ? resource.deleteAt : '' };
563     })((props: { date: string }) => renderDate(props.date));
564
565 export const renderFileSize = (fileSize?: number) =>
566     <Typography noWrap style={{ minWidth: '45px' }}>
567         {formatFileSize(fileSize)}
568     </Typography>;
569
570 export const ResourceFileSize = connect(
571     (state: RootState, props: { uuid: string }) => {
572         const resource = getResource<CollectionResource>(props.uuid)(state.resources);
573
574         if (resource && resource.kind !== ResourceKind.COLLECTION) {
575             return { fileSize: '' };
576         }
577
578         return { fileSize: resource ? resource.fileSizeTotal : 0 };
579     })((props: { fileSize?: number }) => renderFileSize(props.fileSize));
580
581 const renderOwner = (owner: string) =>
582     <Typography noWrap>
583         {owner}
584     </Typography>;
585
586 export const ResourceOwner = connect(
587     (state: RootState, props: { uuid: string }) => {
588         const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
589         return { owner: resource ? resource.ownerUuid : '' };
590     })((props: { owner: string }) => renderOwner(props.owner));
591
592 export const ResourceOwnerName = connect(
593     (state: RootState, props: { uuid: string }) => {
594         const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
595         const ownerNameState = state.ownerName;
596         const ownerName = ownerNameState.find(it => it.uuid === resource!.ownerUuid);
597         return { owner: ownerName ? ownerName!.name : resource!.ownerUuid };
598     })((props: { owner: string }) => renderOwner(props.owner));
599
600 const userFromID =
601     connect(
602         (state: RootState, props: { uuid: string }) => {
603             let userFullname = '';
604             const resource = getResource<GroupContentsResource & UserResource>(props.uuid)(state.resources);
605
606             if (resource) {
607                 userFullname = getUserFullname(resource as User) || (resource as GroupContentsResource).name;
608             }
609
610             return { uuid: props.uuid, userFullname };
611         });
612
613 export const ResourceOwnerWithName =
614     compose(
615         userFromID,
616         withStyles({}, { withTheme: true }))
617         ((props: { uuid: string, userFullname: string, dispatch: Dispatch, theme: ArvadosTheme }) => {
618             const { uuid, userFullname, dispatch, theme } = props;
619
620             if (userFullname === '') {
621                 dispatch<any>(loadResource(uuid, false));
622                 return <Typography style={{ color: theme.palette.primary.main }} inline noWrap>
623                     {uuid}
624                 </Typography>;
625             }
626
627             return <Typography style={{ color: theme.palette.primary.main }} inline noWrap>
628                 {userFullname} ({uuid})
629             </Typography>;
630         });
631
632 export const UserNameFromID =
633     compose(userFromID)(
634         (props: { uuid: string, userFullname: string, dispatch: Dispatch }) => {
635             const { uuid, userFullname, dispatch } = props;
636
637             if (userFullname === '') {
638                 dispatch<any>(loadResource(uuid, false));
639             }
640             return <span>
641                 {userFullname ? userFullname : uuid}
642             </span>;
643         });
644
645 export const ResponsiblePerson =
646     compose(
647         connect(
648             (state: RootState, props: { uuid: string, parentRef: HTMLElement | null }) => {
649                 let responsiblePersonName: string = '';
650                 let responsiblePersonUUID: string = '';
651                 let responsiblePersonProperty: string = '';
652
653                 if (state.auth.config.clusterConfig.Collections.ManagedProperties) {
654                     let index = 0;
655                     const keys = Object.keys(state.auth.config.clusterConfig.Collections.ManagedProperties);
656
657                     while (!responsiblePersonProperty && keys[index]) {
658                         const key = keys[index];
659                         if (state.auth.config.clusterConfig.Collections.ManagedProperties[key].Function === 'original_owner') {
660                             responsiblePersonProperty = key;
661                         }
662                         index++;
663                     }
664                 }
665
666                 let resource: Resource | undefined = getResource<GroupContentsResource & UserResource>(props.uuid)(state.resources);
667
668                 while (resource && resource.kind !== ResourceKind.USER && responsiblePersonProperty) {
669                     responsiblePersonUUID = (resource as CollectionResource).properties[responsiblePersonProperty];
670                     resource = getResource<GroupContentsResource & UserResource>(responsiblePersonUUID)(state.resources);
671                 }
672
673                 if (resource && resource.kind === ResourceKind.USER) {
674                     responsiblePersonName = getUserFullname(resource as UserResource) || (resource as GroupContentsResource).name;
675                 }
676
677                 return { uuid: responsiblePersonUUID, responsiblePersonName, parentRef: props.parentRef };
678             }),
679         withStyles({}, { withTheme: true }))
680         ((props: { uuid: string | null, responsiblePersonName: string, parentRef: HTMLElement | null, theme: ArvadosTheme }) => {
681             const { uuid, responsiblePersonName, parentRef, theme } = props;
682
683             if (!uuid && parentRef) {
684                 parentRef.style.display = 'none';
685                 return null;
686             } else if (parentRef) {
687                 parentRef.style.display = 'block';
688             }
689
690             if (!responsiblePersonName) {
691                 return <Typography style={{ color: theme.palette.primary.main }} inline noWrap>
692                     {uuid}
693                 </Typography>;
694             }
695
696             return <Typography style={{ color: theme.palette.primary.main }} inline noWrap>
697                 {responsiblePersonName} ({uuid})
698                 </Typography>;
699         });
700
701 const renderType = (type: string, subtype: string) =>
702     <Typography noWrap>
703         {resourceLabel(type, subtype)}
704     </Typography>;
705
706 export const ResourceType = connect(
707     (state: RootState, props: { uuid: string }) => {
708         const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
709         return { type: resource ? resource.kind : '', subtype: resource && resource.kind === ResourceKind.GROUP ? resource.groupClass : '' };
710     })((props: { type: string, subtype: string }) => renderType(props.type, props.subtype));
711
712 export const ResourceStatus = connect((state: RootState, props: { uuid: string }) => {
713     return { resource: getResource<GroupContentsResource>(props.uuid)(state.resources) };
714 })((props: { resource: GroupContentsResource }) =>
715     (props.resource && props.resource.kind === ResourceKind.COLLECTION)
716         ? <CollectionStatus uuid={props.resource.uuid} />
717         : <ProcessStatus uuid={props.resource.uuid} />
718 );
719
720 export const CollectionStatus = connect((state: RootState, props: { uuid: string }) => {
721     return { collection: getResource<CollectionResource>(props.uuid)(state.resources) };
722 })((props: { collection: CollectionResource }) =>
723     (props.collection.uuid !== props.collection.currentVersionUuid)
724         ? <Typography>version {props.collection.version}</Typography>
725         : <Typography>head version</Typography>
726 );
727
728 export const ProcessStatus = compose(
729     connect((state: RootState, props: { uuid: string }) => {
730         return { process: getProcess(props.uuid)(state.resources) };
731     }),
732     withStyles({}, { withTheme: true }))
733     ((props: { process?: Process, theme: ArvadosTheme }) => {
734         const status = props.process ? getProcessStatus(props.process) : "-";
735         return <Typography
736             noWrap
737             style={{ color: getProcessStatusColor(status, props.theme) }} >
738             {status}
739         </Typography>;
740     });
741
742 export const ProcessStartDate = connect(
743     (state: RootState, props: { uuid: string }) => {
744         const process = getProcess(props.uuid)(state.resources);
745         return { date: (process && process.container) ? process.container.startedAt : '' };
746     })((props: { date: string }) => renderDate(props.date));
747
748 export const renderRunTime = (time: number) =>
749     <Typography noWrap style={{ minWidth: '45px' }}>
750         {formatTime(time, true)}
751     </Typography>;
752
753 interface ContainerRunTimeProps {
754     process: Process;
755 }
756
757 interface ContainerRunTimeState {
758     runtime: number;
759 }
760
761 export const ContainerRunTime = connect((state: RootState, props: { uuid: string }) => {
762     return { process: getProcess(props.uuid)(state.resources) };
763 })(class extends React.Component<ContainerRunTimeProps, ContainerRunTimeState> {
764     private timer: any;
765
766     constructor(props: ContainerRunTimeProps) {
767         super(props);
768         this.state = { runtime: this.getRuntime() };
769     }
770
771     getRuntime() {
772         return this.props.process ? getProcessRuntime(this.props.process) : 0;
773     }
774
775     updateRuntime() {
776         this.setState({ runtime: this.getRuntime() });
777     }
778
779     componentDidMount() {
780         this.timer = setInterval(this.updateRuntime.bind(this), 5000);
781     }
782
783     componentWillUnmount() {
784         clearInterval(this.timer);
785     }
786
787     render() {
788         return renderRunTime(this.state.runtime);
789     }
790 });