| 1 | 'use client' |
| 2 | |
| 3 | import { useRouter } from '@bprogress/next/app' |
| 4 | import { CheckCircle2, Paperclip |
| 5 | import { useAction } |
| 6 | import { useId, useState |
| 7 | import { toast } |
| 8 | import { Badge } |
| 9 | import { Button } |
| 10 | import { Card, CardContent |
| 11 | import { Checkbox } |
| 12 | import { |
| 13 | Dialog, |
| 14 | DialogContent, |
| 15 | DialogDescription, |
| 16 | DialogFooter, |
| 17 | DialogHeader, |
| 18 | DialogTitle, |
| 19 | } from '@/components/ui/dialog' |
| 20 | import { |
| 21 | Empty, |
| 22 | EmptyDescription, |
| 23 | EmptyHeader, |
| 24 | EmptyMedia, |
| 25 | EmptyTitle, |
| 26 | } from '@/components/ui/empty' |
| 27 | import { |
| 28 | HoverCard, |
| 29 | HoverCardContent, |
| 30 | HoverCardTrigger, |
| 31 | } from '@/components/ui/hover-card' |
| 32 | import { Label } |
| 33 | import { |
| 34 | Table, |
| 35 | TableBody, |
| 36 | TableCell, |
| 37 | TableHead, |
| 38 | TableHeader, |
| 39 | TableRow, |
| 40 | } from '@/components/ui/table' |
| 41 | import { Textarea } |
| 42 | import { clientRespondExpensesAction } |
| 43 | import { formatCurrency } |
| 44 | import type { ExpenseRecipient, |
| 45 | |
| 46 | interface ClientExpensesViewProps { |
| 47 | currentMemberId: string |
| 48 | expenses: ExpenseWithDetails |
| 49 | recipients: ExpenseRecipient |
| 50 | } |
| 51 | |
| 52 | export function ClientExpensesView({ |
| 53 | currentMemberId, |
| 54 | expenses, |
| 55 | recipients, |
| 56 | }: ClientExpensesViewProps) |
| 57 | const rejectReasonId = |
| 58 | const router = |
| 59 | const [selectedIds, |
| 60 | const [rejectOpen, |
| 61 | const [rejectReason, |
| 62 | |
| 63 | const myRecipientMap = |
| 64 | recipients |
| 65 | .filter((r) |
| 66 | .map((r) |
| 67 | ) |
| 68 | |
| 69 | const sentExpenses = |
| 70 | const recipient = |
| 71 | return e.status |
| 72 | }) |
| 73 | const respondedExpenses = |
| 74 | const recipient = |
| 75 | return ( |
| 76 | recipient && |
| 77 | (recipient.status === |
| 78 | ) |
| 79 | }) |
| 80 | |
| 81 | const respondAction = |
| 82 | onSuccess: ({ input |
| 83 | toast.success( |
| 84 | `Expenses ${input.action |
| 85 | ) |
| 86 | setSelectedIds(new Set |
| 87 | setRejectOpen(false) |
| 88 | setRejectReason('') |
| 89 | router.refresh() |
| 90 | }, |
| 91 | onError: ({ error |
| 92 | toast.error(error |
| 93 | }, |
| 94 | }) |
| 95 | |
| 96 | function toggleSelect(id |
| 97 | setSelectedIds((prev) |
| 98 | const next = |
| 99 | if (next.has |
| 100 | next.delete(id |
| 101 | } else { |
| 102 | next.add(id |
| 103 | } |
| 104 | return next |
| 105 | }) |
| 106 | } |
| 107 | |
| 108 | if (expenses.length |
| 109 | return ( |
| 110 | <Empty> |
| 111 | <EmptyHeader> |
| 112 | <EmptyMedia variant=' |
| 113 | <Receipt /> |
| 114 | </EmptyMedia> |
| 115 | <EmptyTitle>No expenses</EmptyTitle |
| 116 | <EmptyDescription> |
| 117 | No expenses have been sent to you for review yet. |
| 118 | </EmptyDescription> |
| 119 | </EmptyHeader> |
| 120 | </Empty> |
| 121 | ) |
| 122 | } |
| 123 | return ( |
| 124 | <div className='space-y-6 |
| 125 | {sentExpenses.length > |
| 126 | <div className='space-y-4 |
| 127 | <div className='flex items-center justify-between |
| 128 | <h2 className='font-medium text-lg |
| 129 | Pending Review ({sentExpenses.length}) |
| 130 | </h2> |
| 131 | <div className='flex items-center gap-2 |
| 132 | <Button |
| 133 | disabled={selectedIds.size |
| 134 | onClick={() => |
| 135 | variant='outline' |
| 136 | > |
| 137 | <XCircle className=' |
| 138 | Reject |
| 139 | </Button> |
| 140 | <Button |
| 141 | disabled={selectedIds.size |
| 142 | loading={respondAction.isPending |
| 143 | onClick={() => |
| 144 | respondAction.execute({ |
| 145 | expenseIds: [...selectedIds |
| 146 | action: 'approve', |
| 147 | }) |
| 148 | } |
| 149 | > |
| 150 | <CheckCircle2 className=' |
| 151 | Approve |
| 152 | </Button> |
| 153 | </div> |
| 154 | </div> |
| 155 | |
| 156 | <Card> |
| 157 | <CardContent className=' |
| 158 | <Table> |
| 159 | <TableHeader> |
| 160 | <TableRow> |
| 161 | <TableHead className=' |
| 162 | <Checkbox |
| 163 | checked={ |
| 164 | sentExpenses.length > 0 |
| 165 | sentExpenses.every((e |
| 166 | } |
| 167 | onCheckedChange={() => |
| 168 | const allSelected = |
| 169 | selectedIds.has(e |
| 170 | ) |
| 171 | setSelectedIds((prev) |
| 172 | const next = |
| 173 | for (const e |
| 174 | if (allSelected) { |
| 175 | next.delete(e |
| 176 | } else { |
| 177 | next.add(e |
| 178 | } |
| 179 | } |
| 180 | return next |
| 181 | }) |
| 182 | }} |
| 183 | /> |
| 184 | </TableHead> |
| 185 | <TableHead>Title</TableHead |
| 186 | <TableHead>Category</TableHead |
| 187 | <TableHead>Date</TableHead |
| 188 | <TableHead className=' |
| 189 | <TableHead className=' |
| 190 | </TableRow> |
| 191 | </TableHeader> |
| 192 | <TableBody> |
| 193 | {sentExpenses.map(( |
| 194 | <TableRow key={ |
| 195 | <TableCell> |
| 196 | <Checkbox |
| 197 | checked={selectedIds.has |
| 198 | onCheckedChange={() => |
| 199 | /> |
| 200 | </TableCell> |
| 201 | <TableCell className=' |
| 202 | {expense.recurring && |
| 203 | <Badge |
| 204 | className='mr-1.5 px-1.5 py-0 font-normal text-[10px]' |
| 205 | variant='outline' |
| 206 | > |
| 207 | Recurring |
| 208 | </Badge> |
| 209 | )} |
| 210 | {expense.description ? |
| 211 | <HoverCard openDelay={ |
| 212 | <HoverCardTrigger asChild> |
| 213 | <span className='cursor-help underline decoration-muted-foreground/40 decoration-dashed underline-offset-4 |
| 214 | {expense.title} |
| 215 | </span> |
| 216 | </HoverCardTrigger> |
| 217 | <HoverCardContent |
| 218 | align='start' |
| 219 | className='w-80' |
| 220 | side='bottom' |
| 221 | > |
| 222 | <div className='space-y-1 |
| 223 | <p className='font-medium text-sm |
| 224 | {expense.title} |
| 225 | </p> |
| 226 | <p className='whitespace-pre-wrap text-muted-foreground text-sm |
| 227 | {expense.description} |
| 228 | </p> |
| 229 | </div> |
| 230 | </HoverCardContent> |
| 231 | </HoverCard> |
| 232 | ) : ( |
| 233 | expense.title |
| 234 | )} |
| 235 | </TableCell> |
| 236 | <TableCell> |
| 237 | {expense.categoryName && |
| 238 | <Badge |
| 239 | className='text-xs' |
| 240 | style={ |
| 241 | expense.categoryColor |
| 242 | ? { |
| 243 | borderColor: expense.categoryColor |
| 244 | color: expense.categoryColor |
| 245 | } |
| 246 | : undefined |
| 247 | } |
| 248 | variant='outline' |
| 249 | > |
| 250 | {expense.categoryName} |
| 251 | </Badge> |
| 252 | )} |
| 253 | </TableCell> |
| 254 | <TableCell className=' |
| 255 | {new Date(expense |
| 256 | month: 'short', |
| 257 | day: 'numeric', |
| 258 | })} |
| 259 | </TableCell> |
| 260 | <TableCell className=' |
| 261 | {formatCurrency(expense.amountCents |
| 262 | </TableCell> |
| 263 | <TableCell> |
| 264 | {expense.receiptMediaId && |
| 265 | <a |
| 266 | className='inline-flex size-7 items-center justify-center rounded-md text-muted-foreground hover:bg-accent hover:text-foreground' |
| 267 | href={`/api/files/${expense |
| 268 | rel='noopener noreferrer' |
| 269 | target='_blank' |
| 270 | title='View receipt' |
| 271 | > |
| 272 | <Paperclip className=' |
| 273 | </a> |
| 274 | )} |
| 275 | </TableCell> |
| 276 | </TableRow> |
| 277 | ))} |
| 278 | </TableBody> |
| 279 | </Table> |
| 280 | </CardContent> |
| 281 | </Card> |
| 282 | </div> |
| 283 | )} |
| 284 | |
| 285 | {respondedExpenses.length > |
| 286 | <div className='space-y-4 |
| 287 | <h2 className='font-medium text-lg |
| 288 | <Card> |
| 289 | <CardContent className=' |
| 290 | <Table> |
| 291 | <TableHeader> |
| 292 | <TableRow> |
| 293 | <TableHead>Description</TableHead |
| 294 | <TableHead>Category</TableHead |
| 295 | <TableHead>Date</TableHead |
| 296 | <TableHead className=' |
| 297 | <TableHead className=' |
| 298 | <TableHead className=' |
| 299 | </TableRow> |
| 300 | </TableHeader> |
| 301 | <TableBody> |
| 302 | {respondedExpenses.map(( |
| 303 | const recipient = |
| 304 | const isApproved = |
| 305 | |
| 306 | return ( |
| 307 | <TableRow key={ |
| 308 | <TableCell className=' |
| 309 | {expense.description ? |
| 310 | <HoverCard openDelay={ |
| 311 | <HoverCardTrigger asChild> |
| 312 | <span className='cursor-help underline decoration-muted-foreground/40 decoration-dashed underline-offset-4 |
| 313 | {expense.title} |
| 314 | </span> |
| 315 | </HoverCardTrigger> |
| 316 | <HoverCardContent |
| 317 | align='start' |
| 318 | className='w-80' |
| 319 | side='bottom' |
| 320 | > |
| 321 | <div className='space-y-1 |
| 322 | <p className='font-medium text-sm |
| 323 | {expense.title} |
| 324 | </p> |
| 325 | <p className='whitespace-pre-wrap text-muted-foreground text-sm |
| 326 | {expense.description} |
| 327 | </p> |
| 328 | </div> |
| 329 | </HoverCardContent> |
| 330 | </HoverCard> |
| 331 | ) : ( |
| 332 | expense.title |
| 333 | )} |
| 334 | </TableCell> |
| 335 | <TableCell> |
| 336 | {expense.categoryName && |
| 337 | <Badge |
| 338 | className='text-xs' |
| 339 | style={ |
| 340 | expense.categoryColor |
| 341 | ? { |
| 342 | borderColor: expense.categoryColor |
| 343 | color: expense.categoryColor |
| 344 | } |
| 345 | : undefined |
| 346 | } |
| 347 | variant='outline' |
| 348 | > |
| 349 | {expense.categoryName} |
| 350 | </Badge> |
| 351 | )} |
| 352 | </TableCell> |
| 353 | <TableCell className=' |
| 354 | {new Date(expense |
| 355 | undefined, |
| 356 | { |
| 357 | month: 'short', |
| 358 | day: 'numeric', |
| 359 | } |
| 360 | )} |
| 361 | </TableCell> |
| 362 | <TableCell className=' |
| 363 | {formatCurrency( |
| 364 | expense.amountCents, |
| 365 | expense.currency |
| 366 | )} |
| 367 | </TableCell> |
| 368 | <TableCell> |
| 369 | {expense.receiptMediaId && |
| 370 | <a |
| 371 | className='inline-flex size-7 items-center justify-center rounded-md text-muted-foreground hover:bg-accent hover:text-foreground' |
| 372 | href={`/api/files/${expense |
| 373 | rel='noopener noreferrer' |
| 374 | target='_blank' |
| 375 | title='View receipt' |
| 376 | > |
| 377 | <Paperclip className=' |
| 378 | </a> |
| 379 | )} |
| 380 | </TableCell> |
| 381 | <TableCell className=' |
| 382 | <Badge |
| 383 | className={ |
| 384 | isApproved |
| 385 | ? 'bg-emerald-100 text-emerald-800 text-xs dark:bg-emerald-900 dark:text-emerald-300' |
| 386 | : 'text-xs' |
| 387 | } |
| 388 | variant={isApproved ? |
| 389 | > |
| 390 | {isApproved ? 'Approved |
| 391 | </Badge> |
| 392 | </TableCell> |
| 393 | </TableRow> |
| 394 | ) |
| 395 | })} |
| 396 | </TableBody> |
| 397 | </Table> |
| 398 | </CardContent> |
| 399 | </Card> |
| 400 | </div> |
| 401 | )} |
| 402 | |
| 403 | <Dialog onOpenChange={ |
| 404 | <DialogContent className=' |
| 405 | <DialogHeader> |
| 406 | <DialogTitle>Reject Expenses</DialogTitle |
| 407 | <DialogDescription> |
| 408 | Provide a reason for rejecting these expenses. |
| 409 | </DialogDescription> |
| 410 | </DialogHeader> |
| 411 | <div className='space-y-2 |
| 412 | <Label htmlFor={ |
| 413 | <Textarea |
| 414 | id={rejectReasonId} |
| 415 | onChange={(e) |
| 416 | placeholder='Please explain why these expenses are being rejected...' |
| 417 | rows={3} |
| 418 | value={rejectReason} |
| 419 | /> |
| 420 | </div> |
| 421 | <DialogFooter> |
| 422 | <Button onClick={ |
| 423 | Cancel |
| 424 | </Button> |
| 425 | <Button |
| 426 | disabled={!rejectReason. |
| 427 | loading={respondAction.isPending |
| 428 | onClick={() => |
| 429 | respondAction.execute({ |
| 430 | expenseIds: [...selectedIds |
| 431 | action: 'reject', |
| 432 | reason: rejectReason, |
| 433 | }) |
| 434 | } |
| 435 | variant='destructive' |
| 436 | > |
| 437 | Reject |
| 438 | </Button> |
| 439 | </DialogFooter> |
| 440 | </DialogContent> |
| 441 | </Dialog> |
| 442 | </div> |
| 443 | ) |
| 444 | } |