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