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