18123: Change edit permission level to context menu.
[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) => {
407     if (item.uuid) {
408         return <Typography noWrap>
409             <IconButton data-cy="resource-delete-button" onClick={() => dispatch<any>(openRemoveGroupMemberDialog(item.uuid))}>
410                 <RemoveIcon />
411             </IconButton>
412         </Typography>;
413     } else {
414       return <Typography noWrap></Typography>;
415     }
416 }
417
418 export const ResourceLinkDelete = connect(
419     (state: RootState, props: { uuid: string }) => {
420         const link = getResource<LinkResource>(props.uuid)(state.resources);
421         return {
422             item: link || { uuid: '', kind: ResourceKind.NONE }
423         };
424     })((props: { item: LinkResource } & DispatchProp<any>) =>
425       renderLinkDelete(props.dispatch, props.item));
426
427 export const ResourceLinkTailEmail = connect(
428     (state: RootState, props: { uuid: string }) => {
429         const link = getResource<LinkResource>(props.uuid)(state.resources);
430         const resource = getResource<UserResource>(link?.tailUuid || '')(state.resources);
431
432         return resource || { email: '' };
433     })(renderEmail);
434
435 export const ResourceLinkTailUsername = connect(
436     (state: RootState, props: { uuid: string }) => {
437         const link = getResource<LinkResource>(props.uuid)(state.resources);
438         const resource = getResource<UserResource>(link?.tailUuid || '')(state.resources);
439
440         return resource || { username: '' };
441     })(renderUsername);
442
443 const renderPermissionLevel = (dispatch: Dispatch, link: LinkResource, canManage: boolean) => {
444     return <Typography noWrap>
445         {formatPermissionLevel(link.name as PermissionLevel)}
446         {canManage ?
447             <IconButton onClick={(event) => dispatch<any>(openPermissionEditContextMenu(event, link))}>
448                 <RenameIcon />
449             </IconButton> :
450             ''
451         }
452     </Typography>;
453 }
454
455 export const ResourceLinkHeadPermissionLevel = connect(
456     (state: RootState, props: { uuid: string }) => {
457         const link = getResource<LinkResource>(props.uuid)(state.resources);
458
459         return {
460             link: link || { uuid: '', name: '', kind: ResourceKind.NONE },
461             canManage: link && getResourceLinkCanManage(state, link),
462         };
463     })((props: { link: LinkResource, canManage: boolean } & DispatchProp<any>) =>
464         renderPermissionLevel(props.dispatch, props.link, props.canManage));
465
466 export const ResourceLinkTailPermissionLevel = connect(
467     (state: RootState, props: { uuid: string }) => {
468         const link = getResource<LinkResource>(props.uuid)(state.resources);
469
470         return {
471             link: link || { uuid: '', name: '', kind: ResourceKind.NONE },
472             canManage: link && getResourceLinkCanManage(state, link),
473         };
474     })((props: { link: LinkResource, canManage: boolean } & DispatchProp<any>) =>
475         renderPermissionLevel(props.dispatch, props.link, props.canManage));
476
477 const getResourceLinkCanManage = (state: RootState, link: LinkResource) => {
478     const headResource = getResource<Resource>(link.headUuid)(state.resources);
479     // const tailResource = getResource<Resource>(link.tailUuid)(state.resources);
480     const userUuid = getUserUuid(state);
481
482     if (headResource && headResource.kind === ResourceKind.GROUP) {
483         return userUuid ? (headResource as GroupResource).writableBy?.includes(userUuid) : false;
484     } else {
485         // true for now
486         return true;
487     }
488 }
489
490 // Process Resources
491 const resourceRunProcess = (dispatch: Dispatch, uuid: string) => {
492     return (
493         <div>
494             {uuid &&
495                 <Tooltip title="Run process">
496                     <IconButton onClick={() => dispatch<any>(openRunProcess(uuid))}>
497                         <ProcessIcon />
498                     </IconButton>
499                 </Tooltip>}
500         </div>
501     );
502 };
503
504 export const ResourceRunProcess = connect(
505     (state: RootState, props: { uuid: string }) => {
506         const resource = getResource<WorkflowResource>(props.uuid)(state.resources);
507         return {
508             uuid: resource ? resource.uuid : ''
509         };
510     })((props: { uuid: string } & DispatchProp<any>) =>
511         resourceRunProcess(props.dispatch, props.uuid));
512
513 const renderWorkflowStatus = (uuidPrefix: string, ownerUuid?: string) => {
514     if (ownerUuid === getPublicUuid(uuidPrefix)) {
515         return renderStatus(WorkflowStatus.PUBLIC);
516     } else {
517         return renderStatus(WorkflowStatus.PRIVATE);
518     }
519 };
520
521 const renderStatus = (status: string) =>
522     <Typography noWrap style={{ width: '60px' }}>{status}</Typography>;
523
524 export const ResourceWorkflowStatus = connect(
525     (state: RootState, props: { uuid: string }) => {
526         const resource = getResource<WorkflowResource>(props.uuid)(state.resources);
527         const uuidPrefix = getUuidPrefix(state);
528         return {
529             ownerUuid: resource ? resource.ownerUuid : '',
530             uuidPrefix
531         };
532     })((props: { ownerUuid?: string, uuidPrefix: string }) => renderWorkflowStatus(props.uuidPrefix, props.ownerUuid));
533
534 export const ResourceLastModifiedDate = connect(
535     (state: RootState, props: { uuid: string }) => {
536         const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
537         return { date: resource ? resource.modifiedAt : '' };
538     })((props: { date: string }) => renderDate(props.date));
539
540 export const ResourceCreatedAtDate = connect(
541     (state: RootState, props: { uuid: string }) => {
542         const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
543         return { date: resource ? resource.createdAt : '' };
544     })((props: { date: string }) => renderDate(props.date));
545
546 export const ResourceTrashDate = connect(
547     (state: RootState, props: { uuid: string }) => {
548         const resource = getResource<TrashableResource>(props.uuid)(state.resources);
549         return { date: resource ? resource.trashAt : '' };
550     })((props: { date: string }) => renderDate(props.date));
551
552 export const ResourceDeleteDate = connect(
553     (state: RootState, props: { uuid: string }) => {
554         const resource = getResource<TrashableResource>(props.uuid)(state.resources);
555         return { date: resource ? resource.deleteAt : '' };
556     })((props: { date: string }) => renderDate(props.date));
557
558 export const renderFileSize = (fileSize?: number) =>
559     <Typography noWrap style={{ minWidth: '45px' }}>
560         {formatFileSize(fileSize)}
561     </Typography>;
562
563 export const ResourceFileSize = connect(
564     (state: RootState, props: { uuid: string }) => {
565         const resource = getResource<CollectionResource>(props.uuid)(state.resources);
566
567         if (resource && resource.kind !== ResourceKind.COLLECTION) {
568             return { fileSize: '' };
569         }
570
571         return { fileSize: resource ? resource.fileSizeTotal : 0 };
572     })((props: { fileSize?: number }) => renderFileSize(props.fileSize));
573
574 const renderOwner = (owner: string) =>
575     <Typography noWrap>
576         {owner}
577     </Typography>;
578
579 export const ResourceOwner = connect(
580     (state: RootState, props: { uuid: string }) => {
581         const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
582         return { owner: resource ? resource.ownerUuid : '' };
583     })((props: { owner: string }) => renderOwner(props.owner));
584
585 export const ResourceOwnerName = connect(
586     (state: RootState, props: { uuid: string }) => {
587         const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
588         const ownerNameState = state.ownerName;
589         const ownerName = ownerNameState.find(it => it.uuid === resource!.ownerUuid);
590         return { owner: ownerName ? ownerName!.name : resource!.ownerUuid };
591     })((props: { owner: string }) => renderOwner(props.owner));
592
593 const userFromID =
594     connect(
595         (state: RootState, props: { uuid: string }) => {
596             let userFullname = '';
597             const resource = getResource<GroupContentsResource & UserResource>(props.uuid)(state.resources);
598
599             if (resource) {
600                 userFullname = getUserFullname(resource as User) || (resource as GroupContentsResource).name;
601             }
602
603             return { uuid: props.uuid, userFullname };
604         });
605
606 export const ResourceOwnerWithName =
607     compose(
608         userFromID,
609         withStyles({}, { withTheme: true }))
610         ((props: { uuid: string, userFullname: string, dispatch: Dispatch, theme: ArvadosTheme }) => {
611             const { uuid, userFullname, dispatch, theme } = props;
612
613             if (userFullname === '') {
614                 dispatch<any>(loadResource(uuid, false));
615                 return <Typography style={{ color: theme.palette.primary.main }} inline noWrap>
616                     {uuid}
617                 </Typography>;
618             }
619
620             return <Typography style={{ color: theme.palette.primary.main }} inline noWrap>
621                 {userFullname} ({uuid})
622             </Typography>;
623         });
624
625 export const UserNameFromID =
626     compose(userFromID)(
627         (props: { uuid: string, userFullname: string, dispatch: Dispatch }) => {
628             const { uuid, userFullname, dispatch } = props;
629
630             if (userFullname === '') {
631                 dispatch<any>(loadResource(uuid, false));
632             }
633             return <span>
634                 {userFullname ? userFullname : uuid}
635             </span>;
636         });
637
638 export const ResponsiblePerson =
639     compose(
640         connect(
641             (state: RootState, props: { uuid: string, parentRef: HTMLElement | null }) => {
642                 let responsiblePersonName: string = '';
643                 let responsiblePersonUUID: string = '';
644                 let responsiblePersonProperty: string = '';
645
646                 if (state.auth.config.clusterConfig.Collections.ManagedProperties) {
647                     let index = 0;
648                     const keys = Object.keys(state.auth.config.clusterConfig.Collections.ManagedProperties);
649
650                     while (!responsiblePersonProperty && keys[index]) {
651                         const key = keys[index];
652                         if (state.auth.config.clusterConfig.Collections.ManagedProperties[key].Function === 'original_owner') {
653                             responsiblePersonProperty = key;
654                         }
655                         index++;
656                     }
657                 }
658
659                 let resource: Resource | undefined = getResource<GroupContentsResource & UserResource>(props.uuid)(state.resources);
660
661                 while (resource && resource.kind !== ResourceKind.USER && responsiblePersonProperty) {
662                     responsiblePersonUUID = (resource as CollectionResource).properties[responsiblePersonProperty];
663                     resource = getResource<GroupContentsResource & UserResource>(responsiblePersonUUID)(state.resources);
664                 }
665
666                 if (resource && resource.kind === ResourceKind.USER) {
667                     responsiblePersonName = getUserFullname(resource as UserResource) || (resource as GroupContentsResource).name;
668                 }
669
670                 return { uuid: responsiblePersonUUID, responsiblePersonName, parentRef: props.parentRef };
671             }),
672         withStyles({}, { withTheme: true }))
673         ((props: { uuid: string | null, responsiblePersonName: string, parentRef: HTMLElement | null, theme: ArvadosTheme }) => {
674             const { uuid, responsiblePersonName, parentRef, theme } = props;
675
676             if (!uuid && parentRef) {
677                 parentRef.style.display = 'none';
678                 return null;
679             } else if (parentRef) {
680                 parentRef.style.display = 'block';
681             }
682
683             if (!responsiblePersonName) {
684                 return <Typography style={{ color: theme.palette.primary.main }} inline noWrap>
685                     {uuid}
686                 </Typography>;
687             }
688
689             return <Typography style={{ color: theme.palette.primary.main }} inline noWrap>
690                 {responsiblePersonName} ({uuid})
691                 </Typography>;
692         });
693
694 const renderType = (type: string, subtype: string) =>
695     <Typography noWrap>
696         {resourceLabel(type, subtype)}
697     </Typography>;
698
699 export const ResourceType = connect(
700     (state: RootState, props: { uuid: string }) => {
701         const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
702         return { type: resource ? resource.kind : '', subtype: resource && resource.kind === ResourceKind.GROUP ? resource.groupClass : '' };
703     })((props: { type: string, subtype: string }) => renderType(props.type, props.subtype));
704
705 export const ResourceStatus = connect((state: RootState, props: { uuid: string }) => {
706     return { resource: getResource<GroupContentsResource>(props.uuid)(state.resources) };
707 })((props: { resource: GroupContentsResource }) =>
708     (props.resource && props.resource.kind === ResourceKind.COLLECTION)
709         ? <CollectionStatus uuid={props.resource.uuid} />
710         : <ProcessStatus uuid={props.resource.uuid} />
711 );
712
713 export const CollectionStatus = connect((state: RootState, props: { uuid: string }) => {
714     return { collection: getResource<CollectionResource>(props.uuid)(state.resources) };
715 })((props: { collection: CollectionResource }) =>
716     (props.collection.uuid !== props.collection.currentVersionUuid)
717         ? <Typography>version {props.collection.version}</Typography>
718         : <Typography>head version</Typography>
719 );
720
721 export const ProcessStatus = compose(
722     connect((state: RootState, props: { uuid: string }) => {
723         return { process: getProcess(props.uuid)(state.resources) };
724     }),
725     withStyles({}, { withTheme: true }))
726     ((props: { process?: Process, theme: ArvadosTheme }) => {
727         const status = props.process ? getProcessStatus(props.process) : "-";
728         return <Typography
729             noWrap
730             style={{ color: getProcessStatusColor(status, props.theme) }} >
731             {status}
732         </Typography>;
733     });
734
735 export const ProcessStartDate = connect(
736     (state: RootState, props: { uuid: string }) => {
737         const process = getProcess(props.uuid)(state.resources);
738         return { date: (process && process.container) ? process.container.startedAt : '' };
739     })((props: { date: string }) => renderDate(props.date));
740
741 export const renderRunTime = (time: number) =>
742     <Typography noWrap style={{ minWidth: '45px' }}>
743         {formatTime(time, true)}
744     </Typography>;
745
746 interface ContainerRunTimeProps {
747     process: Process;
748 }
749
750 interface ContainerRunTimeState {
751     runtime: number;
752 }
753
754 export const ContainerRunTime = connect((state: RootState, props: { uuid: string }) => {
755     return { process: getProcess(props.uuid)(state.resources) };
756 })(class extends React.Component<ContainerRunTimeProps, ContainerRunTimeState> {
757     private timer: any;
758
759     constructor(props: ContainerRunTimeProps) {
760         super(props);
761         this.state = { runtime: this.getRuntime() };
762     }
763
764     getRuntime() {
765         return this.props.process ? getProcessRuntime(this.props.process) : 0;
766     }
767
768     updateRuntime() {
769         this.setState({ runtime: this.getRuntime() });
770     }
771
772     componentDidMount() {
773         this.timer = setInterval(this.updateRuntime.bind(this), 5000);
774     }
775
776     componentWillUnmount() {
777         clearInterval(this.timer);
778     }
779
780     render() {
781         return renderRunTime(this.state.runtime);
782     }
783 });