All files / src/components/Playbooks/outputs PlayBookRunLogTable.tsx

0% Statements 0/35
0% Branches 0/22
0% Functions 0/11
0% Lines 0/33

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178                                                                                                                                                                                                                                                                                                                                                                   
import { useState, useEffect } from "react";
import styles from "./index.module.css";
 
import {
  Table,
  TableBody,
  TableHead,
  TableRow,
  Collapse,
  Button,
  Dialog,
  DialogActions,
} from "@mui/material";
 
import { ArrowRightRounded } from "@mui/icons-material";
import SeeMoreTextWithoutModal from "../../common/SeeMoreTextWithoutModal/index.tsx";
import { isDate, renderTimestamp } from "../../../utils/common/dateUtils.ts";
import Code from "../../common/Code/index.tsx";
import React from "react";
import transformLogData from "../../../utils/execution/transformLogData.ts";
 
const DISPLAY_COLUMNS = [
  "@timestamp",
  "timestamp",
  "@message",
  "message",
  "@log",
  "log",
];
 
const PlayBookRunLogTable = ({ title, result, timestamp, showHeading }) => {
  const [showTable, setShowTable] = useState(false);
  const [open, setOpen] = useState(false);
  const [tableLoading, setTableLoading] = useState(true);
  const [expandedRows, setExpandedRows] = useState({});
  const rows = result?.logs?.rows;
 
  useEffect(() => {
    Iif (rows?.length > 0) setShowTable(true);
    setTableLoading(false);
  }, [rows]);
 
  const handleClose = () => {
    setOpen(false);
  };
 
  const handleRowClick = (rowIndex) => {
    setExpandedRows((prev) => ({
      ...prev,
      [rowIndex]: !prev[rowIndex],
    }));
  };
 
  Iif (tableLoading)
    return (
      <div>
        <p className="text-xs font-semibold">Loading...</p>
      </div>
    );
 
  return (
    <div
      className={`${
        showHeading ? "h-full" : "h-auto"
      } border p-2 rounded mb-1 overflow-auto`}>
      <p className={styles["graph-title"]}>{title}</p>
      {!showTable && <p className={styles["graph-error"]}>No data available</p>}
      {showTable && (
        <Table
          stickyHeader
          className={`text-xs min-w-[50px] !border !rounded !overflow-hidden mt-2 h-full`}>
          <TableHead>
            <TableRow>
              <th className="!w-fit !border" />
              {rows?.[0]?.columns
                ?.filter((x) => DISPLAY_COLUMNS.includes(x.name))
                .map((col, index) => {
                  return (
                    <th
                      key={index}
                      className="!w-[200px] !border text-left p-2">
                      <SeeMoreTextWithoutModal
                        text={col.name}
                        maxLength={50}
                        className="font-bold text-xs"
                      />
                    </th>
                  );
                })}
            </TableRow>
          </TableHead>
          <TableBody>
            {rows?.map((row, rowIndex) => {
              return (
                <>
                  <tr
                    className={`${
                      rowIndex % 2 === 0 ? "bg-white" : "bg-gray-100"
                    }`}
                    key={rowIndex}
                    onClick={() => handleRowClick(rowIndex)}>
                    <td>
                      <ArrowRightRounded
                        fontSize="large"
                        className={`${
                          expandedRows[rowIndex] ? "rotate-90" : "rotate-0"
                        } text-gray-500`}
                      />
                    </td>
                    {row?.columns
                      ?.filter((x) => DISPLAY_COLUMNS.includes(x.name))
                      .map((col, colIndex) => {
                        const colValue = isDate(col.value)
                          ? renderTimestamp(
                              new Date(col.value).getTime() / 1000,
                            )
                          : col.value;
                        return (
                          <td
                            key={colIndex}
                            className={`w-[200px] text-xs p-2`}>
                            <p className="text-ellipsis overflow-hidden whitespace-nowrap max-w-sm p-0 font-medium">
                              {colValue}
                            </p>
                          </td>
                        );
                      })}
                  </tr>
                  <tr
                    className={`${
                      rowIndex % 2 === 0 ? "bg-white" : "bg-gray-100"
                    } p-0`}>
                    <td />
                    <td colSpan={2}>
                      <Collapse
                        in={expandedRows[rowIndex]}
                        timeout="auto"
                        className="max-w-[550px] p-2 relative"
                        unmountOnExit>
                        <Code
                          content={JSON.stringify(
                            transformLogData(row),
                            null,
                            2,
                          )}
                        />
                      </Collapse>
                    </td>
                  </tr>
                </>
              );
            })}
          </TableBody>
        </Table>
      )}
      {!showTable && timestamp && (
        <p className={styles["graph-ts-error"]}>
          <i>Updated at: {timestamp}</i>
        </p>
      )}
      {showTable && timestamp && (
        <p className={styles["graph-ts"]}>
          <i>Updated at: {timestamp}</i>
        </p>
      )}
      <Dialog open={open} onClose={handleClose}>
        <DialogActions>
          <Button onClick={handleClose} color="primary">
            Close
          </Button>
        </DialogActions>
      </Dialog>
    </div>
  );
};
 
export default PlayBookRunLogTable;