1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import * as 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 { ResourceKind, TrashableResource } from '~/models/resource';
9 import { ProjectIcon, CollectionIcon, ProcessIcon, DefaultIcon, WorkflowIcon, ShareIcon } from '~/components/icon/icon';
10 import { formatDate, formatFileSize } 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 } 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 } from '~/views/workflow-panel/workflow-panel-view';
21 import { getUuidPrefix, openRunProcess } from '~/store/workflow-panel/workflow-panel-actions';
22 import { getResourceData } from "~/store/resources-data/resources-data";
23 import { openSharingDialog } from '~/store/sharing-dialog/sharing-dialog-actions';
24 import { UserResource } from '~/models/user';
25 import { toggleIsActive, toggleIsAdmin } from '~/store/users/users-actions';
26 import { LinkResource } from '~/models/link';
27 import { navigateTo } from '~/store/navigation/navigation-action';
28 import { withResourceData } from '~/views-components/data-explorer/with-resources';
30 const renderName = (dispatch: Dispatch, item: { name: string; uuid: string, kind: string }) =>
31 <Grid container alignItems="center" wrap="nowrap" spacing={16}>
33 {renderIcon(item.kind)}
36 <Typography color="primary" style={{ width: 'auto', cursor: 'pointer' }} onClick={() => dispatch<any>(navigateTo(item.uuid))}>
41 <Typography variant="caption">
42 <FavoriteStar resourceUuid={item.uuid} />
43 <PublicFavoriteStar resourceUuid={item.uuid} />
48 export const ResourceName = connect(
49 (state: RootState, props: { uuid: string }) => {
50 const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
51 return resource || { name: '', uuid: '', kind: '' };
52 })((resource: { name: string; uuid: string, kind: string } & DispatchProp<any>) => renderName(resource.dispatch, resource));
54 const renderIcon = (kind: string) => {
56 case ResourceKind.PROJECT:
57 return <ProjectIcon />;
58 case ResourceKind.COLLECTION:
59 return <CollectionIcon />;
60 case ResourceKind.PROCESS:
61 return <ProcessIcon />;
62 case ResourceKind.WORKFLOW:
63 return <WorkflowIcon />;
65 return <DefaultIcon />;
69 const renderDate = (date?: string) => {
70 return <Typography noWrap style={{ minWidth: '100px' }}>{formatDate(date)}</Typography>;
73 const renderWorkflowName = (item: { name: string; uuid: string, kind: string, ownerUuid: string }) =>
74 <Grid container alignItems="center" wrap="nowrap" spacing={16}>
76 {renderIcon(item.kind)}
79 <Typography color="primary" style={{ width: '100px' }}>
85 export const ResourceWorkflowName = connect(
86 (state: RootState, props: { uuid: string }) => {
87 const resource = getResource<WorkflowResource>(props.uuid)(state.resources);
88 return resource || { name: '', uuid: '', kind: '', ownerUuid: '' };
89 })(renderWorkflowName);
91 const getPublicUuid = (uuidPrefix: string) => {
92 return `${uuidPrefix}-tpzed-anonymouspublic`;
95 const resourceShare = (dispatch: Dispatch, uuidPrefix: string, ownerUuid?: string, uuid?: string) => {
96 const isPublic = ownerUuid === getPublicUuid(uuidPrefix);
100 <Tooltip title="Share">
101 <IconButton onClick={() => dispatch<any>(openSharingDialog(uuid))}>
110 export const ResourceShare = connect(
111 (state: RootState, props: { uuid: string }) => {
112 const resource = getResource<WorkflowResource>(props.uuid)(state.resources);
113 const uuidPrefix = getUuidPrefix(state);
115 uuid: resource ? resource.uuid : '',
116 ownerUuid: resource ? resource.ownerUuid : '',
119 })((props: { ownerUuid?: string, uuidPrefix: string, uuid?: string } & DispatchProp<any>) =>
120 resourceShare(props.dispatch, props.uuidPrefix, props.ownerUuid, props.uuid));
122 const renderFirstName = (item: { firstName: string }) => {
123 return <Typography noWrap>{item.firstName}</Typography>;
127 export const ResourceFirstName = connect(
128 (state: RootState, props: { uuid: string }) => {
129 const resource = getResource<UserResource>(props.uuid)(state.resources);
130 return resource || { firstName: '' };
133 const renderLastName = (item: { lastName: string }) =>
134 <Typography noWrap>{item.lastName}</Typography>;
136 export const ResourceLastName = connect(
137 (state: RootState, props: { uuid: string }) => {
138 const resource = getResource<UserResource>(props.uuid)(state.resources);
139 return resource || { lastName: '' };
142 const renderUuid = (item: { uuid: string }) =>
143 <Typography noWrap>{item.uuid}</Typography>;
145 export const ResourceUuid = connect(
146 (state: RootState, props: { uuid: string }) => {
147 const resource = getResource<UserResource>(props.uuid)(state.resources);
148 return resource || { uuid: '' };
151 const renderEmail = (item: { email: string }) =>
152 <Typography noWrap>{item.email}</Typography>;
154 export const ResourceEmail = connect(
155 (state: RootState, props: { uuid: string }) => {
156 const resource = getResource<UserResource>(props.uuid)(state.resources);
157 return resource || { email: '' };
160 const renderIsActive = (props: { uuid: string, isActive: boolean, toggleIsActive: (uuid: string) => void }) =>
163 checked={props.isActive}
164 onClick={() => props.toggleIsActive(props.uuid)} />;
166 export const ResourceIsActive = connect(
167 (state: RootState, props: { uuid: string }) => {
168 const resource = getResource<UserResource>(props.uuid)(state.resources);
169 return resource || { isActive: false };
170 }, { toggleIsActive }
173 const renderIsAdmin = (props: { uuid: string, isAdmin: boolean, toggleIsAdmin: (uuid: string) => void }) =>
176 checked={props.isAdmin}
177 onClick={() => props.toggleIsAdmin(props.uuid)} />;
179 export const ResourceIsAdmin = connect(
180 (state: RootState, props: { uuid: string }) => {
181 const resource = getResource<UserResource>(props.uuid)(state.resources);
182 return resource || { isAdmin: false };
186 const renderUsername = (item: { username: string }) =>
187 <Typography noWrap>{item.username}</Typography>;
189 export const ResourceUsername = connect(
190 (state: RootState, props: { uuid: string }) => {
191 const resource = getResource<UserResource>(props.uuid)(state.resources);
192 return resource || { username: '' };
196 const renderCommonData = (data: string) =>
197 <Typography noWrap>{data}</Typography>;
199 const renderCommonDate = (date: string) =>
200 <Typography noWrap>{formatDate(date)}</Typography>;
202 export const CommonUuid = withResourceData('uuid', renderCommonData);
204 // Api Client Authorizations
205 export const TokenApiClientId = withResourceData('apiClientId', renderCommonData);
207 export const TokenApiToken = withResourceData('apiToken', renderCommonData);
209 export const TokenCreatedByIpAddress = withResourceData('createdByIpAddress', renderCommonDate);
211 export const TokenDefaultOwnerUuid = withResourceData('defaultOwnerUuid', renderCommonData);
213 export const TokenExpiresAt = withResourceData('expiresAt', renderCommonDate);
215 export const TokenLastUsedAt = withResourceData('lastUsedAt', renderCommonDate);
217 export const TokenLastUsedByIpAddress = withResourceData('lastUsedByIpAddress', renderCommonData);
219 export const TokenScopes = withResourceData('scopes', renderCommonData);
221 export const TokenUserId = withResourceData('userId', renderCommonData);
223 // Compute Node Resources
224 const renderNodeInfo = (data: string) => {
225 return <Typography>{JSON.stringify(data, null, 4)}</Typography>;
228 const clusterColors = [
236 export const ResourceCluster = (props: { uuid: string }) => {
237 const CLUSTER_ID_LENGTH = 5;
238 const pos = props.uuid.indexOf('-');
239 const clusterId = pos >= CLUSTER_ID_LENGTH ? props.uuid.substr(0, pos) : '';
240 const ci = pos >= CLUSTER_ID_LENGTH ? (props.uuid.charCodeAt(0) + props.uuid.charCodeAt(1)) % clusterColors.length : 0;
243 backgroundColor: clusterColors[ci][0],
244 color: clusterColors[ci][1],
247 }}>{clusterId}</span>
251 export const ComputeNodeInfo = withResourceData('info', renderNodeInfo);
253 export const ComputeNodeDomain = withResourceData('domain', renderCommonData);
255 export const ComputeNodeFirstPingAt = withResourceData('firstPingAt', renderCommonDate);
257 export const ComputeNodeHostname = withResourceData('hostname', renderCommonData);
259 export const ComputeNodeIpAddress = withResourceData('ipAddress', renderCommonData);
261 export const ComputeNodeJobUuid = withResourceData('jobUuid', renderCommonData);
263 export const ComputeNodeLastPingAt = withResourceData('lastPingAt', renderCommonDate);
266 const renderLinkName = (item: { name: string }) =>
267 <Typography noWrap>{item.name || '(none)'}</Typography>;
269 export const ResourceLinkName = connect(
270 (state: RootState, props: { uuid: string }) => {
271 const resource = getResource<LinkResource>(props.uuid)(state.resources);
272 return resource || { name: '' };
275 const renderLinkClass = (item: { linkClass: string }) =>
276 <Typography noWrap>{item.linkClass}</Typography>;
278 export const ResourceLinkClass = connect(
279 (state: RootState, props: { uuid: string }) => {
280 const resource = getResource<LinkResource>(props.uuid)(state.resources);
281 return resource || { linkClass: '' };
284 const renderLinkTail = (dispatch: Dispatch, item: { uuid: string, tailUuid: string, tailKind: string }) => {
285 const currentLabel = resourceLabel(item.tailKind);
286 const isUnknow = currentLabel === "Unknown";
289 renderLink(dispatch, item.tailUuid, currentLabel)
291 <Typography noWrap color="default">
298 const renderLink = (dispatch: Dispatch, uuid: string, label: string) =>
299 <Typography noWrap color="primary" style={{ 'cursor': 'pointer' }} onClick={() => dispatch<any>(navigateTo(uuid))}>
303 export const ResourceLinkTail = connect(
304 (state: RootState, props: { uuid: string }) => {
305 const resource = getResource<LinkResource>(props.uuid)(state.resources);
307 item: resource || { uuid: '', tailUuid: '', tailKind: ResourceKind.NONE }
309 })((props: { item: any } & DispatchProp<any>) =>
310 renderLinkTail(props.dispatch, props.item));
312 const renderLinkHead = (dispatch: Dispatch, item: { uuid: string, headUuid: string, headKind: ResourceKind }) =>
313 renderLink(dispatch, item.headUuid, resourceLabel(item.headKind));
315 export const ResourceLinkHead = connect(
316 (state: RootState, props: { uuid: string }) => {
317 const resource = getResource<LinkResource>(props.uuid)(state.resources);
319 item: resource || { uuid: '', headUuid: '', headKind: ResourceKind.NONE }
321 })((props: { item: any } & DispatchProp<any>) =>
322 renderLinkHead(props.dispatch, props.item));
324 export const ResourceLinkUuid = connect(
325 (state: RootState, props: { uuid: string }) => {
326 const resource = getResource<LinkResource>(props.uuid)(state.resources);
327 return resource || { uuid: '' };
331 const resourceRunProcess = (dispatch: Dispatch, uuid: string) => {
335 <Tooltip title="Run process">
336 <IconButton onClick={() => dispatch<any>(openRunProcess(uuid))}>
344 export const ResourceRunProcess = connect(
345 (state: RootState, props: { uuid: string }) => {
346 const resource = getResource<WorkflowResource>(props.uuid)(state.resources);
348 uuid: resource ? resource.uuid : ''
350 })((props: { uuid: string } & DispatchProp<any>) =>
351 resourceRunProcess(props.dispatch, props.uuid));
353 const renderWorkflowStatus = (uuidPrefix: string, ownerUuid?: string) => {
354 if (ownerUuid === getPublicUuid(uuidPrefix)) {
355 return renderStatus(ResourceStatus.PUBLIC);
357 return renderStatus(ResourceStatus.PRIVATE);
361 const renderStatus = (status: string) =>
362 <Typography noWrap style={{ width: '60px' }}>{status}</Typography>;
364 export const ResourceWorkflowStatus = connect(
365 (state: RootState, props: { uuid: string }) => {
366 const resource = getResource<WorkflowResource>(props.uuid)(state.resources);
367 const uuidPrefix = getUuidPrefix(state);
369 ownerUuid: resource ? resource.ownerUuid : '',
372 })((props: { ownerUuid?: string, uuidPrefix: string }) => renderWorkflowStatus(props.uuidPrefix, props.ownerUuid));
374 export const ResourceLastModifiedDate = connect(
375 (state: RootState, props: { uuid: string }) => {
376 const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
377 return { date: resource ? resource.modifiedAt : '' };
378 })((props: { date: string }) => renderDate(props.date));
380 export const ResourceTrashDate = connect(
381 (state: RootState, props: { uuid: string }) => {
382 const resource = getResource<TrashableResource>(props.uuid)(state.resources);
383 return { date: resource ? resource.trashAt : '' };
384 })((props: { date: string }) => renderDate(props.date));
386 export const ResourceDeleteDate = connect(
387 (state: RootState, props: { uuid: string }) => {
388 const resource = getResource<TrashableResource>(props.uuid)(state.resources);
389 return { date: resource ? resource.deleteAt : '' };
390 })((props: { date: string }) => renderDate(props.date));
392 export const renderFileSize = (fileSize?: number) =>
393 <Typography noWrap style={{ minWidth: '45px' }}>
394 {formatFileSize(fileSize)}
397 export const ResourceFileSize = connect(
398 (state: RootState, props: { uuid: string }) => {
399 const resource = getResourceData(props.uuid)(state.resourcesData);
400 return { fileSize: resource ? resource.fileSize : 0 };
401 })((props: { fileSize?: number }) => renderFileSize(props.fileSize));
403 const renderOwner = (owner: string) =>
408 export const ResourceOwner = connect(
409 (state: RootState, props: { uuid: string }) => {
410 const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
411 return { owner: resource ? resource.ownerUuid : '' };
412 })((props: { owner: string }) => renderOwner(props.owner));
414 export const ResourceOwnerName = connect(
415 (state: RootState, props: { uuid: string }) => {
416 const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
417 const ownerNameState = state.ownerName;
418 const ownerName = ownerNameState.find(it => it.uuid === resource!.ownerUuid);
419 return { owner: ownerName ? ownerName!.name : resource!.ownerUuid };
420 })((props: { owner: string }) => renderOwner(props.owner));
422 const renderType = (type: string) =>
424 {resourceLabel(type)}
427 export const ResourceType = connect(
428 (state: RootState, props: { uuid: string }) => {
429 const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
430 return { type: resource ? resource.kind : '' };
431 })((props: { type: string }) => renderType(props.type));
433 export const ProcessStatus = compose(
434 connect((state: RootState, props: { uuid: string }) => {
435 return { process: getProcess(props.uuid)(state.resources) };
437 withStyles({}, { withTheme: true }))
438 ((props: { process?: Process, theme: ArvadosTheme }) => {
439 const status = props.process ? getProcessStatus(props.process) : "-";
443 style={{ color: getProcessStatusColor(status, props.theme) }} >