Files
hats-app/app/(dashboard)/task/component/client/ClientMilestoneTimeline.tsx
T
Ichsan Kurniawan 09a049ce98 Add live milestone timelines to client views
Show upcoming assignment milestones with grouped agenda details and status-based colors. Use API status counts for client stage summaries and align status indicators across project timelines.
2026-10-15 18:52:32 +07:00

321 lines
20 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"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[];
}
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 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';
console.log(groups)
return (
<div className={cn("w-full flex flex-col gap-2 min-w-0", isGrid ? "pt-2" : "")}>
{/* Header: Title + 2-Month Date Span */}
<div className="flex items-center justify-between text-xs px-0.5">
<div className="flex items-center gap-2 flex-wrap">
<span className="font-semibold text-foreground tracking-tight text-xs">
Next 2 Months Milestone
</span>
<span className="text-[10px] text-muted-foreground font-medium hidden sm:inline">
({today.format('DD MMM')} – {maxDate.format('DD MMM YYYY')})
</span>
</div>
<div className="flex items-center gap-1.5">
{groups.length > 0 ? (
<span className="text-[10px] px-2 py-0.5 rounded-full bg-primary/10 text-primary font-bold">
{validMilestones.length} {validMilestones.length === 1 ? 'Agenda' : 'Agendas'}
</span>
) : (
<span className="text-[10px] text-muted-foreground italic">
No milestone scheduled
</span>
)}
</div>
</div>
{/* Timeline Viewport Container */}
<div className={cn("w-full min-w-0", isGrid ? "" : "overflow-x-auto scrollbar-hide")}>
<div className={cn("relative pt-3", groups.length > 0 ? 'mb-14' : 'mb-4', isGrid ? "px-3" : "min-w-105 px-6")}>
{/* Continuous 2-Month Horizontal Rail */}
<div className="relative h-1.5 w-full bg-muted/70 dark:bg-zinc-800 rounded-full">
{/* Start Pin (Today) */}
<div
className="absolute -left-1 top-1/2 -translate-y-1/2 size-2.5 rounded-full bg-primary/40 ring-2 ring-background"
title={`Today: ${today.format('DD MMM YYYY')}`}
/>
{/* End Pin (+2 Months) */}
<div
className="absolute -right-1 top-1/2 -translate-y-1/2 size-2.5 rounded-full bg-muted-foreground/40 ring-2 ring-background"
title={`End: ${maxDate.format('DD MMM YYYY')}`}
/>
{/* Month Boundary Tick Markers */}
{monthTicks.map((tick, idx) => (
<div
key={idx}
className="absolute -top-1 -translate-x-1/2 flex flex-col items-center pointer-events-none select-none z-0"
style={{ left: `${tick.percent}%` }}
>
<div className="w-px h-3.5 bg-border/80 dark:bg-zinc-700" />
<span className="text-[9px] text-muted-foreground/70 font-mono mt-0.5 whitespace-nowrap">
{tick.label}
</span>
</div>
))}
{/* If no milestones in range, show gentle notice */}
{groups.length === 0 && (
<div className="absolute inset-0 flex items-center justify-center pointer-events-none">
<span className="text-[10px] text-muted-foreground/70 italic mt-6 select-none whitespace-nowrap">
No upcoming milestones in the next 2 months
</span>
</div>
)}
{/* 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 (
<div
key={group.dateKey}
className="absolute -translate-x-1/2 -top-1 z-20 flex flex-col items-center group/node"
style={{ left: `${group.percent}%` }}
>
{/* Vertical Tick Line connecting Rail to Dot */}
<div
className={cn(
"w-0.5 h-3.5 sm:h-4 rounded-full transition-all duration-200 group-hover/node:scale-y-125",
isMulti ? "bg-amber-400 dark:bg-amber-500" : primaryMeta.tickLine
)}
/>
{/* HoverCard Trigger (Dot + Short Badge + Date) */}
<HoverCard openDelay={40} closeDelay={150}>
<HoverCardTrigger asChild>
<Link
href={`/task/client-detail/${client.id}/${firstItem.project_assignment_id}`}
className="flex flex-col items-center cursor-pointer transition-transform hover:scale-110 focus:outline-none hover:bg-white hover:z-9999"
aria-label={`Milestones on ${group.date.format('DD MMM YYYY')}`}
>
{/* Dot / Multi-Dot Indicator */}
{isMulti ? (
<div
className="relative size-4 sm:size-4.5 rounded-full bg-linear-to-br from-amber-400 via-rose-500 to-violet-600 p-[1.5px] shadow-sm ring-2 ring-background hover:ring-primary/40 transition-shadow"
title={`${group.items.length} agendas on this date`}
>
<div className="w-full h-full rounded-full bg-background flex items-center justify-center">
<span className="text-[9px] font-black text-foreground leading-none">
{group.items.length}
</span>
</div>
</div>
) : (
<div
className={cn(
"size-3 sm:size-3.5 rounded-full ring-2 ring-background shadow-xs transition-all duration-200 group-hover/node:ring-4",
primaryMeta.dotBg,
primaryMeta.ringColor
)}
title={`${primaryMeta.fullName} on ${group.date.format('DD MMM YYYY')}`}
/>
)}
{/* Short Badge underneath dot */}
<div
className={cn(
"mt-1 px-1.5 py-0.5 rounded text-[9px] font-bold uppercase tracking-wider border whitespace-nowrap shadow-2xs leading-none transition-colors",
isMulti
? "bg-amber-500/15 dark:bg-amber-950/60 text-amber-700 dark:text-amber-300 border-amber-500/40"
: cn(primaryMeta.badgeBg, primaryMeta.badgeText, primaryMeta.badgeBorder)
)}
>
{isMulti ? `${group.items.length} Agendas` : primaryMeta.shortName}
</div>
{/* Date Label strictly underneath */}
<span className="text-[9px] sm:text-[10px] text-muted-foreground font-medium whitespace-nowrap mt-0.5 select-none">
{group.date.format('DD MMM')}
</span>
</Link>
</HoverCardTrigger>
{/* Rich Hover Card Content */}
<HoverCardContent
side="top"
align="center"
sideOffset={8}
className="w-[calc(100vw-3rem)] max-w-sm sm:w-96 p-3.5 z-50 shadow-xl border border-border/80 bg-popover/95 backdrop-blur-md rounded-xl"
>
{/* Header */}
<div className="flex items-center justify-between pb-2 mb-2.5 border-b border-border/60">
<div className="flex items-center gap-1.5 text-foreground font-semibold text-xs">
<Calendar className="w-3.5 h-3.5 text-primary shrink-0" />
<span>{group.date.format('dddd, DD MMMM YYYY')}</span>
</div>
<span className="text-[10px] font-medium px-2 py-0.5 rounded-md bg-muted text-muted-foreground">
{group.diffDays === 0
? 'Today'
: group.diffDays === 1
? 'Tomorrow'
: `In ${group.diffDays} days`}
</span>
</div>
{/* Subtitle / Counter */}
<div className="text-[11px] text-muted-foreground mb-2">
{group.items.length === 1
? '1 milestone agenda on this date:'
: `${group.items.length} milestone agendas on this date:`}
</div>
{/* List of Assignment Milestones */}
<div className="space-y-2 max-h-64 overflow-y-auto pr-1">
{group.items.map((item, itemIdx) => (
<Link
key={item.id || itemIdx}
href={`/task/client-detail/${client.id}/${item.project_assignment_id}`}
className="group/item block p-2.5 rounded-lg border border-border/70 bg-muted/30 hover:bg-muted/60 hover:border-his/60 transition-all duration-200"
>
<div className="flex items-center justify-between gap-2">
<BadgeStatus status={item.project_assignment_status?.name} />
<span className="text-[10px] text-muted-foreground font-medium flex items-center gap-0.5 group-hover/item:text-foreground transition-colors">
<span>View Assignment</span>
<ChevronRight className="w-3 h-3 group-hover/item:translate-x-0.5 transition-transform" />
</span>
</div>
<div className="mt-2 space-y-1">
{item.project_name && (
<div className="text-xs font-semibold text-foreground truncate">
{item.project_name}
{item.project_code && (
<span className="ml-1 text-[10px] font-mono font-normal text-muted-foreground">
({item.project_code})
</span>
)}
</div>
)}
{item.wo_name && (
<div className="text-[11px] text-muted-foreground truncate">
WO: <span className="font-medium text-foreground">{item.wo_name}</span>
{item.wo_code && (
<span className="ml-1 text-[10px] font-mono text-muted-foreground/80">
[{item.wo_code}]
</span>
)}
</div>
)}
{item.description && (
<p className="text-[11px] text-muted-foreground italic line-clamp-2 mt-1">
&ldquo;{item.description}&rdquo;
</p>
)}
<div className="pt-1 flex items-center gap-1.5 text-[10px] text-muted-foreground">
<Clock className="w-3 h-3 shrink-0" />
<span>
{dayjs(item.start_date).format('DD MMM YYYY')}
{item.end_date
? ` – ${dayjs(item.end_date).format('DD MMM YYYY')}`
: ''}
</span>
</div>
</div>
</Link>
))}
</div>
</HoverCardContent>
</HoverCard>
</div>
);
})}
</div>
</div>
</div>
</div>
);
}