20225: Remove unused imports / dead code
[arvados.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 {
7     Grid,
8     Typography,
9     withStyles,
10     Tooltip,
11     IconButton,
12     Checkbox,
13     Chip
14 } from '@material-ui/core';
15 import { FavoriteStar, PublicFavoriteStar } from '../favorite-star/favorite-star';
16 import { Resource, ResourceKind, TrashableResource } from 'models/resource';
17 import {
18     FreezeIcon,
19     ProjectIcon,
20     FilterGroupIcon,
21     CollectionIcon,
22     ProcessIcon,
23     DefaultIcon,
24     ShareIcon,
25     CollectionOldVersionIcon,
26     WorkflowIcon,
27     RemoveIcon,
28     RenameIcon,
29     ActiveIcon,
30     SetupIcon,
31     InactiveIcon,
32 } from 'components/icon/icon';
33 import { formatDate, formatFileSize, formatTime } from 'common/formatters';
34 import { resourceLabel } from 'common/labels';
35 import { connect, DispatchProp } from 'react-redux';
36 import { RootState } from 'store/store';
37 import { getResource, filterResources } from 'store/resources/resources';
38 import { GroupContentsResource } from 'services/groups-service/groups-service';
39 import { getProcess, Process, getProcessStatus, getProcessStatusStyles, getProcessRuntime } from 'store/processes/process';
40 import { ArvadosTheme } from 'common/custom-theme';
41 import { compose, Dispatch } from 'redux';
42 import { WorkflowResource } from 'models/workflow';
43 import { ResourceStatus as WorkflowStatus } from 'views/workflow-panel/workflow-panel-view';
44 import { getUuidPrefix, openRunProcess } from 'store/workflow-panel/workflow-panel-actions';
45 import { openSharingDialog } from 'store/sharing-dialog/sharing-dialog-actions';
46 import { getUserFullname, getUserDisplayName, User, UserResource } from 'models/user';
47 import { toggleIsAdmin } from 'store/users/users-actions';
48 import { LinkClass, LinkResource } from 'models/link';
49 import { navigateTo, navigateToGroupDetails, navigateToUserProfile } from 'store/navigation/navigation-action';
50 import { withResourceData } from 'views-components/data-explorer/with-resources';
51 import { CollectionResource } from 'models/collection';
52 import { IllegalNamingWarning } from 'components/warning/warning';
53 import { loadResource } from 'store/resources/resources-actions';
54 import { BuiltinGroups, getBuiltinGroupUuid, GroupClass, GroupResource, isBuiltinGroup } from 'models/group';
55 import { openRemoveGroupMemberDialog } from 'store/group-details-panel/group-details-panel-actions';
56 import { setMemberIsHidden } from 'store/group-details-panel/group-details-panel-actions';
57 import { formatPermissionLevel } from 'views-components/sharing-dialog/permission-select';
58 import { PermissionLevel } from 'models/permission';
59 import { openPermissionEditContextMenu } from 'store/context-menu/context-menu-actions';
60 import { VirtualMachinesResource } from 'models/virtual-machines';
61 import { CopyToClipboardSnackbar } from 'components/copy-to-clipboard-snackbar/copy-to-clipboard-snackbar';
62 import { ProjectResource } from 'models/project';
63 import { ProcessResource } from 'models/process';
64
65
66 const renderName = (dispatch: Dispatch, item: GroupContentsResource) => {
67     const navFunc = ("groupClass" in item && item.groupClass === GroupClass.ROLE ? navigateToGroupDetails : navigateTo);
68     return <Grid container alignItems="center" wrap="nowrap" spacing={16}>
69         <Grid item>
70             {renderIcon(item)}
71         </Grid>
72         <Grid item>
73             <Typography color="primary" style={{ width: 'auto', cursor: 'pointer' }} onClick={() => dispatch<any>(navFunc(item.uuid))}>
74                 {item.kind === ResourceKind.PROJECT || item.kind === ResourceKind.COLLECTION
75                     ? <IllegalNamingWarning name={item.name} />
76                     : null}
77                 {item.name}
78             </Typography>
79         </Grid>
80         <Grid item>
81             <Typography variant="caption">
82                 <FavoriteStar resourceUuid={item.uuid} />
83                 <PublicFavoriteStar resourceUuid={item.uuid} />
84                 {
85                     item.kind === ResourceKind.PROJECT && <FrozenProject item={item} />
86                 }
87             </Typography>
88         </Grid>
89     </Grid>;
90 };
91
92
93 const FrozenProject = (props: { item: ProjectResource }) => {
94     const [fullUsername, setFullusername] = React.useState<any>(null);
95     const getFullName = React.useCallback(() => {
96         if (props.item.frozenByUuid) {
97             setFullusername(<UserNameFromID uuid={props.item.frozenByUuid} />);
98         }
99     }, [props.item, setFullusername])
100
101     if (props.item.frozenByUuid) {
102
103         return <Tooltip onOpen={getFullName} enterDelay={500} title={<span>Project was frozen by {fullUsername}</span>}>
104             <FreezeIcon style={{ fontSize: "inherit" }} />
105         </Tooltip>;
106     } else {
107         return null;
108     }
109 }
110
111 export const ResourceName = connect(
112     (state: RootState, props: { uuid: string }) => {
113         const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
114         return resource;
115     })((resource: GroupContentsResource & DispatchProp<any>) => renderName(resource.dispatch, resource));
116
117
118 const renderIcon = (item: GroupContentsResource) => {
119     switch (item.kind) {
120         case ResourceKind.PROJECT:
121             if (item.groupClass === GroupClass.FILTER) {
122                 return <FilterGroupIcon />;
123             }
124             return <ProjectIcon />;
125         case ResourceKind.COLLECTION:
126             if (item.uuid === item.currentVersionUuid) {
127                 return <CollectionIcon />;
128             }
129             return <CollectionOldVersionIcon />;
130         case ResourceKind.PROCESS:
131             return <ProcessIcon />;
132         case ResourceKind.WORKFLOW:
133             return <WorkflowIcon />;
134         default:
135             return <DefaultIcon />;
136     }
137 };
138
139 const renderDate = (date?: string) => {
140     return <Typography noWrap style={{ minWidth: '100px' }}>{formatDate(date)}</Typography>;
141 };
142
143 const renderWorkflowName = (item: WorkflowResource) =>
144     <Grid container alignItems="center" wrap="nowrap" spacing={16}>
145         <Grid item>
146             {renderIcon(item)}
147         </Grid>
148         <Grid item>
149             <Typography color="primary" style={{ width: '100px' }}>
150                 {item.name}
151             </Typography>
152         </Grid>
153     </Grid>;
154
155 export const ResourceWorkflowName = connect(
156     (state: RootState, props: { uuid: string }) => {
157         const resource = getResource<WorkflowResource>(props.uuid)(state.resources);
158         return resource;
159     })(renderWorkflowName);
160
161 const getPublicUuid = (uuidPrefix: string) => {
162     return `${uuidPrefix}-tpzed-anonymouspublic`;
163 };
164
165 const resourceShare = (dispatch: Dispatch, uuidPrefix: string, ownerUuid?: string, uuid?: string) => {
166     const isPublic = ownerUuid === getPublicUuid(uuidPrefix);
167     return (
168         <div>
169             {!isPublic && uuid &&
170                 <Tooltip title="Share">
171                     <IconButton onClick={() => dispatch<any>(openSharingDialog(uuid))}>
172                         <ShareIcon />
173                     </IconButton>
174                 </Tooltip>
175             }
176         </div>
177     );
178 };
179
180 export const ResourceShare = connect(
181     (state: RootState, props: { uuid: string }) => {
182         const resource = getResource<WorkflowResource>(props.uuid)(state.resources);
183         const uuidPrefix = getUuidPrefix(state);
184         return {
185             uuid: resource ? resource.uuid : '',
186             ownerUuid: resource ? resource.ownerUuid : '',
187             uuidPrefix
188         };
189     })((props: { ownerUuid?: string, uuidPrefix: string, uuid?: string } & DispatchProp<any>) =>
190         resourceShare(props.dispatch, props.uuidPrefix, props.ownerUuid, props.uuid));
191
192 // User Resources
193 const renderFirstName = (item: { firstName: string }) => {
194     return <Typography noWrap>{item.firstName}</Typography>;
195 };
196
197 export const ResourceFirstName = connect(
198     (state: RootState, props: { uuid: string }) => {
199         const resource = getResource<UserResource>(props.uuid)(state.resources);
200         return resource || { firstName: '' };
201     })(renderFirstName);
202
203 const renderLastName = (item: { lastName: string }) =>
204     <Typography noWrap>{item.lastName}</Typography>;
205
206 export const ResourceLastName = connect(
207     (state: RootState, props: { uuid: string }) => {
208         const resource = getResource<UserResource>(props.uuid)(state.resources);
209         return resource || { lastName: '' };
210     })(renderLastName);
211
212 const renderFullName = (dispatch: Dispatch, item: { uuid: string, firstName: string, lastName: string }, link?: boolean) => {
213     const displayName = (item.firstName + " " + item.lastName).trim() || item.uuid;
214     return link ? <Typography noWrap
215         color="primary"
216         style={{ 'cursor': 'pointer' }}
217         onClick={() => dispatch<any>(navigateToUserProfile(item.uuid))}>
218         {displayName}
219     </Typography> :
220         <Typography noWrap>{displayName}</Typography>;
221 }
222
223 export const UserResourceFullName = connect(
224     (state: RootState, props: { uuid: string, link?: boolean }) => {
225         const resource = getResource<UserResource>(props.uuid)(state.resources);
226         return { item: resource || { uuid: '', firstName: '', lastName: '' }, link: props.link };
227     })((props: { item: { uuid: string, firstName: string, lastName: string }, link?: boolean } & DispatchProp<any>) => renderFullName(props.dispatch, props.item, props.link));
228
229 const renderUuid = (item: { uuid: string }) =>
230     <Typography data-cy="uuid" noWrap>
231         {item.uuid}
232         {(item.uuid && <CopyToClipboardSnackbar value={item.uuid} />) || '-'}
233     </Typography>;
234
235 const renderUuidCopyIcon = (item: { uuid: string }) =>
236     <Typography data-cy="uuid" noWrap>
237         {(item.uuid && <CopyToClipboardSnackbar value={item.uuid} />) || '-'}
238     </Typography>;
239
240 export const ResourceUuid = connect((state: RootState, props: { uuid: string }) => (
241     getResource<UserResource>(props.uuid)(state.resources) || { uuid: '' }
242 ))(renderUuid);
243
244 const renderEmail = (item: { email: string }) =>
245     <Typography noWrap>{item.email}</Typography>;
246
247 export const ResourceEmail = connect(
248     (state: RootState, props: { uuid: string }) => {
249         const resource = getResource<UserResource>(props.uuid)(state.resources);
250         return resource || { email: '' };
251     })(renderEmail);
252
253 enum UserAccountStatus {
254     ACTIVE = 'Active',
255     INACTIVE = 'Inactive',
256     SETUP = 'Setup',
257     UNKNOWN = ''
258 }
259
260 const renderAccountStatus = (props: { status: UserAccountStatus }) =>
261     <Grid container alignItems="center" wrap="nowrap" spacing={8} data-cy="account-status">
262         <Grid item>
263             {(() => {
264                 switch (props.status) {
265                     case UserAccountStatus.ACTIVE:
266                         return <ActiveIcon style={{ color: '#4caf50', verticalAlign: "middle" }} />;
267                     case UserAccountStatus.SETUP:
268                         return <SetupIcon style={{ color: '#2196f3', verticalAlign: "middle" }} />;
269                     case UserAccountStatus.INACTIVE:
270                         return <InactiveIcon style={{ color: '#9e9e9e', verticalAlign: "middle" }} />;
271                     default:
272                         return <></>;
273                 }
274             })()}
275         </Grid>
276         <Grid item>
277             <Typography noWrap>
278                 {props.status}
279             </Typography>
280         </Grid>
281     </Grid>;
282
283 const getUserAccountStatus = (state: RootState, props: { uuid: string }) => {
284     const user = getResource<UserResource>(props.uuid)(state.resources);
285     // Get membership links for all users group
286     const allUsersGroupUuid = getBuiltinGroupUuid(state.auth.localCluster, BuiltinGroups.ALL);
287     const permissions = filterResources((resource: LinkResource) =>
288         resource.kind === ResourceKind.LINK &&
289         resource.linkClass === LinkClass.PERMISSION &&
290         resource.headUuid === allUsersGroupUuid &&
291         resource.tailUuid === props.uuid
292     )(state.resources);
293
294     if (user) {
295         return user.isActive ? { status: UserAccountStatus.ACTIVE } : permissions.length > 0 ? { status: UserAccountStatus.SETUP } : { status: UserAccountStatus.INACTIVE };
296     } else {
297         return { status: UserAccountStatus.UNKNOWN };
298     }
299 }
300
301 export const ResourceLinkTailAccountStatus = connect(
302     (state: RootState, props: { uuid: string }) => {
303         const link = getResource<LinkResource>(props.uuid)(state.resources);
304         return link && link.tailKind === ResourceKind.USER ? getUserAccountStatus(state, { uuid: link.tailUuid }) : { status: UserAccountStatus.UNKNOWN };
305     })(renderAccountStatus);
306
307 export const UserResourceAccountStatus = connect(getUserAccountStatus)(renderAccountStatus);
308
309 const renderIsHidden = (props: {
310     memberLinkUuid: string,
311     permissionLinkUuid: string,
312     visible: boolean,
313     canManage: boolean,
314     setMemberIsHidden: (memberLinkUuid: string, permissionLinkUuid: string, hide: boolean) => void
315 }) => {
316     if (props.memberLinkUuid) {
317         return <Checkbox
318             data-cy="user-visible-checkbox"
319             color="primary"
320             checked={props.visible}
321             disabled={!props.canManage}
322             onClick={(e) => {
323                 e.stopPropagation();
324                 props.setMemberIsHidden(props.memberLinkUuid, props.permissionLinkUuid, !props.visible);
325             }} />;
326     } else {
327         return <Typography />;
328     }
329 }
330
331 export const ResourceLinkTailIsVisible = connect(
332     (state: RootState, props: { uuid: string }) => {
333         const link = getResource<LinkResource>(props.uuid)(state.resources);
334         const member = getResource<Resource>(link?.tailUuid || '')(state.resources);
335         const group = getResource<GroupResource>(link?.headUuid || '')(state.resources);
336         const permissions = filterResources((resource: LinkResource) => {
337             return resource.linkClass === LinkClass.PERMISSION
338                 && resource.headUuid === link?.tailUuid
339                 && resource.tailUuid === group?.uuid
340                 && resource.name === PermissionLevel.CAN_READ;
341         })(state.resources);
342
343         const permissionLinkUuid = permissions.length > 0 ? permissions[0].uuid : '';
344         const isVisible = link && group && permissions.length > 0;
345         // Consider whether the current user canManage this resurce in addition when it's possible
346         const isBuiltin = isBuiltinGroup(link?.headUuid || '');
347
348         return member?.kind === ResourceKind.USER
349             ? { memberLinkUuid: link?.uuid, permissionLinkUuid, visible: isVisible, canManage: !isBuiltin }
350             : { memberLinkUuid: '', permissionLinkUuid: '', visible: false, canManage: false };
351     }, { setMemberIsHidden }
352 )(renderIsHidden);
353
354 const renderIsAdmin = (props: { uuid: string, isAdmin: boolean, toggleIsAdmin: (uuid: string) => void }) =>
355     <Checkbox
356         color="primary"
357         checked={props.isAdmin}
358         onClick={(e) => {
359             e.stopPropagation();
360             props.toggleIsAdmin(props.uuid);
361         }} />;
362
363 export const ResourceIsAdmin = connect(
364     (state: RootState, props: { uuid: string }) => {
365         const resource = getResource<UserResource>(props.uuid)(state.resources);
366         return resource || { isAdmin: false };
367     }, { toggleIsAdmin }
368 )(renderIsAdmin);
369
370 const renderUsername = (item: { username: string, uuid: string }) =>
371     <Typography noWrap>{item.username || item.uuid}</Typography>;
372
373 export const ResourceUsername = connect(
374     (state: RootState, props: { uuid: string }) => {
375         const resource = getResource<UserResource>(props.uuid)(state.resources);
376         return resource || { username: '', uuid: props.uuid };
377     })(renderUsername);
378
379 // Virtual machine resource
380
381 const renderHostname = (item: { hostname: string }) =>
382     <Typography noWrap>{item.hostname}</Typography>;
383
384 export const VirtualMachineHostname = connect(
385     (state: RootState, props: { uuid: string }) => {
386         const resource = getResource<VirtualMachinesResource>(props.uuid)(state.resources);
387         return resource || { hostname: '' };
388     })(renderHostname);
389
390 const renderVirtualMachineLogin = (login: { user: string }) =>
391     <Typography noWrap>{login.user}</Typography>
392
393 export const VirtualMachineLogin = connect(
394     (state: RootState, props: { linkUuid: string }) => {
395         const permission = getResource<LinkResource>(props.linkUuid)(state.resources);
396         const user = getResource<UserResource>(permission?.tailUuid || '')(state.resources);
397
398         return { user: user?.username || permission?.tailUuid || '' };
399     })(renderVirtualMachineLogin);
400
401 // Common methods
402 const renderCommonData = (data: string) =>
403     <Typography noWrap>{data}</Typography>;
404
405 const renderCommonDate = (date: string) =>
406     <Typography noWrap>{formatDate(date)}</Typography>;
407
408 export const CommonUuid = withResourceData('uuid', renderCommonData);
409
410 // Api Client Authorizations
411 export const TokenApiClientId = withResourceData('apiClientId', renderCommonData);
412
413 export const TokenApiToken = withResourceData('apiToken', renderCommonData);
414
415 export const TokenCreatedByIpAddress = withResourceData('createdByIpAddress', renderCommonDate);
416
417 export const TokenDefaultOwnerUuid = withResourceData('defaultOwnerUuid', renderCommonData);
418
419 export const TokenExpiresAt = withResourceData('expiresAt', renderCommonDate);
420
421 export const TokenLastUsedAt = withResourceData('lastUsedAt', renderCommonDate);
422
423 export const TokenLastUsedByIpAddress = withResourceData('lastUsedByIpAddress', renderCommonData);
424
425 export const TokenScopes = withResourceData('scopes', renderCommonData);
426
427 export const TokenUserId = withResourceData('userId', renderCommonData);
428
429 const clusterColors = [
430     ['#f44336', '#fff'],
431     ['#2196f3', '#fff'],
432     ['#009688', '#fff'],
433     ['#cddc39', '#fff'],
434     ['#ff9800', '#fff']
435 ];
436
437 export const ResourceCluster = (props: { uuid: string }) => {
438     const CLUSTER_ID_LENGTH = 5;
439     const pos = props.uuid.length > CLUSTER_ID_LENGTH ? props.uuid.indexOf('-') : 5;
440     const clusterId = pos >= CLUSTER_ID_LENGTH ? props.uuid.substring(0, pos) : '';
441     const ci = pos >= CLUSTER_ID_LENGTH ? (((((
442         (props.uuid.charCodeAt(0) * props.uuid.charCodeAt(1))
443         + props.uuid.charCodeAt(2))
444         * props.uuid.charCodeAt(3))
445         + props.uuid.charCodeAt(4))) % clusterColors.length) : 0;
446     return <span style={{
447         backgroundColor: clusterColors[ci][0],
448         color: clusterColors[ci][1],
449         padding: "2px 7px",
450         borderRadius: 3
451     }}>{clusterId}</span>;
452 };
453
454 // Links Resources
455 const renderLinkName = (item: { name: string }) =>
456     <Typography noWrap>{item.name || '-'}</Typography>;
457
458 export const ResourceLinkName = connect(
459     (state: RootState, props: { uuid: string }) => {
460         const resource = getResource<LinkResource>(props.uuid)(state.resources);
461         return resource || { name: '' };
462     })(renderLinkName);
463
464 const renderLinkClass = (item: { linkClass: string }) =>
465     <Typography noWrap>{item.linkClass}</Typography>;
466
467 export const ResourceLinkClass = connect(
468     (state: RootState, props: { uuid: string }) => {
469         const resource = getResource<LinkResource>(props.uuid)(state.resources);
470         return resource || { linkClass: '' };
471     })(renderLinkClass);
472
473 const getResourceDisplayName = (resource: Resource): string => {
474     if ((resource as UserResource).kind === ResourceKind.USER
475         && typeof (resource as UserResource).firstName !== 'undefined') {
476         // We can be sure the resource is UserResource
477         return getUserDisplayName(resource as UserResource);
478     } else {
479         return (resource as GroupContentsResource).name;
480     }
481 }
482
483 const renderResourceLink = (dispatch: Dispatch, item: Resource) => {
484     var displayName = getResourceDisplayName(item);
485
486     return <Typography noWrap color="primary" style={{ 'cursor': 'pointer' }} onClick={() => dispatch<any>(navigateTo(item.uuid))}>
487         {resourceLabel(item.kind, item && item.kind === ResourceKind.GROUP ? (item as GroupResource).groupClass || '' : '')}: {displayName || item.uuid}
488     </Typography>;
489 };
490
491 export const ResourceLinkTail = connect(
492     (state: RootState, props: { uuid: string }) => {
493         const resource = getResource<LinkResource>(props.uuid)(state.resources);
494         const tailResource = getResource<Resource>(resource?.tailUuid || '')(state.resources);
495
496         return {
497             item: tailResource || { uuid: resource?.tailUuid || '', kind: resource?.tailKind || ResourceKind.NONE }
498         };
499     })((props: { item: Resource } & DispatchProp<any>) =>
500         renderResourceLink(props.dispatch, props.item));
501
502 export const ResourceLinkHead = connect(
503     (state: RootState, props: { uuid: string }) => {
504         const resource = getResource<LinkResource>(props.uuid)(state.resources);
505         const headResource = getResource<Resource>(resource?.headUuid || '')(state.resources);
506
507         return {
508             item: headResource || { uuid: resource?.headUuid || '', kind: resource?.headKind || ResourceKind.NONE }
509         };
510     })((props: { item: Resource } & DispatchProp<any>) =>
511         renderResourceLink(props.dispatch, props.item));
512
513 export const ResourceLinkUuid = connect(
514     (state: RootState, props: { uuid: string }) => {
515         const resource = getResource<LinkResource>(props.uuid)(state.resources);
516         return resource || { uuid: '' };
517     })(renderUuid);
518
519 export const ResourceLinkHeadUuid = connect(
520     (state: RootState, props: { uuid: string }) => {
521         const link = getResource<LinkResource>(props.uuid)(state.resources);
522         const headResource = getResource<Resource>(link?.headUuid || '')(state.resources);
523
524         return headResource || { uuid: '' };
525     })(renderUuid);
526
527 export const ResourceLinkTailUuid = connect(
528     (state: RootState, props: { uuid: string }) => {
529         const link = getResource<LinkResource>(props.uuid)(state.resources);
530         const tailResource = getResource<Resource>(link?.tailUuid || '')(state.resources);
531
532         return tailResource || { uuid: '' };
533     })(renderUuid);
534
535 const renderLinkDelete = (dispatch: Dispatch, item: LinkResource, canManage: boolean) => {
536     if (item.uuid) {
537         return canManage ?
538             <Typography noWrap>
539                 <IconButton data-cy="resource-delete-button" onClick={() => dispatch<any>(openRemoveGroupMemberDialog(item.uuid))}>
540                     <RemoveIcon />
541                 </IconButton>
542             </Typography> :
543             <Typography noWrap>
544                 <IconButton disabled data-cy="resource-delete-button">
545                     <RemoveIcon />
546                 </IconButton>
547             </Typography>;
548     } else {
549         return <Typography noWrap></Typography>;
550     }
551 }
552
553 export const ResourceLinkDelete = connect(
554     (state: RootState, props: { uuid: string }) => {
555         const link = getResource<LinkResource>(props.uuid)(state.resources);
556         const isBuiltin = isBuiltinGroup(link?.headUuid || '') || isBuiltinGroup(link?.tailUuid || '');
557
558         return {
559             item: link || { uuid: '', kind: ResourceKind.NONE },
560             canManage: link && getResourceLinkCanManage(state, link) && !isBuiltin,
561         };
562     })((props: { item: LinkResource, canManage: boolean } & DispatchProp<any>) =>
563         renderLinkDelete(props.dispatch, props.item, props.canManage));
564
565 export const ResourceLinkTailEmail = connect(
566     (state: RootState, props: { uuid: string }) => {
567         const link = getResource<LinkResource>(props.uuid)(state.resources);
568         const resource = getResource<UserResource>(link?.tailUuid || '')(state.resources);
569
570         return resource || { email: '' };
571     })(renderEmail);
572
573 export const ResourceLinkTailUsername = connect(
574     (state: RootState, props: { uuid: string }) => {
575         const link = getResource<LinkResource>(props.uuid)(state.resources);
576         const resource = getResource<UserResource>(link?.tailUuid || '')(state.resources);
577
578         return resource || { username: '' };
579     })(renderUsername);
580
581 const renderPermissionLevel = (dispatch: Dispatch, link: LinkResource, canManage: boolean) => {
582     return <Typography noWrap>
583         {formatPermissionLevel(link.name as PermissionLevel)}
584         {canManage ?
585             <IconButton data-cy="edit-permission-button" onClick={(event) => dispatch<any>(openPermissionEditContextMenu(event, link))}>
586                 <RenameIcon />
587             </IconButton> :
588             ''
589         }
590     </Typography>;
591 }
592
593 export const ResourceLinkHeadPermissionLevel = connect(
594     (state: RootState, props: { uuid: string }) => {
595         const link = getResource<LinkResource>(props.uuid)(state.resources);
596         const isBuiltin = isBuiltinGroup(link?.headUuid || '') || isBuiltinGroup(link?.tailUuid || '');
597
598         return {
599             link: link || { uuid: '', name: '', kind: ResourceKind.NONE },
600             canManage: link && getResourceLinkCanManage(state, link) && !isBuiltin,
601         };
602     })((props: { link: LinkResource, canManage: boolean } & DispatchProp<any>) =>
603         renderPermissionLevel(props.dispatch, props.link, props.canManage));
604
605 export const ResourceLinkTailPermissionLevel = connect(
606     (state: RootState, props: { uuid: string }) => {
607         const link = getResource<LinkResource>(props.uuid)(state.resources);
608         const isBuiltin = isBuiltinGroup(link?.headUuid || '') || isBuiltinGroup(link?.tailUuid || '');
609
610         return {
611             link: link || { uuid: '', name: '', kind: ResourceKind.NONE },
612             canManage: link && getResourceLinkCanManage(state, link) && !isBuiltin,
613         };
614     })((props: { link: LinkResource, canManage: boolean } & DispatchProp<any>) =>
615         renderPermissionLevel(props.dispatch, props.link, props.canManage));
616
617 const getResourceLinkCanManage = (state: RootState, link: LinkResource) => {
618     const headResource = getResource<Resource>(link.headUuid)(state.resources);
619     if (headResource && headResource.kind === ResourceKind.GROUP) {
620         return (headResource as GroupResource).canManage;
621     } else {
622         // true for now
623         return true;
624     }
625 }
626
627 // Process Resources
628 const resourceRunProcess = (dispatch: Dispatch, uuid: string) => {
629     return (
630         <div>
631             {uuid &&
632                 <Tooltip title="Run process">
633                     <IconButton onClick={() => dispatch<any>(openRunProcess(uuid))}>
634                         <ProcessIcon />
635                     </IconButton>
636                 </Tooltip>}
637         </div>
638     );
639 };
640
641 export const ResourceRunProcess = connect(
642     (state: RootState, props: { uuid: string }) => {
643         const resource = getResource<WorkflowResource>(props.uuid)(state.resources);
644         return {
645             uuid: resource ? resource.uuid : ''
646         };
647     })((props: { uuid: string } & DispatchProp<any>) =>
648         resourceRunProcess(props.dispatch, props.uuid));
649
650 const renderWorkflowStatus = (uuidPrefix: string, ownerUuid?: string) => {
651     if (ownerUuid === getPublicUuid(uuidPrefix)) {
652         return renderStatus(WorkflowStatus.PUBLIC);
653     } else {
654         return renderStatus(WorkflowStatus.PRIVATE);
655     }
656 };
657
658 const renderStatus = (status: string) =>
659     <Typography noWrap style={{ width: '60px' }}>{status}</Typography>;
660
661 export const ResourceWorkflowStatus = connect(
662     (state: RootState, props: { uuid: string }) => {
663         const resource = getResource<WorkflowResource>(props.uuid)(state.resources);
664         const uuidPrefix = getUuidPrefix(state);
665         return {
666             ownerUuid: resource ? resource.ownerUuid : '',
667             uuidPrefix
668         };
669     })((props: { ownerUuid?: string, uuidPrefix: string }) => renderWorkflowStatus(props.uuidPrefix, props.ownerUuid));
670
671 export const ResourceContainerUuid = connect(
672     (state: RootState, props: { uuid: string }) => {
673         const process = getProcess(props.uuid)(state.resources)
674         return { uuid: process?.container?.uuid ? process?.container?.uuid : '' };
675     })((props: { uuid: string }) => renderUuid({ uuid: props.uuid }));
676
677 enum ColumnSelection {
678     OUTPUT_UUID = 'outputUuid',
679     LOG_UUID = 'logUuid'
680 }
681
682 const renderUuidLinkWithCopyIcon = (dispatch: Dispatch, item: ProcessResource, column: string) => {
683     const selectedColumnUuid = item[column]
684     return <Grid container alignItems="center" wrap="nowrap" >
685         <Grid item>
686             {selectedColumnUuid ?
687                 <Typography color="primary" style={{ width: 'auto', cursor: 'pointer' }} noWrap
688                     onClick={() => dispatch<any>(navigateTo(selectedColumnUuid))}>
689                     {selectedColumnUuid}
690                 </Typography>
691                 : '-'}
692         </Grid>
693         <Grid item>
694             {selectedColumnUuid && renderUuidCopyIcon({ uuid: selectedColumnUuid })}
695         </Grid>
696     </Grid>;
697 };
698
699 export const ResourceOutputUuid = connect(
700     (state: RootState, props: { uuid: string }) => {
701         const resource = getResource<ProcessResource>(props.uuid)(state.resources);
702         return resource;
703     })((process: ProcessResource & DispatchProp<any>) => renderUuidLinkWithCopyIcon(process.dispatch, process, ColumnSelection.OUTPUT_UUID));
704
705 export const ResourceLogUuid = connect(
706     (state: RootState, props: { uuid: string }) => {
707         const resource = getResource<ProcessResource>(props.uuid)(state.resources);
708         return resource;
709     })((process: ProcessResource & DispatchProp<any>) => renderUuidLinkWithCopyIcon(process.dispatch, process, ColumnSelection.LOG_UUID));
710
711 export const ResourceParentProcess = connect(
712     (state: RootState, props: { uuid: string }) => {
713         const process = getProcess(props.uuid)(state.resources)
714         return { parentProcess: process?.containerRequest?.requestingContainerUuid || '' };
715     })((props: { parentProcess: string }) => renderUuid({ uuid: props.parentProcess }));
716
717 export const ResourceModifiedByUserUuid = connect(
718     (state: RootState, props: { uuid: string }) => {
719         const process = getProcess(props.uuid)(state.resources)
720         return { userUuid: process?.containerRequest?.modifiedByUserUuid || '' };
721     })((props: { userUuid: string }) => renderUuid({ uuid: props.userUuid }));
722
723 export const ResourceCreatedAtDate = connect(
724     (state: RootState, props: { uuid: string }) => {
725         const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
726         return { date: resource ? resource.createdAt : '' };
727     })((props: { date: string }) => renderDate(props.date));
728
729 export const ResourceLastModifiedDate = connect(
730     (state: RootState, props: { uuid: string }) => {
731         const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
732         return { date: resource ? resource.modifiedAt : '' };
733     })((props: { date: string }) => renderDate(props.date));
734
735 export const ResourceTrashDate = connect(
736     (state: RootState, props: { uuid: string }) => {
737         const resource = getResource<TrashableResource>(props.uuid)(state.resources);
738         return { date: resource ? resource.trashAt : '' };
739     })((props: { date: string }) => renderDate(props.date));
740
741 export const ResourceDeleteDate = connect(
742     (state: RootState, props: { uuid: string }) => {
743         const resource = getResource<TrashableResource>(props.uuid)(state.resources);
744         return { date: resource ? resource.deleteAt : '' };
745     })((props: { date: string }) => renderDate(props.date));
746
747 export const renderFileSize = (fileSize?: number) =>
748     <Typography noWrap style={{ minWidth: '45px' }}>
749         {formatFileSize(fileSize)}
750     </Typography>;
751
752 export const ResourceFileSize = connect(
753     (state: RootState, props: { uuid: string }) => {
754         const resource = getResource<CollectionResource>(props.uuid)(state.resources);
755
756         if (resource && resource.kind !== ResourceKind.COLLECTION) {
757             return { fileSize: '' };
758         }
759
760         return { fileSize: resource ? resource.fileSizeTotal : 0 };
761     })((props: { fileSize?: number }) => renderFileSize(props.fileSize));
762
763 const renderOwner = (owner: string) =>
764     <Typography noWrap>
765         {owner || '-'}
766     </Typography>;
767
768 export const ResourceOwner = connect(
769     (state: RootState, props: { uuid: string }) => {
770         const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
771         return { owner: resource ? resource.ownerUuid : '' };
772     })((props: { owner: string }) => renderOwner(props.owner));
773
774 export const ResourceOwnerName = connect(
775     (state: RootState, props: { uuid: string }) => {
776         const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
777         const ownerNameState = state.ownerName;
778         const ownerName = ownerNameState.find(it => it.uuid === resource!.ownerUuid);
779         return { owner: ownerName ? ownerName!.name : resource!.ownerUuid };
780     })((props: { owner: string }) => renderOwner(props.owner));
781
782 export const ResourceUUID = connect(
783     (state: RootState, props: { uuid: string }) => {
784         const resource = getResource<CollectionResource>(props.uuid)(state.resources);
785         return { uuid: resource ? resource.uuid : '' };
786     })((props: { uuid: string }) => renderUuid({ uuid: props.uuid }));
787
788 const renderVersion = (version: number) => {
789     return <Typography>{version ?? '-'}</Typography>
790 }
791
792 export const ResourceVersion = connect(
793     (state: RootState, props: { uuid: string }) => {
794         const resource = getResource<CollectionResource>(props.uuid)(state.resources);
795         return { version: resource ? resource.version : '' };
796     })((props: { version: number }) => renderVersion(props.version));
797
798 const renderPortableDataHash = (portableDataHash: string | null) =>
799     <Typography noWrap>
800         {portableDataHash ? <>{portableDataHash}
801             <CopyToClipboardSnackbar value={portableDataHash} /></> : '-'}
802     </Typography>
803
804 export const ResourcePortableDataHash = connect(
805     (state: RootState, props: { uuid: string }) => {
806         const resource = getResource<CollectionResource>(props.uuid)(state.resources);
807         return { portableDataHash: resource ? resource.portableDataHash : '' };
808     })((props: { portableDataHash: string }) => renderPortableDataHash(props.portableDataHash));
809
810
811 const renderFileCount = (fileCount: number) => {
812     return <Typography>{fileCount ?? '-'}</Typography>
813 }
814
815 export const ResourceFileCount = connect(
816     (state: RootState, props: { uuid: string }) => {
817         const resource = getResource<CollectionResource>(props.uuid)(state.resources);
818         return { fileCount: resource ? resource.fileCount : '' };
819     })((props: { fileCount: number }) => renderFileCount(props.fileCount));
820
821 const userFromID =
822     connect(
823         (state: RootState, props: { uuid: string }) => {
824             let userFullname = '';
825             const resource = getResource<GroupContentsResource & UserResource>(props.uuid)(state.resources);
826
827             if (resource) {
828                 userFullname = getUserFullname(resource as User) || (resource as GroupContentsResource).name;
829             }
830
831             return { uuid: props.uuid, userFullname };
832         });
833
834 const ownerFromResourceId =
835     compose(
836         connect((state: RootState, props: { uuid: string }) => {
837             const childResource = getResource<GroupContentsResource & UserResource>(props.uuid)(state.resources);
838             return { uuid: childResource ? (childResource as Resource).ownerUuid : '' };
839         }),
840         userFromID
841     );
842
843
844
845
846
847 const _resourceWithName =
848     withStyles({}, { withTheme: true })
849         ((props: { uuid: string, userFullname: string, dispatch: Dispatch, theme: ArvadosTheme }) => {
850             const { uuid, userFullname, dispatch, theme } = props;
851             if (userFullname === '') {
852                 dispatch<any>(loadResource(uuid, false));
853                 return <Typography style={{ color: theme.palette.primary.main }} inline noWrap>
854                     {uuid}
855                 </Typography>;
856             }
857
858             return <Typography style={{ color: theme.palette.primary.main }} inline noWrap>
859                 {userFullname} ({uuid})
860             </Typography>;
861         });
862
863 export const ResourceOwnerWithName = ownerFromResourceId(_resourceWithName);
864
865 export const ResourceWithName = userFromID(_resourceWithName);
866
867
868
869 export const UserNameFromID =
870     compose(userFromID)(
871         (props: { uuid: string, displayAsText?: string, userFullname: string, dispatch: Dispatch }) => {
872             const { uuid, userFullname, dispatch } = props;
873
874             if (userFullname === '') {
875                 dispatch<any>(loadResource(uuid, false));
876             }
877             return <span>
878                 {userFullname ? userFullname : uuid}
879             </span>;
880         });
881
882 export const ResponsiblePerson =
883     compose(
884         connect(
885             (state: RootState, props: { uuid: string, parentRef: HTMLElement | null }) => {
886                 let responsiblePersonName: string = '';
887                 let responsiblePersonUUID: string = '';
888                 let responsiblePersonProperty: string = '';
889
890                 if (state.auth.config.clusterConfig.Collections.ManagedProperties) {
891                     let index = 0;
892                     const keys = Object.keys(state.auth.config.clusterConfig.Collections.ManagedProperties);
893
894                     while (!responsiblePersonProperty && keys[index]) {
895                         const key = keys[index];
896                         if (state.auth.config.clusterConfig.Collections.ManagedProperties[key].Function === 'original_owner') {
897                             responsiblePersonProperty = key;
898                         }
899                         index++;
900                     }
901                 }
902
903                 let resource: Resource | undefined = getResource<GroupContentsResource & UserResource>(props.uuid)(state.resources);
904
905                 while (resource && resource.kind !== ResourceKind.USER && responsiblePersonProperty) {
906                     responsiblePersonUUID = (resource as CollectionResource).properties[responsiblePersonProperty];
907                     resource = getResource<GroupContentsResource & UserResource>(responsiblePersonUUID)(state.resources);
908                 }
909
910                 if (resource && resource.kind === ResourceKind.USER) {
911                     responsiblePersonName = getUserFullname(resource as UserResource) || (resource as GroupContentsResource).name;
912                 }
913
914                 return { uuid: responsiblePersonUUID, responsiblePersonName, parentRef: props.parentRef };
915             }),
916         withStyles({}, { withTheme: true }))
917         ((props: { uuid: string | null, responsiblePersonName: string, parentRef: HTMLElement | null, theme: ArvadosTheme }) => {
918             const { uuid, responsiblePersonName, parentRef, theme } = props;
919
920             if (!uuid && parentRef) {
921                 parentRef.style.display = 'none';
922                 return null;
923             } else if (parentRef) {
924                 parentRef.style.display = 'block';
925             }
926
927             if (!responsiblePersonName) {
928                 return <Typography style={{ color: theme.palette.primary.main }} inline noWrap>
929                     {uuid}
930                 </Typography>;
931             }
932
933             return <Typography style={{ color: theme.palette.primary.main }} inline noWrap>
934                 {responsiblePersonName} ({uuid})
935             </Typography>;
936         });
937
938 const renderType = (type: string, subtype: string) =>
939     <Typography noWrap>
940         {resourceLabel(type, subtype)}
941     </Typography>;
942
943 export const ResourceType = connect(
944     (state: RootState, props: { uuid: string }) => {
945         const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
946         return { type: resource ? resource.kind : '', subtype: resource && resource.kind === ResourceKind.GROUP ? resource.groupClass : '' };
947     })((props: { type: string, subtype: string }) => renderType(props.type, props.subtype));
948
949 export const ResourceStatus = connect((state: RootState, props: { uuid: string }) => {
950     return { resource: getResource<GroupContentsResource>(props.uuid)(state.resources) };
951 })((props: { resource: GroupContentsResource }) =>
952     (props.resource && props.resource.kind === ResourceKind.COLLECTION)
953         ? <CollectionStatus uuid={props.resource.uuid} />
954         : <ProcessStatus uuid={props.resource.uuid} />
955 );
956
957 export const CollectionStatus = connect((state: RootState, props: { uuid: string }) => {
958     return { collection: getResource<CollectionResource>(props.uuid)(state.resources) };
959 })((props: { collection: CollectionResource }) =>
960     (props.collection.uuid !== props.collection.currentVersionUuid)
961         ? <Typography>version {props.collection.version}</Typography>
962         : <Typography>head version</Typography>
963 );
964
965 export const CollectionName = connect((state: RootState, props: { uuid: string, className?: string }) => {
966     return {
967         collection: getResource<CollectionResource>(props.uuid)(state.resources),
968         uuid: props.uuid,
969         className: props.className,
970     };
971 })((props: { collection: CollectionResource, uuid: string, className?: string }) =>
972     <Typography className={props.className}>{props.collection?.name || props.uuid}</Typography>
973 );
974
975 export const ProcessStatus = compose(
976     connect((state: RootState, props: { uuid: string }) => {
977         return { process: getProcess(props.uuid)(state.resources) };
978     }),
979     withStyles({}, { withTheme: true }))
980     ((props: { process?: Process, theme: ArvadosTheme }) =>
981         props.process
982             ? <Chip label={getProcessStatus(props.process)}
983                 style={{
984                     height: props.theme.spacing.unit * 3,
985                     width: props.theme.spacing.unit * 12,
986                     ...getProcessStatusStyles(
987                         getProcessStatus(props.process), props.theme),
988                     fontSize: '0.875rem',
989                     borderRadius: props.theme.spacing.unit * 0.625,
990                 }}
991             />
992             : <Typography>-</Typography>
993     );
994
995 export const ProcessStartDate = connect(
996     (state: RootState, props: { uuid: string }) => {
997         const process = getProcess(props.uuid)(state.resources);
998         return { date: (process && process.container) ? process.container.startedAt : '' };
999     })((props: { date: string }) => renderDate(props.date));
1000
1001 export const renderRunTime = (time: number) =>
1002     <Typography noWrap style={{ minWidth: '45px' }}>
1003         {formatTime(time, true)}
1004     </Typography>;
1005
1006 interface ContainerRunTimeProps {
1007     process: Process;
1008 }
1009
1010 interface ContainerRunTimeState {
1011     runtime: number;
1012 }
1013
1014 export const ContainerRunTime = connect((state: RootState, props: { uuid: string }) => {
1015     return { process: getProcess(props.uuid)(state.resources) };
1016 })(class extends React.Component<ContainerRunTimeProps, ContainerRunTimeState> {
1017     private timer: any;
1018
1019     constructor(props: ContainerRunTimeProps) {
1020         super(props);
1021         this.state = { runtime: this.getRuntime() };
1022     }
1023
1024     getRuntime() {
1025         return this.props.process ? getProcessRuntime(this.props.process) : 0;
1026     }
1027
1028     updateRuntime() {
1029         this.setState({ runtime: this.getRuntime() });
1030     }
1031
1032     componentDidMount() {
1033         this.timer = setInterval(this.updateRuntime.bind(this), 5000);
1034     }
1035
1036     componentWillUnmount() {
1037         clearInterval(this.timer);
1038     }
1039
1040     render() {
1041         return this.props.process ? renderRunTime(this.state.runtime) : <Typography>-</Typography>;
1042     }
1043 });