csv button changed

This commit is contained in:
amit
2024-11-12 10:35:49 +05:30
parent 32c646af01
commit c7cb8c222f

View File

@@ -1,14 +1,11 @@
import { Box, Tabs, Typography, Tab, Paper } from "@mui/material"; import { Box, Tabs, Typography, Tab, Paper, Button } from "@mui/material";
import Highlight from "react-highlight"; import Highlight from "react-highlight";
import Button from "@mui/material/Button";
import * as React from "react"; import * as React from "react";
import { Data } from "./RunsTable"; import { Data } from "./RunsTable";
import { TabPanel, TabContext } from "@mui/lab"; import { TabPanel, TabContext } from "@mui/lab";
import SettingsIcon from '@mui/icons-material/Settings';
import ImageIcon from '@mui/icons-material/Image';
import ArticleIcon from '@mui/icons-material/Article'; import ArticleIcon from '@mui/icons-material/Article';
import ImageIcon from '@mui/icons-material/Image';
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
import AssignmentIcon from '@mui/icons-material/Assignment';
import Table from '@mui/material/Table'; import Table from '@mui/material/Table';
import TableBody from '@mui/material/TableBody'; import TableBody from '@mui/material/TableBody';
import TableCell from '@mui/material/TableCell'; import TableCell from '@mui/material/TableCell';
@@ -32,7 +29,7 @@ export const RunContent = ({ row, currentLog, interpretationInProgress, logEndRe
useEffect(() => { useEffect(() => {
setTab(tab); setTab(tab);
}, [interpretationInProgress]) }, [interpretationInProgress]);
useEffect(() => { useEffect(() => {
if (row.serializableOutput && Object.keys(row.serializableOutput).length > 0) { if (row.serializableOutput && Object.keys(row.serializableOutput).length > 0) {
@@ -49,27 +46,26 @@ export const RunContent = ({ row, currentLog, interpretationInProgress, logEndRe
// Function to convert table data to CSV format // Function to convert table data to CSV format
const convertToCSV = (data: any[], columns: string[]): string => { const convertToCSV = (data: any[], columns: string[]): string => {
const header = columns.join(','); // Create CSV header row const header = columns.join(',');
const rows = data.map(row => const rows = data.map(row =>
columns.map(col => JSON.stringify(row[col], null, 2)).join(',') columns.map(col => JSON.stringify(row[col], null, 2)).join(',')
); // Map each row of data to a CSV row );
return [header, ...rows].join('\n'); // Combine header and rows with newline characters return [header, ...rows].join('\n');
} };
// Function to download CSV file when called // Function to download CSV file when called
const downloadCSV = () => { const downloadCSV = () => {
const csvContent = convertToCSV(tableData, columns); // Convert data to CSV format const csvContent = convertToCSV(tableData, columns);
const blob = new Blob([csvContent], { type: 'text/csv;charset=utf-8;' }); // Create a Blob object with CSV content const blob = new Blob([csvContent], { type: 'text/csv;charset=utf-8;' });
const url = URL.createObjectURL(blob); // Create a temporary URL for the Blob const url = URL.createObjectURL(blob);
// Create a temporary link to download the CSV file
const link = document.createElement("a"); const link = document.createElement("a");
link.href = url; link.href = url;
link.setAttribute("download", "data.csv"); // Set the download filename link.setAttribute("download", "data.csv");
document.body.appendChild(link); document.body.appendChild(link);
link.click(); // Programmatically click the link to trigger the download link.click();
document.body.removeChild(link); // Remove the link element after download document.body.removeChild(link);
} };
return ( return (
<Box sx={{ width: '100%' }}> <Box sx={{ width: '100%' }}>
@@ -78,7 +74,6 @@ export const RunContent = ({ row, currentLog, interpretationInProgress, logEndRe
<Tabs value={tab} onChange={(e, newTab) => setTab(newTab)} aria-label="run-content-tabs"> <Tabs value={tab} onChange={(e, newTab) => setTab(newTab)} aria-label="run-content-tabs">
<Tab label="Output Data" value='output' /> <Tab label="Output Data" value='output' />
<Tab label="Log" value='log' /> <Tab label="Log" value='log' />
{/* <Tab label="Input" value='input' /> */}
</Tabs> </Tabs>
</Box> </Box>
<TabPanel value='log'> <TabPanel value='log'>
@@ -118,42 +113,50 @@ export const RunContent = ({ row, currentLog, interpretationInProgress, logEndRe
<ArticleIcon sx={{ marginRight: '15px' }} /> <ArticleIcon sx={{ marginRight: '15px' }} />
Captured Data Captured Data
</Typography> </Typography>
{Object.keys(row.serializableOutput).map((key) => { <Box sx={{ display: 'flex', alignItems: 'center', gap: 2, mt: 2 }}>
return ( <Typography>
<div key={`number-of-serializable-output-${key}`}> <a style={{ textDecoration: 'none' }} href={`data:application/json;utf8,${JSON.stringify(row.serializableOutput, null, 2)}`}
<Typography sx={{ margin: '20px 0px 20px 0px' }}> download="data.json">
<a style={{ textDecoration: 'none' }} href={`data:application/json;utf8,${JSON.stringify(row.serializableOutput[key], null, 2)}`} Download as JSON
download={key}>Download as JSON</a> </a>
</Typography> </Typography>
</div> <Button
) onClick={downloadCSV}
})} sx={{
textTransform: 'none',
color: 'primary.main',
padding: 0,
background: 'none',
'&:hover': {
background: 'none',
textDecoration: 'underline',
},
}}
>
Download as CSV
</Button>
</Box>
{tableData.length > 0 ? ( {tableData.length > 0 ? (
<> <TableContainer component={Paper} sx={{ maxHeight: 440, marginTop: 2 }}>
<Button variant="contained" color="primary" onClick={downloadCSV}> <Table stickyHeader aria-label="sticky table">
Download as CSV <TableHead>
</Button> <TableRow>
<TableContainer component={Paper} sx={{ maxHeight: 440, marginTop: 2 }}> {columns.map((column) => (
<Table stickyHeader aria-label="sticky table"> <TableCell key={column}>{column}</TableCell>
<TableHead> ))}
<TableRow> </TableRow>
</TableHead>
<TableBody>
{tableData.map((row, index) => (
<TableRow key={index}>
{columns.map((column) => ( {columns.map((column) => (
<TableCell key={column}>{column}</TableCell> <TableCell key={column}>{row[column]}</TableCell>
))} ))}
</TableRow> </TableRow>
</TableHead> ))}
<TableBody> </TableBody>
{tableData.map((row, index) => ( </Table>
<TableRow key={index}> </TableContainer>
{columns.map((column) => (
<TableCell key={column}>{row[column]}</TableCell>
))}
</TableRow>
))}
</TableBody>
</Table>
</TableContainer>
</>
) : ( ) : (
<Box sx={{ <Box sx={{
width: 'fit-content', width: 'fit-content',
@@ -168,12 +171,12 @@ export const RunContent = ({ row, currentLog, interpretationInProgress, logEndRe
)} )}
</div> </div>
} }
{row.binaryOutput {row.binaryOutput && Object.keys(row.binaryOutput).length !== 0 &&
&& Object.keys(row.binaryOutput).length !== 0 &&
<div> <div>
<Typography variant='h6' sx={{ display: 'flex', alignItems: 'center' }}> <Typography variant='h6' sx={{ display: 'flex', alignItems: 'center' }}>
<ImageIcon sx={{ marginRight: '15px' }} /> <ImageIcon sx={{ marginRight: '15px' }} />
Captured Screenshot</Typography> Captured Screenshot
</Typography>
{Object.keys(row.binaryOutput).map((key) => { {Object.keys(row.binaryOutput).map((key) => {
try { try {
const imageUrl = row.binaryOutput[key]; const imageUrl = row.binaryOutput[key];
@@ -181,10 +184,10 @@ export const RunContent = ({ row, currentLog, interpretationInProgress, logEndRe
<Box key={`number-of-binary-output-${key}`} sx={{ <Box key={`number-of-binary-output-${key}`} sx={{
width: 'max-content', width: 'max-content',
}}> }}>
<Typography key={`binary-output-key-${key}`} sx={{ margin: '20px 0px 20px 0px' }}> <Typography sx={{ margin: '20px 0px' }}>
<a href={imageUrl} download={key} style={{ textDecoration: 'none' }}>Download Screenshot</a> <a href={imageUrl} download={key} style={{ textDecoration: 'none' }}>Download Screenshot</a>
</Typography> </Typography>
<img key={`image-${key}`} src={imageUrl} alt={key} height='auto' width='700px' /> <img src={imageUrl} alt={key} height='auto' width='700px' />
</Box> </Box>
) )
} catch (e) { } catch (e) {
@@ -200,4 +203,4 @@ export const RunContent = ({ row, currentLog, interpretationInProgress, logEndRe
</TabContext> </TabContext>
</Box> </Box>
); );
} };