Files
hats-app/app/(dashboard)/task/component/project-assignment/ConvertedAssignmentBadge.tsx
T
Ichsan Kurniawan c6aa1c912a Add ConvertedAssignmentBadge and IM conversion UI
Introduce ConvertedAssignmentBadge component to surface conversions between Internal Memos (IM) and Projects with hovercard details for both directions. Integrate the badge into AssignmentGrid and AssignmentList, add origin IM link when a project assignment lacks a project, and reuse formatAssignmentDate. Extend IProjectAssignment type to include im and list_wo fields to support the new UI data paths.
2026-10-13 13:29:37 +07:00

239 lines
12 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 Link from 'next/link';
import { IProjectAssignment } from '../../type.assignment';
import {
HoverCard,
HoverCardTrigger,
HoverCardContent,
} from '@/components/ui/hover-card';
import {
FileText,
ArrowUpRight,
FolderGit2,
Calendar,
ArrowRight,
} from 'lucide-react';
import BadgeStatus from '@/components/ui/badge-status';
import { formatAssignmentDate } from './AssignmentList';
interface ConvertedAssignmentBadgeProps {
assignment: IProjectAssignment;
clientId?: string | number;
className?: string;
}
export default function ConvertedAssignmentBadge({
assignment,
clientId,
className = '',
}: ConvertedAssignmentBadgeProps) {
const isProject = !!assignment.wo_name;
const effectiveClientId = clientId || assignment.client?.id;
// --- CASE 1: Project Assignment converted from an IM ---
const isConvertedFromIM = Boolean(isProject && (assignment.im_id || assignment.im));
const imData = assignment.im;
const imId = assignment.im_id || imData?.id;
const imName = imData?.im_name || assignment.im_name || 'Internal Memo';
const imCode = imData?.im_code || assignment.im_code;
const imStartDate = imData?.im_start_date || assignment.im_start_date;
const imEndDate = imData?.im_end_date || assignment.im_end_date;
const imDescription = imData?.im_description || assignment.im_description;
const imDetailUrl = effectiveClientId && imId ? `/task/client-detail/${effectiveClientId}/${imId}` : null;
// --- CASE 2: IM Assignment that has been converted to 1 or more Projects ---
const listWo = assignment.list_wo || [];
const isConvertedIM = Boolean(!isProject && listWo.length > 0);
// If neither condition applies, render nothing
if (!isConvertedFromIM && !isConvertedIM) {
return null;
}
// --- RENDER CASE 1: Project Assignment converted from an IM ---
if (isConvertedFromIM) {
return (
<HoverCard openDelay={120} closeDelay={150}>
<HoverCardTrigger asChild>
<span
role="button"
tabIndex={0}
onClick={(e) => e.stopPropagation()}
className={`inline-flex items-center gap-1 text-[10px] font-semibold px-2 py-0.5 rounded-full bg-purple-500/10 text-purple-700 dark:text-purple-400 border border-purple-500/25 hover:bg-purple-500/20 hover:border-purple-500/40 transition-all cursor-pointer shrink-0 select-none ${className}`}
>
<ArrowUpRight className="w-3 h-3 text-purple-600 dark:text-purple-400 shrink-0" />
<span>Converted from IM</span>
</span>
</HoverCardTrigger>
<HoverCardContent
align="start"
sideOffset={8}
onClick={(e) => e.stopPropagation()}
className="w-80 sm:w-92 p-0 shadow-2xl border-border/80 bg-popover/98 backdrop-blur-md rounded-xl overflow-hidden z-50 text-left"
>
{/* Header: Origin indicator & IM Code */}
<div className="px-3.5 py-3 border-b border-border/60 bg-muted/20 flex items-center justify-between gap-2">
<div className="flex items-center gap-1.5 text-purple-600 dark:text-purple-400">
<FileText className="w-3.5 h-3.5 shrink-0" />
<span className="text-[10px] font-mono font-semibold tracking-wider">
Originating Memo
</span>
</div>
{imCode && (
<span className="text-[10px] font-mono font-semibold px-1.5 py-0.5 rounded bg-muted text-muted-foreground border border-border/60 shrink-0">
#{imCode}
</span>
)}
</div>
{/* Body: Title, Period, and Description callout */}
<div className="p-3.5 space-y-3">
<div>
<h4 className="text-sm font-semibold text-foreground leading-snug line-clamp-2 wrap-break-word">
{imName}
</h4>
{(imStartDate || imEndDate) && (
<p className="text-[11px] text-muted-foreground pt-1 flex items-center gap-1.5">
<Calendar className="w-3 h-3 text-muted-foreground/70 shrink-0" />
<span>
{formatAssignmentDate(imStartDate)} – {formatAssignmentDate(imEndDate)}
</span>
</p>
)}
</div>
{imDescription ? (
<div className="border-l-2 border-purple-500/40 pl-2.5 py-0.5">
<p className="text-xs text-muted-foreground leading-relaxed line-clamp-3 whitespace-pre-wrap">
{imDescription}
</p>
</div>
) : null}
</div>
{/* Footer: Direct Navigation Action */}
{imDetailUrl && (
<div className="px-3.5 py-2.5 border-t border-border/60 bg-muted/30 flex items-center justify-end">
<Link
href={imDetailUrl}
className="group inline-flex items-center gap-1 text-[11px] font-semibold text-his hover:text-his/90 transition-colors"
>
<span>View Internal Memo</span>
<ArrowRight className="w-3 h-3 group-hover:translate-x-0.5 transition-transform" />
</Link>
</div>
)}
</HoverCardContent>
</HoverCard>
);
}
// --- RENDER CASE 2: IM Assignment that has been converted to Project(s) ---
return (
<HoverCard openDelay={120} closeDelay={150}>
<HoverCardTrigger asChild>
<span
role="button"
tabIndex={0}
onClick={(e) => e.stopPropagation()}
className={`inline-flex items-center gap-1 text-[10px] font-semibold px-2 py-0.5 rounded-full bg-emerald-500/10 text-emerald-700 dark:text-emerald-300 border border-emerald-500/25 hover:bg-emerald-500/20 hover:border-emerald-500/40 transition-all cursor-pointer shrink-0 select-none ${className}`}
>
<FolderGit2 className="w-3 h-3 text-emerald-600 dark:text-emerald-400 shrink-0" />
<span>
Converted to {listWo.length} Project{listWo.length > 1 ? 's' : ''}
</span>
</span>
</HoverCardTrigger>
<HoverCardContent
align="start"
sideOffset={8}
onClick={(e) => e.stopPropagation()}
className="w-88 sm:w-104 p-0 shadow-2xl border-border/80 bg-popover/98 backdrop-blur-md rounded-xl overflow-hidden z-50 text-left"
>
{/* Header: Title, Total Count, and Shortcut Hint */}
<div className="px-4 py-3.5 border-b border-border/60 bg-muted/20 flex items-center justify-between gap-3">
<div className="flex items-center gap-2 min-w-0">
<FolderGit2 className="w-3.5 h-3.5 text-emerald-600 dark:text-emerald-400 shrink-0" />
<span className="text-[10px] font-mono font-semibold tracking-wider text-emerald-600 dark:text-emerald-400">
Converted Work Orders
</span>
<span className="text-[10px] font-semibold px-1.5 py-0.2 rounded-full bg-emerald-500/15 text-emerald-700 dark:text-emerald-300 border border-emerald-500/25">
{listWo.length}
</span>
</div>
<span className="text-[10px] text-muted-foreground/70 hidden sm:inline select-none">
Click row to open
</span>
</div>
{/* Body: High-craft flat row list (NO box-in-box nesting!) */}
<div className="divide-y divide-border/40 max-h-72 overflow-y-auto">
{listWo.map((woItem, i) => {
const woDetailUrl = effectiveClientId && woItem.id ? `/task/client-detail/${effectiveClientId}/${woItem.id}` : null;
const woTitle = woItem.wo_name || woItem.project?.name || 'Untitled Project';
const RowContent = (
<div className="px-4 py-2.5 hover:bg-muted/50 dark:hover:bg-zinc-800/50 transition-colors group/row">
{/* Top Line: Title, Code Tag, Arrow Cue */}
<div className="flex items-center justify-between gap-2.5">
<div className="min-w-0 flex-1 flex items-center gap-1.5">
<span className="font-semibold text-xs text-foreground group-hover/row:text-his transition-colors truncate">
{woTitle}
</span>
<ArrowUpRight className="w-3 h-3 text-muted-foreground/60 opacity-0 group-hover/row:opacity-100 group-hover/row:text-his transition-all shrink-0 -translate-x-0.5 group-hover/row:translate-x-0" />
</div>
{woItem.project_assignment_status?.name && (
<div className="shrink-0">
<BadgeStatus status={woItem.project_assignment_status.name} />
</div>
)}
</div>
{/* Bottom Line: Clean Schedule & Status Badge */}
<div className="flex items-center justify-between gap-2 pt-1 text-[11px] text-muted-foreground">
<div className="flex items-center gap-1.5 truncate">
<Calendar className="w-3 h-3 text-muted-foreground/70 shrink-0" />
<span className="truncate">
{formatAssignmentDate(woItem.wo_start_date)} – {formatAssignmentDate(woItem.wo_end_date)}
</span>
</div>
</div>
</div>
);
if (woDetailUrl) {
return (
<Link
key={woItem.id || i}
href={woDetailUrl}
className="block no-underline"
>
{RowContent}
</Link>
);
}
return (
<div key={woItem.id || i}>
{RowContent}
</div>
);
})}
</div>
{/* Footer: Quiet context summary */}
<div className="px-4 py-3 border-t border-border/60 bg-muted/20 flex items-center justify-between text-[11px] text-muted-foreground/80">
<span>Generated from this Internal Memo</span>
<span className="font-mono text-[10px]">
{listWo.length} {listWo.length === 1 ? 'assignment' : 'assignments'}
</span>
</div>
</HoverCardContent>
</HoverCard>
);
}