| 1 | 'use client' |
| 2 | |
| 3 | import { useRouter } from '@bprogress/next/app' |
| 4 | import { Clock, DollarSign |
| 5 | import { useParams } |
| 6 | import { useAction } |
| 7 | import { useMemo, useState |
| 8 | import { toast } |
| 9 | import { Button } |
| 10 | import { Card, CardContent |
| 11 | import { Checkbox } |
| 12 | import { |
| 13 | Empty, |
| 14 | EmptyDescription, |
| 15 | EmptyHeader, |
| 16 | EmptyMedia, |
| 17 | EmptyTitle, |
| 18 | } from '@/components/ui/empty' |
| 19 | import { |
| 20 | Select, |
| 21 | SelectContent, |
| 22 | SelectItem, |
| 23 | SelectTrigger, |
| 24 | SelectValue, |
| 25 | } from '@/components/ui/select' |
| 26 | import { |
| 27 | Table, |
| 28 | TableBody, |
| 29 | TableCell, |
| 30 | TableHead, |
| 31 | TableHeader, |
| 32 | TableRow, |
| 33 | } from '@/components/ui/table' |
| 34 | import { |
| 35 | Tooltip, |
| 36 | TooltipContent, |
| 37 | TooltipTrigger, |
| 38 | } from '@/components/ui/tooltip' |
| 39 | import type { CustomFieldDefinition |
| 40 | import { deleteTimeEntryAction } |
| 41 | import { formatMinutes, formatShortDate |
| 42 | import type { ProjectMember, |
| 43 | import { CustomValuesInline } |
| 44 | import { StatusBadgeWithReason } |
| 45 | import { TimeEntryForm } |
| 46 | |
| 47 | interface TeamEntriesTableProps { |
| 48 | currentMemberId: string |
| 49 | customFields?: CustomFieldDefinition |
| 50 | entries: TimeEntry |
| 51 | isClientInvolved?: boolean |
| 52 | onSelectionChange?: (ids |
| 53 | orgSlug?: string |
| 54 | projectId: string |
| 55 | projectMembers: ProjectMember |
| 56 | projectSlug?: string |
| 57 | requirements: Requirement |
| 58 | selectedIds?: Set |
| 59 | } |
| 60 | |
| 61 | export function TeamEntriesTable({ |
| 62 | entries, |
| 63 | projectMembers, |
| 64 | requirements, |
| 65 | projectId, |
| 66 | selectedIds, |
| 67 | onSelectionChange, |
| 68 | isClientInvolved, |
| 69 | customFields = [], |
| 70 | orgSlug, |
| 71 | projectSlug, |
| 72 | }: TeamEntriesTableProps) |
| 73 | const selectable = |
| 74 | const params = |
| 75 | const router = |
| 76 | const [filterMember, |
| 77 | const [filterStatus, |
| 78 | const [filterRequirement, |
| 79 | const [editEntry, |
| 80 | const hasCustomFields = |
| 81 | const editHrefBase = |
| 82 | orgSlug && projectSlug |
| 83 | const onEditClick = |
| 84 | if (hasCustomFields && |
| 85 | router.push(`${ |
| 86 | return |
| 87 | } |
| 88 | setEditEntry(entry) |
| 89 | } |
| 90 | |
| 91 | const deleteAction = |
| 92 | onSuccess: () => |
| 93 | onError: ({ error |
| 94 | toast.error(error |
| 95 | }) |
| 96 | |
| 97 | const filtered = |
| 98 | () => |
| 99 | entries.filter((e |
| 100 | if (filterMember !== |
| 101 | return false |
| 102 | } |
| 103 | if (filterStatus !== |
| 104 | return false |
| 105 | } |
| 106 | if (filterRequirement !== |
| 107 | if (filterRequirement === |
| 108 | return false |
| 109 | } |
| 110 | if ( |
| 111 | filterRequirement !== 'general' |
| 112 | e.requirementId !== |
| 113 | ) { |
| 114 | return false |
| 115 | } |
| 116 | } |
| 117 | return true |
| 118 | }), |
| 119 | [entries, filterMember, |
| 120 | ) |
| 121 | |
| 122 | const totalMinutes = |
| 123 | () => filtered. |
| 124 | [filtered] |
| 125 | ) |
| 126 | |
| 127 | const hasActiveFilters = |
| 128 | filterMember !== 'all' |
| 129 | filterStatus !== 'all' |
| 130 | filterRequirement !== 'all' |
| 131 | |
| 132 | const selectableEntries = |
| 133 | () => filtered. |
| 134 | [filtered] |
| 135 | ) |
| 136 | |
| 137 | const toggleEntry = |
| 138 | if (!(onSelectionChange |
| 139 | return |
| 140 | } |
| 141 | const next = |
| 142 | if (next.has |
| 143 | next.delete(id |
| 144 | } else { |
| 145 | next.add(id |
| 146 | } |
| 147 | onSelectionChange(next) |
| 148 | } |
| 149 | |
| 150 | const toggleAll = |
| 151 | if (!(onSelectionChange |
| 152 | return |
| 153 | } |
| 154 | if (selectableEntries.every |
| 155 | const next = |
| 156 | for (const e |
| 157 | next.delete(e |
| 158 | } |
| 159 | onSelectionChange(next) |
| 160 | } else { |
| 161 | const next = |
| 162 | for (const e |
| 163 | next.add(e |
| 164 | } |
| 165 | onSelectionChange(next) |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | return ( |
| 170 | <div className='space-y-4 |
| 171 | <div className='flex flex-wrap items-center gap-2 |
| 172 | <Filter className=' |
| 173 | <Select onValueChange={ |
| 174 | <SelectTrigger className=' |
| 175 | <SelectValue placeholder=' |
| 176 | </SelectTrigger> |
| 177 | <SelectContent> |
| 178 | <SelectItem value=' |
| 179 | {projectMembers.map(( |
| 180 | <SelectItem key={ |
| 181 | {m.name ?? |
| 182 | </SelectItem> |
| 183 | ))} |
| 184 | </SelectContent> |
| 185 | </Select> |
| 186 | <Select onValueChange={ |
| 187 | <SelectTrigger className=' |
| 188 | <SelectValue placeholder=' |
| 189 | </SelectTrigger> |
| 190 | <SelectContent> |
| 191 | <SelectItem value=' |
| 192 | <SelectItem value=' |
| 193 | <SelectItem value=' |
| 194 | <SelectItem value=' |
| 195 | <SelectItem value=' |
| 196 | </SelectContent> |
| 197 | </Select> |
| 198 | <Select onValueChange={ |
| 199 | <SelectTrigger className=' |
| 200 | <SelectValue placeholder=' |
| 201 | </SelectTrigger> |
| 202 | <SelectContent> |
| 203 | <SelectItem value=' |
| 204 | <SelectItem value=' |
| 205 | {requirements.map(( |
| 206 | <SelectItem key={ |
| 207 | {r.title} |
| 208 | </SelectItem> |
| 209 | ))} |
| 210 | </SelectContent> |
| 211 | </Select> |
| 212 | {hasActiveFilters && ( |
| 213 | <Button |
| 214 | className='h-8 text-xs' |
| 215 | onClick={() => |
| 216 | setFilterMember('all') |
| 217 | setFilterStatus('all') |
| 218 | setFilterRequirement('all') |
| 219 | }} |
| 220 | variant='ghost' |
| 221 | > |
| 222 | Clear filters |
| 223 | </Button> |
| 224 | )} |
| 225 | <span className='ml-auto text-muted-foreground text-sm |
| 226 | {filtered.length} { |
| 227 | {formatMinutes(totalMinutes)} |
| 228 | </span> |
| 229 | </div> |
| 230 | |
| 231 | {filtered.length === |
| 232 | <Empty> |
| 233 | <EmptyHeader> |
| 234 | <EmptyMedia variant=' |
| 235 | <Clock /> |
| 236 | </EmptyMedia> |
| 237 | <EmptyTitle>No time entries found</EmptyTitle |
| 238 | <EmptyDescription> |
| 239 | {hasActiveFilters |
| 240 | ? 'No time entries match the selected filters.' |
| 241 | : 'No time entries have been logged yet.'} |
| 242 | </EmptyDescription> |
| 243 | </EmptyHeader> |
| 244 | </Empty> |
| 245 | ) : ( |
| 246 | <Card> |
| 247 | <CardContent className=' |
| 248 | <div className='overflow-x-auto |
| 249 | <Table> |
| 250 | <TableHeader> |
| 251 | <TableRow> |
| 252 | {selectable && ( |
| 253 | <TableHead className=' |
| 254 | <Checkbox |
| 255 | checked={ |
| 256 | selectableEntries.length > 0 |
| 257 | selectableEntries.every((e |
| 258 | selectedIds?.has(e |
| 259 | ) |
| 260 | } |
| 261 | disabled={selectableEntries.length |
| 262 | onCheckedChange={toggleAll} |
| 263 | /> |
| 264 | </TableHead> |
| 265 | )} |
| 266 | <TableHead>Member</TableHead |
| 267 | <TableHead>Description</TableHead |
| 268 | <TableHead>Requirement</TableHead |
| 269 | <TableHead className=' |
| 270 | <TableHead className=' |
| 271 | Duration |
| 272 | </TableHead> |
| 273 | <TableHead className=' |
| 274 | Status |
| 275 | </TableHead> |
| 276 | <TableHead className=' |
| 277 | </TableRow> |
| 278 | </TableHeader> |
| 279 | <TableBody> |
| 280 | {filtered.map(( |
| 281 | <TableRow key={ |
| 282 | {selectable && ( |
| 283 | <TableCell> |
| 284 | {entry.status === |
| 285 | !entry.invoiceId ? |
| 286 | <Checkbox |
| 287 | checked={selectedIds?.has |
| 288 | onCheckedChange={() => |
| 289 | /> |
| 290 | ) : ( |
| 291 | <span /> |
| 292 | )} |
| 293 | </TableCell> |
| 294 | )} |
| 295 | <TableCell className=' |
| 296 | {entry.memberName ?? |
| 297 | </TableCell> |
| 298 | <TableCell> |
| 299 | <div className='flex items-center gap-1 |
| 300 | <span className='line-clamp-1 max-w-52 text-sm |
| 301 | {entry.description} |
| 302 | </span> |
| 303 | {entry.billable && |
| 304 | <Tooltip> |
| 305 | <TooltipTrigger> |
| 306 | <DollarSign className=' |
| 307 | </TooltipTrigger> |
| 308 | <TooltipContent>Billable</TooltipContent |
| 309 | </Tooltip> |
| 310 | )} |
| 311 | </div> |
| 312 | <CustomValuesInline |
| 313 | customFields={customFields} |
| 314 | values={entry.customValues |
| 315 | /> |
| 316 | </TableCell> |
| 317 | <TableCell className=' |
| 318 | <span className='line-clamp-1 max-w-36 |
| 319 | {entry.requirementTitle ? |
| 320 | <a |
| 321 | className='hover:underline' |
| 322 | href={`/${params |
| 323 | rel='noopener' |
| 324 | target='_blank' |
| 325 | > |
| 326 | {entry.requirementTitle} |
| 327 | </a> |
| 328 | ) : ( |
| 329 | '—' |
| 330 | )} |
| 331 | </span> |
| 332 | </TableCell> |
| 333 | <TableCell |
| 334 | className='whitespace-nowrap text-sm' |
| 335 | suppressHydrationWarning |
| 336 | > |
| 337 | {formatShortDate(entry.date |
| 338 | </TableCell> |
| 339 | <TableCell className=' |
| 340 | {formatMinutes(entry.durationMinutes |
| 341 | </TableCell> |
| 342 | <TableCell className=' |
| 343 | <StatusBadgeWithReason |
| 344 | entry={entry} |
| 345 | isClientInvolved={isClientInvolved} |
| 346 | /> |
| 347 | </TableCell> |
| 348 | <TableCell> |
| 349 | <div className='flex items-center gap-1 |
| 350 | <Button |
| 351 | className='size-7' |
| 352 | onClick={() => |
| 353 | size='icon' |
| 354 | variant='ghost' |
| 355 | > |
| 356 | <Pencil className=' |
| 357 | </Button> |
| 358 | <Button |
| 359 | className='size-7 text-destructive' |
| 360 | onClick={() => |
| 361 | deleteAction.execute({ |
| 362 | timeEntryId: entry.id |
| 363 | }) |
| 364 | } |
| 365 | size='icon' |
| 366 | variant='ghost' |
| 367 | > |
| 368 | <Trash2 className=' |
| 369 | </Button> |
| 370 | </div> |
| 371 | </TableCell> |
| 372 | </TableRow> |
| 373 | ))} |
| 374 | </TableBody> |
| 375 | </Table> |
| 376 | </div> |
| 377 | </CardContent> |
| 378 | </Card> |
| 379 | )} |
| 380 | |
| 381 | {editEntry && ( |
| 382 | <TimeEntryForm |
| 383 | customFields={customFields} |
| 384 | editEntry={editEntry} |
| 385 | onOpenChange={(open) |
| 386 | if (!open) { |
| 387 | setEditEntry(null) |
| 388 | } |
| 389 | }} |
| 390 | open={!!editEntry} |
| 391 | projectId={projectId} |
| 392 | requirements={requirements} |
| 393 | /> |
| 394 | )} |
| 395 | </div> |
| 396 | ) |
| 397 | } |