Skip to content

Commit

Permalink
fix holiday ordering
Browse files Browse the repository at this point in the history
  • Loading branch information
thatmattlove committed Sep 6, 2024
1 parent dc2d71f commit 21653c5
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 30 deletions.
65 changes: 41 additions & 24 deletions lib/server/holidays.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,21 @@ interface HolidayIn extends BaseHoliday {
alsoObservedAs?: string;
}

enum EvePosition {
BEFORE = 0,
AFTER = 1,
}

export interface Holiday {
name: string;
date: string;
ts: number;
active: boolean;
elapsed: boolean;
}

export interface Holidays {
all: Holiday[];
active: number | null;
elapsed: number[];
next: number;
}

Expand All @@ -43,16 +49,33 @@ const holidayNames = [
"Christmas Day",
];

const eves = [
["Christmas Day", "Christmas Eve"],
["Thanksgiving Day", "Black Friday"],
const eves: [string, string, EvePosition][] = [
["Christmas Day", "Christmas Eve", EvePosition.BEFORE],
["Thanksgiving Day", "Black Friday", EvePosition.AFTER],
];

function matchEve(holiday: EveArgs): Holiday | undefined {
for (const [day, eve] of eves) {
function matchEve(holiday: EveArgs, start: dayjs.Dayjs, end: dayjs.Dayjs): Holiday | undefined {
for (const [day, eve, pos] of eves) {
if (holiday.name.test(day)) {
const date = holiday.date.clone().subtract(1, "day");
return { name: eve, date: date.format(DATE_FORMAT) };
if (pos === EvePosition.BEFORE) {
const date = holiday.date.clone().subtract(1, "day");
return {
name: eve,
date: date.format(DATE_FORMAT),
active: date.isBetween(start, end),
ts: date.unix(),
elapsed: date.isBefore(dayjs(), "day"),
};
} else if (pos === EvePosition.AFTER) {
const date = holiday.date.clone().add(1, "day");
return {
name: eve,
date: date.format(DATE_FORMAT),
active: date.isBetween(start, end),
ts: date.unix(),
elapsed: date.isBefore(dayjs(), "day"),
};
}
}
}
return;
Expand All @@ -73,9 +96,6 @@ export function getHolidays(): Holidays {
.add(3.6e6 - 1);
const allHolidays: HolidayIn[] = allForYear(now.year());
let all: Holiday[] = [];
let active: number | null = null;
let elapsed: number[] = [];
let idx = -1;

const holidayPatterns = holidayNames.map(h => new RegExp(h));

Expand All @@ -99,28 +119,25 @@ export function getHolidays(): Holidays {
}
}
if (typeof name === "string" && typeof date !== "undefined" && typeof pattern !== "undefined") {
idx++;
// idx++;
out.date = date.format(DATE_FORMAT);
out.ts = date.unix();
out.active = false;
out.name = name;
const eve = matchEve({ name: pattern, date });
const eve = matchEve({ name: pattern, date }, start, end);
if (date.isBefore(now, "day")) {
elapsed = [...elapsed, idx];
out.elapsed = true;
}
if (date.isBetween(start, end)) {
active = idx;
out.active = true;
}
if (typeof eve !== "undefined") {
all = [...all, eve];
}
all = [...all, out];
}
}
const next = elapsed.length === 0 ? 0 : elapsed.length;
const result: Holidays = {
all,
active,
elapsed,
next,
};
return result;
all = all.sort((a, b) => (a.ts > b.ts ? 1 : -1));
const next = all.filter(h => h.elapsed).length;
return { all, next };
}
11 changes: 5 additions & 6 deletions src/components/holiday-table/table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,17 @@ export const HolidayTable = (props: HolidayTableProps) => {
</thead>
<tbody>
{holidays.all.map((holiday, idx) => {
const elapsed = holidays.elapsed.includes(idx);
return (
<Tr
key={holiday.date}
opacity={elapsed ? 0.5 : 1}
fontStyle={elapsed ? "italic" : undefined}
opacity={holiday.elapsed ? 0.5 : 1}
fontStyle={holiday.elapsed ? "italic" : undefined}
bg={
holidays.active === idx
holiday.active
? activeColor
: elapsed
: holiday.elapsed
? elapsedColor
: holidays.next === idx && holidays.active === null
: holidays.next === idx && !holiday.active
? nextColor
: undefined
}
Expand Down

0 comments on commit 21653c5

Please sign in to comment.