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