Skip to content

Commit

Permalink
feat: storage calendar height
Browse files Browse the repository at this point in the history
  • Loading branch information
jsun969 committed Oct 3, 2024
1 parent ab367e3 commit bb83465
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 24 deletions.
7 changes: 2 additions & 5 deletions src/components/Calendar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@ import { create } from 'zustand';
import { WEEK_DAYS } from '../constants/week-days';
import { YEAR } from '../constants/year';
import { useCourseColor, useEnrolledCourse } from '../data/enrolled-courses';
import {
useCalendar,
useCalendarHourHeight,
useOtherWeekCourseTimes,
} from '../helpers/calendar';
import { useCalendar, useOtherWeekCourseTimes } from '../helpers/calendar';
import { useCalendarHourHeight } from '../helpers/calendar-hour-height';
import { calcHoursDuration } from '../helpers/hours-duration';
import type dayjs from '../lib/dayjs';
import type { DateTimeRange, WeekCourse, WeekCourses } from '../types/course';
Expand Down
2 changes: 1 addition & 1 deletion src/components/ZoomButtons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
MAX_HOUR_HEIGHT,
MIN_HOUR_HEIGHT,
useCalendarHourHeight,
} from '../helpers/calendar';
} from '../helpers/calendar-hour-height';

export const ZoomButtons = () => {
const { t } = useTranslation();
Expand Down
1 change: 1 addition & 0 deletions src/constants/local-storage-keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ export const enum LocalStorageKey {
Term = 'MTT.term',
EnrolledCourses = 'MTT.enrolled-courses',
FirstTime = 'MTT.first-time',
CalendarHeight = 'MTT.calendar-height',
}
28 changes: 28 additions & 0 deletions src/helpers/calendar-hour-height.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { create } from 'zustand';
import { persist } from 'zustand/middleware';

import { LocalStorageKey } from '../constants/local-storage-keys';

export const MIN_HOUR_HEIGHT = 3;
export const MAX_HOUR_HEIGHT = 10;
export const useCalendarHourHeight = create<{
height: number;
setHeight: (getNewHeight: (height: number) => number) => void;
}>()(
persist(
(set) => ({
height: 4.5,
setHeight: (getNewHeight) =>
set((state) => {
const height = getNewHeight(state.height);
return {
height: Math.min(
Math.max(height, MIN_HOUR_HEIGHT),
MAX_HOUR_HEIGHT,
),
};
}),
}),
{ name: LocalStorageKey.CalendarHeight },
),
);
17 changes: 0 additions & 17 deletions src/helpers/calendar.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { useEffect, useState } from 'react';
import { create } from 'zustand';

import { WEEK_DAYS } from '../constants/week-days';
import { useGetCourseClasses } from '../data/course-info';
Expand Down Expand Up @@ -220,19 +219,3 @@ export const useOtherWeekCourseTimes = ({

return times;
};

export const MIN_HOUR_HEIGHT = 3;
export const MAX_HOUR_HEIGHT = 10;
export const useCalendarHourHeight = create<{
height: number;
setHeight: (getNewHeight: (height: number) => number) => void;
}>()((set) => ({
height: 4.5,
setHeight: (getNewHeight) =>
set((state) => {
const height = getNewHeight(state.height);
return {
height: Math.min(Math.max(height, MIN_HOUR_HEIGHT), MAX_HOUR_HEIGHT),
};
}),
}));
2 changes: 1 addition & 1 deletion src/helpers/zoom.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useCallback, useEffect, useRef } from 'react';

import { useCalendarHourHeight } from './calendar';
import { useCalendarHourHeight } from './calendar-hour-height';

type UseZoomProps = {
element: HTMLElement;
Expand Down

0 comments on commit bb83465

Please sign in to comment.