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, 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));
134 const renderFirstName = (item: { firstName: string }) => {
135 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 renderUuid = (item: { uuid: string }) =>
155 <Typography noWrap>{item.uuid}</Typography>;
157 export const ResourceUuid = connect(
158 (state: RootState, props: { uuid: string }) => {
159 const resource = getResource<UserResource>(props.uuid)(state.resources);
160 return resource || { uuid: '' };
163 const renderEmail = (item: { email: string }) =>
164 <Typography noWrap>{item.email}</Typography>;
166 export const ResourceEmail = connect(
167 (state: RootState, props: { uuid: string }) => {
168 const resource = getResource<UserResource>(props.uuid)(state.resources);
169 return resource || { email: '' };
172 const renderIsActive = (props: { uuid: string, isActive: boolean, toggleIsActive: (uuid: string) => void }) =>
175 checked={props.isActive}
176 onClick={() => props.toggleIsActive(props.uuid)} />;
178 export const ResourceIsActive = connect(
179 (state: RootState, props: { uuid: string }) => {
180 const resource = getResource<UserResource>(props.uuid)(state.resources);
181 return resource || { isActive: false };
182 }, { toggleIsActive }
185 const renderIsAdmin = (props: { uuid: string, isAdmin: boolean, toggleIsAdmin: (uuid: string) => void }) =>
188 checked={props.isAdmin}
189 onClick={() => props.toggleIsAdmin(props.uuid)} />;
191 export const ResourceIsAdmin = connect(
192 (state: RootState, props: { uuid: string }) => {
193 const resource = getResource<UserResource>(props.uuid)(state.resources);
194 return resource || { isAdmin: false };
198 const renderUsername = (item: { username: string }) =>
199 <Typography noWrap>{item.username}</Typography>;
201 export const ResourceUsername = connect(
202 (state: RootState, props: { uuid: string }) => {
203 const resource = getResource<UserResource>(props.uuid)(state.resources);
204 return resource || { username: '' };
208 const renderCommonData = (data: string) =>
209 <Typography noWrap>{data}</Typography>;
211 const renderCommonDate = (date: string) =>
212 <Typography noWrap>{formatDate(date)}</Typography>;
214 export const CommonUuid = withResourceData('uuid', renderCommonData);
216 // Api Client Authorizations
217 export const TokenApiClientId = withResourceData('apiClientId', renderCommonData);
219 export const TokenApiToken = withResourceData('apiToken', renderCommonData);
221 export const TokenCreatedByIpAddress = withResourceData('createdByIpAddress', renderCommonDate);
223 export const TokenDefaultOwnerUuid = withResourceData('defaultOwnerUuid', renderCommonData);
225 export const TokenExpiresAt = withResourceData('expiresAt', renderCommonDate);
227 export const TokenLastUsedAt = withResourceData('lastUsedAt', renderCommonDate);
229 export const TokenLastUsedByIpAddress = withResourceData('lastUsedByIpAddress', renderCommonData);
231 export const TokenScopes = withResourceData('scopes', renderCommonData);
233 export const TokenUserId = withResourceData('userId', renderCommonData);
235 const clusterColors = [
243 export const ResourceCluster = (props: { uuid: string }) => {
244 const CLUSTER_ID_LENGTH = 5;
245 const pos = props.uuid.length > CLUSTER_ID_LENGTH ? props.uuid.indexOf('-') : 5;
246 const clusterId = pos >= CLUSTER_ID_LENGTH ? props.uuid.substr(0, pos) : '';
247 const ci = pos >= CLUSTER_ID_LENGTH ? (((((
248 (props.uuid.charCodeAt(0) * props.uuid.charCodeAt(1))
249 + props.uuid.charCodeAt(2))
250 * props.uuid.charCodeAt(3))
251 + props.uuid.charCodeAt(4))) % clusterColors.length) : 0;
252 return <span style={{
253 backgroundColor: clusterColors[ci][0],
254 color: clusterColors[ci][1],
257 }}>{clusterId}</span>;
261 const renderLinkName = (item: { name: string }) =>
262 <Typography noWrap>{item.name || '(none)'}</Typography>;
264 export const ResourceLinkName = connect(
265 (state: RootState, props: { uuid: string }) => {
266 const resource = getResource<LinkResource>(props.uuid)(state.resources);
267 return resource || { name: '' };
270 const renderLinkClass = (item: { linkClass: string }) =>
271 <Typography noWrap>{item.linkClass}</Typography>;
273 export const ResourceLinkClass = connect(
274 (state: RootState, props: { uuid: string }) => {
275 const resource = getResource<LinkResource>(props.uuid)(state.resources);
276 return resource || { linkClass: '' };
279 const renderLinkTail = (dispatch: Dispatch, item: { uuid: string, tailUuid: string, tailKind: string }) => {
280 const currentLabel = resourceLabel(item.tailKind);
281 const isUnknow = currentLabel === "Unknown";
284 renderLink(dispatch, item.tailUuid, currentLabel)
286 <Typography noWrap color="default">
293 const renderLink = (dispatch: Dispatch, uuid: string, label: string) =>
294 <Typography noWrap color="primary" style={{ 'cursor': 'pointer' }} onClick={() => dispatch<any>(navigateTo(uuid))}>
298 export const ResourceLinkTail = connect(
299 (state: RootState, props: { uuid: string }) => {
300 const resource = getResource<LinkResource>(props.uuid)(state.resources);
302 item: resource || { uuid: '', tailUuid: '', tailKind: ResourceKind.NONE }
304 })((props: { item: any } & DispatchProp<any>) =>
305 renderLinkTail(props.dispatch, props.item));
307 const renderLinkHead = (dispatch: Dispatch, item: { uuid: string, headUuid: string, headKind: ResourceKind }) =>
308 renderLink(dispatch, item.headUuid, resourceLabel(item.headKind));
310 export const ResourceLinkHead = connect(
311 (state: RootState, props: { uuid: string }) => {
312 const resource = getResource<LinkResource>(props.uuid)(state.resources);
314 item: resource || { uuid: '', headUuid: '', headKind: ResourceKind.NONE }
316 })((props: { item: any } & DispatchProp<any>) =>
317 renderLinkHead(props.dispatch, props.item));
319 export const ResourceLinkUuid = connect(
320 (state: RootState, props: { uuid: string }) => {
321 const resource = getResource<LinkResource>(props.uuid)(state.resources);
322 return resource || { uuid: '' };
326 const resourceRunProcess = (dispatch: Dispatch, uuid: string) => {
330 <Tooltip title="Run process">
331 <IconButton onClick={() => dispatch<any>(openRunProcess(uuid))}>
339 export const ResourceRunProcess = connect(
340 (state: RootState, props: { uuid: string }) => {
341 const resource = getResource<WorkflowResource>(props.uuid)(state.resources);
343 uuid: resource ? resource.uuid : ''
345 })((props: { uuid: string } & DispatchProp<any>) =>
346 resourceRunProcess(props.dispatch, props.uuid));
348 const renderWorkflowStatus = (uuidPrefix: string, ownerUuid?: string) => {
349 if (ownerUuid === getPublicUuid(uuidPrefix)) {
350 return renderStatus(WorkflowStatus.PUBLIC);
352 return renderStatus(WorkflowStatus.PRIVATE);
356 const renderStatus = (status: string) =>
357 <Typography noWrap style={{ width: '60px' }}>{status}</Typography>;
359 export const ResourceWorkflowStatus = connect(
360 (state: RootState, props: { uuid: string }) => {
361 const resource = getResource<WorkflowResource>(props.uuid)(state.resources);
362 const uuidPrefix = getUuidPrefix(state);
364 ownerUuid: resource ? resource.ownerUuid : '',
367 })((props: { ownerUuid?: string, uuidPrefix: string }) => renderWorkflowStatus(props.uuidPrefix, props.ownerUuid));
369 export const ResourceLastModifiedDate = connect(
370 (state: RootState, props: { uuid: string }) => {
371 const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
372 return { date: resource ? resource.modifiedAt : '' };
373 })((props: { date: string }) => renderDate(props.date));
375 export const ResourceCreatedAtDate = connect(
376 (state: RootState, props: { uuid: string }) => {
377 const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
378 return { date: resource ? resource.createdAt : '' };
379 })((props: { date: string }) => renderDate(props.date));
381 export const ResourceTrashDate = connect(
382 (state: RootState, props: { uuid: string }) => {
383 const resource = getResource<TrashableResource>(props.uuid)(state.resources);
384 return { date: resource ? resource.trashAt : '' };
385 })((props: { date: string }) => renderDate(props.date));
387 export const ResourceDeleteDate = connect(
388 (state: RootState, props: { uuid: string }) => {
389 const resource = getResource<TrashableResource>(props.uuid)(state.resources);
390 return { date: resource ? resource.deleteAt : '' };
391 })((props: { date: string }) => renderDate(props.date));
393 export const renderFileSize = (fileSize?: number) =>
394 <Typography noWrap style={{ minWidth: '45px' }}>
395 {formatFileSize(fileSize)}
398 export const ResourceFileSize = connect(
399 (state: RootState, props: { uuid: string }) => {
400 const resource = getResource<CollectionResource>(props.uuid)(state.resources);
402 if (resource && resource.kind !== ResourceKind.COLLECTION) {
403 return { fileSize: '' };
406 return { fileSize: resource ? resource.fileSizeTotal : 0 };
407 })((props: { fileSize?: number }) => renderFileSize(props.fileSize));
409 const renderOwner = (owner: string) =>
414 export const ResourceOwner = connect(
415 (state: RootState, props: { uuid: string }) => {
416 const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
417 return { owner: resource ? resource.ownerUuid : '' };
418 })((props: { owner: string }) => renderOwner(props.owner));
420 export const ResourceOwnerName = connect(
421 (state: RootState, props: { uuid: string }) => {
422 const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
423 const ownerNameState = state.ownerName;
424 const ownerName = ownerNameState.find(it => it.uuid === resource!.ownerUuid);
425 return { owner: ownerName ? ownerName!.name : resource!.ownerUuid };
426 })((props: { owner: string }) => renderOwner(props.owner));
430 (state: RootState, props: { uuid: string }) => {
431 let userFullname = '';
432 const resource = getResource<GroupContentsResource & UserResource>(props.uuid)(state.resources);
435 userFullname = getUserFullname(resource as User) || (resource as GroupContentsResource).name;
438 return { uuid: props.uuid, userFullname };
441 export const ResourceOwnerWithName =
444 withStyles({}, { withTheme: true }))
445 ((props: { uuid: string, userFullname: string, dispatch: Dispatch, theme: ArvadosTheme }) => {
446 const { uuid, userFullname, dispatch, theme } = props;
448 if (userFullname === '') {
449 dispatch<any>(loadResource(uuid, false));
450 return <Typography style={{ color: theme.palette.primary.main }} inline noWrap>
455 return <Typography style={{ color: theme.palette.primary.main }} inline noWrap>
456 {userFullname} ({uuid})
460 export const UserNameFromID =
462 (props: { uuid: string, userFullname: string, dispatch: Dispatch }) => {
463 const { uuid, userFullname, dispatch } = props;
465 if (userFullname === '') {
466 dispatch<any>(loadResource(uuid, false));
469 {userFullname ? userFullname : uuid}
473 export const ResponsiblePerson =
476 (state: RootState, props: { uuid: string, parentRef: HTMLElement | null }) => {
477 let responsiblePersonName: string = '';
478 let responsiblePersonUUID: string = '';
479 let responsiblePersonProperty: string = '';
481 if (state.auth.config.clusterConfig.Collections.ManagedProperties) {
483 const keys = Object.keys(state.auth.config.clusterConfig.Collections.ManagedProperties);
485 while (!responsiblePersonProperty && keys[index]) {
486 const key = keys[index];
487 if (state.auth.config.clusterConfig.Collections.ManagedProperties[key].Function === 'original_owner') {
488 responsiblePersonProperty = key;
494 let resource: Resource | undefined = getResource<GroupContentsResource & UserResource>(props.uuid)(state.resources);
496 while (resource && resource.kind !== ResourceKind.USER && responsiblePersonProperty) {
497 responsiblePersonUUID = (resource as CollectionResource).properties[responsiblePersonProperty];
498 resource = getResource<GroupContentsResource & UserResource>(responsiblePersonUUID)(state.resources);
501 if (resource && resource.kind === ResourceKind.USER) {
502 responsiblePersonName = getUserFullname(resource as UserResource) || (resource as GroupContentsResource).name;
505 return { uuid: responsiblePersonUUID, responsiblePersonName, parentRef: props.parentRef };
507 withStyles({}, { withTheme: true }))
508 ((props: { uuid: string | null, responsiblePersonName: string, parentRef: HTMLElement | null, theme: ArvadosTheme }) => {
509 const { uuid, responsiblePersonName, parentRef, theme } = props;
511 if (!uuid && parentRef) {
512 parentRef.style.display = 'none';
514 } else if (parentRef) {
515 parentRef.style.display = 'block';
518 if (!responsiblePersonName) {
519 return <Typography style={{ color: theme.palette.primary.main }} inline noWrap>
524 return <Typography style={{ color: theme.palette.primary.main }} inline noWrap>
525 {responsiblePersonName} ({uuid})
529 const renderType = (type: string, subtype: string) =>
531 {resourceLabel(type, subtype)}
534 export const ResourceType = connect(
535 (state: RootState, props: { uuid: string }) => {
536 const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
537 return { type: resource ? resource.kind : '', subtype: resource && resource.kind === ResourceKind.GROUP ? resource.groupClass : '' };
538 })((props: { type: string, subtype: string }) => renderType(props.type, props.subtype));
540 export const ResourceStatus = connect((state: RootState, props: { uuid: string }) => {
541 return { resource: getResource<GroupContentsResource>(props.uuid)(state.resources) };
542 })((props: { resource: GroupContentsResource }) =>
543 (props.resource && props.resource.kind === ResourceKind.COLLECTION)
544 ? <CollectionStatus uuid={props.resource.uuid} />
545 : <ProcessStatus uuid={props.resource.uuid} />
548 export const CollectionStatus = connect((state: RootState, props: { uuid: string }) => {
549 return { collection: getResource<CollectionResource>(props.uuid)(state.resources) };
550 })((props: { collection: CollectionResource }) =>
551 (props.collection.uuid !== props.collection.currentVersionUuid)
552 ? <Typography>version {props.collection.version}</Typography>
553 : <Typography>head version</Typography>
556 export const ProcessStatus = compose(
557 connect((state: RootState, props: { uuid: string }) => {
558 return { process: getProcess(props.uuid)(state.resources) };
560 withStyles({}, { withTheme: true }))
561 ((props: { process?: Process, theme: ArvadosTheme }) => {
562 const status = props.process ? getProcessStatus(props.process) : "-";
565 style={{ color: getProcessStatusColor(status, props.theme) }} >
570 export const ProcessStartDate = connect(
571 (state: RootState, props: { uuid: string }) => {
572 const process = getProcess(props.uuid)(state.resources);
573 return { date: (process && process.container) ? process.container.startedAt : '' };
574 })((props: { date: string }) => renderDate(props.date));
576 export const renderRunTime = (time: number) =>
577 <Typography noWrap style={{ minWidth: '45px' }}>
578 {formatTime(time, true)}
581 interface ContainerRunTimeProps {
585 interface ContainerRunTimeState {
589 export const ContainerRunTime = connect((state: RootState, props: { uuid: string }) => {
590 return { process: getProcess(props.uuid)(state.resources) };
591 })(class extends React.Component<ContainerRunTimeProps, ContainerRunTimeState> {
594 constructor(props: ContainerRunTimeProps) {
596 this.state = { runtime: this.getRuntime() };
600 return this.props.process ? getProcessRuntime(this.props.process) : 0;
604 this.setState({ runtime: this.getRuntime() });
607 componentDidMount() {
608 this.timer = setInterval(this.updateRuntime.bind(this), 5000);
611 componentWillUnmount() {
612 clearInterval(this.timer);
616 return renderRunTime(this.state.runtime);