feat(race-setup): group the race list into today, upcoming and folded done, with progress and search (1.52.0)
This commit is contained in:
1 parent
b2e08db3d8
commit
b23410c190
13 files changed
+213
-29
No files matched your search
@@ -0,0 +1,38 @@
|
||||
// The race list in Race Setup, grouped so it stays short: today, upcoming and done (folded
|
||||
// away), each grouped per date, with how far each race has come.
|
||||
import { orderSessionsForRunning } from "./event_race_logic.js";
|
||||
|
||||
const isDone = (session) => ["finished", "stopped"].includes(session.status);
|
||||
const isLive = (session) => ["running", "paused"].includes(session.status);
|
||||
|
||||
// { total, done, live, next }: live/next are session names (in running order).
|
||||
export function getEventProgress(event, sessions = []) {
|
||||
const ordered = orderSessionsForRunning(sessions, event?.sessionOrder);
|
||||
const live = ordered.find(isLive) || null;
|
||||
const next = ordered.find((session) => !isDone(session) && !isLive(session)) || null;
|
||||
return { total: ordered.length, done: ordered.filter(isDone).length, live: live?.name || "", next: next?.name || "" };
|
||||
}
|
||||
|
||||
// A race is done when every session has been run, or its date has passed.
|
||||
export function groupEventsForList(events = [], getSessions = () => [], today = "") {
|
||||
const groups = { today: [], upcoming: [], done: [] };
|
||||
events.forEach((event) => {
|
||||
const progress = getEventProgress(event, getSessions(event.id));
|
||||
const allRun = progress.total > 0 && progress.done === progress.total;
|
||||
const date = String(event.date || "");
|
||||
const key = progress.live || date === today ? "today" : allRun || (date && date < today) ? "done" : "upcoming";
|
||||
groups[key].push({ event, progress });
|
||||
});
|
||||
const byDay = (items, newestFirst) => {
|
||||
const days = new Map();
|
||||
items.forEach((item) => {
|
||||
const date = String(item.event.date || "");
|
||||
if (!days.has(date)) days.set(date, []);
|
||||
days.get(date).push(item);
|
||||
});
|
||||
return [...days.entries()]
|
||||
.sort(([a], [b]) => (newestFirst ? b.localeCompare(a) : (a || "9999").localeCompare(b || "9999")))
|
||||
.map(([date, list]) => ({ date, items: list.sort((a, b) => String(a.event.name || "").localeCompare(String(b.event.name || ""), undefined, { numeric: true })) }));
|
||||
};
|
||||
return { today: byDay(groups.today, false), upcoming: byDay(groups.upcoming, false), done: byDay(groups.done, true) };
|
||||
}
|
||||
Reference in new issue
Block a user