From fd2c8cedbcf107005fe1c5466cddeb5bc1d2e7d8 Mon Sep 17 00:00:00 2001 From: Sidney Alcantara Date: Wed, 10 Nov 2021 19:09:40 +1100 Subject: [PATCH] build logs: group by day --- .../TableHeader/CloudLogs/BuildLogs/index.tsx | 143 +++++++++++------- 1 file changed, 92 insertions(+), 51 deletions(-) diff --git a/src/components/Table/TableHeader/CloudLogs/BuildLogs/index.tsx b/src/components/Table/TableHeader/CloudLogs/BuildLogs/index.tsx index 702ec9ba..1d71fcf0 100644 --- a/src/components/Table/TableHeader/CloudLogs/BuildLogs/index.tsx +++ b/src/components/Table/TableHeader/CloudLogs/BuildLogs/index.tsx @@ -1,5 +1,6 @@ -import { format } from "date-fns"; +import { format, differenceInCalendarDays } from "date-fns"; import { useAtom } from "jotai"; +import _get from "lodash/get"; import { styled, @@ -7,6 +8,8 @@ import { AccordionSummary as MuiAccordionSummary, Tooltip, AccordionDetails, + List, + ListProps, } from "@mui/material"; import SuccessIcon from "@mui/icons-material/Check"; import FailIcon from "@mui/icons-material/Close"; @@ -16,6 +19,7 @@ import ExpandMoreIcon from "@mui/icons-material/ExpandMore"; import EmptyState from "@src/components/EmptyState"; import BuildLogList from "./BuildLogList"; +import CloudLogSubheader from "../CloudLogSubheader"; import { DATE_TIME_FORMAT } from "@src/constants/dates"; import useBuildLogs from "./useBuildLogs"; @@ -24,7 +28,6 @@ import { cloudLogFiltersAtom } from "../utils"; const Accordion = styled(MuiAccordion)(({ theme }) => ({ background: "none", marginTop: 0, - margin: theme.spacing(0, -1.5), "&::before": { display: "none" }, ...theme.typography.caption, @@ -100,7 +103,7 @@ const AccordionSummary = styled(MuiAccordionSummary)(({ theme }) => ({ }, })); -export default function BuildLogs() { +export default function BuildLogs(props: Partial) { const { collectionState, latestStatus } = useBuildLogs(); const [cloudLogFilters, setCloudLogFilters] = useAtom(cloudLogFiltersAtom); @@ -113,53 +116,91 @@ export default function BuildLogs() { /> ); - return collectionState.documents.map((logEntry, index) => ( - - setCloudLogFilters((c) => ({ - ...c, - buildLogExpanded: expanded ? index : -1, - })) - } + const renderedLogItems: React.ReactNodeArray = []; + + for (let i = 0; i < collectionState.documents.length; i++) { + const logEntry = collectionState.documents[i]; + const prevEntry = collectionState.documents[i - 1]; + + // Group by day + const diff = differenceInCalendarDays( + Date.now(), + _get(logEntry, "startTimeStamp") ?? 0 + ); + const prevDiff = differenceInCalendarDays( + Date.now(), + _get(prevEntry, "startTimeStamp") ?? 0 + ); + + if (diff !== prevDiff) { + renderedLogItems.push( + + {diff === 0 ? "Today" : diff === 1 ? "Yesterday" : `${diff} days ago`} + + ); + } + + renderedLogItems.push( +
  • + + setCloudLogFilters((c) => ({ + ...c, + buildLogExpanded: expanded ? i : -1, + })) + } + > + } + aria-controls={`${logEntry.id}-content`} + id={`${logEntry.id}-header`} + > + {logEntry.status === "BUILDING" && ( + + + + )} + {logEntry.status === "SUCCESS" && ( + + + + )} + {logEntry.status === "FAIL" && ( + + + + )} + + {format(logEntry.startTimeStamp, DATE_TIME_FORMAT + ":ss.SSS X")} + + + + + + +
  • + ); + } + + return ( + - } - aria-controls={`${logEntry.id}-content`} - id={`${logEntry.id}-header`} - > - {logEntry.status === "BUILDING" && ( - - - - )} - {logEntry.status === "SUCCESS" && ( - - - - )} - {logEntry.status === "FAIL" && ( - - - - )} - - {format(logEntry.startTimeStamp, DATE_TIME_FORMAT + ":ss.SSS X")} - - - - - -
    - )); + {renderedLogItems} + + ); }