16073: Fix erroneously showing no parameters message
[arvados-workbench2.git] / src / views / process-panel / process-io-card.tsx
index d60d71c4122649bc66e5fb4fe9769680639c7c2f..0d148d7251ffe83e01bebbea733a4a66ec0d8017 100644 (file)
@@ -24,6 +24,7 @@ import {
     Paper,
     Grid,
     Chip,
+    CircularProgress,
 } from '@material-ui/core';
 import { ArvadosTheme } from 'common/custom-theme';
 import { CloseIcon, ImageIcon, InputIcon, ImageOffIcon, OutputIcon, MaximizeIcon } from 'components/icon/icon';
@@ -62,6 +63,7 @@ import { RootState } from 'store/store';
 import { ProcessOutputCollectionFiles } from './process-output-collection-files';
 import { Process } from 'store/processes/process';
 import { navigateTo } from 'store/navigation/navigation-action';
+import classNames from 'classnames';
 
 type CssRules =
   | "card"
@@ -77,11 +79,13 @@ type CssRules =
   | "collectionLink"
   | "imagePreview"
   | "valArray"
+  | "secondaryVal"
   | "emptyValue"
   | "halfRow"
   | "symmetricTabs"
   | "imagePlaceholder"
-  | "rowWithPreview";
+  | "rowWithPreview"
+  | "labelColumn";
 
 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
     card: {
@@ -158,6 +162,9 @@ const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
             display: 'inline',
         }
     },
+    secondaryVal: {
+        paddingLeft: '20px',
+    },
     emptyValue: {
         color: theme.customs.colors.grey500,
     },
