import { CalendarMonth } from '@mui/icons-material'
import { Avatar, Box, Chip, Grid, Typography } from '@mui/joy'
import moment from 'moment'
import { useState } from 'react'
import Calendar from 'react-calendar'
import 'react-calendar/dist/Calendar.css'
import { useNavigate } from 'react-router-dom'
import { useCircleMembers, useUserProfile } from '../../queries/UserQueries'
import { TASK_COLOR } from '../../utils/Colors'
import './Calendar.css'
const getAssigneeColor = (assignee, userProfile) => {
return assignee === userProfile.id
? TASK_COLOR.ASSIGNED_TO_ME
: TASK_COLOR.ASSIGNED_TO_OTHER
}
const CalendarView = ({ chores }) => {
const { data: userProfile } = useUserProfile()
const [selectedDate, setSeletedDate] = useState(null)
const Navigate = useNavigate()
// Fetch circle members data to get assignee names
const { data: circleMembersData } = useCircleMembers()
const circleMembers = circleMembersData?.res || []
// Helper function to get assignee display name
const getAssigneeName = assignedTo => {
if (assignedTo === userProfile.id) {
return userProfile.displayName
}
const assignee = circleMembers.find(member => member.userId === assignedTo)
return assignee ? `${assignee.displayName}` : 'Assigned to other'
}
const tileContent = ({ date, view }) => {
if (view === 'month') {
const dayChores = chores.filter(chore => {
const choreDate = new Date(chore.nextDueDate).toLocaleDateString()
const tileDate = date.toLocaleDateString()
return choreDate === tileDate
})
if (dayChores.length === 0) {
return (
)
}
if (dayChores.length > 3) {
return (
)
}
return (
{dayChores.map((chore, index) => {
return (
)
})}
)
}
return null
}
return (
{/* Calendar Header */}
Calendar Overview
{
setSeletedDate(new Date(d))
}}
// format the days from MON, TUE, WED, THU, FRI, SAT, SUN to first three letters:
formatShortWeekday={(locale, date) =>
['S', 'M', 'T', 'W', 'T', 'F', 'S'][date.getDay()]
}
/>
{!selectedDate && (
{/* Show legend with current user first, then other circle members who have assignments */}
{(() => {
const assignedUserIds = new Set(
chores.map(chore => chore.assignedTo).filter(Boolean),
)
const legendItems = []
// Add current user if they have assignments
if (assignedUserIds.has(userProfile.id)) {
legendItems.push({
name: 'Assigned to me',
color: TASK_COLOR.ASSIGNED_TO_ME,
})
}
// Add other circle members who have assignments
circleMembers.forEach(member => {
if (
member.userId !== userProfile.id &&
assignedUserIds.has(member.userId)
) {
legendItems.push({
name: `Assigned to others`,
color: TASK_COLOR.ASSIGNED_TO_OTHER,
})
}
})
return legendItems.map((item, index) => (
{item.name}
))
})()}
)}
{selectedDate && (
{moment(selectedDate).format('MMMM D, YYYY')}
{(() => {
const count = chores.filter(chore => {
const choreDate = new Date(
chore.nextDueDate,
).toLocaleDateString()
const selectedLocalDate = selectedDate.toLocaleDateString()
return choreDate === selectedLocalDate
}).length
return `${count} Tasks`
})()}
{chores
.filter(chore => {
const choreDate = new Date(
chore.nextDueDate,
).toLocaleDateString()
const selectedLocalDate = selectedDate.toLocaleDateString()
return choreDate === selectedLocalDate
})
.sort((a, b) => moment(a.nextDueDate).diff(moment(b.nextDueDate)))
.map((chore, idx) => (
{
Navigate('/chores/' + chore.id)
}}
sx={{
display: 'flex',
flexDirection: 'column',
gap: 0.25,
cursor: 'pointer',
position: 'relative',
pl: '16px',
py: 0.75,
transition: 'all 0.2s ease-in-out',
borderRadius: 'sm',
'&:hover': {
// transform: 'translateX(4px)',
bgcolor: 'background.level1',
},
'&::before': {
content: '""',
position: 'absolute',
left: 0,
top: 0,
bottom: 0,
width: '3px',
backgroundColor: getAssigneeColor(
chore.assignedTo,
userProfile,
),
borderRadius: '2px',
},
}}
>
{chore.name}
{moment(chore.nextDueDate).format('h:mm A')}
{/*
{getAssigneeName(chore.assignedTo)}
*/}
member.userId === chore.assignedTo,
)?.image
}
/>
}
sx={{
backgroundColor: getAssigneeColor(
chore.assignedTo,
userProfile,
),
color: 'white',
}}
>
{getAssigneeName(chore.assignedTo)}
))}
)}
)
}
export default CalendarView