1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import React, { ReactElement, useState } from 'react';
26 } from '@material-ui/core';
27 import { ArvadosTheme } from 'common/custom-theme';
28 import { CloseIcon, InfoIcon, ProcessIcon, InputIcon, OutputIcon } from 'components/icon/icon';
29 import { MPVPanelProps } from 'components/multi-panel-view/multi-panel-view';
31 BooleanCommandInputParameter,
32 CommandInputParameter,
35 DirectoryArrayCommandInputParameter,
36 DirectoryCommandInputParameter,
37 EnumCommandInputParameter,
38 FileArrayCommandInputParameter,
39 FileCommandInputParameter,
40 FloatArrayCommandInputParameter,
41 FloatCommandInputParameter,
42 IntArrayCommandInputParameter,
43 IntCommandInputParameter,
46 StringArrayCommandInputParameter,
47 StringCommandInputParameter,
48 } from "models/workflow";
49 import { CommandOutputParameter } from 'cwlts/mappings/v1.0/CommandOutputParameter';
50 import { File } from 'models/workflow';
51 import { getInlineFileUrl } from 'views-components/context-menu/actions/helpers';
52 import { AuthState } from 'store/auth/auth-reducer';
53 import mime from 'mime';
54 import { DefaultView } from 'components/default-view/default-view';
55 import { getNavUrl } from 'routes/routes';
56 import { Link as RouterLink } from 'react-router-dom';
57 import { Link as MuiLink } from '@material-ui/core';
58 import { InputCollectionMount } from 'store/processes/processes-actions';
59 import { connect } from 'react-redux';
60 import { RootState } from 'store/store';
61 import { ProcessOutputCollectionFiles } from './process-output-collection-files';
62 import { Process } from 'store/processes/process';
64 type CssRules = 'card' | 'content' | 'title' | 'header' | 'avatar' | 'iconHeader' | 'tableWrapper' | 'tableRoot' | 'paramValue' | 'keepLink' | 'imagePreview' | 'valArray' | 'emptyValue';
66 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
71 paddingTop: theme.spacing.unit,
72 paddingBottom: theme.spacing.unit,
76 color: theme.customs.colors.green700,
79 alignSelf: 'flex-start',
80 paddingTop: theme.spacing.unit * 0.5
83 padding: theme.spacing.unit * 1.0,
84 paddingTop: theme.spacing.unit * 0.5,
86 paddingBottom: theme.spacing.unit * 1,
91 paddingTop: theme.spacing.unit * 0.5
99 verticalAlign: 'bottom',
100 paddingBottom: '10px',
105 alignItems: 'flex-start',
106 flexDirection: 'column',
109 color: theme.palette.primary.main,
110 textDecoration: 'none',
111 overflowWrap: 'break-word',
117 marginBottom: theme.spacing.unit,
128 color: theme.customs.colors.grey500,
132 export enum ProcessIOCardType {
136 export interface ProcessIOCardDataProps {
138 label: ProcessIOCardType;
139 params: ProcessIOParameter[];
141 mounts?: InputCollectionMount[];
145 type ProcessIOCardProps = ProcessIOCardDataProps & WithStyles<CssRules> & MPVPanelProps;
147 export const ProcessIOCard = withStyles(styles)(
148 ({ classes, label, params, raw, mounts, outputUuid, doHidePanel, panelName, process }: ProcessIOCardProps) => {
149 const [mainProcTabState, setMainProcTabState] = useState(0);
150 const handleMainProcTabChange = (event: React.MouseEvent<HTMLElement>, value: number) => {
151 setMainProcTabState(value);
154 const PanelIcon = label == ProcessIOCardType.INPUT ? InputIcon : OutputIcon;
156 return <Card className={classes.card} data-cy="process-io-card">
158 className={classes.header}
160 content: classes.title,
161 avatar: classes.avatar,
163 avatar={<PanelIcon className={classes.iconHeader} />}
165 <Typography noWrap variant='h6' color='inherit'>
172 <Tooltip title={`Close ${panelName || 'panel'}`} disableFocusListener>
173 <IconButton onClick={doHidePanel}><CloseIcon /></IconButton>
177 <CardContent className={classes.content}>
179 {!process.containerRequest.requestingContainerUuid ?
181 <Tabs value={mainProcTabState} onChange={handleMainProcTabChange} variant="fullWidth">
182 <Tab label="Parameters" />
185 {mainProcTabState === 0 && <div className={classes.tableWrapper}>
187 <ProcessIOPreview data={params} /> :
188 <Grid container item alignItems='center' justify='center'>
189 <DefaultView messages={["No parameters found"]} icon={InfoIcon} />
192 {mainProcTabState === 1 && <div className={classes.tableWrapper}>
194 <ProcessIORaw data={raw || params} /> :
195 <Grid container item alignItems='center' justify='center'>
196 <DefaultView messages={["No parameters found"]} icon={InfoIcon} />
201 <Tabs value={0} variant="fullWidth">
202 {label === ProcessIOCardType.INPUT && <Tab label="Collections" />}
203 {label === ProcessIOCardType.OUTPUT && <Tab label="Collection" />}
205 <div className={classes.tableWrapper}>
206 {label === ProcessIOCardType.INPUT && <ProcessInputMounts mounts={mounts || []} />}
207 {label === ProcessIOCardType.OUTPUT && <ProcessOutputCollectionFiles isWritable={false} currentItemUuid={outputUuid} />}
217 export type ProcessIOValue = {
218 display: ReactElement<any, any>;
222 export type ProcessIOParameter = {
225 value: ProcessIOValue[];
228 interface ProcessIOPreviewDataProps {
229 data: ProcessIOParameter[];
232 type ProcessIOPreviewProps = ProcessIOPreviewDataProps & WithStyles<CssRules>;
234 const ProcessIOPreview = withStyles(styles)(
235 ({ classes, data }: ProcessIOPreviewProps) =>
236 <Table className={classes.tableRoot} aria-label="Process IO Preview">
239 <TableCell>Label</TableCell>
240 <TableCell>Description</TableCell>
241 <TableCell>Value</TableCell>
245 {data.map((param: ProcessIOParameter) => {
246 return <TableRow key={param.id}>
247 <TableCell component="th" scope="row">
250 <TableCell>{param.doc}</TableCell>
251 <TableCell>{param.value.map(val => (
252 <Typography className={classes.paramValue}>
253 {val.imageUrl ? <img className={classes.imagePreview} src={val.imageUrl} alt="Inline Preview" /> : ""}
254 <span className={classes.valArray}>
265 const handleClick = (url) => {
266 window.open(url, '_blank');
269 const ProcessIORaw = withStyles(styles)(
270 ({ data }: ProcessIOPreviewProps) =>
271 <Paper elevation={0}>
273 {JSON.stringify(data, null, 2)}
278 interface ProcessInputMountsDataProps {
279 mounts: InputCollectionMount[];
282 type ProcessInputMountsProps = ProcessInputMountsDataProps & WithStyles<CssRules>;
284 const ProcessInputMounts = withStyles(styles)(connect((state: RootState) => ({
286 }))(({ mounts, classes, auth }: ProcessInputMountsProps & { auth: AuthState }) => (
287 <Table className={classes.tableRoot} aria-label="Process Input Mounts">
290 <TableCell>Path</TableCell>
291 <TableCell>Portable Data Hash</TableCell>
295 {mounts.map(mount => (
296 <TableRow key={mount.path}>
297 <TableCell><pre>{mount.path}</pre></TableCell>
299 <RouterLink to={getNavUrl(mount.pdh, auth)} className={classes.keepLink}>{mount.pdh}</RouterLink>
307 type FileWithSecondaryFiles = {
308 secondaryFiles: File[];
311 export const getIOParamDisplayValue = (auth: AuthState, input: CommandInputParameter | CommandOutputParameter, pdh?: string): ProcessIOValue[] => {
313 case isPrimitiveOfType(input, CWLType.BOOLEAN):
314 const boolValue = (input as BooleanCommandInputParameter).value;
316 return boolValue !== undefined &&
317 !(Array.isArray(boolValue) && boolValue.length === 0) ?
318 [{display: <pre>{String(boolValue)}</pre> }] :
319 [{display: <EmptyValue />}];
321 case isPrimitiveOfType(input, CWLType.INT):
322 case isPrimitiveOfType(input, CWLType.LONG):
323 const intValue = (input as IntCommandInputParameter).value;
325 return intValue !== undefined &&
326 // Missing values are empty array
327 !(Array.isArray(intValue) && intValue.length === 0) ?
328 [{display: <pre>{String(intValue)}</pre> }]
329 : [{display: <EmptyValue />}];
331 case isPrimitiveOfType(input, CWLType.FLOAT):
332 case isPrimitiveOfType(input, CWLType.DOUBLE):
333 const floatValue = (input as FloatCommandInputParameter).value;
335 return floatValue !== undefined &&
336 !(Array.isArray(floatValue) && floatValue.length === 0) ?
337 [{display: <pre>{String(floatValue)}</pre> }]:
338 [{display: <EmptyValue />}];
340 case isPrimitiveOfType(input, CWLType.STRING):
341 const stringValue = (input as StringCommandInputParameter).value || undefined;
343 return stringValue !== undefined &&
344 !(Array.isArray(stringValue) && stringValue.length === 0) ?
345 [{display: <pre>{stringValue}</pre> }] :
346 [{display: <EmptyValue />}];
348 case isPrimitiveOfType(input, CWLType.FILE):
349 const mainFile = (input as FileCommandInputParameter).value;
350 // secondaryFiles: File[] is not part of CommandOutputParameter so we cast to access secondaryFiles
351 const secondaryFiles = ((mainFile as unknown) as FileWithSecondaryFiles)?.secondaryFiles || [];
353 ...(mainFile && !(Array.isArray(mainFile) && mainFile.length === 0) ? [mainFile] : []),
357 return files.length ?
358 files.map(file => fileToProcessIOValue(file, auth, pdh)) :
359 [{display: <EmptyValue />}];
361 case isPrimitiveOfType(input, CWLType.DIRECTORY):
362 const directory = (input as DirectoryCommandInputParameter).value;
364 return directory !== undefined &&
365 !(Array.isArray(directory) && directory.length === 0) ?
366 [directoryToProcessIOValue(directory, auth, pdh)] :
367 [{display: <EmptyValue />}];
369 case typeof input.type === 'object' &&
370 !(input.type instanceof Array) &&
371 input.type.type === 'enum':
372 const enumValue = (input as EnumCommandInputParameter).value;
374 return enumValue !== undefined ?
375 [{ display: <pre>{(input as EnumCommandInputParameter).value || ''}</pre> }] :
376 [{display: <EmptyValue />}];
378 case isArrayOfType(input, CWLType.STRING):
379 const strArray = (input as StringArrayCommandInputParameter).value || [];
380 return strArray.length ?
381 [{ display: <>{((input as StringArrayCommandInputParameter).value || []).map((val) => <Chip label={val} />)}</> }] :
382 [{display: <EmptyValue />}];
384 case isArrayOfType(input, CWLType.INT):
385 case isArrayOfType(input, CWLType.LONG):
386 const intArray = (input as IntArrayCommandInputParameter).value || [];
388 return intArray.length ?
389 [{ display: <>{((input as IntArrayCommandInputParameter).value || []).map((val) => <Chip label={val} />)}</> }] :
390 [{display: <EmptyValue />}];
392 case isArrayOfType(input, CWLType.FLOAT):
393 case isArrayOfType(input, CWLType.DOUBLE):
394 const floatArray = (input as FloatArrayCommandInputParameter).value || [];
396 return floatArray.length ?
397 [{ display: <>{floatArray.map((val) => <Chip label={val} />)}</> }] :
398 [{display: <EmptyValue />}];
400 case isArrayOfType(input, CWLType.FILE):
401 const fileArrayMainFile = ((input as FileArrayCommandInputParameter).value || []);
402 const fileArraySecondaryFiles = fileArrayMainFile.map((file) => (
403 ((file as unknown) as FileWithSecondaryFiles)?.secondaryFiles || []
404 )).reduce((acc: File[], params: File[]) => (acc.concat(params)), []);
406 const fileArrayFiles = [
407 ...fileArrayMainFile,
408 ...fileArraySecondaryFiles
411 return fileArrayFiles.length ?
412 fileArrayFiles.map(file => fileToProcessIOValue(file, auth, pdh)) :
413 [{display: <EmptyValue />}];
415 case isArrayOfType(input, CWLType.DIRECTORY):
416 const directories = (input as DirectoryArrayCommandInputParameter).value || [];
418 return directories.length ?
419 directories.map(directory => directoryToProcessIOValue(directory, auth, pdh)) :
420 [{display: <EmptyValue />}];
427 const getKeepUrl = (file: File | Directory, pdh?: string): string => {
428 const isKeepUrl = file.location?.startsWith('keep:') || false;
429 const keepUrl = isKeepUrl ? file.location : pdh ? `keep:${pdh}/${file.location}` : file.location;
430 return keepUrl || '';
433 interface KeepUrlProps {
435 res: File | Directory;
439 const KeepUrlBase = withStyles(styles)(({auth, res, pdh, classes}: KeepUrlProps & WithStyles<CssRules>) => {
440 const keepUrl = getKeepUrl(res, pdh);
441 const pdhUrl = keepUrl ? keepUrl.split('/').slice(0, 1)[0] : '';
442 // Passing a pdh always returns a relative wb2 collection url
443 const pdhWbPath = getNavUrl(pdhUrl.replace('keep:', ''), auth);
444 return pdhUrl && pdhWbPath ?
445 <Tooltip title={"View collection in Workbench"}><RouterLink to={pdhWbPath} className={classes.keepLink}>{pdhUrl}</RouterLink></Tooltip> :
449 const KeepUrlPath = withStyles(styles)(({auth, res, pdh, classes}: KeepUrlProps & WithStyles<CssRules>) => {
450 const keepUrl = getKeepUrl(res, pdh);
451 const keepUrlParts = keepUrl ? keepUrl.split('/') : [];
452 const keepUrlPath = keepUrlParts.length > 1 ? keepUrlParts.slice(1).join('/') : '';
454 const keepUrlPathNav = getKeepNavUrl(auth, res, pdh);
455 return keepUrlPath && keepUrlPathNav ?
456 <Tooltip title={"View in keep-web"}><MuiLink className={classes.keepLink} onClick={() => handleClick(keepUrlPathNav)}>{keepUrlPath}</MuiLink></Tooltip> :
457 // Show No value for root collection io that lacks path part
461 const getKeepNavUrl = (auth: AuthState, file: File | Directory, pdh?: string): string => {
462 let keepUrl = getKeepUrl(file, pdh).replace('keep:', '');
463 return (getInlineFileUrl(`${auth.config.keepWebServiceUrl}/c=${keepUrl}?api_token=${auth.apiToken}`, auth.config.keepWebServiceUrl, auth.config.keepWebInlineServiceUrl));
466 const getImageUrl = (auth: AuthState, file: File, pdh?: string): string => {
467 const keepUrl = getKeepUrl(file, pdh).replace('keep:', '');
468 return getInlineFileUrl(`${auth.config.keepWebServiceUrl}/c=${keepUrl}?api_token=${auth.apiToken}`, auth.config.keepWebServiceUrl, auth.config.keepWebInlineServiceUrl);
471 const isFileImage = (basename?: string): boolean => {
472 return basename ? (mime.getType(basename) || "").startsWith('image/') : false;
475 const normalizeDirectoryLocation = (directory: Directory): Directory => {
476 if (!directory.location) {
481 location: (directory.location || '').endsWith('/') ? directory.location : directory.location + '/',
485 const directoryToProcessIOValue = (directory: Directory, auth: AuthState, pdh?: string): ProcessIOValue => {
486 const normalizedDirectory = normalizeDirectoryLocation(directory);
489 <KeepUrlBase auth={auth} res={normalizedDirectory} pdh={pdh}/>/<KeepUrlPath auth={auth} res={normalizedDirectory} pdh={pdh}/>
494 const fileToProcessIOValue = (file: File, auth: AuthState, pdh?: string): ProcessIOValue => {
497 <KeepUrlBase auth={auth} res={file} pdh={pdh}/>/<KeepUrlPath auth={auth} res={file} pdh={pdh}/>
499 imageUrl: isFileImage(file.basename) ? getImageUrl(auth, file, pdh) : undefined,
503 const EmptyValue = withStyles(styles)(
504 ({classes}: WithStyles<CssRules>) => <span className={classes.emptyValue}>No value</span>