1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import React, { ReactElement, useState } from 'react';
6 import { Dispatch } from 'redux';
28 } from '@material-ui/core';
29 import { ArvadosTheme } from 'common/custom-theme';
30 import { CloseIcon, ImageIcon, InputIcon, ImageOffIcon, OutputIcon, MaximizeIcon } from 'components/icon/icon';
31 import { MPVPanelProps } from 'components/multi-panel-view/multi-panel-view';
33 BooleanCommandInputParameter,
34 CommandInputParameter,
37 DirectoryArrayCommandInputParameter,
38 DirectoryCommandInputParameter,
39 EnumCommandInputParameter,
40 FileArrayCommandInputParameter,
41 FileCommandInputParameter,
42 FloatArrayCommandInputParameter,
43 FloatCommandInputParameter,
44 IntArrayCommandInputParameter,
45 IntCommandInputParameter,
48 StringArrayCommandInputParameter,
49 StringCommandInputParameter,
50 } from "models/workflow";
51 import { CommandOutputParameter } from 'cwlts/mappings/v1.0/CommandOutputParameter';
52 import { File } from 'models/workflow';
53 import { getInlineFileUrl } from 'views-components/context-menu/actions/helpers';
54 import { AuthState } from 'store/auth/auth-reducer';
55 import mime from 'mime';
56 import { DefaultView } from 'components/default-view/default-view';
57 import { getNavUrl } from 'routes/routes';
58 import { Link as RouterLink } from 'react-router-dom';
59 import { Link as MuiLink } from '@material-ui/core';
60 import { InputCollectionMount } from 'store/processes/processes-actions';
61 import { connect } from 'react-redux';
62 import { RootState } from 'store/store';
63 import { ProcessOutputCollectionFiles } from './process-output-collection-files';
64 import { Process } from 'store/processes/process';
65 import { navigateTo } from 'store/navigation/navigation-action';
88 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
93 paddingTop: theme.spacing.unit,
94 paddingBottom: theme.spacing.unit,
98 color: theme.customs.colors.green700,
101 alignSelf: 'flex-start',
102 paddingTop: theme.spacing.unit * 0.5
105 height: `calc(100% - ${theme.spacing.unit * 7}px - ${theme.spacing.unit * 1.5}px)`,
106 padding: theme.spacing.unit * 1.0,
107 paddingTop: theme.spacing.unit * 0.5,
109 paddingBottom: theme.spacing.unit * 1,
114 paddingTop: theme.spacing.unit * 0.5
117 height: `calc(100% - ${theme.spacing.unit * 6}px)`,
123 verticalAlign: 'bottom',
124 paddingBottom: '10px',
127 paddingRight: '25px',
132 alignItems: 'flex-start',
133 flexDirection: 'column',
136 color: theme.palette.primary.main,
137 textDecoration: 'none',
138 overflowWrap: 'break-word',
144 color: theme.palette.primary.main,
145 textDecoration: 'none',
146 overflowWrap: 'break-word',
153 marginBottom: theme.spacing.unit,
164 color: theme.customs.colors.grey500,
168 borderBottom: 'none',
180 alignItems: 'center',
181 justifyContent: 'center',
182 backgroundColor: '#cecece',
183 borderRadius: '10px',
186 verticalAlign: 'bottom',
193 export enum ProcessIOCardType {
197 export interface ProcessIOCardDataProps {
199 label: ProcessIOCardType;
200 params?: ProcessIOParameter[];
202 mounts?: InputCollectionMount[];
206 export interface ProcessIOCardActionProps {
207 navigateTo: (uuid: string) => void;
210 const mapDispatchToProps = (dispatch: Dispatch): ProcessIOCardActionProps => ({
211 navigateTo: (uuid) => dispatch<any>(navigateTo(uuid)),
214 type ProcessIOCardProps = ProcessIOCardDataProps & ProcessIOCardActionProps & WithStyles<CssRules> & MPVPanelProps;
216 export const ProcessIOCard = withStyles(styles)(connect(null, mapDispatchToProps)(
217 ({ classes, label, params, raw, mounts, outputUuid, doHidePanel, doMaximizePanel, panelMaximized, panelName, process, navigateTo }: ProcessIOCardProps) => {
218 const [mainProcTabState, setMainProcTabState] = useState(0);
219 const handleMainProcTabChange = (event: React.MouseEvent<HTMLElement>, value: number) => {
220 setMainProcTabState(value);
223 const [showImagePreview, setShowImagePreview] = useState(false);
225 const PanelIcon = label === ProcessIOCardType.INPUT ? InputIcon : OutputIcon;
226 const mainProcess = !process.containerRequest.requestingContainerUuid;
228 return <Card className={classes.card} data-cy="process-io-card">
230 className={classes.header}
232 content: classes.title,
233 avatar: classes.avatar,
235 avatar={<PanelIcon className={classes.iconHeader} />}
237 <Typography noWrap variant='h6' color='inherit'>
243 { mainProcess && <Tooltip title={"Toggle Image Preview"} disableFocusListener>
244 <IconButton data-cy="io-preview-image-toggle" onClick={() =>{setShowImagePreview(!showImagePreview)}}>{showImagePreview ? <ImageIcon /> : <ImageOffIcon />}</IconButton>
246 { doMaximizePanel && !panelMaximized &&
247 <Tooltip title={`Maximize ${panelName || 'panel'}`} disableFocusListener>
248 <IconButton onClick={doMaximizePanel}><MaximizeIcon /></IconButton>
251 <Tooltip title={`Close ${panelName || 'panel'}`} disableFocusListener>
252 <IconButton onClick={doHidePanel}><CloseIcon /></IconButton>
256 <CardContent className={classes.content}>
259 {params === undefined && <Grid container item alignItems='center' justify='center'>
262 {params && params.length > 0 &&
264 <Tabs value={mainProcTabState} onChange={handleMainProcTabChange} variant="fullWidth" className={classes.symmetricTabs}>
265 <Tab label="Parameters" />
268 {mainProcTabState === 0 && <div className={classes.tableWrapper}>
269 <ProcessIOPreview data={params} showImagePreview={showImagePreview} />
271 {mainProcTabState === 1 && <div className={classes.tableWrapper}>
272 <ProcessIORaw data={raw || params} />
275 {params && params.length === 0 && <Grid container item alignItems='center' justify='center'>
276 <DefaultView messages={["No parameters found"]} />
281 {((mounts && mounts.length) || outputUuid) ?
283 <Tabs value={0} variant="fullWidth" className={classes.symmetricTabs}>
284 {label === ProcessIOCardType.INPUT && <Tab label="Collections" />}
285 {label === ProcessIOCardType.OUTPUT && <Tab label="Collection" />}
287 <div className={classes.tableWrapper}>
288 {label === ProcessIOCardType.INPUT && <ProcessInputMounts mounts={mounts || []} />}
289 {label === ProcessIOCardType.OUTPUT && <>
290 {outputUuid && <Typography className={classes.collectionLink}>
291 Output Collection: <MuiLink className={classes.keepLink} onClick={() => {navigateTo(outputUuid || "")}}>
293 </MuiLink></Typography>}
294 <ProcessOutputCollectionFiles isWritable={false} currentItemUuid={outputUuid} />
298 <Grid container item alignItems='center' justify='center'>
299 <DefaultView messages={["No collection(s) found"]} />
309 export type ProcessIOValue = {
310 display: ReactElement<any, any>;
312 collection?: ReactElement<any, any>;
315 export type ProcessIOParameter = {
318 value: ProcessIOValue[];
321 interface ProcessIOPreviewDataProps {
322 data: ProcessIOParameter[];
323 showImagePreview: boolean;
326 type ProcessIOPreviewProps = ProcessIOPreviewDataProps & WithStyles<CssRules>;
328 const ProcessIOPreview = withStyles(styles)(
329 ({ classes, data, showImagePreview }: ProcessIOPreviewProps) => {
330 const showLabel = data.some((param: ProcessIOParameter) => param.label);
331 return <Table className={classes.tableRoot} aria-label="Process IO Preview">
334 <TableCell>Name</TableCell>
335 {showLabel && <TableCell className={classes.labelColumn}>Label</TableCell>}
336 <TableCell>Value</TableCell>
337 <TableCell>Collection</TableCell>
341 {data.map((param: ProcessIOParameter) => {
342 const firstVal = param.value.length > 0 ? param.value[0] : undefined;
343 const rest = param.value.slice(1);
344 const rowClass = rest.length > 0 ? classes.halfRow : undefined;
347 <TableRow className={rowClass} data-cy="process-io-param">
351 {showLabel && <TableCell >{param.label}</TableCell>}
353 {firstVal && <ProcessValuePreview value={firstVal} showImagePreview={showImagePreview} />}
355 <TableCell className={firstVal?.imageUrl ? classes.rowWithPreview : undefined}>
356 <Typography className={classes.paramValue}>
357 {firstVal?.collection}
361 {rest.map((val, i) => (
362 <TableRow className={(i < rest.length-1) ? rowClass : undefined}>
364 {showLabel && <TableCell />}
366 <ProcessValuePreview value={val} showImagePreview={showImagePreview} />
368 <TableCell className={firstVal?.imageUrl ? classes.rowWithPreview : undefined}>
369 <Typography className={classes.paramValue}>
381 interface ProcessValuePreviewProps {
382 value: ProcessIOValue;
383 showImagePreview: boolean;
386 const ProcessValuePreview = withStyles(styles)(
387 ({value, showImagePreview, classes}: ProcessValuePreviewProps & WithStyles<CssRules>) =>
388 <Typography className={classes.paramValue}>
389 {value.imageUrl && showImagePreview ? <img className={classes.imagePreview} src={value.imageUrl} alt="Inline Preview" /> : ""}
390 {value.imageUrl && !showImagePreview ? <ImagePlaceholder /> : ""}
391 <span className={classes.valArray}>
397 interface ProcessIORawDataProps {
398 data: ProcessIOParameter[];
401 const ProcessIORaw = withStyles(styles)(
402 ({ data }: ProcessIORawDataProps) =>
403 <Paper elevation={0}>
405 {JSON.stringify(data, null, 2)}
410 interface ProcessInputMountsDataProps {
411 mounts: InputCollectionMount[];
414 type ProcessInputMountsProps = ProcessInputMountsDataProps & WithStyles<CssRules>;
416 const ProcessInputMounts = withStyles(styles)(connect((state: RootState) => ({
418 }))(({ mounts, classes, auth }: ProcessInputMountsProps & { auth: AuthState }) => (
419 <Table className={classes.tableRoot} aria-label="Process Input Mounts">
422 <TableCell>Path</TableCell>
423 <TableCell>Portable Data Hash</TableCell>
427 {mounts.map(mount => (
428 <TableRow key={mount.path}>
429 <TableCell><pre>{mount.path}</pre></TableCell>
431 <RouterLink to={getNavUrl(mount.pdh, auth)} className={classes.keepLink}>{mount.pdh}</RouterLink>
439 type FileWithSecondaryFiles = {
440 secondaryFiles: File[];
443 export const getIOParamDisplayValue = (auth: AuthState, input: CommandInputParameter | CommandOutputParameter, pdh?: string): ProcessIOValue[] => {
445 case isPrimitiveOfType(input, CWLType.BOOLEAN):
446 const boolValue = (input as BooleanCommandInputParameter).value;
448 return boolValue !== undefined &&
449 !(Array.isArray(boolValue) && boolValue.length === 0) ?
450 [{display: <pre>{String(boolValue)}</pre> }] :
451 [{display: <EmptyValue />}];
453 case isPrimitiveOfType(input, CWLType.INT):
454 case isPrimitiveOfType(input, CWLType.LONG):
455 const intValue = (input as IntCommandInputParameter).value;
457 return intValue !== undefined &&
458 // Missing values are empty array
459 !(Array.isArray(intValue) && intValue.length === 0) ?
460 [{display: <pre>{String(intValue)}</pre> }]
461 : [{display: <EmptyValue />}];
463 case isPrimitiveOfType(input, CWLType.FLOAT):
464 case isPrimitiveOfType(input, CWLType.DOUBLE):
465 const floatValue = (input as FloatCommandInputParameter).value;
467 return floatValue !== undefined &&
468 !(Array.isArray(floatValue) && floatValue.length === 0) ?
469 [{display: <pre>{String(floatValue)}</pre> }]:
470 [{display: <EmptyValue />}];
472 case isPrimitiveOfType(input, CWLType.STRING):
473 const stringValue = (input as StringCommandInputParameter).value || undefined;
475 return stringValue !== undefined &&
476 !(Array.isArray(stringValue) && stringValue.length === 0) ?
477 [{display: <pre>{stringValue}</pre> }] :
478 [{display: <EmptyValue />}];
480 case isPrimitiveOfType(input, CWLType.FILE):
481 const mainFile = (input as FileCommandInputParameter).value;
482 // secondaryFiles: File[] is not part of CommandOutputParameter so we cast to access secondaryFiles
483 const secondaryFiles = ((mainFile as unknown) as FileWithSecondaryFiles)?.secondaryFiles || [];
485 ...(mainFile && !(Array.isArray(mainFile) && mainFile.length === 0) ? [mainFile] : []),
489 return files.length ?
490 files.map(file => fileToProcessIOValue(file, auth, pdh)) :
491 [{display: <EmptyValue />}];
493 case isPrimitiveOfType(input, CWLType.DIRECTORY):
494 const directory = (input as DirectoryCommandInputParameter).value;
496 return directory !== undefined &&
497 !(Array.isArray(directory) && directory.length === 0) ?
498 [directoryToProcessIOValue(directory, auth, pdh)] :
499 [{display: <EmptyValue />}];
501 case typeof input.type === 'object' &&
502 !(input.type instanceof Array) &&
503 input.type.type === 'enum':
504 const enumValue = (input as EnumCommandInputParameter).value;
506 return enumValue !== undefined ?
507 [{ display: <pre>{(input as EnumCommandInputParameter).value || ''}</pre> }] :
508 [{display: <EmptyValue />}];
510 case isArrayOfType(input, CWLType.STRING):
511 const strArray = (input as StringArrayCommandInputParameter).value || [];
512 return strArray.length ?
513 [{ display: <>{((input as StringArrayCommandInputParameter).value || []).map((val) => <Chip label={val} />)}</> }] :
514 [{display: <EmptyValue />}];
516 case isArrayOfType(input, CWLType.INT):
517 case isArrayOfType(input, CWLType.LONG):
518 const intArray = (input as IntArrayCommandInputParameter).value || [];
520 return intArray.length ?
521 [{ display: <>{((input as IntArrayCommandInputParameter).value || []).map((val) => <Chip label={val} />)}</> }] :
522 [{display: <EmptyValue />}];
524 case isArrayOfType(input, CWLType.FLOAT):
525 case isArrayOfType(input, CWLType.DOUBLE):
526 const floatArray = (input as FloatArrayCommandInputParameter).value || [];
528 return floatArray.length ?
529 [{ display: <>{floatArray.map((val) => <Chip label={val} />)}</> }] :
530 [{display: <EmptyValue />}];
532 case isArrayOfType(input, CWLType.FILE):
533 const fileArrayMainFile = ((input as FileArrayCommandInputParameter).value || []);
534 const fileArraySecondaryFiles = fileArrayMainFile.map((file) => (
535 ((file as unknown) as FileWithSecondaryFiles)?.secondaryFiles || []
536 )).reduce((acc: File[], params: File[]) => (acc.concat(params)), []);
538 const fileArrayFiles = [
539 ...fileArrayMainFile,
540 ...fileArraySecondaryFiles
543 return fileArrayFiles.length ?
544 fileArrayFiles.map(file => fileToProcessIOValue(file, auth, pdh)) :
545 [{display: <EmptyValue />}];
547 case isArrayOfType(input, CWLType.DIRECTORY):
548 const directories = (input as DirectoryArrayCommandInputParameter).value || [];
550 return directories.length ?
551 directories.map(directory => directoryToProcessIOValue(directory, auth, pdh)) :
552 [{display: <EmptyValue />}];
560 * @returns keep url without keep: prefix
562 const getKeepUrl = (file: File | Directory, pdh?: string): string => {
563 const isKeepUrl = file.location?.startsWith('keep:') || false;
564 const keepUrl = isKeepUrl ?
565 file.location?.replace('keep:', '') :
566 pdh ? `${pdh}/${file.location}` : file.location;
567 return keepUrl || '';
570 interface KeepUrlProps {
572 res: File | Directory;
576 const KeepUrlBase = withStyles(styles)(({auth, res, pdh, classes}: KeepUrlProps & WithStyles<CssRules>) => {
577 const keepUrl = getKeepUrl(res, pdh);
578 const pdhUrl = keepUrl ? keepUrl.split('/').slice(0, 1)[0] : '';
579 // Passing a pdh always returns a relative wb2 collection url
580 const pdhWbPath = getNavUrl(pdhUrl, auth);
581 return pdhUrl && pdhWbPath ?
582 <Tooltip title={"View collection in Workbench"}><RouterLink to={pdhWbPath} className={classes.keepLink}>{pdhUrl}</RouterLink></Tooltip> :
586 const KeepUrlPath = withStyles(styles)(({auth, res, pdh, classes}: KeepUrlProps & WithStyles<CssRules>) => {
587 const keepUrl = getKeepUrl(res, pdh);
588 const keepUrlParts = keepUrl ? keepUrl.split('/') : [];
589 const keepUrlPath = keepUrlParts.length > 1 ? keepUrlParts.slice(1).join('/') : '';
591 const keepUrlPathNav = getKeepNavUrl(auth, res, pdh);
592 return keepUrlPath && keepUrlPathNav ?
593 <Tooltip title={"View in keep-web"}><a className={classes.keepLink} href={keepUrlPathNav} target="_blank" rel="noopener noreferrer">{keepUrlPath}</a></Tooltip> :
594 // Show No value for root collection io that lacks path part
598 const getKeepNavUrl = (auth: AuthState, file: File | Directory, pdh?: string): string => {
599 let keepUrl = getKeepUrl(file, pdh);
600 return (getInlineFileUrl(`${auth.config.keepWebServiceUrl}/c=${keepUrl}?api_token=${auth.apiToken}`, auth.config.keepWebServiceUrl, auth.config.keepWebInlineServiceUrl));
603 const getImageUrl = (auth: AuthState, file: File, pdh?: string): string => {
604 const keepUrl = getKeepUrl(file, pdh);
605 return getInlineFileUrl(`${auth.config.keepWebServiceUrl}/c=${keepUrl}?api_token=${auth.apiToken}`, auth.config.keepWebServiceUrl, auth.config.keepWebInlineServiceUrl);
608 const isFileImage = (basename?: string): boolean => {
609 return basename ? (mime.getType(basename) || "").startsWith('image/') : false;
612 const normalizeDirectoryLocation = (directory: Directory): Directory => {
613 if (!directory.location) {
618 location: (directory.location || '').endsWith('/') ? directory.location : directory.location + '/',
622 const directoryToProcessIOValue = (directory: Directory, auth: AuthState, pdh?: string): ProcessIOValue => {
623 const normalizedDirectory = normalizeDirectoryLocation(directory);
625 display: <KeepUrlPath auth={auth} res={normalizedDirectory} pdh={pdh}/>,
626 collection: <KeepUrlBase auth={auth} res={normalizedDirectory} pdh={pdh}/>,
630 const fileToProcessIOValue = (file: File, auth: AuthState, pdh?: string): ProcessIOValue => {
632 display: <KeepUrlPath auth={auth} res={file} pdh={pdh}/>,
633 imageUrl: isFileImage(file.basename) ? getImageUrl(auth, file, pdh) : undefined,
634 collection: <KeepUrlBase auth={auth} res={file} pdh={pdh}/>,
638 const EmptyValue = withStyles(styles)(
639 ({classes}: WithStyles<CssRules>) => <span className={classes.emptyValue}>No value</span>
642 const ImagePlaceholder = withStyles(styles)(
643 ({classes}: WithStyles<CssRules>) => <span className={classes.imagePlaceholder}><ImageIcon /></span>