1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import React from 'react';
6 import { Grid, Typography, withStyles, Tooltip, IconButton, Checkbox } from '@material-ui/core';
7 import { FavoriteStar, PublicFavoriteStar } from '../favorite-star/favorite-star';
8 import { Resource, ResourceKind, TrashableResource } from 'models/resource';
9 import { ProjectIcon, FilterGroupIcon, CollectionIcon, ProcessIcon, DefaultIcon, ShareIcon, CollectionOldVersionIcon, WorkflowIcon } from 'components/icon/icon';
10 import { formatDate, formatFileSize, formatTime } from 'common/formatters';
11 import { resourceLabel } from 'common/labels';
12 import { connect, DispatchProp } from 'react-redux';
13 import { RootState } from 'store/store';
14 import { getResource } from 'store/resources/resources';
15 import { GroupContentsResource } from 'services/groups-service/groups-service';
16 import { getProcess, Process, getProcessStatus, getProcessStatusColor, getProcessRuntime } from 'store/processes/process';
17 import { ArvadosTheme } from 'common/custom-theme';
18 import { compose, Dispatch } from 'redux';
19 import { WorkflowResource } from 'models/workflow';
20 import { ResourceStatus as WorkflowStatus } from 'views/workflow-panel/workflow-panel-view';
21 import { getUuidPrefix, openRunProcess } from 'store/workflow-panel/workflow-panel-actions';
22 import { openSharingDialog } from 'store/sharing-dialog/sharing-dialog-actions';
23 import { getUserFullname, getUserDisplayName, User, UserResource } from 'models/user';
24 import { toggleIsActive, toggleIsAdmin } from 'store/users/users-actions';
25 import { LinkResource } from 'models/link';
26 import { navigateTo } from 'store/navigation/navigation-action';
27 import { withResourceData } from 'views-components/data-explorer/with-resources';
28 import { CollectionResource } from 'models/collection';
29 import { IllegalNamingWarning } from 'components/warning/warning';
30 import { loadResource } from 'store/resources/resources-actions';
31 import { GroupClass } from 'models/group';
33 const renderName = (dispatch: Dispatch, item: GroupContentsResource) =>
34 <Grid container alignItems="center" wrap="nowrap" spacing={16}>
39 <Typography color="primary" style={{ width: 'auto', cursor: 'pointer' }} onClick={() => dispatch<any>(navigateTo(item.uuid))}>
40 {item.kind === ResourceKind.PROJECT || item.kind === ResourceKind.COLLECTION
41 ? <IllegalNamingWarning name={item.name} />
47 <Typography variant="caption">
48 <FavoriteStar resourceUuid={item.uuid} />
49 <PublicFavoriteStar resourceUuid={item.uuid} />
54 export const ResourceName = connect(
55 (state: RootState, props: { uuid: string }) => {
56 const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
58 })((resource: GroupContentsResource & DispatchProp<any>) => renderName(resource.dispatch, resource));
60 const renderIcon = (item: GroupContentsResource) => {
62 case ResourceKind.PROJECT:
63 if (item.groupClass === GroupClass.FILTER) {
64 return <FilterGroupIcon />;
66 return <ProjectIcon />;
67 case ResourceKind.COLLECTION:
68 if (item.uuid === item.currentVersionUuid) {
69 return <CollectionIcon />;
71 return <CollectionOldVersionIcon />;
72 case ResourceKind.PROCESS:
73 return <ProcessIcon />;
74 case ResourceKind.WORKFLOW:
75 return <WorkflowIcon />;
77 return <DefaultIcon />;
81 const renderDate = (date?: string) => {
82 return <Typography noWrap style={{ minWidth: '100px' }}>{formatDate(date)}</Typography>;
85 const renderWorkflowName = (item: WorkflowResource) =>
86 <Grid container alignItems="center" wrap="nowrap" spacing={16}>
91 <Typography color="primary" style={{ width: '100px' }}>
97 export const ResourceWorkflowName = connect(
98 (state: RootState, props: { uuid: string }) => {
99 const resource = getResource<WorkflowResource>(props.uuid)(state.resources);
101 })(renderWorkflowName);
103 const getPublicUuid = (uuidPrefix: string) => {
104 return `${uuidPrefix}-tpzed-anonymouspublic`;
107 const resourceShare = (dispatch: Dispatch, uuidPrefix: string, ownerUuid?: string, uuid?: string) => {
108 const isPublic = ownerUuid === getPublicUuid(uuidPrefix);
111 {!isPublic && uuid &&
112 <Tooltip title="Share">
113 <IconButton onClick={() => dispatch<any>(openSharingDialog(uuid))}>
122 export const ResourceShare = connect(
123 (state: RootState, props: { uuid: string }) => {
124 const resource = getResource<WorkflowResource>(props.uuid)(state.resources);
125 const uuidPrefix = getUuidPrefix(state);
127 uuid: resource ? resource.uuid : '',
128 ownerUuid: resource ? resource.ownerUuid : '',
131 })((props: { ownerUuid?: string, uuidPrefix: string, uuid?: string } & DispatchProp<any>) =>
132 resourceShare(props.dispatch, props.uuidPrefix, props.ownerUuid, props.uuid));
135 const renderFirstName = (item: { firstName: string }) => {
136 return <Typography noWrap>{item.firstName}</Typography>;
139 export const ResourceFirstName = connect(
140 (state: RootState, props: { uuid: string }) => {
141 const resource = getResource<UserResource>(props.uuid)(state.resources);
142 return resource || { firstName: '' };
145 const renderLastName = (item: { lastName: string }) =>
146 <Typography noWrap>{item.lastName}</Typography>;
148 export const ResourceLastName = connect(
149 (state: RootState, props: { uuid: string }) => {
150 const resource = getResource<UserResource>(props.uuid)(state.resources);
151 return resource || { lastName: '' };
154 const renderFullName = (item: { firstName: string, lastName: string }) =>
155 <Typography noWrap>{(item.firstName + " " + item.lastName).trim()}</Typography>;
157 export const ResourceFullName = connect(
158 (state: RootState, props: { uuid: string }) => {
159 const resource = getResource<UserResource>(props.uuid)(state.resources);
160 return resource || { firstName: '', lastName: '' };
164 const renderUuid = (item: { uuid: string }) =>
165 <Typography noWrap>{item.uuid}</Typography>;
167 export const ResourceUuid = connect(
168 (state: RootState, props: { uuid: string }) => {
169 const resource = getResource<UserResource>(props.uuid)(state.resources);
170 return resource || { uuid: '' };
173 const renderEmail = (item: { email: string }) =>
174 <Typography noWrap>{item.email}</Typography>;
176 export const ResourceEmail = connect(
177 (state: RootState, props: { uuid: string }) => {
178 const resource = getResource<UserResource>(props.uuid)(state.resources);
179 return resource || { email: '' };
182 const renderIsActive = (props: { uuid: string, isActive: boolean, toggleIsActive: (uuid: string) => void }) =>
185 checked={props.isActive}
186 onClick={() => props.toggleIsActive(props.uuid)} />;
188 export const ResourceIsActive = connect(
189 (state: RootState, props: { uuid: string }) => {
190 const resource = getResource<UserResource>(props.uuid)(state.resources);
191 return resource || { isActive: false };
192 }, { toggleIsActive }
195 const renderIsAdmin = (props: { uuid: string, isAdmin: boolean, toggleIsAdmin: (uuid: string) => void }) =>
198 checked={props.isAdmin}
199 onClick={() => props.toggleIsAdmin(props.uuid)} />;
201 export const ResourceIsAdmin = connect(
202 (state: RootState, props: { uuid: string }) => {
203 const resource = getResource<UserResource>(props.uuid)(state.resources);
204 return resource || { isAdmin: false };
208 const renderUsername = (item: { username: string }) =>
209 <Typography noWrap>{item.username}</Typography>;
211 export const ResourceUsername = connect(
212 (state: RootState, props: { uuid: string }) => {
213 const resource = getResource<UserResource>(props.uuid)(state.resources);
214 return resource || { username: '' };
218 const renderCommonData = (data: string) =>
219 <Typography noWrap>{data}</Typography>;
221 const renderCommonDate = (date: string) =>
222 <Typography noWrap>{formatDate(date)}</Typography>;
224 export const CommonUuid = withResourceData('uuid', renderCommonData);
226 // Api Client Authorizations
227 export const TokenApiClientId = withResourceData('apiClientId', renderCommonData);
229 export const TokenApiToken = withResourceData('apiToken', renderCommonData);
231 export const TokenCreatedByIpAddress = withResourceData('createdByIpAddress', renderCommonDate);
233 export const TokenDefaultOwnerUuid = withResourceData('defaultOwnerUuid', renderCommonData);
235 export const TokenExpiresAt = withResourceData('expiresAt', renderCommonDate);
237 export const TokenLastUsedAt = withResourceData('lastUsedAt', renderCommonDate);
239 export const TokenLastUsedByIpAddress = withResourceData('lastUsedByIpAddress', renderCommonData);
241 export const TokenScopes = withResourceData('scopes', renderCommonData);
243 export const TokenUserId = withResourceData('userId', renderCommonData);
245 const clusterColors = [
253 export const ResourceCluster = (props: { uuid: string }) => {
254 const CLUSTER_ID_LENGTH = 5;
255 const pos = props.uuid.length > CLUSTER_ID_LENGTH ? props.uuid.indexOf('-') : 5;
256 const clusterId = pos >= CLUSTER_ID_LENGTH ? props.uuid.substr(0, pos) : '';
257 const ci = pos >= CLUSTER_ID_LENGTH ? (((((
258 (props.uuid.charCodeAt(0) * props.uuid.charCodeAt(1))
259 + props.uuid.charCodeAt(2))
260 * props.uuid.charCodeAt(3))
261 + props.uuid.charCodeAt(4))) % clusterColors.length) : 0;
262 return <span style={{
263 backgroundColor: clusterColors[ci][0],
264 color: clusterColors[ci][1],
267 }}>{clusterId}</span>;
271 const renderLinkName = (item: { name: string }) =>
272 <Typography noWrap>{item.name || '(none)'}</Typography>;
274 export const ResourceLinkName = connect(
275 (state: RootState, props: { uuid: string }) => {
276 const resource = getResource<LinkResource>(props.uuid)(state.resources);
277 return resource || { name: '' };
280 const renderLinkClass = (item: { linkClass: string }) =>
281 <Typography noWrap>{item.linkClass}</Typography>;
283 export const ResourceLinkClass = connect(
284 (state: RootState, props: { uuid: string }) => {
285 const resource = getResource<LinkResource>(props.uuid)(state.resources);
286 return resource || { linkClass: '' };
289 // const renderLinkTail = (dispatch: Dispatch, item: { uuid: string, tailUuid: string, tailKind: string }) => {
290 // const currentLabel = resourceLabel(item.tailKind);
291 // const isUnknow = currentLabel === "Unknown";
294 // renderLink(dispatch, item.tailUuid, "name", currentLabel)
296 // <Typography noWrap color="default">
303 const renderLink = (dispatch: Dispatch, item: Resource) => {
304 const name = (item as LinkResource).name;
305 const fullName = getUserDisplayName(item as UserResource);
307 return <Typography noWrap color="primary" style={{ 'cursor': 'pointer' }} onClick={() => dispatch<any>(navigateTo(item.uuid))}>
308 {resourceLabel(item.kind)}: {name || fullName || item.uuid}
312 export const ResourceLinkTail = connect(
313 (state: RootState, props: { uuid: string }) => {
314 const resource = getResource<LinkResource>(props.uuid)(state.resources);
315 const tailResource = getResource<Resource>(resource?.tailUuid || '')(state.resources);
318 item: tailResource || { uuid: resource?.tailUuid || '', kind: resource?.headKind || ResourceKind.NONE }
320 })((props: { item: Resource } & DispatchProp<any>) =>
321 renderLink(props.dispatch, props.item));
323 export const ResourceLinkHead = connect(
324 (state: RootState, props: { uuid: string }) => {
325 const resource = getResource<LinkResource>(props.uuid)(state.resources);
326 const headResource = getResource<Resource>(resource?.headUuid || '')(state.resources);
329 item: headResource || { uuid: resource?.headUuid || '', kind: resource?.headKind || ResourceKind.NONE }
331 })((props: { item: Resource } & DispatchProp<any>) =>
332 renderLink(props.dispatch, props.item));
334 export const ResourceLinkUuid = connect(
335 (state: RootState, props: { uuid: string }) => {
336 const resource = getResource<LinkResource>(props.uuid)(state.resources);
337 return resource || { uuid: '' };
341 const resourceRunProcess = (dispatch: Dispatch, uuid: string) => {
345 <Tooltip title="Run process">
346 <IconButton onClick={() => dispatch<any>(openRunProcess(uuid))}>
354 export const ResourceRunProcess = connect(
355 (state: RootState, props: { uuid: string }) => {
356 const resource = getResource<WorkflowResource>(props.uuid)(state.resources);
358 uuid: resource ? resource.uuid : ''
360 })((props: { uuid: string } & DispatchProp<any>) =>
361 resourceRunProcess(props.dispatch, props.uuid));
363 const renderWorkflowStatus = (uuidPrefix: string, ownerUuid?: string) => {
364 if (ownerUuid === getPublicUuid(uuidPrefix)) {
365 return renderStatus(WorkflowStatus.PUBLIC);
367 return renderStatus(WorkflowStatus.PRIVATE);
371 const renderStatus = (status: string) =>
372 <Typography noWrap style={{ width: '60px' }}>{status}</Typography>;
374 export const ResourceWorkflowStatus = connect(
375 (state: RootState, props: { uuid: string }) => {
376 const resource = getResource<WorkflowResource>(props.uuid)(state.resources);
377 const uuidPrefix = getUuidPrefix(state);
379 ownerUuid: resource ? resource.ownerUuid : '',
382 })((props: { ownerUuid?: string, uuidPrefix: string }) => renderWorkflowStatus(props.uuidPrefix, props.ownerUuid));
384 export const ResourceLastModifiedDate = connect(
385 (state: RootState, props: { uuid: string }) => {
386 const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
387 return { date: resource ? resource.modifiedAt : '' };
388 })((props: { date: string }) => renderDate(props.date));
390 export const ResourceCreatedAtDate = connect(
391 (state: RootState, props: { uuid: string }) => {
392 const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
393 return { date: resource ? resource.createdAt : '' };
394 })((props: { date: string }) => renderDate(props.date));
396 export const ResourceTrashDate = connect(
397 (state: RootState, props: { uuid: string }) => {
398 const resource = getResource<TrashableResource>(props.uuid)(state.resources);
399 return { date: resource ? resource.trashAt : '' };
400 })((props: { date: string }) => renderDate(props.date));
402 export const ResourceDeleteDate = connect(
403 (state: RootState, props: { uuid: string }) => {
404 const resource = getResource<TrashableResource>(props.uuid)(state.resources);
405 return { date: resource ? resource.deleteAt : '' };
406 })((props: { date: string }) => renderDate(props.date));
408 export const renderFileSize = (fileSize?: number) =>
409 <Typography noWrap style={{ minWidth: '45px' }}>
410 {formatFileSize(fileSize)}
413 export const ResourceFileSize = connect(
414 (state: RootState, props: { uuid: string }) => {
415 const resource = getResource<CollectionResource>(props.uuid)(state.resources);
417 if (resource && resource.kind !== ResourceKind.COLLECTION) {
418 return { fileSize: '' };
421 return { fileSize: resource ? resource.fileSizeTotal : 0 };
422 })((props: { fileSize?: number }) => renderFileSize(props.fileSize));
424 const renderOwner = (owner: string) =>
429 export const ResourceOwner = connect(
430 (state: RootState, props: { uuid: string }) => {
431 const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
432 return { owner: resource ? resource.ownerUuid : '' };
433 })((props: { owner: string }) => renderOwner(props.owner));
435 export const ResourceOwnerName = connect(
436 (state: RootState, props: { uuid: string }) => {
437 const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
438 const ownerNameState = state.ownerName;
439 const ownerName = ownerNameState.find(it => it.uuid === resource!.ownerUuid);
440 return { owner: ownerName ? ownerName!.name : resource!.ownerUuid };
441 })((props: { owner: string }) => renderOwner(props.owner));
445 (state: RootState, props: { uuid: string }) => {
446 let userFullname = '';
447 const resource = getResource<GroupContentsResource & UserResource>(props.uuid)(state.resources);
450 userFullname = getUserFullname(resource as User) || (resource as GroupContentsResource).name;
453 return { uuid: props.uuid, userFullname };
456 export const ResourceOwnerWithName =
459 withStyles({}, { withTheme: true }))
460 ((props: { uuid: string, userFullname: string, dispatch: Dispatch, theme: ArvadosTheme }) => {
461 const { uuid, userFullname, dispatch, theme } = props;
463 if (userFullname === '') {
464 dispatch<any>(loadResource(uuid, false));
465 return <Typography style={{ color: theme.palette.primary.main }} inline noWrap>
470 return <Typography style={{ color: theme.palette.primary.main }} inline noWrap>
471 {userFullname} ({uuid})
475 export const UserNameFromID =
477 (props: { uuid: string, userFullname: string, dispatch: Dispatch }) => {
478 const { uuid, userFullname, dispatch } = props;
480 if (userFullname === '') {
481 dispatch<any>(loadResource(uuid, false));
484 {userFullname ? userFullname : uuid}
488 export const ResponsiblePerson =
491 (state: RootState, props: { uuid: string, parentRef: HTMLElement | null }) => {
492 let responsiblePersonName: string = '';
493 let responsiblePersonUUID: string = '';
494 let responsiblePersonProperty: string = '';
496 if (state.auth.config.clusterConfig.Collections.ManagedProperties) {
498 const keys = Object.keys(state.auth.config.clusterConfig.Collections.ManagedProperties);
500 while (!responsiblePersonProperty && keys[index]) {
501 const key = keys[index];
502 if (state.auth.config.clusterConfig.Collections.ManagedProperties[key].Function === 'original_owner') {
503 responsiblePersonProperty = key;
509 let resource: Resource | undefined = getResource<GroupContentsResource & UserResource>(props.uuid)(state.resources);
511 while (resource && resource.kind !== ResourceKind.USER && responsiblePersonProperty) {
512 responsiblePersonUUID = (resource as CollectionResource).properties[responsiblePersonProperty];
513 resource = getResource<GroupContentsResource & UserResource>(responsiblePersonUUID)(state.resources);
516 if (resource && resource.kind === ResourceKind.USER) {
517 responsiblePersonName = getUserFullname(resource as UserResource) || (resource as GroupContentsResource).name;
520 return { uuid: responsiblePersonUUID, responsiblePersonName, parentRef: props.parentRef };
522 withStyles({}, { withTheme: true }))
523 ((props: { uuid: string | null, responsiblePersonName: string, parentRef: HTMLElement | null, theme: ArvadosTheme }) => {
524 const { uuid, responsiblePersonName, parentRef, theme } = props;
526 if (!uuid && parentRef) {
527 parentRef.style.display = 'none';
529 } else if (parentRef) {
530 parentRef.style.display = 'block';
533 if (!responsiblePersonName) {
534 return <Typography style={{ color: theme.palette.primary.main }} inline noWrap>
539 return <Typography style={{ color: theme.palette.primary.main }} inline noWrap>
540 {responsiblePersonName} ({uuid})
544 const renderType = (type: string, subtype: string) =>
546 {resourceLabel(type, subtype)}
549 export const ResourceType = connect(
550 (state: RootState, props: { uuid: string }) => {
551 const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
552 return { type: resource ? resource.kind : '', subtype: resource && resource.kind === ResourceKind.GROUP ? resource.groupClass : '' };
553 })((props: { type: string, subtype: string }) => renderType(props.type, props.subtype));
555 export const ResourceStatus = connect((state: RootState, props: { uuid: string }) => {
556 return { resource: getResource<GroupContentsResource>(props.uuid)(state.resources) };
557 })((props: { resource: GroupContentsResource }) =>
558 (props.resource && props.resource.kind === ResourceKind.COLLECTION)
559 ? <CollectionStatus uuid={props.resource.uuid} />
560 : <ProcessStatus uuid={props.resource.uuid} />
563 export const CollectionStatus = connect((state: RootState, props: { uuid: string }) => {
564 return { collection: getResource<CollectionResource>(props.uuid)(state.resources) };
565 })((props: { collection: CollectionResource }) =>
566 (props.collection.uuid !== props.collection.currentVersionUuid)
567 ? <Typography>version {props.collection.version}</Typography>
568 : <Typography>head version</Typography>
571 export const ProcessStatus = compose(
572 connect((state: RootState, props: { uuid: string }) => {
573 return { process: getProcess(props.uuid)(state.resources) };
575 withStyles({}, { withTheme: true }))
576 ((props: { process?: Process, theme: ArvadosTheme }) => {
577 const status = props.process ? getProcessStatus(props.process) : "-";
580 style={{ color: getProcessStatusColor(status, props.theme) }} >
585 export const ProcessStartDate = connect(
586 (state: RootState, props: { uuid: string }) => {
587 const process = getProcess(props.uuid)(state.resources);
588 return { date: (process && process.container) ? process.container.startedAt : '' };
589 })((props: { date: string }) => renderDate(props.date));
591 export const renderRunTime = (time: number) =>
592 <Typography noWrap style={{ minWidth: '45px' }}>
593 {formatTime(time, true)}
596 interface ContainerRunTimeProps {
600 interface ContainerRunTimeState {
604 export const ContainerRunTime = connect((state: RootState, props: { uuid: string }) => {
605 return { process: getProcess(props.uuid)(state.resources) };
606 })(class extends React.Component<ContainerRunTimeProps, ContainerRunTimeState> {
609 constructor(props: ContainerRunTimeProps) {
611 this.state = { runtime: this.getRuntime() };
615 return this.props.process ? getProcessRuntime(this.props.process) : 0;
619 this.setState({ runtime: this.getRuntime() });
622 componentDidMount() {
623 this.timer = setInterval(this.updateRuntime.bind(this), 5000);
626 componentWillUnmount() {
627 clearInterval(this.timer);
631 return renderRunTime(this.state.runtime);