@@ -182,7 +189,10 @@ const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
     },
     rowWithPreview: {
         verticalAlign: 'bottom',
-    }
+    },
+    labelColumn: {
+        minWidth: '120px',
+    },
 });
 
 export enum ProcessIOCardType {
@@ -192,7 +202,7 @@ export enum ProcessIOCardType {
 export interface ProcessIOCardDataProps {
     process: Process;
     label: ProcessIOCardType;
-    params: ProcessIOParameter[];
+    params?: ProcessIOParameter[];
     raw?: any;
     mounts?: InputCollectionMount[];
     outputUuid?: string;
@@ -220,6 +230,10 @@ export const ProcessIOCard = withStyles(styles)(connect(null, mapDispatchToProps
         const PanelIcon = label === ProcessIOCardType.INPUT ? InputIcon : OutputIcon;
         const mainProcess = !process.containerRequest.requestingContainerUuid;
 
+        const loading = raw === undefined || params === undefined;
+        const hasRaw = !!(raw && Object.keys(raw).length > 0);
+        const hasParams = !!(params && params.length > 0);
+
         return <Card className={classes.card} data-cy="process-io-card">
             <CardHeader
                 className={classes.header}
@@ -251,24 +265,33 @@ export const ProcessIOCard = withStyles(styles)(connect(null, mapDispatchToProps
             <CardContent className={classes.content}>
                 {mainProcess ?
                     (<>
-                        {params.length ?
+                        {/* raw is undefined until params are loaded */}
+                        {loading && <Grid container item alignItems='center' justify='center'>
+                            <CircularProgress />
+                        </Grid>}
+                        {/* Once loaded, either raw or params may still be empty
+                          *   Raw when all params are empty
+                          *   Params when raw is provided by containerRequest properties but workflow mount is absent for preview
+                          */}
+                        {(!loading && (hasRaw || hasParams)) &&
                             <>
                                 <Tabs value={mainProcTabState} onChange={handleMainProcTabChange} variant="fullWidth" className={classes.symmetricTabs}>
-                                    <Tab label="Parameters" />
+                                    {/* params will be empty on processes without workflow definitions in mounts, so we only show raw */}
+                                    {hasParams && <Tab label="Parameters" />}
                                     <Tab label="JSON" />
                                 </Tabs>
-                                {mainProcTabState === 0 && <div className={classes.tableWrapper}>
+                                {(mainProcTabState === 0 && params && hasParams) && <div className={classes.tableWrapper}>
                                         <ProcessIOPreview data={params} showImagePreview={showImagePreview} />
                                     </div>}
-                                {mainProcTabState === 1 && <div className={classes.tableWrapper}>
-                                        <ProcessIORaw data={raw || params} />
+                                {(mainProcTabState === 1 || !hasParams) && <div className={classes.tableWrapper}>
+                                        <ProcessIORaw data={raw} />
                                     </div>}
-                            </> :
-                            <Grid container item alignItems='center' justify='center'>
-                                <DefaultView messages={["No parameters found"]} />
-                            </Grid>
-                        }
+                            </>}
+                        {!loading && !hasRaw && !hasParams && <Grid container item alignItems='center' justify='center'>
+                            <DefaultView messages={["No parameters found"]} />
+                        </Grid>}
                     </>) :
+                    // Subprocess
                     (<>
                         {((mounts && mounts.length) || outputUuid) ?
                             <>
@@ -302,6 +325,7 @@ export type ProcessIOValue = {
     display: ReactElement<any, any>;
     imageUrl?: string;
     collection?: ReactElement<any, any>;
+    secondary?: boolean;
 }
 
 export type ProcessIOParameter = {
@@ -324,7 +348,7 @@ const ProcessIOPreview = withStyles(styles)(
             <TableHead>
                 <TableRow>
                     <TableCell>Name</TableCell>
-                    {showLabel && <TableCell>Label</TableCell>}
+                    {showLabel && <TableCell className={classes.labelColumn}>Label</TableCell>}
                     <TableCell>Value</TableCell>
                     <TableCell>Collection</TableCell>
                 </TableRow>
@@ -380,17 +404,12 @@ const ProcessValuePreview = withStyles(styles)(
         <Typography className={classes.paramValue}>
             {value.imageUrl && showImagePreview ? <img className={classes.imagePreview} src={value.imageUrl} alt="Inline Preview" /> : ""}
             {value.imageUrl && !showImagePreview ? <ImagePlaceholder /> : ""}
-            <span className={classes.valArray}>
+            <span className={classNames(classes.valArray, value.secondary && classes.secondaryVal)}>
                 {value.display}
             </span>
         </Typography>
 )
 
-const handleClick = (url) => {
-    window.open(url, '_blank');
-}
-
-
 interface ProcessIORawDataProps {
     data: ProcessIOParameter[];
 }
@@ -482,9 +501,10 @@ export const getIOParamDisplayValue = (auth: AuthState, input: CommandInputParam
                 ...(mainFile && !(Array.isArray(mainFile) && mainFile.length === 0) ? [mainFile] : []),
                 ...secondaryFiles
             ];
+            const mainFilePdhUrl = mainFile ? getResourcePdhUrl(mainFile, pdh) : "";
 
             return files.length ?
-                files.map(file => fileToProcessIOValue(file, auth, pdh)) :
+                files.map((file, i) => fileToProcessIOValue(file, (i > 0), auth, pdh, (i > 0 ? mainFilePdhUrl : ""))) :
                 [{display: <EmptyValue />}];
 
         case isPrimitiveOfType(input, CWLType.DIRECTORY):
@@ -527,18 +547,22 @@ export const getIOParamDisplayValue = (auth: AuthState, input: CommandInputParam
                 [{display: <EmptyValue />}];
 
         case isArrayOfType(input, CWLType.FILE):
-            const fileArrayMainFile = ((input as FileArrayCommandInputParameter).value || []);
-            const fileArraySecondaryFiles = fileArrayMainFile.map((file) => (
-                ((file as unknown) as FileWithSecondaryFiles)?.secondaryFiles || []
-            )).reduce((acc: File[], params: File[]) => (acc.concat(params)), []);
-
-            const fileArrayFiles = [
-                ...fileArrayMainFile,
-                ...fileArraySecondaryFiles
-            ];
-
-            return fileArrayFiles.length ?
-                fileArrayFiles.map(file => fileToProcessIOValue(file, auth, pdh)) :
+            const fileArrayMainFiles = ((input as FileArrayCommandInputParameter).value || []);
+            const firstMainFilePdh = fileArrayMainFiles.length > 0 ? getResourcePdhUrl(fileArrayMainFiles[0], pdh) : "";
+
+            // Convert each main file into separate arrays of ProcessIOValue to preserve secondaryFile grouping
+            const fileArrayValues = fileArrayMainFiles.map((mainFile: File, i): ProcessIOValue[] => {
+                const secondaryFiles = ((mainFile as unknown) as FileWithSecondaryFiles)?.secondaryFiles || [];
+                return [
+                    // Pass firstMainFilePdh to secondary files and every main file besides the first to hide pdh if equal
+                    fileToProcessIOValue(mainFile, false, auth, pdh, i > 0 ? firstMainFilePdh : ""),
+                    ...(secondaryFiles.map(file => fileToProcessIOValue(file, true, auth, pdh, firstMainFilePdh)))
+                ];
+            // Reduce each mainFile/secondaryFile group into single array preserving ordering
+            }).reduce((acc: ProcessIOValue[], mainFile: ProcessIOValue[]) => (acc.concat(mainFile)), []);
+
+            return fileArrayValues.length ?
+                fileArrayValues :
                 [{display: <EmptyValue />}];
 
         case isArrayOfType(input, CWLType.DIRECTORY):
@@ -570,9 +594,13 @@ interface KeepUrlProps {
     pdh?: string;
 }
 
-const KeepUrlBase = withStyles(styles)(({auth, res, pdh, classes}: KeepUrlProps & WithStyles<CssRules>) => {
+const getResourcePdhUrl = (res: File | Directory, pdh?: string): string => {
     const keepUrl = getKeepUrl(res, pdh);
-    const pdhUrl = keepUrl ? keepUrl.split('/').slice(0, 1)[0] : '';
+    return keepUrl ? keepUrl.split('/').slice(0, 1)[0] : '';
+};
+
+const KeepUrlBase = withStyles(styles)(({auth, res, pdh, classes}: KeepUrlProps & WithStyles<CssRules>) => {
+    const pdhUrl = getResourcePdhUrl(res, pdh);
     // Passing a pdh always returns a relative wb2 collection url
     const pdhWbPath = getNavUrl(pdhUrl, auth);
     return pdhUrl && pdhWbPath ?
@@ -587,7 +615,7 @@ const KeepUrlPath = withStyles(styles)(({auth, res, pdh, classes}: KeepUrlProps
 
     const keepUrlPathNav = getKeepNavUrl(auth, res, pdh);
     return keepUrlPath && keepUrlPathNav ?
-        <Tooltip title={"View in keep-web"}><a className={classes.keepLink} href={keepUrlPathNav} target="_blank">{keepUrlPath}</a></Tooltip> :
+        <Tooltip title={"View in keep-web"}><a className={classes.keepLink} href={keepUrlPathNav} target="_blank" rel="noopener noreferrer">{keepUrlPath}</a></Tooltip> :
         // Show No value for root collection io that lacks path part
         <EmptyValue />;
 });
@@ -624,11 +652,13 @@ const directoryToProcessIOValue = (directory: Directory, auth: AuthState, pdh?:
     };
 };
 
-const fileToProcessIOValue = (file: File, auth: AuthState, pdh?: string): ProcessIOValue => {
+const fileToProcessIOValue = (file: File, secondary: boolean, auth: AuthState, pdh: string | undefined, mainFilePdh: string): ProcessIOValue => {
+    const resourcePdh = getResourcePdhUrl(file, pdh);
     return {
         display: <KeepUrlPath auth={auth} res={file} pdh={pdh}/>,
+        secondary,
         imageUrl: isFileImage(file.basename) ? getImageUrl(auth, file, pdh) : undefined,
-        collection: <KeepUrlBase auth={auth} res={file} pdh={pdh}/>,
+        collection: (resourcePdh !== mainFilePdh) ? <KeepUrlBase auth={auth} res={file} pdh={pdh}/> : <></>,
     }
 };