"use client"; import React from 'react'; import Link from 'next/link'; import dayjs from 'dayjs'; import { Calendar, ChevronRight, Clock } from 'lucide-react'; import { HoverCard, HoverCardContent, HoverCardTrigger } from '@/components/ui/hover-card'; import BadgeStatus from '@/components/ui/badge-status'; import { cn } from '@/lib/utils'; import { IClient, IClientMilestone } from '../../types'; import { resolveTechnicalStatusMeta } from '../../utils/client-overview-helpers'; interface ClientMilestoneTimelineProps { client: IClient; variant?: 'list' | 'grid'; } interface GroupedMilestone { dateKey: string; date: dayjs.Dayjs; percent: number; diffDays: number; items: IClientMilestone[]; } interface GradientSegment { fromPercent: number; toPercent: number; fromHex: string; toHex: string; } // Starting color for active milestone line: Merah rose (Development) const START_LINE_HEX = '#f43f5e'; export default function ClientMilestoneTimeline({ client, variant = 'list' }: ClientMilestoneTimelineProps) { const today = dayjs().startOf('day'); const maxDate = today.add(2, 'month').startOf('day'); const totalDays = maxDate.diff(today, 'day') || 60; // Filter milestones whose start_date is within the 2-month range [today, today + 2 months] const rawMilestones = client.milestone || []; const validMilestones = rawMilestones.filter((m) => { if (!m.start_date) return false; const mDate = dayjs(m.start_date).startOf('day'); const diff = mDate.diff(today, 'day'); return diff >= 0 && diff <= totalDays; }); // Group milestones by start_date to handle multiple agendas on the exact same date const groupsMap: { [key: string]: GroupedMilestone } = {}; validMilestones.forEach((item) => { const mDate = dayjs(item.start_date).startOf('day'); const dateKey = mDate.format('YYYY-MM-DD'); const diffDays = mDate.diff(today, 'day'); const rawPercent = (diffDays / totalDays) * 100; const percent = Math.max(0, Math.min(100, rawPercent)); if (!groupsMap[dateKey]) { groupsMap[dateKey] = { dateKey, date: mDate, percent, diffDays, items: [], }; } groupsMap[dateKey].items.push(item); }); const groups = Object.values(groupsMap).sort((a, b) => a.diffDays - b.diffDays); // Compute sequential line gradient segments: // Starts at Hari Ini (0%, Merah Rose) -> Agenda 1 -> Agenda 2 -> ... -> Agenda N const gradientSegments: GradientSegment[] = []; if (groups.length > 0) { let currentPercent = 0; let currentHex = START_LINE_HEX; for (let i = 0; i < groups.length; i++) { const group = groups[i]; const targetPercent = group.percent; const targetMeta = resolveTechnicalStatusMeta(group.items[0]?.project_assignment_status?.name); const targetHex = targetMeta.hexColor; if (targetPercent > currentPercent) { gradientSegments.push({ fromPercent: currentPercent, toPercent: targetPercent, fromHex: currentHex, toHex: targetHex, }); } currentPercent = targetPercent; currentHex = targetHex; } } // Compute month boundaries for tick lines along the 2-month rail const nextMonthStart = today.add(1, 'month').startOf('month'); const month2Start = today.add(2, 'month').startOf('month'); const monthTicks: { label: string; percent: number }[] = []; if (nextMonthStart.isAfter(today) && nextMonthStart.isBefore(maxDate)) { const diff = nextMonthStart.diff(today, 'day'); monthTicks.push({ label: nextMonthStart.format('1 MMM'), percent: (diff / totalDays) * 100, }); } if (month2Start.isAfter(today) && month2Start.isBefore(maxDate)) { const diff = month2Start.diff(today, 'day'); monthTicks.push({ label: month2Start.format('1 MMM'), percent: (diff / totalDays) * 100, }); } const isGrid = variant === 'grid'; const hasAgendas = groups.length > 0; return (
{/* Header: Title + 2-Month Date Span */}
Next 2 Months Milestone ({today.format('DD MMM')} – {maxDate.format('DD MMM YYYY')})
{hasAgendas ? ( {validMilestones.length} {validMilestones.length === 1 ? 'Agenda' : 'Agendas'} ) : ( No milestone scheduled )}
{/* Timeline Viewport Container with group/rail for sibling hover coordination */}
{/* Continuous 2-Month Horizontal Rail */}
{/* Gradient Segments connecting Hari Ini -> Agenda 1 -> Agenda 2 -> ... */}
{gradientSegments.map((seg, idx) => (
))}
{/* Start Pin (Today) - Rose if active progression, gray if default */}
{/* End Pin (+2 Months) */}
{/* Month Boundary Tick Markers */} {monthTicks.map((tick, idx) => (
{tick.label}
))} {/* If no milestones in range, show gentle notice */} {!hasAgendas && (
No upcoming milestones in the next 2 months
)} {/* Agenda Milestone Markers */} {groups.map((group) => { const isMulti = group.items.length > 1; const firstItem = group.items[0]; const primaryMeta = resolveTechnicalStatusMeta(firstItem.project_assignment_status?.name); return (
{/* Vertical Tick Line connecting Rail to Dot */}
{/* HoverCard Trigger (Dot + Short Badge + Date) */} {/* Dot / Multi-Dot Indicator */} {isMulti ? (
{group.items.length}
) : (
)} {/* Short Badge underneath dot */}
{isMulti ? `${group.items.length} Agendas` : primaryMeta.shortName}
{/* Date Label strictly underneath */} {group.date.format('DD MMM')} {/* Rich Hover Card Content */} {/* Header */}
{group.date.format('dddd, DD MMMM YYYY')}
{group.diffDays === 0 ? 'Today' : group.diffDays === 1 ? 'Tomorrow' : `In ${group.diffDays} days`}
{/* Subtitle / Counter */}
{group.items.length === 1 ? '1 milestone agenda on this date:' : `${group.items.length} milestone agendas on this date:`}
{/* List of Assignment Milestones */}
{group.items.map((item, itemIdx) => (
View Assignment
{item.project_name && (
{item.project_name} {item.project_code && ( ({item.project_code}) )}
)} {item.wo_name && (
WO: {item.wo_name} {item.wo_code && ( [{item.wo_code}] )}
)} {item.description && (

“{item.description}”

)}
{dayjs(item.start_date).format('DD MMM YYYY')} {item.end_date ? ` – ${dayjs(item.end_date).format('DD MMM YYYY')}` : ''}
))}
); })}
); }