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