mirror of
https://github.com/ichsankurnia/hats-app.git
synced 2026-12-05 10:56:28 +00:00
279b642a7a
Enforce page access and action-level permissions across admin and task views, including exports and record actions. Move user creation to a client component behind server-side permission checks, standardize data fetching, and remove the deprecated user-role route.
282 lines
20 KiB
TypeScript
282 lines
20 KiB
TypeScript
"use client";
|
|
|
|
import { motion } from 'motion/react';
|
|
import Link from 'next/link';
|
|
import { FileText, Users, ClipboardList, MoreHorizontal, ExternalLink, Trash, PencilLine } from 'lucide-react';
|
|
import { Card900 } from '@/components/ui/card';
|
|
import { LazyImage } from '@/components/ui/lazy-image';
|
|
import { Button } from '@/components/ui/button';
|
|
import {
|
|
DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger,
|
|
} from '@/components/ui/dropdown-menu';
|
|
import {
|
|
Tooltip, TooltipContent, TooltipTrigger,
|
|
} from '@/components/ui/tooltip';
|
|
import {
|
|
HoverCard, HoverCardContent, HoverCardTrigger,
|
|
} from '@/components/ui/hover-card';
|
|
import BadgeStatus from '@/components/ui/badge-status';
|
|
import { cn } from '@/lib/utils';
|
|
import { IClient, CLIENT_FALLBACK_IMAGE } from '../../types';
|
|
import { getProjectStatusBreakdown } from '../../utils/client-overview-helpers';
|
|
import dynamic from 'next/dynamic';
|
|
|
|
const ClientMilestoneTimeline = dynamic(() => import('./ClientMilestoneTimeline'), {
|
|
ssr: false,
|
|
loading: () => (
|
|
<div className="h-14 sm:h-16 w-full rounded-xl bg-muted/20 border border-border/40 animate-pulse flex items-center justify-between px-4 my-1">
|
|
<div className="h-2.5 w-16 sm:w-20 rounded bg-muted/60" />
|
|
<div className="h-1 flex-1 mx-4 sm:mx-8 rounded-full bg-muted/40" />
|
|
<div className="h-2.5 w-14 sm:w-16 rounded bg-muted/60" />
|
|
</div>
|
|
),
|
|
});
|
|
|
|
interface ClientListProps {
|
|
actions: string[]
|
|
clients: IClient[];
|
|
onEdit: (client: IClient) => void;
|
|
onDelete: (client: IClient) => void;
|
|
}
|
|
|
|
export default function ClientList({ clients, onEdit, onDelete, actions }: ClientListProps) {
|
|
return (
|
|
<div className="flex flex-col gap-6">
|
|
{clients.map((client, index) => {
|
|
const isActive = (client.status_name || '').toLowerCase() === 'active';
|
|
const stages = getProjectStatusBreakdown(client);
|
|
const maxStageCount = 10;
|
|
|
|
const totalProjects = client.total_project ?? 0;
|
|
const assignedPeople = client.total_user_assigned || client.users?.length || 0;
|
|
const activeWorkOrders = client.total_work_order ?? 0;
|
|
|
|
return (
|
|
<motion.div
|
|
key={client.id || index}
|
|
initial={{ opacity: 0, y: 10 }}
|
|
animate={{ opacity: 1, y: 0 }}
|
|
transition={{ duration: 0.25, delay: index * 0.04 }}
|
|
className="w-full"
|
|
>
|
|
<Card900 className="group overflow-hidden rounded-2xl border border-border/80 hover:border-his transition-all duration-300 p-5 sm:p-6 pb-0 sm:pb-0 shadow-xs flex flex-col gap-6">
|
|
{/* Top Section: 3-column layout (Identity | Status Breakdown | Quick Stats) */}
|
|
<div className="grid grid-cols-1 lg:grid-cols-12 gap-6 items-start">
|
|
{/* Left Column: Client Identity & Team (col-span-3) */}
|
|
<div className="lg:col-span-3 flex flex-col justify-between h-full gap-4 min-w-0">
|
|
<div className="space-y-3 min-w-0">
|
|
{/* Code Badge + Status Pill with HoverCard */}
|
|
<div className="flex items-center gap-2 flex-wrap">
|
|
{client.code && (
|
|
<Tooltip>
|
|
<TooltipTrigger asChild>
|
|
<span className="text-xs font-mono font-semibold px-2.5 py-0.5 rounded-md bg-muted/80 dark:bg-zinc-800/80 text-foreground border border-border/70 shrink-0 cursor-help">
|
|
{client.code}
|
|
</span>
|
|
</TooltipTrigger>
|
|
<TooltipContent className="text-xs">
|
|
<p className="font-semibold text-popover-foreground">Client Code</p>
|
|
<p className="text-[11px] font-mono text-muted-foreground">{client.code}</p>
|
|
</TooltipContent>
|
|
</Tooltip>
|
|
)}
|
|
|
|
{/* Status Pill with Hover Content Summary Status */}
|
|
<HoverCard openDelay={10} closeDelay={100}>
|
|
<HoverCardTrigger asChild>
|
|
<button
|
|
type="button"
|
|
className={cn(
|
|
"group/pill inline-flex items-center gap-1.5 rounded-full px-2.5 py-0.5 text-[11px] font-bold uppercase tracking-wider border shrink-0 cursor-help transition-all duration-200",
|
|
isActive
|
|
? "bg-teal-500/10 text-teal-500 border-teal-500/30 hover:bg-teal-500/20 dark:bg-teal-950/60 dark:text-teal-300 dark:border-teal-700/50 dark:hover:bg-teal-950/80"
|
|
: "bg-amber-500/10 text-amber-700 border-amber-500/30 hover:bg-amber-500/20 dark:bg-amber-950/60 dark:text-amber-300 dark:border-amber-700/50 dark:hover:bg-amber-950/80"
|
|
)}
|
|
>
|
|
<span
|
|
className={cn(
|
|
"w-1.5 h-1.5 rounded-full animate-pulse",
|
|
isActive ? "bg-teal-600 dark:bg-teal-400" : "bg-amber-600 dark:bg-amber-400"
|
|
)}
|
|
/>
|
|
<span>{client.status_name || (isActive ? 'ACTIVE' : 'INACTIVE')}</span>
|
|
</button>
|
|
</HoverCardTrigger>
|
|
<HoverCardContent side="bottom" align="start" className="w-[calc(100vw-3rem)] max-w-xs sm:w-72 p-3 text-xs">
|
|
<div className="font-semibold text-foreground">Status Summary</div>
|
|
<p className="text-muted-foreground mt-0.5">Status summary from WO or IM related to this client.</p>
|
|
<div className="flex flex-col mt-3 gap-2">
|
|
{client.project_status_summary && client.project_status_summary.length > 0 ? (
|
|
client.project_status_summary.map((item, key) => (
|
|
<div className="flex justify-between items-center gap-2" key={key}>
|
|
<BadgeStatus status={item.project_status} />
|
|
<span className="font-bold text-foreground">{item.total}</span>
|
|
</div>
|
|
))
|
|
) : (
|
|
<span className="italic text-muted-foreground">No status summary recorded.</span>
|
|
)}
|
|
</div>
|
|
</HoverCardContent>
|
|
</HoverCard>
|
|
</div>
|
|
|
|
{/* Brand Logo Presentation */}
|
|
<div className="pt-1">
|
|
<Link
|
|
href={`/task/client-detail/${client.id}`}
|
|
className="max-w-55 h-12 relative flex items-center group/logo"
|
|
>
|
|
<LazyImage
|
|
src={client.photo}
|
|
fallbackSrc={CLIENT_FALLBACK_IMAGE}
|
|
alt={client.name}
|
|
className="h-full w-auto bg-transparent max-h-12"
|
|
imgClassName="object-contain rounded object-left max-h-12 group-hover/logo:scale-105 transition-transform"
|
|
/>
|
|
</Link>
|
|
</div>
|
|
|
|
{/* Client Full Corporate Name */}
|
|
<Link
|
|
href={`/task/client-detail/${client.id}`}
|
|
className="text-xs sm:text-sm font-semibold uppercase tracking-tight text-foreground/90 hover:text-primary transition-colors block line-clamp-2"
|
|
title={client.name}
|
|
>
|
|
{client.name}
|
|
</Link>
|
|
|
|
{/* Team Avatars */}
|
|
<div className="flex -space-x-2 overflow-hidden items-center pt-1">
|
|
{client.users?.slice(0, 5).map((user, uKey) => (
|
|
<div key={user.id || uKey}>
|
|
<Tooltip>
|
|
<TooltipTrigger asChild>
|
|
<div className="inline-block relative">
|
|
<LazyImage
|
|
src={user?.avatar ? `${process.env.BASE_URL_IMG}/${user?.avatar}` : '/assets/no-avatar.jpg'}
|
|
alt={user?.fullname || user?.name || 'Team Member'}
|
|
className="w-7 h-7 rounded-full border-2 border-background cursor-help hover:scale-110 hover:z-10 transition-transform object-cover"
|
|
/>
|
|
</div>
|
|
</TooltipTrigger>
|
|
<TooltipContent className="text-xs">
|
|
<p className="font-semibold text-popover-foreground">{user?.name || user?.fullname}</p>
|
|
<p className="text-[10px] text-muted-foreground">{user?.position?.name || 'Team Member'}</p>
|
|
</TooltipContent>
|
|
</Tooltip>
|
|
</div>
|
|
))}
|
|
{client.users && client.users.length > 5 && (
|
|
<div className="w-7 h-7 rounded-full bg-accent ring-2 ring-background flex items-center justify-center text-[10px] font-bold text-foreground shrink-0">
|
|
+{client.users.length - 5}
|
|
</div>
|
|
)}
|
|
{(!client.users || client.users.length === 0) && (
|
|
<span className="text-[11px] text-muted-foreground italic">No team assigned</span>
|
|
)}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
{/* Middle Column: Project Status Breakdown (col-span-6) */}
|
|
<div className="lg:col-span-6 flex flex-col justify-start space-y-2.5 lg:px-4 lg:border-l lg:border-border/60 min-w-0 pt-4 lg:pt-0 border-t lg:border-t-0 border-border/50">
|
|
<h4 className="text-xs sm:text-sm font-bold text-foreground tracking-tight">
|
|
Project Status
|
|
</h4>
|
|
|
|
<div className="space-y-1.5 pt-0.5">
|
|
{stages.map((stage) => {
|
|
const barPercent = Math.min(100, (stage.count / maxStageCount) * 100);
|
|
return (
|
|
<div key={stage.id} className="flex items-center gap-2 sm:gap-3 text-xs">
|
|
{/* Stage Name + Dot matching technical status */}
|
|
<div className="flex items-center gap-2 w-48 sm:w-56 shrink-0 min-w-0">
|
|
<span className={cn("w-2 h-2 rounded-full shrink-0", stage.colorDot)} />
|
|
<span className="text-muted-foreground text-[11px] sm:text-xs truncate font-medium">
|
|
{stage.name}
|
|
</span>
|
|
</div>
|
|
|
|
{/* Progress Bar Container */}
|
|
<div className="flex-1 max-w-50 sm:max-w-60 bg-zinc-200/70 dark:bg-zinc-700/50 rounded-full h-2 overflow-hidden shrink-0">
|
|
<div
|
|
className={cn("h-full rounded-full transition-all duration-500", stage.colorBar)}
|
|
style={{ width: `${barPercent}%` }}
|
|
/>
|
|
</div>
|
|
|
|
{/* Count Number */}
|
|
<span className="text-[11px] sm:text-xs font-semibold text-foreground w-4 text-right shrink-0">
|
|
{stage.count}
|
|
</span>
|
|
</div>
|
|
);
|
|
})}
|
|
</div>
|
|
</div>
|
|
|
|
{/* Right Column: Quick Stats & Actions (col-span-3) */}
|
|
<div className="lg:col-span-3 flex flex-col justify-between h-full lg:border-l lg:border-border/60 lg:pl-5 space-y-4 pt-4 lg:pt-0 border-t lg:border-t-0 border-border/50">
|
|
<div className="space-y-3">
|
|
<div className="flex items-center justify-between text-xs py-1 border-b border-border/30">
|
|
<div className="flex items-center gap-2 text-muted-foreground">
|
|
<FileText className="w-4 h-4 text-muted-foreground" />
|
|
<span>Total Projects</span>
|
|
</div>
|
|
<span className="font-bold text-foreground text-sm">{totalProjects}</span>
|
|
</div>
|
|
|
|
<div className="flex items-center justify-between text-xs py-1 border-b border-border/30">
|
|
<div className="flex items-center gap-2 text-muted-foreground">
|
|
<Users className="w-4 h-4 text-muted-foreground" />
|
|
<span>Assigned People</span>
|
|
</div>
|
|
<span className="font-bold text-foreground text-sm">{assignedPeople}</span>
|
|
</div>
|
|
|
|
<div className="flex items-center justify-between text-xs py-1 border-b border-border/30">
|
|
<div className="flex items-center gap-2 text-muted-foreground">
|
|
<ClipboardList className="w-4 h-4 text-muted-foreground" />
|
|
<span>Work Orders</span>
|
|
</div>
|
|
<span className="font-bold text-foreground text-sm">{activeWorkOrders}</span>
|
|
</div>
|
|
</div>
|
|
|
|
{/* Action Buttons: View Detail + More Actions Dropdown */}
|
|
<div className="flex items-center justify-end gap-2 pt-2">
|
|
{actions?.includes('VIEW_CLIENT') &&
|
|
<Link href={`/task/client-detail/${client.id}`} className="flex-1">
|
|
<Button variant='default' size="sm" className="w-full text-xs h-8 gap-1.5">
|
|
<span>View Detail</span>
|
|
<ExternalLink className="w-3.5 h-3.5" />
|
|
</Button>
|
|
</Link>
|
|
}
|
|
{actions?.includes('UPDATE_CLIENT') &&
|
|
<Button onClick={() => onEdit(client)} title='Update Client' size='icon-sm' variant='outline' className='text-teal-500'>
|
|
<PencilLine />
|
|
</Button>
|
|
}
|
|
{actions?.includes('DELETE_CLIENT') &&
|
|
<Button onClick={() => onDelete(client)} title='Delete Client' size='icon-sm' variant='outline' className='text-rose-500'>
|
|
<Trash />
|
|
</Button>
|
|
}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
{/* Bottom Section: 2-Month Milestone Timeline */}
|
|
<div className="pt-3 border-t border-border/50">
|
|
<ClientMilestoneTimeline client={client} variant="list" />
|
|
</div>
|
|
</Card900>
|
|
</motion.div>
|
|
);
|
|
})}
|
|
</div>
|
|
);
|
|
}
|