Skip to content

Commit a6bf554

Browse files
authored
Route list: group by day (commaai#310)
1 parent 1e9964c commit a6bf554

File tree

1 file changed

+42
-7
lines changed

1 file changed

+42
-7
lines changed

src/pages/dashboard/components/RouteList.tsx

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,9 @@ const RouteCard: VoidComponent<RouteCardProps> = (props) => {
3232
<Card class="max-w-none" href={`/${props.route.dongle_id}/${props.route.fullname.slice(17)}`} activeClass="md:before:bg-primary">
3333
<CardHeader
3434
headline={
35-
<div class="flex gap-2">
36-
<span>{startTime().format('ddd, MMM D, YYYY')}</span>&middot;
37-
<span>
38-
{startTime().format('h:mm A')} to {endTime().format('h:mm A')}
39-
</span>
40-
</div>
35+
<span>
36+
{startTime().format('h:mm A')} to {endTime().format('h:mm A')}
37+
</span>
4138
}
4239
subhead={<Suspense>{location()}</Suspense>}
4340
trailing={
@@ -103,6 +100,28 @@ const RouteList: VoidComponent<{ dongleId: string }> = (props) => {
103100
}
104101
})
105102

103+
// Group and display headers for each day
104+
let prevDayHeader: string | null = null
105+
function getDayHeader(route: RouteSegments): string | null {
106+
const date = dayjs(route.start_time_utc_millis)
107+
let dayHeader = null
108+
if (date.isSame(dayjs(), 'day')) {
109+
dayHeader = `Today – ${date.format('dddd, MMM D')}`
110+
} else if (date.isSame(dayjs().subtract(1, 'day'), 'day')) {
111+
dayHeader = `Yesterday – ${date.format('dddd, MMM D')}`
112+
} else if (date.year() === dayjs().year()) {
113+
dayHeader = date.format('dddd, MMM D')
114+
} else {
115+
dayHeader = date.format('dddd, MMM D, YYYY')
116+
}
117+
118+
if (dayHeader !== prevDayHeader) {
119+
prevDayHeader = dayHeader
120+
return dayHeader
121+
}
122+
return null
123+
}
124+
106125
return (
107126
<div class="flex w-full flex-col justify-items-stretch gap-4">
108127
<For each={pageNumbers()}>
@@ -114,7 +133,23 @@ const RouteList: VoidComponent<{ dongleId: string }> = (props) => {
114133
<Index each={new Array(PAGE_SIZE)}>{() => <div class="skeleton-loader flex h-[140px] flex-col rounded-lg" />}</Index>
115134
}
116135
>
117-
<For each={routes()}>{(route) => <RouteCard route={route} />}</For>
136+
<For each={routes()}>
137+
{(route) => {
138+
const firstHeader = prevDayHeader === null
139+
const dayHeader = getDayHeader(route)
140+
return (
141+
<>
142+
<Show when={dayHeader}>
143+
<Show when={!firstHeader}>
144+
<div class="6 w-full" />
145+
</Show>
146+
<h2 class="px-4 text-xl font-bold">{dayHeader}</h2>
147+
</Show>
148+
<RouteCard route={route} />
149+
</>
150+
)
151+
}}
152+
</For>
118153
</Suspense>
119154
)
120155
}}

0 commit comments

Comments
 (0)