| 1 | 'use client' |
| 2 | |
| 3 | import { useRouter } from '@bprogress/next/app' |
| 4 | import { format } |
| 5 | import { |
| 6 | AlertOctagon, |
| 7 | ArrowLeft, |
| 8 | Calendar, |
| 9 | CheckCircle2, |
| 10 | Circle, |
| 11 | Clock, |
| 12 | Link2Off, |
| 13 | Loader2, |
| 14 | Plus, |
| 15 | Trash2, |
| 16 | } from 'lucide-react' |
| 17 | import Link from 'next/link |
| 18 | import { useAction } |
| 19 | import { useState } |
| 20 | import { toast } |
| 21 | import StatusBadge from '@/components/status-badge |
| 22 | import { Badge } |
| 23 | import { Button } |
| 24 | import { |
| 25 | Dialog, |
| 26 | DialogContent, |
| 27 | DialogDescription, |
| 28 | DialogFooter, |
| 29 | DialogHeader, |
| 30 | DialogTitle, |
| 31 | } from '@/components/ui/dialog' |
| 32 | import { Input } |
| 33 | import { Label } |
| 34 | import { |
| 35 | Select, |
| 36 | SelectContent, |
| 37 | SelectItem, |
| 38 | SelectTrigger, |
| 39 | SelectValue, |
| 40 | } from '@/components/ui/select' |
| 41 | import { cn } |
| 42 | import type { milestones, |
| 43 | import { |
| 44 | completeMilestoneAction, |
| 45 | deleteMilestoneAction, |
| 46 | linkRequirementAction, |
| 47 | unlinkRequirementAction, |
| 48 | updateMilestoneAction, |
| 49 | } from '../actions' |
| 50 | |
| 51 | type Milestone |
| 52 | type Requirement |
| 53 | |
| 54 | interface LinkedRequirement { |
| 55 | id: string |
| 56 | milestoneId: string |
| 57 | requirementId: string |
| 58 | requirementSlug: string |
| 59 | requirementStatus: Requirement |
| 60 | requirementTitle: string |
| 61 | sortOrder: number |
| 62 | } |
| 63 | |
| 64 | interface Progress { |
| 65 | changesRequested: number |
| 66 | draft: number |
| 67 | sentForSign: number |
| 68 | signed: number |
| 69 | total: number |
| 70 | } |
| 71 | |
| 72 | const statusConfig = { |
| 73 | pending: { |
| 74 | label: 'Pending', |
| 75 | icon: Circle, |
| 76 | className: '', |
| 77 | }, |
| 78 | in_progress: { |
| 79 | label: 'In Progress', |
| 80 | icon: Loader2, |
| 81 | className: 'border-blue-500/50 text-blue-600 dark:text-blue-400', |
| 82 | }, |
| 83 | completed: { |
| 84 | label: 'Completed', |
| 85 | icon: CheckCircle2, |
| 86 | className: 'border-emerald-500/50 text-emerald-600 dark:text-emerald-400', |
| 87 | }, |
| 88 | blocked: { |
| 89 | label: 'Blocked', |
| 90 | icon: AlertOctagon, |
| 91 | className: 'border-destructive/50 text-destructive', |
| 92 | }, |
| 93 | } as const |
| 94 | |
| 95 | interface MilestoneDetailClientProps { |
| 96 | allRequirements: Requirement |
| 97 | canComplete: boolean |
| 98 | canDelete: boolean |
| 99 | canUpdate: boolean |
| 100 | isClientInvolvedInRequirements?: boolean |
| 101 | linkedRequirements: LinkedRequirement |
| 102 | milestone: Milestone |
| 103 | orgSlug: string |
| 104 | progress: Progress |
| 105 | projectSlug: string |
| 106 | } |
| 107 | |
| 108 | export function MilestoneDetailClient({ |
| 109 | milestone, |
| 110 | linkedRequirements, |
| 111 | progress, |
| 112 | allRequirements, |
| 113 | orgSlug, |
| 114 | projectSlug, |
| 115 | canUpdate, |
| 116 | canComplete, |
| 117 | canDelete, |
| 118 | isClientInvolvedInRequirements, |
| 119 | }: MilestoneDetailClientProps) |
| 120 | const router = |
| 121 | const [deleteDialogOpen, |
| 122 | const [blockDialogOpen, |
| 123 | const [blockReason, |
| 124 | const [linkDialogOpen, |
| 125 | const [selectedRequirementId, |
| 126 | |
| 127 | const status = |
| 128 | const StatusIcon = |
| 129 | const progressPercent = |
| 130 | progress.total > |
| 131 | ? Math.round(( |
| 132 | : 0 |
| 133 | |
| 134 | const linkedIds = |
| 135 | const unlinkableRequirements = |
| 136 | (r) => |
| 137 | ) |
| 138 | |
| 139 | const { execute: |
| 140 | completeMilestoneAction, |
| 141 | { |
| 142 | onSuccess() { |
| 143 | toast.success('Milestone completed |
| 144 | router.refresh() |
| 145 | }, |
| 146 | onError({ error |
| 147 | toast.error(error |
| 148 | }, |
| 149 | } |
| 150 | ) |
| 151 | |
| 152 | const { execute: |
| 153 | deleteMilestoneAction, |
| 154 | { |
| 155 | onSuccess() { |
| 156 | toast.success('Milestone deleted |
| 157 | router.push(`/ |
| 158 | }, |
| 159 | onError({ error |
| 160 | toast.error(error |
| 161 | }, |
| 162 | } |
| 163 | ) |
| 164 | |
| 165 | const { execute: |
| 166 | onSuccess() { |
| 167 | toast.success('Milestone updated |
| 168 | setBlockDialogOpen(false) |
| 169 | setBlockReason('') |
| 170 | router.refresh() |
| 171 | }, |
| 172 | onError({ error |
| 173 | toast.error(error |
| 174 | }, |
| 175 | }) |
| 176 | |
| 177 | const { execute: |
| 178 | linkRequirementAction, |
| 179 | { |
| 180 | onSuccess() { |
| 181 | toast.success('Requirement linked |
| 182 | setLinkDialogOpen(false) |
| 183 | setSelectedRequirementId('') |
| 184 | router.refresh() |
| 185 | }, |
| 186 | onError({ error |
| 187 | toast.error(error |
| 188 | }, |
| 189 | } |
| 190 | ) |
| 191 | |
| 192 | const { execute: |
| 193 | onSuccess() { |
| 194 | toast.success('Requirement unlinked |
| 195 | router.refresh() |
| 196 | }, |
| 197 | onError({ error |
| 198 | toast.error(error |
| 199 | }, |
| 200 | }) |
| 201 | |
| 202 | return ( |
| 203 | <div className='w-full |
| 204 | <div className='mb-6 |
| 205 | <Link |
| 206 | className='mb-4 inline-flex items-center gap-1 text-muted-foreground text-sm hover:text-foreground' |
| 207 | href={`/${orgSlug |
| 208 | > |
| 209 | <ArrowLeft className=' |
| 210 | Back to milestones |
| 211 | </Link> |
| 212 | |
| 213 | <div className='flex items-start justify-between gap-4 |
| 214 | <div> |
| 215 | <h1 className='font-semibold text-2xl |
| 216 | {milestone.description && |
| 217 | <p className='mt-1 text-muted-foreground |
| 218 | {milestone.description} |
| 219 | </p> |
| 220 | )} |
| 221 | </div> |
| 222 | <Badge className={ |
| 223 | <StatusIcon className=' |
| 224 | {status.label} |
| 225 | </Badge> |
| 226 | </div> |
| 227 | |
| 228 | <div className='mt-3 flex flex-wrap items-center gap-4 text-muted-foreground text-sm |
| 229 | {milestone.dueDate && |
| 230 | <span className='flex items-center gap-1 |
| 231 | <Calendar className=' |
| 232 | Due {format(new Date |
| 233 | </span> |
| 234 | )} |
| 235 | {milestone.completedAt && |
| 236 | <span className='flex items-center gap-1 |
| 237 | <CheckCircle2 className=' |
| 238 | Completed {format(new Date |
| 239 | </span> |
| 240 | )} |
| 241 | {milestone.budgetAmountCents != |
| 242 | <span> |
| 243 | Budget: ${(milestone.budgetAmountCents |
| 244 | </span> |
| 245 | )} |
| 246 | {milestone.budgetMinutes != |
| 247 | <span className='flex items-center gap-1 |
| 248 | <Clock className=' |
| 249 | {Math.floor(milestone |
| 250 | {milestone.budgetMinutes % |
| 251 | </span> |
| 252 | )} |
| 253 | </div> |
| 254 | |
| 255 | {milestone.status === |
| 256 | <div className='mt-3 rounded-md border border-destructive/50 bg-destructive/10 p-3 text-destructive text-sm |
| 257 | <strong>Blocked:</strong> { |
| 258 | </div> |
| 259 | )} |
| 260 | </div> |
| 261 | |
| 262 | {(canUpdate || canComplete |
| 263 | <div className='mb-6 flex flex-wrap gap-2 |
| 264 | {canComplete && milestone |
| 265 | <Button |
| 266 | disabled={isCompleting} |
| 267 | onClick={() => |
| 268 | > |
| 269 | <CheckCircle2 className=' |
| 270 | {isCompleting ? 'Completing... |
| 271 | </Button> |
| 272 | )} |
| 273 | {canUpdate && milestone |
| 274 | <Button onClick={ |
| 275 | <AlertOctagon className=' |
| 276 | Mark as Blocked |
| 277 | </Button> |
| 278 | )} |
| 279 | {canUpdate && milestone |
| 280 | <Button |
| 281 | onClick={() => |
| 282 | executeUpdate({ |
| 283 | milestoneId: milestone.id |
| 284 | status: 'in_progress', |
| 285 | }) |
| 286 | } |
| 287 | variant='outline' |
| 288 | > |
| 289 | Unblock |
| 290 | </Button> |
| 291 | )} |
| 292 | {canUpdate && milestone |
| 293 | <Button |
| 294 | onClick={() => |
| 295 | executeUpdate({ |
| 296 | milestoneId: milestone.id |
| 297 | status: 'in_progress', |
| 298 | }) |
| 299 | } |
| 300 | variant='outline' |
| 301 | > |
| 302 | <Loader2 className=' |
| 303 | Start Progress |
| 304 | </Button> |
| 305 | )} |
| 306 | {canDelete && ( |
| 307 | <Button |
| 308 | onClick={() => |
| 309 | variant='destructive' |
| 310 | > |
| 311 | <Trash2 className=' |
| 312 | Delete |
| 313 | </Button> |
| 314 | )} |
| 315 | </div> |
| 316 | )} |
| 317 | |
| 318 | {progress.total > |
| 319 | <div className='mb-6 rounded-lg border p-4 |
| 320 | <div className='mb-2 flex items-center justify-between text-sm |
| 321 | <span className='font-medium |
| 322 | <span className='text-muted-foreground |
| 323 | {progress.signed}/ |
| 324 | </span> |
| 325 | </div> |
| 326 | <div className='mb-2 h-1.5 w-full overflow-hidden rounded-full bg-secondary |
| 327 | <div |
| 328 | className='h-full rounded-full bg-emerald-500 transition-all' |
| 329 | style={{ width: |
| 330 | /> |
| 331 | </div> |
| 332 | <div className='flex flex-wrap gap-3 text-muted-foreground text-xs |
| 333 | {progress.draft > |
| 334 | {progress.sentForSign > |
| 335 | <span>{progress.sentForSign |
| 336 | )} |
| 337 | {progress.changesRequested > |
| 338 | <span>{progress.changesRequested |
| 339 | )} |
| 340 | </div> |
| 341 | </div> |
| 342 | )} |
| 343 | |
| 344 | <div> |
| 345 | <div className='mb-3 flex items-center justify-between |
| 346 | <h2 className='font-medium text-muted-foreground text-sm uppercase tracking-wide |
| 347 | Linked Requirements |
| 348 | </h2> |
| 349 | {canUpdate && unlinkableRequirements |
| 350 | <Button onClick={ |
| 351 | <Plus className=' |
| 352 | Link Requirement |
| 353 | </Button> |
| 354 | )} |
| 355 | </div> |
| 356 | |
| 357 | {linkedRequirements.length === |
| 358 | <p className='text-muted-foreground text-sm |
| 359 | No requirements linked to this milestone yet. |
| 360 | </p> |
| 361 | ) : ( |
| 362 | <div className='divide-y rounded-lg border |
| 363 | {linkedRequirements.map(( |
| 364 | <div |
| 365 | className='flex items-center justify-between gap-3 px-4 py-2.5' |
| 366 | key={lr.id |
| 367 | > |
| 368 | <Link |
| 369 | className='min-w-0 flex-1 truncate text-sm hover:underline' |
| 370 | href={`/${orgSlug |
| 371 | > |
| 372 | {lr.requirementTitle} |
| 373 | </Link> |
| 374 | <div className='flex shrink-0 items-center gap-2 |
| 375 | <StatusBadge |
| 376 | isClientInvolved={isClientInvolvedInRequirements} |
| 377 | status={lr.requirementStatus |
| 378 | /> |
| 379 | {canUpdate && ( |
| 380 | <Button |
| 381 | className='size-7 p-0 text-muted-foreground hover:text-destructive' |
| 382 | onClick={() => |
| 383 | executeUnlink({ |
| 384 | milestoneId: milestone.id |
| 385 | requirementId: lr.requirementId |
| 386 | }) |
| 387 | } |
| 388 | variant='ghost' |
| 389 | > |
| 390 | <Link2Off className=' |
| 391 | </Button> |
| 392 | )} |
| 393 | </div> |
| 394 | </div> |
| 395 | ))} |
| 396 | </div> |
| 397 | )} |
| 398 | </div> |
| 399 | |
| 400 | <Dialog onOpenChange={ |
| 401 | <DialogContent> |
| 402 | <DialogHeader> |
| 403 | <DialogTitle>Delete Milestone</DialogTitle |
| 404 | <DialogDescription> |
| 405 | Are you sure you want to delete "{milestone.name |
| 406 | action cannot be undone. Linked requirements will not be deleted. |
| 407 | </DialogDescription> |
| 408 | </DialogHeader> |
| 409 | <DialogFooter> |
| 410 | <Button |
| 411 | onClick={() => |
| 412 | variant='outline' |
| 413 | > |
| 414 | Cancel |
| 415 | </Button> |
| 416 | <Button |
| 417 | disabled={isDeleting} |
| 418 | onClick={() => |
| 419 | variant='destructive' |
| 420 | > |
| 421 | {isDeleting ? 'Deleting... |
| 422 | </Button> |
| 423 | </DialogFooter> |
| 424 | </DialogContent> |
| 425 | </Dialog> |
| 426 | |
| 427 | <Dialog onOpenChange={ |
| 428 | <DialogContent> |
| 429 | <DialogHeader> |
| 430 | <DialogTitle>Mark as Blocked</DialogTitle |
| 431 | <DialogDescription> |
| 432 | Provide a reason for blocking this milestone. |
| 433 | </DialogDescription> |
| 434 | </DialogHeader> |
| 435 | <div className='mt-4 grid gap-2 |
| 436 | <Label>Block Reason</Label |
| 437 | <Input |
| 438 | onChange={(e) |
| 439 | placeholder='e.g. Waiting on client approval...' |
| 440 | value={blockReason} |
| 441 | /> |
| 442 | </div> |
| 443 | <DialogFooter className=' |
| 444 | <Button onClick={ |
| 445 | Cancel |
| 446 | </Button> |
| 447 | <Button |
| 448 | disabled={!blockReason. |
| 449 | onClick={() => |
| 450 | executeUpdate({ |
| 451 | milestoneId: milestone.id |
| 452 | status: 'blocked', |
| 453 | blockReason, |
| 454 | }) |
| 455 | } |
| 456 | > |
| 457 | Block Milestone |
| 458 | </Button> |
| 459 | </DialogFooter> |
| 460 | </DialogContent> |
| 461 | </Dialog> |
| 462 | |
| 463 | <Dialog onOpenChange={ |
| 464 | <DialogContent> |
| 465 | <DialogHeader> |
| 466 | <DialogTitle>Link Requirement</DialogTitle |
| 467 | <DialogDescription> |
| 468 | Choose a requirement to link to this milestone. |
| 469 | </DialogDescription> |
| 470 | </DialogHeader> |
| 471 | <div className='mt-4 grid gap-2 |
| 472 | <Label>Requirement</Label |
| 473 | <Select |
| 474 | onValueChange={setSelectedRequirementId} |
| 475 | value={selectedRequirementId} |
| 476 | > |
| 477 | <SelectTrigger> |
| 478 | <SelectValue placeholder=' |
| 479 | </SelectTrigger> |
| 480 | <SelectContent> |
| 481 | {unlinkableRequirements.map(( |
| 482 | <SelectItem key={ |
| 483 | {r.title} |
| 484 | </SelectItem> |
| 485 | ))} |
| 486 | </SelectContent> |
| 487 | </Select> |
| 488 | </div> |
| 489 | <DialogFooter className=' |
| 490 | <Button onClick={ |
| 491 | Cancel |
| 492 | </Button> |
| 493 | <Button |
| 494 | disabled={!selectedRequirementId |
| 495 | onClick={() => |
| 496 | executeLink({ |
| 497 | milestoneId: milestone.id |
| 498 | requirementId: selectedRequirementId, |
| 499 | }) |
| 500 | } |
| 501 | > |
| 502 | {isLinking ? 'Linking... |
| 503 | </Button> |
| 504 | </DialogFooter> |
| 505 | </DialogContent> |
| 506 | </Dialog> |
| 507 | </div> |
| 508 | ) |
| 509 | } |