| 1 | import { format } from 'date-fns' |
| 2 | import { |
| 3 | AlertTriangleIcon, |
| 4 | ClipboardList, |
| 5 | Clock, |
| 6 | FileSpreadsheet, |
| 7 | FileText, |
| 8 | Milestone as MilestoneIcon, |
| 9 | Receipt, |
| 10 | ShieldAlert, |
| 11 | Users, |
| 12 | } from 'lucide-react' |
| 13 | import type { Metadata |
| 14 | import { headers } |
| 15 | |
| 16 | import { |
| 17 | requirePermission, |
| 18 | resolveProjectContext, |
| 19 | } from '@/app/(organization)/[org]/cache' |
| 20 | import { expensesServices } |
| 21 | import { invoicesService } |
| 22 | import { milestonesService } |
| 23 | import { proposalsService } |
| 24 | import { requirementsService } |
| 25 | import { teamService } |
| 26 | import { timesheetService } |
| 27 | import { FinancialOverviewCard } |
| 28 | import { |
| 29 | ActionItemLink, |
| 30 | NeedsAttentionCard, |
| 31 | } from '@/components/dashboard/needs-attention' |
| 32 | import { OverdueInvoicesAccordion } |
| 33 | import { StatCard } |
| 34 | import { Badge } |
| 35 | import { createMetadata } |
| 36 | import { |
| 37 | classifyExpenses, |
| 38 | classifyInvoices, |
| 39 | classifyMilestones, |
| 40 | summarizeTimeEntries, |
| 41 | toOverdueInvoiceEntries, |
| 42 | } from '@/lib/overview-stats' |
| 43 | import { ActiveMilestonesCard } |
| 44 | import { BudgetCard } |
| 45 | import { |
| 46 | ProposalsPendingCard, |
| 47 | RequirementsAwaitingSignatureCard, |
| 48 | } from './_components/pending-signatures-cards' |
| 49 | import { ProjectHeaderStatus } |
| 50 | import { TimesheetReportsCard } |
| 51 | |
| 52 | export const metadata: |
| 53 | title: 'Project Overview', |
| 54 | description: 'Project at a glance — milestones, budget, and pending work.', |
| 55 | openGraph: { images: [ |
| 56 | twitter: { images: [ |
| 57 | }) |
| 58 | |
| 59 | export default async function |
| 60 | params, |
| 61 | }: PageProps< |
| 62 | const { org, |
| 63 | const { organization, |
| 64 | await resolveProjectContext(org |
| 65 | |
| 66 | requirePermission( |
| 67 | role, |
| 68 | { project: ['read' |
| 69 | 'You do not have permission to view this project' |
| 70 | ) |
| 71 | |
| 72 | const h = |
| 73 | const isClient = |
| 74 | const isAdmin = |
| 75 | |
| 76 | const canReadMilestones = |
| 77 | const canReadRequirements = |
| 78 | const canReadTimesheets = |
| 79 | const canReadExpenses = |
| 80 | const canReadInvoices = |
| 81 | const canReadProposals = |
| 82 | const canReadReports = |
| 83 | |
| 84 | const [ |
| 85 | milestones, |
| 86 | requirements, |
| 87 | timeEntries, |
| 88 | budgetStatus, |
| 89 | expenses, |
| 90 | invoices, |
| 91 | proposals, |
| 92 | timesheetReports, |
| 93 | members, |
| 94 | clients, |
| 95 | ] = await Promise |
| 96 | canReadMilestones |
| 97 | ? milestonesService.listByProjectWithProgress( |
| 98 | : Promise.resolve([]) |
| 99 | canReadRequirements |
| 100 | ? requirementsService.listByProject( |
| 101 | : Promise.resolve([]) |
| 102 | canReadTimesheets |
| 103 | ? timesheetService.listByProject( |
| 104 | : Promise.resolve([]) |
| 105 | isAdmin |
| 106 | ? timesheetService.getProjectBudgetStatus( |
| 107 | : Promise.resolve( |
| 108 | canReadExpenses |
| 109 | ? expensesServices.listByProject( |
| 110 | : Promise.resolve([]) |
| 111 | canReadInvoices |
| 112 | ? invoicesService.listByProject( |
| 113 | : Promise.resolve([]) |
| 114 | canReadProposals |
| 115 | ? proposalsService.listByProject( |
| 116 | : Promise.resolve([]) |
| 117 | canReadReports |
| 118 | ? timesheetService.listReportsByProject( |
| 119 | : Promise.resolve([]) |
| 120 | teamService.getProjectMembers(project |
| 121 | teamService.getProjectClients(project |
| 122 | ]) |
| 123 | |
| 124 | const basePath = |
| 125 | |
| 126 | const ms = |
| 127 | const time = |
| 128 | const inv = |
| 129 | const exp = |
| 130 | const requirementsAwaitingSignature = |
| 131 | (r) => |
| 132 | ) |
| 133 | const requirementsWithChanges = |
| 134 | (r) => |
| 135 | ) |
| 136 | const proposalsAwaitingSignature = |
| 137 | (p) => |
| 138 | ) |
| 139 | const reportsDisputed = |
| 140 | (r) => |
| 141 | ) |
| 142 | const reportsPending = |
| 143 | const overdueInvoiceEntries = |
| 144 | inv.overdue, |
| 145 | projectSlug |
| 146 | ) |
| 147 | |
| 148 | const hasActionItems = |
| 149 | overdueInvoiceEntries.length > 0 |
| 150 | (isAdmin && |
| 151 | (time.pendingApproval.length |
| 152 | exp.pendingAdmin.length |
| 153 | inv.disputed.length |
| 154 | reportsDisputed.length > 0 |
| 155 | requirementsWithChanges.length > 0 |
| 156 | (isClient && |
| 157 | (requirementsAwaitingSignature.length > |
| 158 | proposalsAwaitingSignature.length > 0 |
| 159 | reportsPending.length > 0 |
| 160 | exp.pendingClient.length |
| 161 | (!(isClient || |
| 162 | (canReadMilestones && (ms |
| 163 | |
| 164 | const hasDetailGridContent = |
| 165 | (canReadMilestones && ms |
| 166 | (canReadRequirements && requirementsAwaitingSignature |
| 167 | (canReadProposals && proposalsAwaitingSignature |
| 168 | (isAdmin && !!budgetStatus |
| 169 | (canReadReports && timesheetReports |
| 170 | |
| 171 | return ( |
| 172 | <div className='w-full space-y-8 |
| 173 | <header className='flex flex-wrap items-start justify-between gap-4 |
| 174 | <div> |
| 175 | <h1 className='font-semibold text-2xl tracking-tight |
| 176 | {project.name} |
| 177 | </h1> |
| 178 | {project.description && |
| 179 | <p className='mt-1 max-w-prose text-muted-foreground text-sm |
| 180 | {project.description} |
| 181 | </p> |
| 182 | )} |
| 183 | </div> |
| 184 | <div className='flex flex-wrap items-center gap-2 |
| 185 | <ProjectHeaderStatus |
| 186 | canEdit={isAdmin} |
| 187 | dueDate={project.dueDate |
| 188 | organizationId={organization.id |
| 189 | projectId={project.id |
| 190 | status={project.status |
| 191 | /> |
| 192 | <Badge className=' |
| 193 | <Users className=' |
| 194 | {members.length} { |
| 195 | {clients.length > |
| 196 | ` · ${clients.length} |
| 197 | </Badge> |
| 198 | </div> |
| 199 | </header> |
| 200 | |
| 201 | {hasActionItems && ( |
| 202 | <section className='space-y-3 |
| 203 | <SectionLabel>Needs your attention</SectionLabel |
| 204 | <NeedsAttentionCard> |
| 205 | {overdueInvoiceEntries.length > |
| 206 | <OverdueInvoicesAccordion |
| 207 | invoices={overdueInvoiceEntries} |
| 208 | orgSlug={org} |
| 209 | /> |
| 210 | )} |
| 211 | {isAdmin && time |
| 212 | <ActionItemLink |
| 213 | count={time.pendingApproval |
| 214 | href={`${basePath |
| 215 | icon={Clock} |
| 216 | label='Timesheets pending approval' |
| 217 | /> |
| 218 | )} |
| 219 | {isAdmin && exp |
| 220 | <ActionItemLink |
| 221 | count={exp.pendingAdmin |
| 222 | href={`${basePath |
| 223 | icon={Receipt} |
| 224 | label='Expenses pending approval' |
| 225 | /> |
| 226 | )} |
| 227 | {isAdmin && inv |
| 228 | <ActionItemLink |
| 229 | count={inv.disputed |
| 230 | href={`${basePath |
| 231 | icon={FileSpreadsheet} |
| 232 | label='Disputed invoices' |
| 233 | variant='destructive' |
| 234 | /> |
| 235 | )} |
| 236 | {isAdmin && reportsDisputed |
| 237 | <ActionItemLink |
| 238 | count={reportsDisputed.length |
| 239 | href={`${basePath |
| 240 | icon={ShieldAlert} |
| 241 | label='Disputed timesheet reports' |
| 242 | variant='destructive' |
| 243 | /> |
| 244 | )} |
| 245 | {isAdmin && requirementsWithChanges |
| 246 | <ActionItemLink |
| 247 | count={requirementsWithChanges.length |
| 248 | href={`${basePath |
| 249 | icon={ClipboardList} |
| 250 | label='Requirements with change requests' |
| 251 | /> |
| 252 | )} |
| 253 | {isClient && requirementsAwaitingSignature |
| 254 | <ActionItemLink |
| 255 | count={requirementsAwaitingSignature.length |
| 256 | href={`${basePath |
| 257 | icon={ClipboardList} |
| 258 | label='Requirements awaiting your signature' |
| 259 | /> |
| 260 | )} |
| 261 | {isClient && proposalsAwaitingSignature |
| 262 | <ActionItemLink |
| 263 | count={proposalsAwaitingSignature.length |
| 264 | href={`${basePath |
| 265 | icon={FileText} |
| 266 | label='Proposals awaiting your signature' |
| 267 | /> |
| 268 | )} |
| 269 | {isClient && reportsPending |
| 270 | <ActionItemLink |
| 271 | count={reportsPending.length |
| 272 | href={`${basePath |
| 273 | icon={Clock} |
| 274 | label='Timesheet reports to review' |
| 275 | /> |
| 276 | )} |
| 277 | {isClient && exp |
| 278 | <ActionItemLink |
| 279 | count={exp.pendingClient |
| 280 | href={`${basePath |
| 281 | icon={Receipt} |
| 282 | label='Expenses to review' |
| 283 | /> |
| 284 | )} |
| 285 | {!(isClient || |
| 286 | <ActionItemLink |
| 287 | count={time.rejected |
| 288 | href={`${basePath |
| 289 | icon={Clock} |
| 290 | label='Rejected timesheets to fix' |
| 291 | variant='destructive' |
| 292 | /> |
| 293 | )} |
| 294 | {canReadMilestones && ms |
| 295 | <ActionItemLink |
| 296 | count={ms.blocked |
| 297 | href={`${basePath |
| 298 | icon={ShieldAlert} |
| 299 | label='Blocked milestones' |
| 300 | variant='destructive' |
| 301 | /> |
| 302 | )} |
| 303 | {canReadMilestones && ms |
| 304 | <ActionItemLink |
| 305 | count={ms.overdue |
| 306 | href={`${basePath |
| 307 | icon={AlertTriangleIcon} |
| 308 | label='Overdue milestones' |
| 309 | variant='destructive' |
| 310 | /> |
| 311 | )} |
| 312 | </NeedsAttentionCard> |
| 313 | </section> |
| 314 | )} |
| 315 | |
| 316 | {canReadInvoices && orgMember |
| 317 | <FinancialOverviewCard |
| 318 | expenses={canReadExpenses ? |
| 319 | invoices={invoices} |
| 320 | isAdmin={isAdmin} |
| 321 | isClient={isClient} |
| 322 | viewAllHref={`${basePath |
| 323 | /> |
| 324 | )} |
| 325 | |
| 326 | <section className='space-y-3 |
| 327 | <SectionLabel>At a glance</SectionLabel |
| 328 | <div className='grid gap-4 sm:grid-cols-2 lg:grid-cols-4 |
| 329 | {canReadMilestones && ( |
| 330 | <StatCard |
| 331 | accent='amber' |
| 332 | href={`${basePath |
| 333 | icon={MilestoneIcon} |
| 334 | label='Milestones' |
| 335 | sublabel={ |
| 336 | milestones.length > 0 |
| 337 | ? `${ms.completed |
| 338 | : undefined |
| 339 | } |
| 340 | value={ |
| 341 | milestones.length > 0 |
| 342 | ? `${ms.completionPercent |
| 343 | : milestones.length |
| 344 | } |
| 345 | /> |
| 346 | )} |
| 347 | {canReadTimesheets && ( |
| 348 | <StatCard |
| 349 | accent='sky' |
| 350 | href={`${basePath |
| 351 | icon={Clock} |
| 352 | label='Time Logged' |
| 353 | sublabel={ |
| 354 | budgetStatus |
| 355 | ? `${budgetStatus.percentageUsed |
| 356 | : undefined |
| 357 | } |
| 358 | value={`${time |
| 359 | /> |
| 360 | )} |
| 361 | {canReadInvoices && ( |
| 362 | <StatCard |
| 363 | accent='teal' |
| 364 | href={`${basePath |
| 365 | icon={FileSpreadsheet} |
| 366 | label='Invoices' |
| 367 | sublabel={ |
| 368 | invoices.length > 0 |
| 369 | ? `${inv.paid |
| 370 | : undefined |
| 371 | } |
| 372 | value={invoices.length |
| 373 | /> |
| 374 | )} |
| 375 | {canReadExpenses && ( |
| 376 | <StatCard |
| 377 | accent='rose' |
| 378 | href={`${basePath |
| 379 | icon={Receipt} |
| 380 | label='Expenses' |
| 381 | sublabel={ |
| 382 | expenses.length > 0 |
| 383 | ? `${exp.billableApproved |
| 384 | : undefined |
| 385 | } |
| 386 | value={expenses.length |
| 387 | /> |
| 388 | )} |
| 389 | </div> |
| 390 | </section> |
| 391 | |
| 392 | {hasDetailGridContent && ( |
| 393 | <section className='space-y-3 |
| 394 | <SectionLabel>Active work</SectionLabel |
| 395 | <div className='grid gap-4 lg:grid-cols-2 |
| 396 | {canReadMilestones && ms |
| 397 | <ActiveMilestonesCard |
| 398 | basePath={basePath} |
| 399 | milestones={ms.inProgress |
| 400 | /> |
| 401 | )} |
| 402 | {canReadRequirements && |
| 403 | requirementsAwaitingSignature.length > 0 |
| 404 | <RequirementsAwaitingSignatureCard |
| 405 | basePath={basePath} |
| 406 | requirements={requirementsAwaitingSignature} |
| 407 | /> |
| 408 | )} |
| 409 | {canReadProposals && proposalsAwaitingSignature |
| 410 | <ProposalsPendingCard |
| 411 | basePath={basePath} |
| 412 | proposals={proposalsAwaitingSignature} |
| 413 | /> |
| 414 | )} |
| 415 | {isAdmin && budgetStatus |
| 416 | {canReadReports && timesheetReports |
| 417 | <TimesheetReportsCard |
| 418 | basePath={basePath} |
| 419 | reports={timesheetReports} |
| 420 | /> |
| 421 | )} |
| 422 | </div> |
| 423 | </section> |
| 424 | )} |
| 425 | </div> |
| 426 | ) |
| 427 | } |
| 428 | |
| 429 | function SectionLabel({ children |
| 430 | return ( |
| 431 | <h2 className='font-medium text-muted-foreground text-sm uppercase tracking-wider |
| 432 | {children} |
| 433 | </h2> |
| 434 | ) |
| 435 | } |