18123: Invert group member is visible logic
[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, isBuiltinGroup } 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 data-cy="uuid" 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: {
219                             memberLinkUuid: string,
220                             permissionLinkUuid: string,
221                             visible: boolean,
222                             canManage: boolean,
223                             setMemberIsHidden: (memberLinkUuid: string, permissionLinkUuid: string, hide: boolean) => void 
224                         }) => {
225     if (props.memberLinkUuid) {
226         return <Checkbox
227                 data-cy="user-visible-checkbox"
228                 color="primary"
229                 checked={props.visible}
230                 disabled={!props.canManage}
231                 onClick={() => props.setMemberIsHidden(props.memberLinkUuid, props.permissionLinkUuid, !props.visible)} />;
232     } else {
233         return <Typography />;
234     }
235 }
236
237 export const ResourceLinkTailIsVisible = connect(
238     (state: RootState, props: { uuid: string }) => {
239         const link = getResource<LinkResource>(props.uuid)(state.resources);
240         const member = getResource<Resource>(link?.tailUuid || '')(state.resources);
241         const group = getResource<GroupResource>(link?.headUuid || '')(state.resources);
242         const permissions = filterResources((resource: LinkResource) => {
243             return resource.linkClass === LinkClass.PERMISSION
244                 && resource.headUuid === link?.tailUuid
245                 && resource.tailUuid === group?.uuid
246                 && resource.name === PermissionLevel.CAN_READ;
247         })(state.resources);
248
249         const permissionLinkUuid = permissions.length > 0 ? permissions[0].uuid : '';
250         const isVisible = link && group && permissions.length > 0;
251         // Consider whether the current user canManage this resurce in addition when it's possible
252         const isBuiltin = isBuiltinGroup(link?.headUuid || '');
253
254         return member?.kind === ResourceKind.USER
255             ? { memberLinkUuid: link?.uuid, permissionLinkUuid, visible: isVisible, canManage: !isBuiltin }
256             : { memberLinkUuid: '', permissionLinkUuid: '', visible: false, canManage: false };
257     }, { setMemberIsHidden }
258 )(renderIsHidden);
259
260 const renderIsAdmin = (props: { uuid: string, isAdmin: boolean, toggleIsAdmin: (uuid: string) => void }) =>
261     <Checkbox
262         color="primary"
263         checked={props.isAdmin}
264         onClick={() => props.toggleIsAdmin(props.uuid)} />;
265
266 export const ResourceIsAdmin = connect(
267     (state: RootState, props: { uuid: string }) => {
268         const resource = getResource<UserResource>(props.uuid)(state.resources);
269         return resource || { isAdmin: false };
270     }, { toggleIsAdmin }
271 )(renderIsAdmin);
272
273 const renderUsername = (item: { username: string }) =>
274     <Typography noWrap>{item.username}</Typography>;
275
276 export const ResourceUsername = connect(
277     (state: RootState, props: { uuid: string }) => {
278         const resource = getResource<UserResource>(props.uuid)(state.resources);
279         return resource || { username: '' };
280     })(renderUsername);
281
282 // Common methods
283 const renderCommonData = (data: string) =>
284     <Typography noWrap>{data}</Typography>;
285
286 const renderCommonDate = (date: string) =>
287     <Typography noWrap>{formatDate(date)}</Typography>;
288
289 export const CommonUuid = withResourceData('uuid', renderCommonData);
290
291 // Api Client Authorizations
292 export const TokenApiClientId = withResourceData('apiClientId', renderCommonData);
293
294 export const TokenApiToken = withResourceData('apiToken', renderCommonData);
295
296 export const TokenCreatedByIpAddress = withResourceData('createdByIpAddress', renderCommonDate);
297
298 export const TokenDefaultOwnerUuid = withResourceData('defaultOwnerUuid', renderCommonData);
299
300 export const TokenExpiresAt = withResourceData('expiresAt', renderCommonDate);
301
302 export const TokenLastUsedAt = withResourceData('lastUsedAt', renderCommonDate);
303
304 export const TokenLastUsedByIpAddress = withResourceData('lastUsedByIpAddress', renderCommonData);
305
306 export const TokenScopes = withResourceData('scopes', renderCommonData);
307
308 export const TokenUserId = withResourceData('userId', renderCommonData);
309
310 const clusterColors = [
311     ['#f44336', '#fff'],
312     ['#2196f3', '#fff'],
313     ['#009688', '#fff'],
314     ['#cddc39', '#fff'],
315     ['#ff9800', '#fff']
316 ];
317
318 export const ResourceCluster = (props: { uuid: string }) => {
319     const CLUSTER_ID_LENGTH = 5;
320     const pos = props.uuid.length > CLUSTER_ID_LENGTH ? props.uuid.indexOf('-') : 5;
321     const clusterId = pos >= CLUSTER_ID_LENGTH ? props.uuid.substr(0, pos) : '';
322     const ci = pos >= CLUSTER_ID_LENGTH ? (((((
323         (props.uuid.charCodeAt(0) * props.uuid.charCodeAt(1))
324         + props.uuid.charCodeAt(2))
325         * props.uuid.charCodeAt(3))
326         + props.uuid.charCodeAt(4))) % clusterColors.length) : 0;
327     return <span style={{
328         backgroundColor: clusterColors[ci][0],
329         color: clusterColors[ci][1],
330         padding: "2px 7px",
331         borderRadius: 3
332     }}>{clusterId}</span>;
333 };
334
335 // Links Resources
336 const renderLinkName = (item: { name: string }) =>
337     <Typography noWrap>{item.name || '(none)'}</Typography>;
338
339 export const ResourceLinkName = connect(
340     (state: RootState, props: { uuid: string }) => {
341         const resource = getResource<LinkResource>(props.uuid)(state.resources);
342         return resource || { name: '' };
343     })(renderLinkName);
344
345 const renderLinkClass = (item: { linkClass: string }) =>
346     <Typography noWrap>{item.linkClass}</Typography>;
347
348 export const ResourceLinkClass = connect(
349     (state: RootState, props: { uuid: string }) => {
350         const resource = getResource<LinkResource>(props.uuid)(state.resources);
351         return resource || { linkClass: '' };
352     })(renderLinkClass);
353
354 const getResourceDisplayName = (resource: Resource): string => {
355     if ((resource as UserResource).kind === ResourceKind.USER
356           && typeof (resource as UserResource).firstName !== 'undefined') {
357         // We can be sure the resource is UserResource
358         return getUserDisplayName(resource as UserResource);
359     } else {
360         return (resource as GroupContentsResource).name;
361     }
362 }
363
364 const renderResourceLink = (dispatch: Dispatch, item: Resource) => {
365     var displayName = getResourceDisplayName(item);
366
367     return <Typography noWrap color="primary" style={{ 'cursor': 'pointer' }} onClick={() => dispatch<any>(navigateTo(item.uuid))}>
368         {resourceLabel(item.kind)}: {displayName || item.uuid}
369     </Typography>;
370 };
371
372 export const ResourceLinkTail = connect(
373     (state: RootState, props: { uuid: string }) => {
374         const resource = getResource<LinkResource>(props.uuid)(state.resources);
375         const tailResource = getResource<Resource>(resource?.tailUuid || '')(state.resources);
376
377         return {
378             item: tailResource || { uuid: resource?.tailUuid || '', kind: resource?.tailKind || ResourceKind.NONE }
379         };
380     })((props: { item: Resource } & DispatchProp<any>) =>
381         renderResourceLink(props.dispatch, props.item));
382
383 export const ResourceLinkHead = connect(
384     (state: RootState, props: { uuid: string }) => {
385         const resource = getResource<LinkResource>(props.uuid)(state.resources);
386         const headResource = getResource<Resource>(resource?.headUuid || '')(state.resources);
387
388         return {
389             item: headResource || { uuid: resource?.headUuid || '', kind: resource?.headKind || ResourceKind.NONE }
390         };
391     })((props: { item: Resource } & DispatchProp<any>) =>
392         renderResourceLink(props.dispatch, props.item));
393
394 export const ResourceLinkUuid = connect(
395     (state: RootState, props: { uuid: string }) => {
396         const resource = getResource<LinkResource>(props.uuid)(state.resources);
397         return resource || { uuid: '' };
398     })(renderUuid);
399
400 export const ResourceLinkHeadUuid = connect(
401     (state: RootState, props: { uuid: string }) => {
402         const link = getResource<LinkResource>(props.uuid)(state.resources);
403         const headResource = getResource<Resource>(link?.headUuid || '')(state.resources);
404
405         return headResource || { uuid: '' };
406     })(renderUuid);
407
408 export const ResourceLinkTailUuid = connect(
409     (state: RootState, props: { uuid: string }) => {
410         const link = getResource<LinkResource>(props.uuid)(state.resources);
411         const tailResource = getResource<Resource>(link?.tailUuid || '')(state.resources);
412
413         return tailResource || { uuid: '' };
414     })(renderUuid);
415
416 const renderLinkDelete = (dispatch: Dispatch, item: LinkResource, canManage: boolean) => {
417     if (item.uuid) {
418         return canManage ?
419             <Typography noWrap>
420                 <IconButton data-cy="resource-delete-button" onClick={() => dispatch<any>(openRemoveGroupMemberDialog(item.uuid))}>
421                     <RemoveIcon />
422                 </IconButton>
423             </Typography> :
424             <Typography noWrap>
425                 <IconButton disabled data-cy="resource-delete-button">
426                     <RemoveIcon />
427                 </IconButton>
428             </Typography>;
429     } else {
430       return <Typography noWrap></Typography>;
431     }
432 }
433
434 export const ResourceLinkDelete = connect(
435     (state: RootState, props: { uuid: string }) => {
436         const link = getResource<LinkResource>(props.uuid)(state.resources);
437         const isBuiltin = isBuiltinGroup(link?.headUuid || '') || isBuiltinGroup(link?.tailUuid || '');
438
439         return {
440             item: link || { uuid: '', kind: ResourceKind.NONE },
441             canManage: link && getResourceLinkCanManage(state, link) && !isBuiltin,
442         };
443     })((props: { item: LinkResource, canManage: boolean } & DispatchProp<any>) =>
444       renderLinkDelete(props.dispatch, props.item, props.canManage));
445
446 export const ResourceLinkTailEmail = connect(
447     (state: RootState, props: { uuid: string }) => {
448         const link = getResource<LinkResource>(props.uuid)(state.resources);
449         const resource = getResource<UserResource>(link?.tailUuid || '')(state.resources);
450
451         return resource || { email: '' };
452     })(renderEmail);
453
454 export const ResourceLinkTailUsername = connect(
455     (state: RootState, props: { uuid: string }) => {
456         const link = getResource<LinkResource>(props.uuid)(state.resources);
457         const resource = getResource<UserResource>(link?.tailUuid || '')(state.resources);
458
459         return resource || { username: '' };
460     })(renderUsername);
461
462 const renderPermissionLevel = (dispatch: Dispatch, link: LinkResource, canManage: boolean) => {
463     return <Typography noWrap>
464         {formatPermissionLevel(link.name as PermissionLevel)}
465         {canManage ?
466             <IconButton data-cy="edit-permission-button" onClick={(event) => dispatch<any>(openPermissionEditContextMenu(event, link))}>
467                 <RenameIcon />
468             </IconButton> :
469             ''
470         }
471     </Typography>;
472 }
473
474 export const ResourceLinkHeadPermissionLevel = connect(
475     (state: RootState, props: { uuid: string }) => {
476         const link = getResource<LinkResource>(props.uuid)(state.resources);
477         const isBuiltin = isBuiltinGroup(link?.headUuid || '') || isBuiltinGroup(link?.tailUuid || '');
478
479         return {
480             link: link || { uuid: '', name: '', kind: ResourceKind.NONE },
481             canManage: link && getResourceLinkCanManage(state, link) && !isBuiltin,
482         };
483     })((props: { link: LinkResource, canManage: boolean } & DispatchProp<any>) =>
484         renderPermissionLevel(props.dispatch, props.link, props.canManage));
485
486 export const ResourceLinkTailPermissionLevel = connect(
487     (state: RootState, props: { uuid: string }) => {
488         const link = getResource<LinkResource>(props.uuid)(state.resources);
489         const isBuiltin = isBuiltinGroup(link?.headUuid || '') || isBuiltinGroup(link?.tailUuid || '');
490
491         return {
492             link: link || { uuid: '', name: '', kind: ResourceKind.NONE },
493             canManage: link && getResourceLinkCanManage(state, link) && !isBuiltin,
494         };
495     })((props: { link: LinkResource, canManage: boolean } & DispatchProp<any>) =>
496         renderPermissionLevel(props.dispatch, props.link, props.canManage));
497
498 const getResourceLinkCanManage = (state: RootState, link: LinkResource) => {
499     const headResource = getResource<Resource>(link.headUuid)(state.resources);
500     // const tailResource = getResource<Resource>(link.tailUuid)(state.resources);
501     const userUuid = getUserUuid(state);
502
503     if (headResource && headResource.kind === ResourceKind.GROUP) {
504         return userUuid ? (headResource as GroupResource).writableBy?.includes(userUuid) : false;
505     } else {
506         // true for now
507         return true;
508     }
509 }
510
511 // Process Resources
512 const resourceRunProcess = (dispatch: Dispatch, uuid: string) => {
513     return (
514         <div>
515             {uuid &&
516                 <Tooltip title="Run process">
517                     <IconButton onClick={() => dispatch<any>(openRunProcess(uuid))}>
518                         <ProcessIcon />
519                     </IconButton>
520                 </Tooltip>}
521         </div>
522     );
523 };
524
525 export const ResourceRunProcess = connect(
526     (state: RootState, props: { uuid: string }) => {
527         const resource = getResource<WorkflowResource>(props.uuid)(state.resources);
528         return {
529             uuid: resource ? resource.uuid : ''
530         };
531     })((props: { uuid: string } & DispatchProp<any>) =>
532         resourceRunProcess(props.dispatch, props.uuid));
533
534 const renderWorkflowStatus = (uuidPrefix: string, ownerUuid?: string) => {
535     if (ownerUuid === getPublicUuid(uuidPrefix)) {
536         return renderStatus(WorkflowStatus.PUBLIC);
537     } else {
538         return renderStatus(WorkflowStatus.PRIVATE);
539     }
540 };
541
542 const renderStatus = (status: string) =>
543     <Typography noWrap style={{ width: '60px' }}>{status}</Typography>;
544
545 export const ResourceWorkflowStatus = connect(
546     (state: RootState, props: { uuid: string }) => {
547         const resource = getResource<WorkflowResource>(props.uuid)(state.resources);
548         const uuidPrefix = getUuidPrefix(state);
549         return {
550             ownerUuid: resource ? resource.ownerUuid : '',
551             uuidPrefix
552         };
553     })((props: { ownerUuid?: string, uuidPrefix: string }) => renderWorkflowStatus(props.uuidPrefix, props.ownerUuid));
554
555 export const ResourceLastModifiedDate = connect(
556     (state: RootState, props: { uuid: string }) => {
557         const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
558         return { date: resource ? resource.modifiedAt : '' };
559     })((props: { date: string }) => renderDate(props.date));
560
561 export const ResourceCreatedAtDate = connect(
562     (state: RootState, props: { uuid: string }) => {
563         const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
564         return { date: resource ? resource.createdAt : '' };
565     })((props: { date: string }) => renderDate(props.date));
566
567 export const ResourceTrashDate = connect(
568     (state: RootState, props: { uuid: string }) => {
569         const resource = getResource<TrashableResource>(props.uuid)(state.resources);
570         return { date: resource ? resource.trashAt : '' };
571     })((props: { date: string }) => renderDate(props.date));
572
573 export const ResourceDeleteDate = connect(
574     (state: RootState, props: { uuid: string }) => {
575         const resource = getResource<TrashableResource>(props.uuid)(state.resources);
576         return { date: resource ? resource.deleteAt : '' };
577     })((props: { date: string }) => renderDate(props.date));
578
579 export const renderFileSize = (fileSize?: number) =>
580     <Typography noWrap style={{ minWidth: '45px' }}>
581         {formatFileSize(fileSize)}
582     </Typography>;
583
584 export const ResourceFileSize = connect(
585     (state: RootState, props: { uuid: string }) => {
586         const resource = getResource<CollectionResource>(props.uuid)(state.resources);
587
588         if (resource && resource.kind !== ResourceKind.COLLECTION) {
589             return { fileSize: '' };
590         }
591
592         return { fileSize: resource ? resource.fileSizeTotal : 0 };
593     })((props: { fileSize?: number }) => renderFileSize(props.fileSize));
594
595 const renderOwner = (owner: string) =>
596     <Typography noWrap>
597         {owner}
598     </Typography>;
599
600 export const ResourceOwner = connect(
601     (state: RootState, props: { uuid: string }) => {
602         const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
603         return { owner: resource ? resource.ownerUuid : '' };
604     })((props: { owner: string }) => renderOwner(props.owner));
605
606 export const ResourceOwnerName = connect(
607     (state: RootState, props: { uuid: string }) => {
608         const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
609         const ownerNameState = state.ownerName;
610         const ownerName = ownerNameState.find(it => it.uuid === resource!.ownerUuid);
611         return { owner: ownerName ? ownerName!.name : resource!.ownerUuid };
612     })((props: { owner: string }) => renderOwner(props.owner));
613
614 const userFromID =
615     connect(
616         (state: RootState, props: { uuid: string }) => {
617             let userFullname = '';
618             const resource = getResource<GroupContentsResource & UserResource>(props.uuid)(state.resources);
619
620             if (resource) {
621                 userFullname = getUserFullname(resource as User) || (resource as GroupContentsResource).name;
622             }
623
624             return { uuid: props.uuid, userFullname };
625         });
626
627 export const ResourceOwnerWithName =
628     compose(
629         userFromID,
630         withStyles({}, { withTheme: true }))
631         ((props: { uuid: string, userFullname: string, dispatch: Dispatch, theme: ArvadosTheme }) => {
632             const { uuid, userFullname, dispatch, theme } = props;
633
634             if (userFullname === '') {
635                 dispatch<any>(loadResource(uuid, false));
636                 return <Typography style={{ color: theme.palette.primary.main }} inline noWrap>
637                     {uuid}
638                 </Typography>;
639             }
640
641             return <Typography style={{ color: theme.palette.primary.main }} inline noWrap>
642                 {userFullname} ({uuid})
643             </Typography>;
644         });
645
646 export const UserNameFromID =
647     compose(userFromID)(
648         (props: { uuid: string, userFullname: string, dispatch: Dispatch }) => {
649             const { uuid, userFullname, dispatch } = props;
650
651             if (userFullname === '') {
652                 dispatch<any>(loadResource(uuid, false));
653             }
654             return <span>
655                 {userFullname ? userFullname : uuid}
656             </span>;
657         });
658
659 export const ResponsiblePerson =
660     compose(
661         connect(
662             (state: RootState, props: { uuid: string, parentRef: HTMLElement | null }) => {
663                 let responsiblePersonName: string = '';
664                 let responsiblePersonUUID: string = '';
665                 let responsiblePersonProperty: string = '';
666
667                 if (state.auth.config.clusterConfig.Collections.ManagedProperties) {
668                     let index = 0;
669                     const keys = Object.keys(state.auth.config.clusterConfig.Collections.ManagedProperties);
670
671                     while (!responsiblePersonProperty && keys[index]) {
672                         const key = keys[index];
673                         if (state.auth.config.clusterConfig.Collections.ManagedProperties[key].Function === 'original_owner') {
674                             responsiblePersonProperty = key;
675                         }
676                         index++;
677                     }
678                 }
679
680                 let resource: Resource | undefined = getResource<GroupContentsResource & UserResource>(props.uuid)(state.resources);
681
682                 while (resource && resource.kind !== ResourceKind.USER && responsiblePersonProperty) {
683                     responsiblePersonUUID = (resource as CollectionResource).properties[responsiblePersonProperty];
684                     resource = getResource<GroupContentsResource & UserResource>(responsiblePersonUUID)(state.resources);
685                 }
686
687                 if (resource && resource.kind === ResourceKind.USER) {
688                     responsiblePersonName = getUserFullname(resource as UserResource) || (resource as GroupContentsResource).name;
689                 }
690
691                 return { uuid: responsiblePersonUUID, responsiblePersonName, parentRef: props.parentRef };
692             }),
693         withStyles({}, { withTheme: true }))
694         ((props: { uuid: string | null, responsiblePersonName: string, parentRef: HTMLElement | null, theme: ArvadosTheme }) => {
695             const { uuid, responsiblePersonName, parentRef, theme } = props;
696
697             if (!uuid && parentRef) {
698                 parentRef.style.display = 'none';
699                 return null;
700             } else if (parentRef) {
701                 parentRef.style.display = 'block';
702             }
703
704             if (!responsiblePersonName) {
705                 return <Typography style={{ color: theme.palette.primary.main }} inline noWrap>
706                     {uuid}
707                 </Typography>;
708             }
709
710             return <Typography style={{ color: theme.palette.primary.main }} inline noWrap>
711                 {responsiblePersonName} ({uuid})
712                 </Typography>;
713         });
714
715 const renderType = (type: string, subtype: string) =>
716     <Typography noWrap>
717         {resourceLabel(type, subtype)}
718     </Typography>;
719
720 export const ResourceType = connect(
721     (state: RootState, props: { uuid: string }) => {
722         const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
723         return { type: resource ? resource.kind : '', subtype: resource && resource.kind === ResourceKind.GROUP ? resource.groupClass : '' };
724     })((props: { type: string, subtype: string }) => renderType(props.type, props.subtype));
725
726 export const ResourceStatus = connect((state: RootState, props: { uuid: string }) => {
727     return { resource: getResource<GroupContentsResource>(props.uuid)(state.resources) };
728 })((props: { resource: GroupContentsResource }) =>
729     (props.resource && props.resource.kind === ResourceKind.COLLECTION)
730         ? <CollectionStatus uuid={props.resource.uuid} />
731         : <ProcessStatus uuid={props.resource.uuid} />
732 );
733
734 export const CollectionStatus = connect((state: RootState, props: { uuid: string }) => {
735     return { collection: getResource<CollectionResource>(props.uuid)(state.resources) };
736 })((props: { collection: CollectionResource }) =>
737     (props.collection.uuid !== props.collection.currentVersionUuid)
738         ? <Typography>version {props.collection.version}</Typography>
739         : <Typography>head version</Typography>
740 );
741
742 export const ProcessStatus = compose(
743     connect((state: RootState, props: { uuid: string }) => {
744         return { process: getProcess(props.uuid)(state.resources) };
745     }),
746     withStyles({}, { withTheme: true }))
747     ((props: { process?: Process, theme: ArvadosTheme }) => {
748         const status = props.process ? getProcessStatus(props.process) : "-";
749         return <Typography
750             noWrap
751             style={{ color: getProcessStatusColor(status, props.theme) }} >
752             {status}
753         </Typography>;
754     });
755
756 export const ProcessStartDate = connect(
757     (state: RootState, props: { uuid: string }) => {
758         const process = getProcess(props.uuid)(state.resources);
759         return { date: (process && process.container) ? process.container.startedAt : '' };
760     })((props: { date: string }) => renderDate(props.date));
761
762 export const renderRunTime = (time: number) =>
763     <Typography noWrap style={{ minWidth: '45px' }}>
764         {formatTime(time, true)}
765     </Typography>;
766
767 interface ContainerRunTimeProps {
768     process: Process;
769 }
770
771 interface ContainerRunTimeState {
772     runtime: number;
773 }
774
775 export const ContainerRunTime = connect((state: RootState, props: { uuid: string }) => {
776     return { process: getProcess(props.uuid)(state.resources) };
777 })(class extends React.Component<ContainerRunTimeProps, ContainerRunTimeState> {
778     private timer: any;
779
780     constructor(props: ContainerRunTimeProps) {
781         super(props);
782         this.state = { runtime: this.getRuntime() };
783     }
784
785     getRuntime() {
786         return this.props.process ? getProcessRuntime(this.props.process) : 0;
787     }
788
789     updateRuntime() {
790         this.setState({ runtime: this.getRuntime() });
791     }
792
793     componentDidMount() {
794         this.timer = setInterval(this.updateRuntime.bind(this), 5000);
795     }
796
797     componentWillUnmount() {
798         clearInterval(this.timer);
799     }
800
801     render() {
802         return renderRunTime(this.state.runtime);
803     }
804 });