Files
hats-app/app/(dashboard)/task/client-detail/[client_id]/[assignment_id]/DevelopmentActivityLog.tsx
T
Ichsan Kurniawan 3fbecab072 Add assignment progress and activity logging
Add modals for updating real progress and recording activity logs, and enhance the milestone timeline with date-based positioning and activity details. Update dashboard metrics to show active internal memos.
2026-10-16 15:37:20 +07:00

270 lines
15 KiB
TypeScript

"use client";
import { useState } from 'react';
import dynamic from 'next/dynamic';
import { IProjectAssignmentHistory } from '../../../type.assignment';
import { FileText, ChevronRight, ChevronLeft, History, Plus } from 'lucide-react';
import { Button } from '@/components/ui/button';
import BadgeStatus from '@/components/ui/badge-status';
import dayjs from 'dayjs';
const ActivityHistoryModal = dynamic(
() => import('./ActivityHistoryModal').then((mod) => mod.ActivityHistoryModal),
{ ssr: false }
);
const AddActivityLogModal = dynamic(
() => import('./AddActivityLogModal'),
{ ssr: false }
);
interface DevelopmentActivityLogProps {
history?: IProjectAssignmentHistory[];
assignmentId?: number;
assignmentName?: string;
assignmentCode?: string;
onLogAdded?: () => void;
}
export function DevelopmentActivityLog({
history = [],
assignmentId,
assignmentName,
assignmentCode,
onLogAdded,
}: DevelopmentActivityLogProps) {
const [selectedItem, setSelectedItem] = useState<IProjectAssignmentHistory | null>(null);
const [isAddModalOpen, setIsAddModalOpen] = useState(false);
const [currentPage, setCurrentPage] = useState(1);
const [itemsPerPage, setItemsPerPage] = useState(5);
// Sort newest first
const sortedHistory = [...history].sort((a, b) => {
return dayjs(b.created_at).valueOf() - dayjs(a.created_at).valueOf();
});
const totalItems = sortedHistory.length;
const totalPages = Math.max(1, Math.ceil(totalItems / itemsPerPage));
const safeCurrentPage = Math.min(currentPage, totalPages);
const startIndex = (safeCurrentPage - 1) * itemsPerPage;
const paginatedItems = sortedHistory.slice(startIndex, startIndex + itemsPerPage);
const getDotColor = (index: number, desc: string) => {
const lower = desc.toLowerCase();
// Technical status progression colors matching badge variants
if (lower.includes('development') || lower.includes('dev')) return 'bg-rose-400 dark:bg-rose-600';
if (lower.includes('system integration') || lower.includes('sit')) return 'bg-orange-400 dark:bg-orange-600';
if (lower.includes('user acceptance') || lower.includes('uat')) return 'bg-yellow-400 dark:bg-yellow-500';
if (lower.includes('production trial') || lower.includes('ptr')) return 'bg-emerald-400 dark:bg-emerald-600';
if (lower.includes('go-live') || lower.includes('go live')) return 'bg-cyan-400 dark:bg-cyan-600';
if (lower.includes('bast') || lower.includes('handover')) return 'bg-violet-400 dark:bg-violet-600';
if (lower.includes('warranty')) return 'bg-fuchsia-400 dark:bg-fuchsia-600';
// Administrative and other events
if (lower.includes('create')) return 'bg-emerald-500';
if (lower.includes('assign')) return 'bg-sky-500';
if (lower.includes('status to active') || lower.includes('status to completed')) return 'bg-emerald-500';
if (lower.includes('on hold') || lower.includes('issue')) return 'bg-amber-500';
const colors = ['bg-rose-400', 'bg-orange-400', 'bg-yellow-400', 'bg-emerald-400', 'bg-cyan-400', 'bg-violet-400', 'bg-fuchsia-400'];
return colors[index % colors.length];
};
return (
<>
<section className="rounded-2xl border border-border/80 bg-card p-5 sm:p-6 transition-all duration-200">
<div className="mb-4 flex items-center justify-between gap-3 flex-wrap">
<h2 className="flex items-center gap-2.5 text-base font-semibold tracking-tight text-foreground">
<span className="flex h-7 w-7 items-center justify-center rounded-lg bg-muted text-muted-foreground">
<FileText className="h-4 w-4" />
</span>
Development Activity Log
{totalItems > 0 && (
<span className="rounded-lg bg-muted px-2 py-0.5 font-mono text-xs font-medium text-muted-foreground">
{totalItems}
</span>
)}
</h2>
{assignmentId && (
<Button
type="button"
size="sm"
onClick={() => setIsAddModalOpen(true)}
className="h-8 gap-1.5 bg-his hover:bg-his/90 text-white font-medium text-xs shadow-xs transition-transform active:scale-[0.98]"
>
<Plus className="h-3.5 w-3.5" />
<span>Add Activity Log</span>
</Button>
)}
</div>
{totalItems === 0 ? (
<div className="flex flex-col items-center justify-center py-10 text-center rounded-xl border border-dashed border-border/70 bg-muted/10">
<History className="h-8 w-8 text-muted-foreground/50 mb-2" />
<p className="text-sm font-medium text-foreground">No development activity logs yet</p>
<p className="text-xs text-muted-foreground mt-0.5">
Status updates and assignment events will be logged here automatically.
</p>
</div>
) : (
<div className="space-y-3">
<div className="overflow-x-auto rounded-xl border border-border/70">
<table className="w-full text-left text-xs border-collapse">
<thead>
<tr className="border-b border-border/70 bg-muted/40 text-muted-foreground">
<th className="py-3 px-4 font-semibold w-40 sm:w-48">Date</th>
<th className="py-3 px-4 font-semibold w-24">Type</th>
<th className="py-3 px-4 font-semibold">Description</th>
<th className="py-3 px-3 w-8" aria-label="Action"></th>
</tr>
</thead>
<tbody className="divide-y divide-border/60">
{paginatedItems.map((item, idx) => {
const globalIndex = startIndex + idx;
const dotColor = getDotColor(globalIndex, item.description);
const formattedDate = dayjs(item.created_at).isValid()
? dayjs(item.created_at).format('D MMM YYYY')
: 'N/A';
return (
<tr
key={item.id}
onClick={() => setSelectedItem(item)}
className="group cursor-pointer hover:bg-muted/40 transition-colors"
title="Click to view JSON comparison"
>
{/* Date Column with dot */}
<td className="py-3.5 px-4 font-medium text-foreground whitespace-nowrap">
<div className="flex items-center gap-2.5">
<span className={`h-2.5 w-2.5 shrink-0 rounded-full ${dotColor}`} />
<span className="font-mono text-xs">{formattedDate}</span>
</div>
</td>
{/* Type Column with BadgeStatus */}
<td className="py-3.5 px-4 whitespace-nowrap">
{item.type ? (
<BadgeStatus status={item.type} />
) : (
<span className="text-muted-foreground/40 text-[11px] font-mono">—</span>
)}
</td>
{/* Description Column */}
<td className="py-3.5 px-4 text-foreground/90 font-normal leading-relaxed group-hover:text-foreground transition-colors">
<span className="line-clamp-2">{item.description}</span>
</td>
{/* Subtle Arrow Chevron */}
<td className="py-3.5 px-3 text-muted-foreground/40 group-hover:text-his transition-colors text-right">
<ChevronRight className="h-4 w-4 inline-block group-hover:translate-x-0.5 transition-transform" />
</td>
</tr>
);
})}
</tbody>
</table>
</div>
{/* Pagination Bar with Show per page selector */}
<div className="flex flex-col sm:flex-row items-center justify-between gap-3 pt-2 px-1 text-xs text-muted-foreground">
{/* Left: Show per page & count summary */}
<div className="flex items-center gap-3 font-mono text-[11px]">
<div className="flex items-center gap-1.5">
<span>Show</span>
<select
value={itemsPerPage}
onChange={(e) => {
setItemsPerPage(Number(e.target.value));
setCurrentPage(1);
}}
className="h-6.5 rounded-md border border-border bg-muted/40 px-2 py-0.5 text-xs text-foreground focus:outline-none focus:ring-1 focus:ring-his font-mono cursor-pointer"
>
<option value={5}>5</option>
<option value={10}>10</option>
<option value={20}>20</option>
<option value={50}>50</option>
</select>
<span>per page</span>
</div>
<span className="text-border">·</span>
<div>
Showing <span className="font-semibold text-foreground">{startIndex + 1}</span> to{' '}
<span className="font-semibold text-foreground">
{Math.min(startIndex + itemsPerPage, totalItems)}
</span>{' '}
of <span className="font-semibold text-foreground">{totalItems}</span> logs
</div>
</div>
{/* Right: Page navigation buttons */}
{totalPages > 1 && (
<div className="flex items-center gap-1.5">
<Button
variant="outline"
size="sm"
onClick={() => setCurrentPage((p) => Math.max(1, p - 1))}
disabled={safeCurrentPage <= 1}
className="h-7 px-2.5 text-xs gap-1"
>
<ChevronLeft className="h-3.5 w-3.5" />
<span>Prev</span>
</Button>
{Array.from({ length: totalPages }, (_, i) => i + 1).map((pageNum) => (
<Button
key={pageNum}
variant={pageNum === safeCurrentPage ? 'default' : 'outline'}
size="sm"
onClick={() => setCurrentPage(pageNum)}
className={`h-7 w-7 p-0 text-xs font-mono ${
pageNum === safeCurrentPage
? 'bg-his text-white hover:bg-his/90'
: 'text-muted-foreground hover:text-foreground'
}`}
>
{pageNum}
</Button>
))}
<Button
variant="outline"
size="sm"
onClick={() => setCurrentPage((p) => Math.min(totalPages, p + 1))}
disabled={safeCurrentPage >= totalPages}
className="h-7 px-2.5 text-xs gap-1"
>
<span>Next</span>
<ChevronRight className="h-3.5 w-3.5" />
</Button>
</div>
)}
</div>
</div>
)}
</section>
{/* Modal for JSON comparison on row click */}
<ActivityHistoryModal
historyItem={selectedItem}
open={!!selectedItem}
onClose={() => setSelectedItem(null)}
/>
{/* Modal for adding manual activity log */}
{assignmentId && (
<AddActivityLogModal
assignmentId={assignmentId}
assignmentName={assignmentName}
assignmentCode={assignmentCode}
open={isAddModalOpen}
onClose={() => setIsAddModalOpen(false)}
onSuccess={onLogAdded}
/>
)}
</>
);
}