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