Skip to content

Commit 87f4d11

Browse files
authored
fix: use stable color assignment for schedule courses (#70) (#94)
1 parent 90aa778 commit 87f4d11

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

apps/web/src/modules/schedule-calendar/schedule-calendar.tsx

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,17 @@ export const allClassColors: EventColor[] = [
7171
"cyan",
7272
];
7373

74+
function getColor(id: string): EventColor {
75+
let hash = 0;
76+
for (let i = 0; i < id.length; i++) {
77+
const char = id.charCodeAt(i);
78+
hash = (hash << 5) - hash + char;
79+
hash = hash & hash; // Convert to 32-bit integer
80+
}
81+
const colorIndex = Math.abs(hash) % allClassColors.length;
82+
return allClassColors[colorIndex];
83+
}
84+
7485
export function getUserClassesByTerm(
7586
classes:
7687
| FunctionReturnType<typeof api.userCourseOfferings.getUserCourseOfferings>
@@ -101,8 +112,6 @@ export function ScheduleCalendar({
101112
return <Skeleton className="h-full w-full rounded-lg" />;
102113
}
103114

104-
let colorIndex = 0;
105-
106115
const transformedClasses: Class[] = classes.map((c) => {
107116
const offering = c.courseOffering;
108117
const startTime = `${offering.startTime.split(":")[0]} ${offering.startTime.split(":")[1]}`;
@@ -114,8 +123,7 @@ export function ScheduleCalendar({
114123
return `${dayName} ${startTime} ${endTime}`;
115124
});
116125

117-
const color = allClassColors[colorIndex % allClassColors.length];
118-
colorIndex++;
126+
const color = getColor(offering._id);
119127

120128
const slots: { start: Date; end: Date }[] = [];
121129

@@ -231,7 +239,7 @@ export function ScheduleCalendar({
231239
id: `preview-${offering._id}`,
232240
courseCode: offering.courseCode,
233241
title: `${offering.courseCode} - ${offering.title}`,
234-
color: allClassColors[colorIndex % allClassColors.length],
242+
color: getColor(offering._id),
235243
times: slots,
236244
description: `${offering.instructor.join(", ")}${offering.section.toUpperCase()} • Preview`,
237245
isPreview: true,

0 commit comments

Comments
 (0)