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