import React from 'react'; import { useParams, useNavigate, Link } from 'react-router-dom'; import { useAuth } from '../../context/AuthContext'; import { useMockStore } from '../../data/mockStore'; import { scopedProjectForSub } from '../../data/selectors'; import { ArrowLeft, MapPin, CalendarDays, CheckSquare, DollarSign, ChevronRight, Flag, Wallet, Lock } from 'lucide-react'; // --- helpers --- const fmtUSD = (n) => Number(n || 0).toLocaleString('en-US', { style: 'currency', currency: 'USD', maximumFractionDigits: 0 }); const statusTone = (s) => { if (!s) return 'bg-zinc-100 text-zinc-500 dark:bg-white/10 dark:text-zinc-400'; const lower = s.toLowerCase().replace(/[ _]/g, ''); if (lower === 'completed' || lower === 'complete' || lower === 'done') return 'bg-emerald-100 text-emerald-700 dark:bg-emerald-500/15 dark:text-emerald-400'; if (lower === 'inprogress' || lower === 'active') return 'bg-blue-100 text-blue-700 dark:bg-blue-500/15 dark:text-blue-400'; if (lower === 'onhold' || lower === 'pending') return 'bg-amber-100 text-amber-700 dark:bg-amber-500/15 dark:text-amber-400'; if (lower === 'cancelled' || lower === 'canceled') return 'bg-zinc-100 text-zinc-500 dark:bg-white/10 dark:text-zinc-400'; return 'bg-zinc-100 text-zinc-600 dark:bg-white/10 dark:text-zinc-300'; }; // --- main component --- const SubcontractorProjectDetailPage = () => { const { projectId } = useParams(); const navigate = useNavigate(); const { user } = useAuth(); const { projects, subcontractorTasks } = useMockStore(); const project = projects.find((p) => p.id === projectId); const scoped = scopedProjectForSub(project, user?.id, subcontractorTasks); // Access guard const hasAccess = scoped && !( scoped.myTasks.length === 0 && !(project?.subcontractorIds || []).includes(user?.id) ); if (!hasAccess) { return (

You don't have access to this project

This project either doesn't exist or you haven't been assigned to it.

Back to My Projects
); } const pct = Math.min(Math.max(scoped.completionPercentage ?? 0, 0), 100); return (
{/* Back link */} {/* ── Project header ── */}
{/* Title row */}

{scoped.projectType || 'Project'}

{scoped.address || '—'}

{scoped.customerName && (

{scoped.customerName}

)}
{/* Status badges */}
{scoped.status && ( {scoped.status.replace(/_/g, ' ')} )} {scoped.phase && ( {scoped.phase} )} {scoped.lifecycleStage && ( {scoped.lifecycleStage} )}
{/* Completion progress */}
Completion {pct}%
{/* Start / end dates */} {(scoped.startDate || scoped.endDate) && (
{scoped.startDate && ( Start: {scoped.startDate} )} {scoped.endDate && ( End: {scoped.endDate} )}
)}
{/* ── Schedule (milestones) ── */}

Schedule

{scoped.milestones.length} milestone{scoped.milestones.length !== 1 ? 's' : ''}
{scoped.milestones.length === 0 ? (

No milestones on record.

) : ( scoped.milestones.map((m) => (

{m.name}

{m.dueDate && (

Due: {m.dueDate}

)}
{m.status && ( {m.status.replace(/_/g, ' ')} )}
)) )}
{/* ── My Tasks on this job ── */}

My Tasks on this Job

{scoped.myTasks.length} task{scoped.myTasks.length !== 1 ? 's' : ''}
{scoped.myTasks.length === 0 ? (

No tasks assigned to you on this project.

) : ( scoped.myTasks.map((t) => ( )) )}
{/* ── My Payments ── */}

My Payments

{[ { label: 'Total Earnings', value: fmtUSD(scoped.myPayments.earnings), icon: DollarSign, tone: 'text-zinc-900 dark:text-white' }, { label: 'Paid', value: fmtUSD(scoped.myPayments.paid), icon: DollarSign, tone: 'text-emerald-600 dark:text-emerald-400' }, { label: 'Unpaid', value: fmtUSD(scoped.myPayments.unpaid), icon: DollarSign, tone: 'text-amber-600 dark:text-amber-400' }, ].map(({ label, value, icon: Icon, tone }) => (
{label}

{value}

))}
{/* Back link (bottom) */}
Back to My Projects
); }; export default SubcontractorProjectDetailPage;