From 390d03204a24f68abb5e7b4c1550c605e366efdc Mon Sep 17 00:00:00 2001 From: Ichsan Kurniawan <43689091+ichsankurnia@users.noreply.github.com> Date: Fri, 16 Oct 2026 19:26:25 +0700 Subject: [PATCH] Add activity log timeline Show assignment history on a date-based milestone timeline and refine milestone date positioning. Update activity log submission and description limits, refresh progress editing controls, and adjust the system log badge color. --- .../ActivityLogMilestoneTimeline.tsx | 418 ++++++++++++++++++ .../[assignment_id]/AddActivityLogModal.tsx | 38 +- .../AssignmentDetailComponent.tsx | 23 +- .../ProjectMilestoneTimeline.tsx | 136 +++--- .../UpdateRealProgressModal.tsx | 109 +++-- components/ui/badge.tsx | 2 +- 6 files changed, 591 insertions(+), 135 deletions(-) create mode 100644 app/(dashboard)/task/client-detail/[client_id]/[assignment_id]/ActivityLogMilestoneTimeline.tsx diff --git a/app/(dashboard)/task/client-detail/[client_id]/[assignment_id]/ActivityLogMilestoneTimeline.tsx b/app/(dashboard)/task/client-detail/[client_id]/[assignment_id]/ActivityLogMilestoneTimeline.tsx new file mode 100644 index 0000000..c93226e --- /dev/null +++ b/app/(dashboard)/task/client-detail/[client_id]/[assignment_id]/ActivityLogMilestoneTimeline.tsx @@ -0,0 +1,418 @@ +"use client"; + +import { useState, useMemo } from 'react'; +import { IProjectAssignmentHistory } from '../../../type.assignment'; +import dayjs from 'dayjs'; +import isSameOrAfter from 'dayjs/plugin/isSameOrAfter'; +import isSameOrBefore from 'dayjs/plugin/isSameOrBefore'; +import { History, Activity, Calendar, Clock, User, ChevronRight, Info } from 'lucide-react'; +import { + HoverCard, + HoverCardTrigger, + HoverCardContent, +} from '@/components/ui/hover-card'; +import BadgeStatus from '@/components/ui/badge-status'; + +dayjs.extend(isSameOrAfter); +dayjs.extend(isSameOrBefore); + +interface ActivityLogMilestoneTimelineProps { + history?: IProjectAssignmentHistory[]; + startDate?: string | null; + endDate?: string | null; +} + +interface IMonthBoundary { + date: string; + label: string; + percent: number; +} + +interface IDateActivityGroup { + dateKey: string; + compactDate: string; + fullDate: string; + percent: number; + activities: IProjectAssignmentHistory[]; +} + +export function ActivityLogMilestoneTimeline({ + history = [], + startDate, + endDate, +}: ActivityLogMilestoneTimelineProps) { + const [hoveredKey, setHoveredKey] = useState(null); + + // Extract valid history items with valid timestamps + const validHistory = useMemo(() => { + return history + .filter((item) => Boolean(item?.created_at && dayjs(item.created_at).isValid())) + .sort((a, b) => dayjs(a.created_at).valueOf() - dayjs(b.created_at).valueOf()); + }, [history]); + + // Boundary dates: WO or IM start date to WO end date (falling back to earliest/latest activity date) + const effectiveStart = useMemo(() => { + if (startDate && dayjs(startDate).isValid()) return startDate; + if (validHistory.length > 0) return dayjs(validHistory[0].created_at).format('YYYY-MM-DD'); + return dayjs().format('YYYY-MM-DD'); + }, [startDate, validHistory]); + + const effectiveEnd = useMemo(() => { + if (endDate && dayjs(endDate).isValid()) return endDate; + if (validHistory.length > 0) return dayjs(validHistory[validHistory.length - 1].created_at).format('YYYY-MM-DD'); + return dayjs(effectiveStart).add(60, 'day').format('YYYY-MM-DD'); + }, [endDate, validHistory, effectiveStart]); + + // Day boundaries for exact calendar day percentage calculation + const startDay = useMemo(() => dayjs(effectiveStart).startOf('day'), [effectiveStart]); + const endDay = useMemo(() => dayjs(effectiveEnd).startOf('day'), [effectiveEnd]); + const totalDays = useMemo(() => Math.max(1, endDay.diff(startDay, 'day')), [endDay, startDay]); + + const startFormatted = effectiveStart && dayjs(effectiveStart).isValid() ? dayjs(effectiveStart).format('D MMM YYYY') : ''; + const endFormatted = effectiveEnd && dayjs(effectiveEnd).isValid() ? dayjs(effectiveEnd).format('D MMM YYYY') : ''; + + const todayDay = useMemo(() => dayjs().startOf('day'), []); + const todayFormatted = dayjs().format('D MMM YYYY'); + + // Today in range calculation + const isTodayInRange = useMemo(() => { + if (!effectiveStart || !effectiveEnd) return false; + return (todayDay.isSame(startDay, 'day') || todayDay.isAfter(startDay)) && + (todayDay.isSame(endDay, 'day') || todayDay.isBefore(endDay)); + }, [effectiveStart, effectiveEnd, todayDay, startDay, endDay]); + + const todayPercent = useMemo(() => { + if (!isTodayInRange) return 0; + const diff = todayDay.diff(startDay, 'day'); + const pct = (diff / totalDays) * 100; + return Math.min(100, Math.max(0, pct)); + }, [isTodayInRange, todayDay, startDay, totalDays]); + + // Calculate month boundary vertical ticks (e.g. 1 Oct, 1 Nov, 1 Dec) + const monthBoundaries = useMemo(() => { + if (!effectiveStart || !effectiveEnd) return []; + const list: IMonthBoundary[] = []; + let cur = startDay.startOf('month'); + const limit = endDay.endOf('month'); + + while (cur.isSameOrBefore(limit, 'day')) { + if ((cur.isSame(startDay, 'day') || cur.isAfter(startDay)) && (cur.isSame(endDay, 'day') || cur.isBefore(endDay))) { + const diff = cur.diff(startDay, 'day'); + const pct = (diff / totalDays) * 100; + list.push({ + date: cur.format('YYYY-MM-DD'), + label: cur.format('1 MMM'), + percent: Math.min(100, Math.max(0, pct)), + }); + } + cur = cur.add(1, 'month').startOf('month'); + } + return list; + }, [effectiveStart, effectiveEnd, startDay, endDay, totalDays]); + + // Group activities by date (YYYY-MM-DD) and calculate proportional position + const dateGroups = useMemo(() => { + const groupsMap = new Map(); + + validHistory.forEach((item) => { + const dateKey = dayjs(item.created_at).format('YYYY-MM-DD'); + const list = groupsMap.get(dateKey) || []; + list.push(item); + groupsMap.set(dateKey, list); + }); + + const result: IDateActivityGroup[] = []; + + groupsMap.forEach((activities, dateKey) => { + const targetDay = dayjs(dateKey).startOf('day'); + const diff = targetDay.diff(startDay, 'day'); + const pct = (diff / totalDays) * 100; + + // Sort newest first within the date + activities.sort((a, b) => dayjs(b.created_at).valueOf() - dayjs(a.created_at).valueOf()); + + result.push({ + dateKey, + compactDate: dayjs(dateKey).format('D MMM'), + fullDate: dayjs(dateKey).format('dddd, D MMMM YYYY'), + percent: Math.min(100, Math.max(0, pct)), + activities, + }); + }); + + // Sort chronologically by date + return result.sort((a, b) => dayjs(a.dateKey).valueOf() - dayjs(b.dateKey).valueOf()); + }, [validHistory, startDay, totalDays]); + + const isAnyHovered = hoveredKey !== null; + const totalActivities = validHistory.length; + + return ( +
+ {/* Header */} +
+
+ + + +

+ Milestone Activity Log +

+ {totalActivities > 0 && ( + + {totalActivities} {totalActivities === 1 ? 'activity' : 'activities'} · {dateGroups.length} {dateGroups.length === 1 ? 'date' : 'dates'} + + )} +
+
+ {startFormatted} + → + {endFormatted} +
+
+ + {/* Continuous Timeline Rail Container with Horizontal Scroll */} +
+
+ {/* The Proportional Rail Area */} +
+ {/* Base Track Line */} +
+ + {/* Active Milestone Progress Track (colored green from Start to Today if today is in milestone) */} + {isTodayInRange && todayPercent > 0 && ( +
+ )} + {todayDay.isAfter(endDay) && ( +
+ )} + + {/* Month Boundary Vertical Tick Marks (e.g. 1 Oct, 1 Nov, 1 Dec) */} + {monthBoundaries.map((mb, idx) => { + if (mb.percent <= 1 || mb.percent >= 99) return null; + + return ( +
+ {/* Vertical tick line passing across the rail */} +
+ + {/* Month milestone label underneath */} +
+ + {mb.label} + +
+
+ ); + })} + + {/* 1. START BOUNDARY NODE (at 0%) */} +
+
+ + Start + +
+
+ {startFormatted && ( +
+ + {dayjs(effectiveStart).format('D MMM')} + +
+ )} +
+ + {/* 2. DYNAMIC TODAY MARKER (Only if in range) */} + {isTodayInRange && ( +
+
+
+ Today, {todayFormatted} +
+
+
+
+
+ )} + + {/* 3. ACTIVITY LOG NODES (Vertical small line with dot beneath containing activity count) */} + {dateGroups.map((group) => { + const isHovered = hoveredKey === group.dateKey; + const isDimmed = isAnyHovered && !isHovered; + const count = group.activities.length; + + return ( + setHoveredKey(open ? group.dateKey : null)} + > + +
setHoveredKey(group.dateKey)} + onMouseLeave={() => setHoveredKey(null)} + className={`group absolute top-1/2 -translate-x-1/2 flex flex-col items-center cursor-pointer transition-all duration-200 ${isHovered + ? 'z-50 scale-105' + : isDimmed + ? 'z-10 opacity-30 blur-[0.2px] scale-95' + : 'z-20 opacity-100 hover:scale-110' + }`} + style={{ left: `${group.percent}%` }} + > + {/* Extended vertical hover hit zone */} +
+ + {/* Vertical Small Line (Garis vertikal kecil di bawah rail) */} +
+ + {/* Dot beneath the vertical line containing the activity count */} +
= 10 + ? 'min-w-6 h-6 px-1 text-[10px]' + : 'w-5.5 h-5.5 text-[10px]' + } ${isHovered + ? 'scale-115 bg-his text-white ring-4 ring-his/40 shadow-lg' + : 'bg-his text-white shadow-2xs group-hover:ring-2 group-hover:ring-his/30' + }`} + > + {count} +
+ + {/* Date beneath the dot */} +
+ {isHovered && + + {group.compactDate} + + } +
+
+ + + {/* Hover Card Content displaying all activities for this date */} + +
+ {/* Header */} +
+
+

+ + {group.fullDate} +

+

+ {count} {count === 1 ? 'activity logged' : 'activities logged'} on this date +

+
+ + {count} {count === 1 ? 'activity' : 'activities'} + +
+ + {/* Scrollable list of activities on this date */} +
+ {group.activities.map((item, idx) => { + const timeStr = dayjs(item.created_at).format('HH:mm'); + const authorName = item.user?.fullname || item.user?.nickname || 'System'; + + return ( +
+ {/* Activity Meta Header */} +
+
+ + {timeStr} + · + + + {authorName} + +
+ + {item.type && ( + + )} +
+ + {/* Description text */} +

+ {item.description} +

+
+ ); + })} +
+
+
+ + ); + })} + + {/* 4. END BOUNDARY NODE (at 100%) */} +
+
+ + End + +
+
+ {endFormatted && ( +
+ + {dayjs(effectiveEnd).format('D MMM')} + +
+ )} +
+
+
+
+ + {/* Empty state notification if no logs exist */} + {dateGroups.length === 0 && ( +
+ + Belum ada riwayat aktivitas (activity log) yang tercatat pada rentang timeline ini. +
+ )} +
+ ); +} diff --git a/app/(dashboard)/task/client-detail/[client_id]/[assignment_id]/AddActivityLogModal.tsx b/app/(dashboard)/task/client-detail/[client_id]/[assignment_id]/AddActivityLogModal.tsx index 2505623..7d306f7 100644 --- a/app/(dashboard)/task/client-detail/[client_id]/[assignment_id]/AddActivityLogModal.tsx +++ b/app/(dashboard)/task/client-detail/[client_id]/[assignment_id]/AddActivityLogModal.tsx @@ -54,10 +54,7 @@ export default function AddActivityLogModal({ const [description, setDescription] = useState(''); const [isSubmitting, setIsSubmitting] = useState(false); - // Fetch types from GET v1/project-assignment-status?type=log - const { data: fetchedData, isLoading: isLoadingTypes } = useFetchWithAxios( - open ? '/project-assignment-status?type=log' : null - ); + const { data: fetchedData, isLoading: isLoadingTypes } = useFetchWithAxios(open ? '/project-assignment-status?type=log' : null); const logTypes: IActivityLogType[] = useMemo(() => { if (Array.isArray(fetchedData) && fetchedData.length > 0) { @@ -93,17 +90,10 @@ export default function AddActivityLogModal({ const payload = { project_assignment_id: Number(assignmentId), type: typeName, - project_assignment_status_id: activeType?.id ? Number(activeType.id) : undefined, description: description.trim(), }; - // Try primary endpoint POST /project-assignment/history - let res = await serverApiCall('POST', '/project-assignment/history', payload); - - // Fallback endpoint if 404 - if (!res.success && res.message?.includes('404')) { - res = await serverApiCall('POST', `/project-assignment/${assignmentId}/history`, payload); - } + let res = await serverApiCall('POST', '/project-assignment-history', payload); setIsSubmitting(false); @@ -165,11 +155,7 @@ export default function AddActivityLogModal({ )} - {activeType && ( @@ -182,20 +168,12 @@ export default function AddActivityLogModal({ {logTypes.map((type) => ( - +
- - {type.name} - + {type.name} {type.description && type.description !== type.name && ( - - {type.description} - + {type.description} )}
@@ -213,13 +191,13 @@ export default function AddActivityLogModal({ Description * - {description.length} / 1000 + {description.length} / 500