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