Skip to content

Commit

Permalink
Merge pull request #485 from DataRecce/feature/drc-766-history-items-…
Browse files Browse the repository at this point in the history
…are-segmented-by-date

[Enhancement] Show date segment in the history list
  • Loading branch information
popcornylu authored Nov 13, 2024
2 parents a466787 + ca3b3b8 commit 69c564c
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 12 deletions.
47 changes: 38 additions & 9 deletions js/src/components/run/RunList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { RepeatIcon } from "@chakra-ui/icons";
import { useLocation } from "wouter";
import SimpleBar from "simplebar-react";
import "simplebar/dist/simplebar.min.css";
import { RunStatusAndDate } from "./RunStatusAndDate";
import { formatRunDate, RunStatusAndDate } from "./RunStatusAndDate";

const RunListItem = ({
run,
Expand Down Expand Up @@ -123,6 +123,23 @@ const RunListItem = ({
);
};

const DateSegmentItem = ({ runAt }: { runAt?: string }) => {
const dateTime = runAt ? formatRunDate(new Date(runAt)) : null;

return (
<Flex
minWidth="200px"
width="100%"
p="5px 20px"
borderBottom={"solid 1px lightgray"}
color="gray.500"
fontSize={"11pt"}
>
{dateTime}
</Flex>
);
};

export const RunList = () => {
const {
data: runs,
Expand Down Expand Up @@ -160,6 +177,8 @@ export const RunList = () => {
[setLocation]
);

let previousDate: string | null = null;

return (
<Flex direction="column" height="100%">
<HStack
Expand Down Expand Up @@ -189,15 +208,25 @@ export const RunList = () => {
) : (
<SimpleBar style={{ minHeight: "100%", height: 0 }}>
{(runs || []).map((run, index) => {
const currentDate = new Date(run.run_at).toDateString();
const shouldRenderDateSegment =
previousDate != null && previousDate !== currentDate;
previousDate = currentDate;

return (
<RunListItem
key={run.run_id}
run={run}
isSelected={run.run_id === runId}
onSelectRun={handleSelectRun}
onGoToCheck={handleGoToCheck}
onAddToChecklist={handleAddToChecklist}
/>
<>
{shouldRenderDateSegment && (
<DateSegmentItem key={currentDate} runAt={run.run_at} />
)}
<RunListItem
key={run.run_id}
run={run}
isSelected={run.run_id === runId}
onSelectRun={handleSelectRun}
onGoToCheck={handleGoToCheck}
onAddToChecklist={handleAddToChecklist}
/>
</>
);
})}
</SimpleBar>
Expand Down
39 changes: 36 additions & 3 deletions js/src/components/run/RunStatusAndDate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,40 @@ import { Run } from "@/lib/api/types";
import { Flex, Spinner, Text } from "@chakra-ui/react";
import { format } from "date-fns";

export function formatRunDate(date: Date | null) {
const today = new Date();
const yesterday = new Date();
yesterday.setDate(today.getDate() - 1);
if (date == null) {
return null;
}

if (today.toDateString() === date.toDateString()) {
return "Today";
} else if (yesterday.toDateString() === date.toDateString()) {
return "Yesterday";
} else {
return format(date, "MMM d");
}
}

export function formatRunDateTime(date: Date | null) {
const today = new Date();
const yesterday = new Date();
yesterday.setDate(today.getDate() - 1);
if (date == null) {
return null;
}

if (today.toDateString() === date.toDateString()) {
return "Today, " + format(date, "HH:mm");
} else if (yesterday.toDateString() === date.toDateString()) {
return "Yesterday, " + format(date, "HH:mm");
} else {
return format(date, "MMM d, HH:mm");
}
}

export const RunStatusAndDate = ({ run }: { run: Run }) => {
const isRunning = run?.status === "running";

Expand Down Expand Up @@ -32,9 +66,7 @@ export const RunStatusAndDate = ({ run }: { run: Run }) => {
color = "green";
message = "Finished";
}
const dateTime = run?.run_at
? format(new Date(run.run_at), "MMM d, HH:mm")
: null;
const dateTime = run?.run_at ? formatRunDateTime(new Date(run.run_at)) : null;

return (
<Flex
Expand All @@ -56,3 +88,4 @@ export const RunStatusAndDate = ({ run }: { run: Run }) => {
</Flex>
);
};

0 comments on commit 69c564c

Please sign in to comment.