Skip to content

Commit d81da9b

Browse files
authored
hashing by session parent id (#263)
1 parent 5c54948 commit d81da9b

4 files changed

Lines changed: 35 additions & 9 deletions

File tree

frontend/src/components/SessionPreviewModal.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ export default function SessionPreviewModal({
128128
{session.session_name}
129129
</h2>
130130
<p className="text-sm text-gray-600">
131-
{session.location || 'School 3'}
131+
{session.location}
132132
</p>
133133
</div>
134134

frontend/src/components/calendar/calendarView.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const colorMap = {
1616
yellow: '#F4B860',
1717
}
1818

19-
// Hash function to consistently map session ID to a color
19+
// Hash function to consistently map parent session ID to a color
2020
function hashIdToColor(id: string | number): keyof typeof colorMap {
2121
const colors: Array<keyof typeof colorMap> = ['blue', 'yellow', 'pink']
2222

@@ -84,7 +84,6 @@ export default function CalendarView({
8484
const maxTime = new Date()
8585
maxTime.setHours(19, 0, 0)
8686

87-
8887
return (
8988
<Calendar
9089
localizer={localizer}
@@ -104,7 +103,8 @@ export default function CalendarView({
104103
min={minTime}
105104
max={maxTime}
106105
eventPropGetter={(event) => {
107-
const colorKey = hashIdToColor(event.id)
106+
// Use parentId for consistent coloring across recurring sessions
107+
const colorKey = hashIdToColor(event.parentId)
108108
const backgroundColor = colorMap[colorKey]
109109

110110
return {

frontend/src/components/schedule/StudentSchedule.tsx

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,25 @@ import '@/app/calendar/override-calendar.css'
1111
const localizer = momentLocalizer(moment)
1212

1313
// Color palette for events - using disabled variants from global styling
14-
const eventColors = ['#f9aeda', '#bac0ff', '#ffdfb1', '#bac0ff', '#ffdfb1']
15-
const getEventColor = (index: number) => eventColors[index % eventColors.length]
14+
const colorMap = {
15+
pink: '#F9AEDA',
16+
blue: '#BAC0FF',
17+
yellow: '#F4B860',
18+
}
19+
20+
// Hash function to consistently map parent session ID to a color
21+
function hashIdToColor(id: string | number): keyof typeof colorMap {
22+
const colors: Array<keyof typeof colorMap> = ['blue', 'yellow', 'pink']
23+
24+
let hash = 0
25+
const idStr = String(id)
26+
for (let i = 0; i < idStr.length; i++) {
27+
hash = ((hash << 5) - hash) + idStr.charCodeAt(i)
28+
hash = hash & hash
29+
}
30+
31+
return colors[Math.abs(hash) % colors.length]
32+
}
1633

1734
interface StudentScheduleProps {
1835
studentId?: string
@@ -33,17 +50,22 @@ export default function StudentSchedule({ studentId, initialView = 'day', classN
3350
// Always build events array (empty if error or no data)
3451
const events = error
3552
? []
36-
: sessions.map((s, idx) => {
53+
: sessions.map((s) => {
3754
// Check if this is from studentHook (nested) or allHook (flat)
3855
const sessionData = studentHook ? (s as any).session : s
56+
const parentId = sessionData.session_parent_id || sessionData.id
57+
const colorKey = hashIdToColor(parentId)
58+
const backgroundColor = colorMap[colorKey]
59+
3960
return {
4061
id: sessionData.id,
62+
parentId,
4163
title: sessionData.session_name ? sessionData.session_name : 'Session',
4264
start: new Date(sessionData.start_datetime),
4365
end: new Date(sessionData.end_datetime),
4466
allDay: false,
4567
resource: {
46-
color: getEventColor(idx),
68+
color: backgroundColor,
4769
school: (sessionData as any).school || 'School',
4870
},
4971
}

frontend/src/hooks/useCalendar.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// hooks/useCalendar.ts - UPDATED WITH PARENT ID
2+
13
import type { View } from 'react-big-calendar'
24
import type { Session } from '@/lib/api/theSpecialStandardAPI.schemas'
35
import moment from 'moment'
@@ -34,6 +36,7 @@ export function useCalendarState() {
3436

3537
export interface CalendarEvent {
3638
id: string
39+
parentId: string // Add parent ID for color hashing
3740
title: string
3841
start: Date
3942
end: Date
@@ -62,6 +65,7 @@ export function useCalendarData(date: Date, view: View) {
6265
() =>
6366
sessions.map(session => ({
6467
id: session.id,
68+
parentId: session.session_parent_id || session.id, // Use parent ID if available, else fall back to session ID
6569
title: session.session_name,
6670
location: session.location,
6771
start: new Date(session.start_datetime),
@@ -80,4 +84,4 @@ export function useCalendarData(date: Date, view: View) {
8084
addSession,
8185
viewRange,
8286
}
83-
}
87+
}

0 commit comments

Comments
 (0)