Files
parcer/src/components/molecules/ColapsibleRow.tsx

174 lines
6.6 KiB
TypeScript
Raw Normal View History

2024-06-24 22:32:34 +05:30
import { useEffect, useRef, useState } from "react";
import * as React from "react";
import TableRow from "@mui/material/TableRow";
import TableCell from "@mui/material/TableCell";
2024-10-28 20:41:16 +05:30
import { Box, Collapse, IconButton, Typography, Chip, TextField } from "@mui/material";
import { DeleteForever, KeyboardArrowDown, KeyboardArrowUp, Settings } from "@mui/icons-material";
2024-06-24 22:32:34 +05:30
import { deleteRunFromStorage } from "../../api/storage";
import { columns, Data } from "./RunsTable";
import { RunContent } from "./RunContent";
2024-10-28 20:41:16 +05:30
import { GenericModal } from "../atoms/GenericModal";
import { modalStyle } from "./AddWhereCondModal";
2024-10-28 21:18:33 +05:30
import { getUserById } from "../../api/auth";
2024-10-28 20:41:16 +05:30
interface RunTypeChipProps {
runByUserId?: string;
runByScheduledId?: string;
runByAPI: boolean;
}
const RunTypeChip: React.FC<RunTypeChipProps> = ({ runByUserId, runByScheduledId, runByAPI }) => {
if (runByUserId) return <Chip label="Manual Run" color="primary" variant="outlined" />;
if (runByScheduledId) return <Chip label="Scheduled Run" color="primary" variant="outlined" />;
if (runByAPI) return <Chip label="API" color="primary" variant="outlined" />;
2024-10-28 20:41:34 +05:30
return <Chip label="Unknown Run Type" color="primary" variant="outlined" />;
2024-10-28 20:41:16 +05:30
};
2024-06-24 22:32:34 +05:30
interface CollapsibleRowProps {
row: Data;
handleDelete: () => void;
isOpen: boolean;
currentLog: string;
abortRunHandler: () => void;
runningRecordingName: string;
}
2024-10-19 03:55:36 +05:30
export const CollapsibleRow = ({ row, handleDelete, isOpen, currentLog, abortRunHandler, runningRecordingName }: CollapsibleRowProps) => {
2024-06-24 22:32:34 +05:30
const [open, setOpen] = useState(isOpen);
2024-10-28 20:41:16 +05:30
const [openSettingsModal, setOpenSettingsModal] = useState(false);
2024-10-28 21:18:33 +05:30
const [userEmail, setUserEmail] = useState<string | null>(null);
2024-10-28 20:41:16 +05:30
const runByLabel = row.runByUserId
2024-10-28 21:18:33 +05:30
? `${userEmail}`
2024-10-28 20:41:34 +05:30
: row.runByScheduleId
? `${row.runByScheduleId}`
2024-10-28 20:41:34 +05:30
: row.runByAPI
? 'API'
: 'Unknown';
2024-06-24 22:32:34 +05:30
2024-10-19 03:55:36 +05:30
const logEndRef = useRef<HTMLDivElement | null>(null);
2024-06-24 22:32:34 +05:30
const scrollToLogBottom = () => {
if (logEndRef.current) {
logEndRef.current.scrollIntoView({ behavior: "smooth" });
}
}
const handleAbort = () => {
abortRunHandler();
}
useEffect(() => {
scrollToLogBottom();
}, [currentLog])
2024-10-28 21:18:33 +05:30
useEffect(() => {
const fetchUserEmail = async () => {
if (row.runByUserId) {
const userData = await getUserById(row.runByUserId);
if (userData && userData.user) {
setUserEmail(userData.user.email);
}
}
};
fetchUserEmail();
}, [row.runByUserId]);
2024-06-24 22:32:34 +05:30
return (
<React.Fragment>
<TableRow sx={{ '& > *': { borderBottom: 'unset' } }} hover role="checkbox" tabIndex={-1} key={row.id}>
<TableCell>
<IconButton
aria-label="expand row"
size="small"
onClick={() => {
setOpen(!open);
scrollToLogBottom();
}}
>
{open ? <KeyboardArrowUp /> : <KeyboardArrowDown />}
</IconButton>
</TableCell>
{columns.map((column) => {
// @ts-ignore
2024-10-19 03:55:36 +05:30
const value: any = row[column.id];
2024-06-24 22:32:34 +05:30
if (value !== undefined) {
return (
<TableCell key={column.id} align={column.align}>
2024-10-28 20:41:16 +05:30
{value}
2024-06-24 22:32:34 +05:30
</TableCell>
);
} else {
switch (column.id) {
2024-10-28 20:41:16 +05:30
case 'runStatus':
2024-10-28 20:14:02 +05:30
return (
<TableCell key={column.id} align={column.align}>
{row.status === 'success' && <Chip label="Success" color="success" variant="outlined" />}
{row.status === 'running' && <Chip label="Running" color="warning" variant="outlined" />}
{row.status === 'scheduled' && <Chip label="Scheduled" variant="outlined" />}
{row.status === 'failed' && <Chip label="Failed" color="error" variant="outlined" />}
</TableCell>
)
2024-06-24 22:32:34 +05:30
case 'delete':
return (
<TableCell key={column.id} align={column.align}>
2024-10-19 03:55:36 +05:30
<IconButton aria-label="add" size="small" onClick={() => {
2024-10-10 02:25:48 +05:30
deleteRunFromStorage(`${row.runId}`).then((result: boolean) => {
2024-06-24 22:32:34 +05:30
if (result) {
handleDelete();
}
})
2024-10-19 03:55:27 +05:30
}}>
2024-10-19 03:55:36 +05:30
<DeleteForever />
2024-06-24 22:32:34 +05:30
</IconButton>
</TableCell>
);
2024-10-28 20:41:16 +05:30
case 'settings':
return (
<TableCell key={column.id} align={column.align}>
<IconButton aria-label="settings" size="small" onClick={() => setOpenSettingsModal(true)}>
<Settings />
</IconButton>
<GenericModal
isOpen={openSettingsModal}
onClose={() => setOpenSettingsModal(false)}
modalStyle={modalStyle}
>
<>
<Typography variant="h5" style={{ marginBottom: '20px' }}>Run Settings</Typography>
<Box style={{ display: 'flex', flexDirection: 'column', gap: '20px' }}>
2024-10-28 21:18:49 +05:30
<TextField
2024-10-28 20:44:16 +05:30
label="Run ID"
value={row.runId}
InputProps={{ readOnly: true }}
/>
2024-10-28 20:41:16 +05:30
<TextField
2024-10-28 20:43:22 +05:30
label={row.runByUserId ? "Run by User" : row.runByScheduleId ? "Run by Schedule ID" : "Run by API"}
2024-10-28 20:41:16 +05:30
value={runByLabel}
InputProps={{ readOnly: true }}
/>
<Box style={{ display: 'flex', alignItems: 'center', gap: '10px' }}>
<Typography variant="body1">Run Type:</Typography>
<RunTypeChip runByUserId={row.runByUserId} runByScheduledId={row.runByScheduleId} runByAPI={row.runByAPI ?? false} />
</Box>
</Box>
</>
</GenericModal>
</TableCell>
)
2024-06-24 22:32:34 +05:30
default:
return null;
}
}
})}
</TableRow>
<TableRow>
<TableCell style={{ paddingBottom: 0, paddingTop: 0 }} colSpan={6}>
<Collapse in={open} timeout="auto" unmountOnExit>
<RunContent row={row} abortRunHandler={handleAbort} currentLog={currentLog}
2024-10-19 03:55:36 +05:30
logEndRef={logEndRef} interpretationInProgress={runningRecordingName === row.name} />
2024-06-24 22:32:34 +05:30
</Collapse>
</TableCell>
</TableRow>
</React.Fragment>
);
}