Skip to content

Commit

Permalink
feat: course colors (#175)
Browse files Browse the repository at this point in the history
* feat: course colors

* docs: fix typo in jsdoc
  • Loading branch information
Samathingamajig authored Mar 17, 2024
1 parent afa634f commit dc77cc2
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 29 deletions.
4 changes: 4 additions & 0 deletions src/shared/types/Course.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { type CourseColors, getCourseColors } from '@shared/util/colors';
import type { Serialized } from 'chrome-extension-toolkit';

import type { CourseMeeting } from './CourseMeeting';
Expand Down Expand Up @@ -75,6 +76,8 @@ export class Course {
semester: Semester;
/** Unix timestamp of when the course was last scraped */
scrapedAt: number;
/** The colors of the course when displayed */
colors: CourseColors;

constructor(course: Serialized<Course>) {
Object.assign(this, course);
Expand All @@ -83,6 +86,7 @@ export class Course {
if (!course.scrapedAt) {
this.scrapedAt = Date.now();
}
this.colors = course.colors ? structuredClone(course.colors) : getCourseColors('emerald', 500);
}

/**
Expand Down
3 changes: 3 additions & 0 deletions src/stories/components/ConflictsWithWarning.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Course, Status } from '@shared/types/Course';
import { CourseMeeting } from '@shared/types/CourseMeeting';
import Instructor from '@shared/types/Instructor';
import { getCourseColors } from '@shared/util/colors';
import type { Meta, StoryObj } from '@storybook/react';
import ConflictsWithWarning from '@views/components/common/ConflictsWithWarning/ConflictsWithWarning';

Expand Down Expand Up @@ -45,6 +46,7 @@ export const ExampleCourse: Course = new Course({
uniqueId: 12345,
url: 'https://utdirect.utexas.edu/apps/registrar/course_schedule/20242/12345/',
scrapedAt: Date.now(),
colors: getCourseColors('blue', 500),
});
export const ExampleCourse2: Course = new Course({
courseName: 'PRINCIPLES OF COMPUTER SYSTEMS',
Expand Down Expand Up @@ -92,6 +94,7 @@ export const ExampleCourse2: Course = new Course({
uniqueId: 67890,
url: 'https://utdirect.utexas.edu/apps/registrar/course_schedule/20242/12345/',
scrapedAt: Date.now(),
colors: getCourseColors('yellow', 500),
});

const meta = {
Expand Down
13 changes: 6 additions & 7 deletions src/stories/components/List.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import type { DraggableProvidedDragHandleProps } from '@hello-pangea/dnd';
import { Course, Status } from '@shared/types/Course';
import { CourseMeeting } from '@shared/types/CourseMeeting';
import Instructor from '@shared/types/Instructor';
import type { CourseColors } from '@shared/util/colors';
import { tailwindColorways } from '@shared/util/storybook';
import type { Meta, StoryObj } from '@storybook/react';
import List from '@views/components/common/List/List';
Expand Down Expand Up @@ -63,6 +62,7 @@ const generateCourses = (count: number): Course[] => {
status: Status.WAITLISTED,
uniqueId: 12345 + i, // Make uniqueId different for each course
url: 'https://utdirect.utexas.edu/apps/registrar/course_schedule/20242/12345/',
colors: tailwindColorways[i],
});

courses.push(course);
Expand All @@ -72,10 +72,9 @@ const generateCourses = (count: number): Course[] => {
};

const exampleCourses = generateCourses(numberOfCourses);
const generateCourseBlocks = (
{ course, colors }: { course: Course; colors: CourseColors },
dragHandleProps: DraggableProvidedDragHandleProps
) => <PopupCourseBlock key={course.uniqueId} course={course} colors={colors} dragHandleProps={dragHandleProps} />;
const generateCourseBlocks = (course: Course, dragHandleProps: DraggableProvidedDragHandleProps) => (
<PopupCourseBlock key={course.uniqueId} course={course} colors={course.colors} dragHandleProps={dragHandleProps} />
);

const meta = {
title: 'Components/Common/List',
Expand All @@ -94,9 +93,9 @@ type Story = StoryObj<typeof meta>;

export const Default: Story = {
args: {
draggables: exampleCourses.map((course, i) => ({ course, colors: tailwindColorways[i] })),
draggables: exampleCourses,
children: generateCourseBlocks,
itemKey: (item: { course: Course }) => item.course.uniqueId,
itemKey: (item: Course) => item.uniqueId,
gap: 12,
},
render: args => (
Expand Down
1 change: 1 addition & 0 deletions src/stories/components/PopupCourseBlock.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export const ExampleCourse: Course = new Course({
status: Status.WAITLISTED,
uniqueId: 12345,
url: 'https://utdirect.utexas.edu/apps/registrar/course_schedule/20242/12345/',
colors: getCourseColors('cyan', 500),
});

// More on how to set up stories at: https://storybook.js.org/docs/writing-stories#default-export
Expand Down
2 changes: 2 additions & 0 deletions src/stories/components/calendar/CalendarBottomBar.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const exampleGovCourse: Course = new Course({
status: Status.OPEN,
uniqueId: 12345,
url: 'https://utdirect.utexas.edu/apps/registrar/course_schedule/20242/12345/',
colors: getCourseColors('red', 500),
});

const examplePsyCourse: Course = new Course({
Expand Down Expand Up @@ -65,6 +66,7 @@ const examplePsyCourse: Course = new Course({
status: Status.CLOSED,
uniqueId: 12346,
url: 'https://utdirect.utexas.edu/apps/registrar/course_schedule/20242/12345/',
colors: getCourseColors('blue', 500),
});

const meta = {
Expand Down
4 changes: 2 additions & 2 deletions src/stories/components/calendar/CalendarCourse.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Course, Status } from '@shared/types/Course';
import { CourseMeeting, DAY_MAP } from '@shared/types/CourseMeeting';
import { CourseSchedule } from '@shared/types/CourseSchedule';
import Instructor from '@shared/types/Instructor';
import { getCourseColors } from '@shared/util/colors';
import type { Meta, StoryObj } from '@storybook/react';
import CalendarCourse from '@views/components/calendar/CalendarCourseBlock/CalendarCourseMeeting';

Expand All @@ -15,7 +16,6 @@ const meta = {
argTypes: {
course: { control: 'object' },
meetingIdx: { control: 'number' },
color: { control: 'color' },
rightIcon: { control: 'object' },
},
} satisfies Meta<typeof CalendarCourse>;
Expand Down Expand Up @@ -56,8 +56,8 @@ export const Default: Story = {
season: 'Spring',
},
scrapedAt: Date.now(),
colors: getCourseColors('emerald', 500),
}),
meetingIdx: 0,
color: 'red',
},
};
4 changes: 4 additions & 0 deletions src/stories/injected/mocked.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Course, Status } from '@shared/types/Course';
import { CourseMeeting, DAY_MAP } from '@shared/types/CourseMeeting';
import Instructor from '@shared/types/Instructor';
import { UserSchedule } from '@shared/types/UserSchedule';
import { getCourseColors } from '@shared/util/colors';

export const exampleCourse: Course = new Course({
courseName: 'ELEMS OF COMPTRS/PROGRAMMNG-WB',
Expand Down Expand Up @@ -51,6 +52,7 @@ export const exampleCourse: Course = new Course({
status: Status.OPEN,
uniqueId: 12345,
url: 'https://utdirect.utexas.edu/apps/registrar/course_schedule/20242/12345/',
colors: getCourseColors('blue', 500),
});

export const exampleSchedule: UserSchedule = new UserSchedule({
Expand Down Expand Up @@ -104,6 +106,7 @@ export const bevoCourse: Course = new Course({
season: 'Spring',
},
scrapedAt: Date.now(),
colors: getCourseColors('green', 500),
});

export const bevoSchedule: UserSchedule = new UserSchedule({
Expand Down Expand Up @@ -158,6 +161,7 @@ export const mikeScottCS314Course: Course = new Course({
season: 'Spring',
},
scrapedAt: Date.now(),
colors: getCourseColors('orange', 500),
});

export const mikeScottCS314Schedule: UserSchedule = new UserSchedule({
Expand Down
14 changes: 5 additions & 9 deletions src/views/components/PopupMain.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { background } from '@shared/messages';
import { UserScheduleStore } from '@shared/storage/UserScheduleStore';
import { tailwindColorways } from '@shared/util/storybook';
import Divider from '@views/components/common/Divider/Divider';
import ExtensionRoot from '@views/components/common/ExtensionRoot/ExtensionRoot';
import List from '@views/components/common/List/List';
Expand Down Expand Up @@ -86,22 +85,19 @@ export default function PopupMain(): JSX.Element {
<div className='flex-1 self-stretch overflow-y-auto px-5'>
{activeSchedule?.courses?.length > 0 && (
<List
draggables={activeSchedule.courses.map((course, i) => ({
course,
colors: tailwindColorways[i],
}))}
draggables={activeSchedule.courses}
onReordered={reordered => {
activeSchedule.courses = reordered.map(c => c.course);
activeSchedule.courses = reordered;
replaceSchedule(getActiveSchedule(), activeSchedule);
}}
itemKey={e => e.course.uniqueId}
itemKey={e => e.uniqueId}
gap={10}
>
{({ course, colors }, handleProps) => (
{(course, handleProps) => (
<PopupCourseBlock
key={course.uniqueId}
course={course}
colors={colors}
colors={course.colors}
dragHandleProps={handleProps}
/>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ export interface CalendarCourseMeetingProps {
course: Course;
/* index into course meeting array to display */
meetingIdx?: number;
/** The background color for the course. */
color: string;
/** The icon to display on the right side of the course. This is optional. */
rightIcon?: React.ReactNode;
}
Expand All @@ -22,12 +20,11 @@ export interface CalendarCourseMeetingProps {
* `CalendarCourseMeeting` is a functional component that displays a course meeting.
*
* @example
* <CalendarCourseMeeting course={course} meeting={meeting} color="red" rightIcon={<Icon />} />
* <CalendarCourseMeeting course={course} meeting={meeting} rightIcon={<Icon />} />
*/
export default function CalendarCourseMeeting({
course,
meetingIdx,
color,
rightIcon,
}: CalendarCourseMeetingProps): JSX.Element {
let meeting: CourseMeeting | null = meetingIdx !== undefined ? course.schedule.meetings[meetingIdx] : null;
Expand Down
9 changes: 2 additions & 7 deletions src/views/components/calendar/CalendarGrid/CalendarGrid.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { Course } from '@shared/types/Course';
import { getCourseColors } from '@shared/util/colors';
import CalendarCourseCell from '@views/components/calendar/CalendarCourseCell/CalendarCourseCell';
import Text from '@views/components/common/Text/Text';
import type { CalendarGridCourse } from '@views/hooks/useFlattenedCourseSchedule';
Expand Down Expand Up @@ -120,11 +119,8 @@ function AccountForCourseConflicts({ courseCells, setCourse }: AccountForCourseC
});
});

// Part of TODO: block.course is definitely a course object
// console.log(courseCells);

return courseCells.map((block, i) => {
const { courseDeptAndInstr, timeAndLocation, status, colors } = courseCells[i].componentProps;
const { courseDeptAndInstr, timeAndLocation, status } = courseCells[i].componentProps;

return (
<div
Expand All @@ -141,8 +137,7 @@ function AccountForCourseConflicts({ courseCells, setCourse }: AccountForCourseC
courseDeptAndInstr={courseDeptAndInstr}
timeAndLocation={timeAndLocation}
status={status}
// TODO: Change to block.componentProps.colors when colors are integrated to the rest of the project
colors={getCourseColors('emerald', 500) /* block.componentProps.colors */}
colors={block.course.colors}
onClick={() => setCourse(block.course)}
/>
</div>
Expand Down
2 changes: 2 additions & 0 deletions src/views/lib/CourseCatalogScraper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { InstructionMode, ScrapedRow, Semester, StatusType } from '@shared/
import { Course, Status } from '@shared/types/Course';
import { CourseSchedule } from '@shared/types/CourseSchedule';
import Instructor from '@shared/types/Instructor';
import { getCourseColors } from '@shared/util/colors';
import type { SiteSupportType } from '@views/lib/getSiteSupport';

/**
Expand Down Expand Up @@ -93,6 +94,7 @@ export class CourseCatalogScraper {
description: this.getDescription(document),
semester: this.getSemester(),
scrapedAt: Date.now(),
colors: getCourseColors('emerald', 500),
});
courses.push({
element: row,
Expand Down

0 comments on commit dc77cc2

Please sign in to comment.