Skip to content

Commit 4c56bbb

Browse files
Copilothuangyiirene
andcommitted
Fix code review issues in calendar-view
- Remove unused CalendarIcon import - Extract default event color to constant - Fix date mutation issue in getEventsForDate function - Improve date comparison logic Co-authored-by: huangyiirene <7665279+huangyiirene@users.noreply.github.com>
1 parent 96fbc8e commit 4c56bbb

1 file changed

Lines changed: 19 additions & 7 deletions

File tree

packages/components/src/ui/calendar-view.tsx

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"use client"
22

33
import * as React from "react"
4-
import { ChevronLeftIcon, ChevronRightIcon, CalendarIcon } from "lucide-react"
4+
import { ChevronLeftIcon, ChevronRightIcon } from "lucide-react"
55
import { cn } from "@/lib/utils"
66
import { Button } from "@/ui/button"
77
import {
@@ -12,6 +12,8 @@ import {
1212
SelectValue,
1313
} from "@/ui/select"
1414

15+
const DEFAULT_EVENT_COLOR = "bg-blue-500 text-white"
16+
1517
export interface CalendarEvent {
1618
id: string | number
1719
title: string
@@ -232,10 +234,20 @@ function isSameDay(date1: Date, date2: Date): boolean {
232234
function getEventsForDate(date: Date, events: CalendarEvent[]): CalendarEvent[] {
233235
return events.filter((event) => {
234236
const eventStart = new Date(event.start)
235-
const eventEnd = event.end ? new Date(event.end) : eventStart
237+
const eventEnd = event.end ? new Date(event.end) : new Date(eventStart)
238+
239+
// Create new date objects for comparison to avoid mutation
240+
const dateStart = new Date(date)
241+
dateStart.setHours(0, 0, 0, 0)
242+
const dateEnd = new Date(date)
243+
dateEnd.setHours(23, 59, 59, 999)
244+
245+
const eventStartTime = new Date(eventStart)
246+
eventStartTime.setHours(0, 0, 0, 0)
247+
const eventEndTime = new Date(eventEnd)
248+
eventEndTime.setHours(23, 59, 59, 999)
236249

237-
return date >= new Date(eventStart.setHours(0, 0, 0, 0)) &&
238-
date <= new Date(eventEnd.setHours(23, 59, 59, 999))
250+
return dateStart <= eventEndTime && dateEnd >= eventStartTime
239251
})
240252
}
241253

@@ -296,7 +308,7 @@ function MonthView({ date, events, onEventClick, onDateClick }: MonthViewProps)
296308
key={event.id}
297309
className={cn(
298310
"text-xs px-2 py-1 rounded truncate cursor-pointer hover:opacity-80",
299-
event.color || "bg-blue-500 text-white"
311+
event.color || DEFAULT_EVENT_COLOR
300312
)}
301313
style={
302314
event.color && event.color.startsWith("#")
@@ -385,7 +397,7 @@ function WeekView({ date, events, onEventClick, onDateClick }: WeekViewProps) {
385397
key={event.id}
386398
className={cn(
387399
"text-sm px-3 py-2 rounded cursor-pointer hover:opacity-80",
388-
event.color || "bg-blue-500 text-white"
400+
event.color || DEFAULT_EVENT_COLOR
389401
)}
390402
style={
391403
event.color && event.color.startsWith("#")
@@ -454,7 +466,7 @@ function DayView({ date, events, onEventClick }: DayViewProps) {
454466
key={event.id}
455467
className={cn(
456468
"px-3 py-2 rounded cursor-pointer hover:opacity-80",
457-
event.color || "bg-blue-500 text-white"
469+
event.color || DEFAULT_EVENT_COLOR
458470
)}
459471
style={
460472
event.color && event.color.startsWith("#")

0 commit comments

Comments
 (0)