| 1 | 'use client' |
| 2 | |
| 3 | import { useRouter } from '@bprogress/next/app' |
| 4 | import { |
| 5 | addMonths, |
| 6 | eachDayOfInterval, |
| 7 | endOfMonth, |
| 8 | format, |
| 9 | getDay, |
| 10 | isSameDay, |
| 11 | isSameMonth, |
| 12 | isToday, |
| 13 | startOfMonth, |
| 14 | startOfWeek, |
| 15 | } from 'date-fns' |
| 16 | import { |
| 17 | ChevronLeft, |
| 18 | ChevronRight, |
| 19 | DollarSign, |
| 20 | Pencil, |
| 21 | Send, |
| 22 | Trash2, |
| 23 | } from 'lucide-react' |
| 24 | import { useParams } |
| 25 | import { useAction } |
| 26 | import { useMemo, useState |
| 27 | import { toast } |
| 28 | import { Button } |
| 29 | import { Card, CardContent |
| 30 | import { Checkbox } |
| 31 | import { |
| 32 | Dialog, |
| 33 | DialogContent, |
| 34 | DialogHeader, |
| 35 | DialogTitle, |
| 36 | } from '@/components/ui/dialog' |
| 37 | import { |
| 38 | Table, |
| 39 | TableBody, |
| 40 | TableCell, |
| 41 | TableHead, |
| 42 | TableHeader, |
| 43 | TableRow, |
| 44 | } from '@/components/ui/table' |
| 45 | import { |
| 46 | Tooltip, |
| 47 | TooltipContent, |
| 48 | TooltipTrigger, |
| 49 | } from '@/components/ui/tooltip' |
| 50 | import { useSetSelection } |
| 51 | import type { CustomFieldDefinition |
| 52 | import { cn } |
| 53 | import { deleteTimeEntryAction, submitTimesheetAction |
| 54 | import { canDeleteTimeEntry, canEditTimeEntry |
| 55 | import type { Requirement, |
| 56 | import { CustomValuesInline } |
| 57 | import { exportTimeEntries } |
| 58 | import { StatusBadgeWithReason } |
| 59 | import { TimeEntryForm } |
| 60 | |
| 61 | interface MonthlyTimesheetProps { |
| 62 | currentMemberId: string |
| 63 | customFields?: CustomFieldDefinition |
| 64 | entries: TimeEntry |
| 65 | isAdmin?: boolean |
| 66 | isClientInvolved?: boolean |
| 67 | isTeamView?: boolean |
| 68 | onAddEntry?: (day |
| 69 | orgSlug?: string |
| 70 | projectId: string |
| 71 | projectSlug?: string |
| 72 | requirements: Requirement |
| 73 | } |
| 74 | |
| 75 | const WEEKDAY_HEADERS = [ |
| 76 | |
| 77 | export function MonthlyTimesheet({ |
| 78 | entries, |
| 79 | requirements, |
| 80 | projectId, |
| 81 | currentMemberId, |
| 82 | isAdmin = false, |
| 83 | isTeamView = false, |
| 84 | isClientInvolved, |
| 85 | onAddEntry, |
| 86 | customFields = [], |
| 87 | orgSlug, |
| 88 | projectSlug, |
| 89 | }: MonthlyTimesheetProps) |
| 90 | const [monthOffset, |
| 91 | const { selectedIds, |
| 92 | const [selectedDay, |
| 93 | const [editEntry, |
| 94 | const params = |
| 95 | const router = |
| 96 | const hasCustomFields = |
| 97 | const editHrefBase = |
| 98 | orgSlug && projectSlug |
| 99 | const onEditClick = |
| 100 | if (hasCustomFields && |
| 101 | router.push(`${ |
| 102 | return |
| 103 | } |
| 104 | setEditEntry(entry) |
| 105 | } |
| 106 | |
| 107 | const { currentMonth, |
| 108 | const month = |
| 109 | const start = |
| 110 | const end = |
| 111 | const calendarStart = |
| 112 | const lastDayOfWeek = |
| 113 | const calendarEnd = |
| 114 | if (lastDayOfWeek !== |
| 115 | calendarEnd.setDate(calendarEnd |
| 116 | } |
| 117 | const days = |
| 118 | const grid: |
| 119 | for (let i |
| 120 | grid.push(days |
| 121 | } |
| 122 | return { |
| 123 | currentMonth: month, |
| 124 | monthStart: start, |
| 125 | monthEnd: end, |
| 126 | weeks: grid, |
| 127 | } |
| 128 | }, [monthOffset]) |
| 129 | |
| 130 | const monthEntries = |
| 131 | () => |
| 132 | entries.filter((e |
| 133 | const d = |
| 134 | return d >= |
| 135 | }), |
| 136 | [entries, monthStart, |
| 137 | ) |
| 138 | |
| 139 | const submittableEntries = |
| 140 | () => |
| 141 | monthEntries.filter( |
| 142 | (e) => |
| 143 | ), |
| 144 | [monthEntries, currentMemberId] |
| 145 | ) |
| 146 | |
| 147 | const monthTotal = |
| 148 | () => monthEntries. |
| 149 | [monthEntries] |
| 150 | ) |
| 151 | |
| 152 | const submitAction = |
| 153 | onSuccess: () => |
| 154 | toast.success('Timesheet submitted for approval |
| 155 | clear() |
| 156 | router.refresh() |
| 157 | }, |
| 158 | onError: ({ error |
| 159 | toast.error(error |
| 160 | }) |
| 161 | |
| 162 | const deleteAction = |
| 163 | onSuccess: () => |
| 164 | toast.success('Time entry deleted |
| 165 | router.refresh() |
| 166 | }, |
| 167 | onError: ({ error |
| 168 | toast.error(error |
| 169 | }) |
| 170 | |
| 171 | function handleSubmit() { |
| 172 | if (selectedIds.size |
| 173 | toast.error('Select at least one draft entry to submit |
| 174 | return |
| 175 | } |
| 176 | submitAction.execute({ |
| 177 | } |
| 178 | |
| 179 | function getEntriesForDay(day |
| 180 | return monthEntries.filter |
| 181 | } |
| 182 | |
| 183 | function getDayTotal(day |
| 184 | return getEntriesForDay(day |
| 185 | } |
| 186 | |
| 187 | const showActions = |
| 188 | const showCheckboxes = |
| 189 | |
| 190 | const dayEntries = |
| 191 | |
| 192 | return ( |
| 193 | <div className='space-y-4 |
| 194 | <Card> |
| 195 | <CardHeader className=' |
| 196 | <div className='flex items-center justify-between |
| 197 | <div className='flex items-center gap-2 |
| 198 | <Button |
| 199 | className='size-8' |
| 200 | onClick={() => |
| 201 | size='icon' |
| 202 | variant='outline' |
| 203 | > |
| 204 | <ChevronLeft className=' |
| 205 | </Button> |
| 206 | <CardTitle className=' |
| 207 | {format(currentMonth, |
| 208 | </CardTitle> |
| 209 | <Button |
| 210 | className='size-8' |
| 211 | onClick={() => |
| 212 | size='icon' |
| 213 | variant='outline' |
| 214 | > |
| 215 | <ChevronRight className=' |
| 216 | </Button> |
| 217 | {monthOffset !== 0 |
| 218 | <Button |
| 219 | className='text-xs' |
| 220 | onClick={() => |
| 221 | variant='ghost' |
| 222 | > |
| 223 | Today |
| 224 | </Button> |
| 225 | )} |
| 226 | </div> |
| 227 | <div className='flex items-center gap-3 |
| 228 | <span className='text-muted-foreground text-sm |
| 229 | Total:{' '} |
| 230 | <span className='font-medium text-foreground |
| 231 | {formatMinutes(monthTotal)} |
| 232 | </span> |
| 233 | </span> |
| 234 | <Button |
| 235 | disabled={monthEntries.length |
| 236 | kbd='e' |
| 237 | onClick={() => |
| 238 | exportTimeEntries( |
| 239 | monthEntries, |
| 240 | `time-entries-${format(currentMonth |
| 241 | ) |
| 242 | } |
| 243 | variant='secondary' |
| 244 | > |
| 245 | Export |
| 246 | </Button> |
| 247 | {!isTeamView && submittableEntries |
| 248 | <Button |
| 249 | disabled={selectedIds.size |
| 250 | loading={submitAction.isPending |
| 251 | onClick={handleSubmit} |
| 252 | > |
| 253 | <Send className=' |
| 254 | Submit Selected |
| 255 | </Button> |
| 256 | )} |
| 257 | </div> |
| 258 | </div> |
| 259 | </CardHeader> |
| 260 | <CardContent className=' |
| 261 | {/* Calendar grid */} |
| 262 | <div className='grid grid-cols-7 |
| 263 | {WEEKDAY_HEADERS.map(( |
| 264 | <div |
| 265 | className='border-b px-2 py-1.5 text-center font-medium text-muted-foreground text-xs' |
| 266 | key={day} |
| 267 | > |
| 268 | {day} |
| 269 | </div> |
| 270 | ))} |
| 271 | </div> |
| 272 | {weeks.map(( |
| 273 | <div className='grid grid-cols-7 |
| 274 | {week.map(( |
| 275 | const dayTotal = |
| 276 | const dayEntriesCount = |
| 277 | const inMonth = |
| 278 | const today = |
| 279 | |
| 280 | return ( |
| 281 | <button |
| 282 | className={cn( |
| 283 | 'flex min-h-20 flex-col items-start border-r border-b p-2 text-left transition-colors hover:bg-muted/50', |
| 284 | !inMonth && 'bg-muted/20 text-muted-foreground/50 |
| 285 | today && 'bg-primary/5' |
| 286 | )} |
| 287 | key={day.toISOString |
| 288 | onClick={() => |
| 289 | if (dayEntriesCount > 0 |
| 290 | setSelectedDay(day) |
| 291 | } else if (onAddEntry |
| 292 | onAddEntry(day) |
| 293 | } |
| 294 | }} |
| 295 | type='button' |
| 296 | > |
| 297 | <span |
| 298 | className={cn( |
| 299 | 'text-sm', |
| 300 | today && |
| 301 | 'flex size-6 items-center justify-center rounded-full bg-primary font-medium text-primary-foreground' |
| 302 | )} |
| 303 | > |
| 304 | {format(day, |
| 305 | </span> |
| 306 | {dayTotal > 0 |
| 307 | <div className='mt-1 space-y-0.5 |
| 308 | <span className='rounded bg-primary/10 px-1 py-0.5 font-medium text-primary text-xs |
| 309 | {formatMinutes(dayTotal)} |
| 310 | </span> |
| 311 | <p className='text-muted-foreground text-xs |
| 312 | {dayEntriesCount}{' '} |
| 313 | {dayEntriesCount === 1 |
| 314 | </p> |
| 315 | </div> |
| 316 | )} |
| 317 | </button> |
| 318 | ) |
| 319 | })} |
| 320 | </div> |
| 321 | ))} |
| 322 | </CardContent> |
| 323 | </Card> |
| 324 | |
| 325 | <Dialog |
| 326 | onOpenChange={(open) |
| 327 | if (!open) { |
| 328 | setSelectedDay(null) |
| 329 | } |
| 330 | }} |
| 331 | open={!!selectedDay} |
| 332 | > |
| 333 | <DialogContent className=' |
| 334 | <DialogHeader> |
| 335 | <div className='flex items-center justify-between |
| 336 | <DialogTitle> |
| 337 | {selectedDay && format |
| 338 | </DialogTitle> |
| 339 | {onAddEntry && ( |
| 340 | <Button |
| 341 | className='mr-2' |
| 342 | onClick={() => |
| 343 | const day = |
| 344 | setSelectedDay(null) |
| 345 | onAddEntry(day ?? |
| 346 | }} |
| 347 | > |
| 348 | Log Time |
| 349 | </Button> |
| 350 | )} |
| 351 | </div> |
| 352 | </DialogHeader> |
| 353 | <div className='overflow-x-auto |
| 354 | <Table> |
| 355 | <TableHeader> |
| 356 | <TableRow> |
| 357 | {showCheckboxes && <TableHead |
| 358 | {isTeamView && <TableHead |
| 359 | <TableHead>Description</TableHead |
| 360 | <TableHead>Requirement</TableHead |
| 361 | <TableHead className=' |
| 362 | <TableHead className=' |
| 363 | {showActions && <TableHead |
| 364 | </TableRow> |
| 365 | </TableHeader> |
| 366 | <TableBody> |
| 367 | {dayEntries.map(( |
| 368 | const isDraft = |
| 369 | entry.status === |
| 370 | entry.memberId === |
| 371 | |
| 372 | return ( |
| 373 | <TableRow key={ |
| 374 | {showCheckboxes && ( |
| 375 | <TableCell> |
| 376 | {isDraft && ( |
| 377 | <Checkbox |
| 378 | checked={selectedIds.has |
| 379 | onCheckedChange={() => |
| 380 | /> |
| 381 | )} |
| 382 | </TableCell> |
| 383 | )} |
| 384 | {isTeamView && ( |
| 385 | <TableCell className=' |
| 386 | {entry.memberName ?? |
| 387 | </TableCell> |
| 388 | )} |
| 389 | <TableCell> |
| 390 | <div className='flex items-center gap-1 |
| 391 | <span className='text-sm |
| 392 | {entry.billable && |
| 393 | <Tooltip> |
| 394 | <TooltipTrigger> |
| 395 | <DollarSign className=' |
| 396 | </TooltipTrigger> |
| 397 | <TooltipContent>Billable</TooltipContent |
| 398 | </Tooltip> |
| 399 | )} |
| 400 | </div> |
| 401 | <CustomValuesInline |
| 402 | customFields={customFields} |
| 403 | values={entry.customValues |
| 404 | /> |
| 405 | </TableCell> |
| 406 | <TableCell className=' |
| 407 | <a |
| 408 | className='hover:underline' |
| 409 | href={`/${params |
| 410 | rel='noopener' |
| 411 | target='_blank' |
| 412 | > |
| 413 | {entry.requirementTitle ?? |
| 414 | </a> |
| 415 | </TableCell> |
| 416 | <TableCell className=' |
| 417 | {formatMinutes(entry.durationMinutes |
| 418 | </TableCell> |
| 419 | <TableCell className=' |
| 420 | <StatusBadgeWithReason |
| 421 | entry={entry} |
| 422 | isClientInvolved={isClientInvolved} |
| 423 | /> |
| 424 | </TableCell> |
| 425 | {showActions && ( |
| 426 | <TableCell> |
| 427 | <div className='flex items-center gap-1 |
| 428 | {canEditTimeEntry( |
| 429 | entry, |
| 430 | currentMemberId, |
| 431 | isAdmin |
| 432 | ) && ( |
| 433 | <Button |
| 434 | className='size-7' |
| 435 | onClick={() => |
| 436 | setSelectedDay(null) |
| 437 | onEditClick(entry) |
| 438 | }} |
| 439 | size='icon' |
| 440 | variant='ghost' |
| 441 | > |
| 442 | <Pencil className=' |
| 443 | </Button> |
| 444 | )} |
| 445 | {canDeleteTimeEntry( |
| 446 | entry, |
| 447 | currentMemberId, |
| 448 | isAdmin |
| 449 | ) && ( |
| 450 | <Button |
| 451 | className='size-7 text-destructive' |
| 452 | onClick={() => |
| 453 | deleteAction.execute({ |
| 454 | timeEntryId: entry.id |
| 455 | }) |
| 456 | } |
| 457 | size='icon' |
| 458 | variant='ghost' |
| 459 | > |
| 460 | <Trash2 className=' |
| 461 | </Button> |
| 462 | )} |
| 463 | </div> |
| 464 | </TableCell> |
| 465 | )} |
| 466 | </TableRow> |
| 467 | ) |
| 468 | })} |
| 469 | </TableBody> |
| 470 | </Table> |
| 471 | </div> |
| 472 | </DialogContent> |
| 473 | </Dialog> |
| 474 | |
| 475 | {editEntry && ( |
| 476 | <TimeEntryForm |
| 477 | customFields={customFields} |
| 478 | editEntry={editEntry} |
| 479 | onOpenChange={(open) |
| 480 | if (!open) { |
| 481 | setEditEntry(null) |
| 482 | } |
| 483 | }} |
| 484 | open={!!editEntry} |
| 485 | projectId={projectId} |
| 486 | requirements={requirements} |
| 487 | /> |
| 488 | )} |
| 489 | </div> |
| 490 | ) |
| 491 | } |