mirror of
https://github.com/ichsankurnia/hats-app.git
synced 2026-12-05 11:06:29 +00:00
2853954cbc
Guard against null/undefined values by adding optional chaining to property accesses. In AssignmentDetailComponent.tsx, use party?.name, party?.photo, party?.description and make initials accept a possibly undefined name (name?.split...). In AssignedPeople.tsx, check people?.length and use people?.slice and people?.length in the UI so rendering won't crash when the people prop is missing or undefined. These changes prevent runtime errors when props are absent.
273 lines
16 KiB
TypeScript
273 lines
16 KiB
TypeScript
"use client"
|
|
|
|
import { motion } from 'motion/react';
|
|
import { ArrowLeft, Briefcase, Calendar, History, UserPlus, ExternalLink, Building2, Hash, Users, Icon, FileText, CalendarRange } from 'lucide-react';
|
|
import { Button } from '@/components/ui/button';
|
|
import { Card, CardContent, CardHeader, CardTitle, CardDescription, Card900 } from '@/components/ui/card';
|
|
import { Badge } from '@/components/ui/badge';
|
|
import { cn } from '@/lib/utils';
|
|
import { useRouter } from 'next/navigation';
|
|
import { IClient } from '../../../types';
|
|
import { IProjectAssignment } from '../../../type.assignment';
|
|
import { useState } from 'react';
|
|
import BadgeStatus from '@/components/ui/badge-status';
|
|
import Link from 'next/link';
|
|
import AssignedPeople from '../project-detail/[project_id]/AssignedPeople';
|
|
import { ManageTeam } from '../../../component/ManageTeam';
|
|
import UpdateTimelineStatus from '../../../component/UpdateTimelineStatus';
|
|
import { LazyImage } from '@/components/ui/lazy-image';
|
|
|
|
interface Props {
|
|
client: IClient | undefined;
|
|
initialAssignment: IProjectAssignment;
|
|
}
|
|
|
|
export default function AssignmentDetail({ client, initialAssignment: assignment }: Props) {
|
|
const [action, setAction] = useState<'MANAGE_EMPLOYEE' | 'UPDATE_STATUS' | undefined>()
|
|
|
|
const router = useRouter();
|
|
|
|
const isProject = assignment.project?.code
|
|
|
|
const assignmentType = isProject ? 'Project' : 'IM'
|
|
const assignmentName = assignment.wo_name || assignment.im_name
|
|
const assignmentCode = assignment.wo_code || assignment.im_code
|
|
const assignmentStartDate = assignment.wo_start_date || assignment.im_start_date
|
|
const assignmentEndDate = assignment.wo_end_date || assignment.im_end_date
|
|
const assignmentDesc = assignment.wo_description || assignment.im_description
|
|
const assignmentStatus = assignment.project?.status || assignment.project_assignment_status?.name
|
|
|
|
const statuslist = assignment.project_assignment_status_detail?.filter(status => status.start_date) || []
|
|
|
|
const handleClose = () => {
|
|
setAction(undefined)
|
|
}
|
|
|
|
const formatDate = (date: string) =>
|
|
new Date(date).toLocaleDateString('en-US', { month: 'long', day: 'numeric', year: 'numeric' });
|
|
|
|
return (
|
|
<>
|
|
<motion.div
|
|
initial={{ opacity: 0 }}
|
|
animate={{ opacity: 1 }}
|
|
className="flex-1 space-y-8 p-6 sm:p-8 pt-6"
|
|
>
|
|
{/* Header */}
|
|
<div className="flex flex-col md:flex-row md:items-center justify-between gap-6">
|
|
<div className="space-y-2">
|
|
<Button variant="ghost" size="sm" onClick={() => router.back()} className="p-0 h-auto hover:bg-transparent text-muted-foreground mb-2">
|
|
<ArrowLeft className="w-4 h-4 mr-1" />
|
|
Back to {client?.name}
|
|
</Button>
|
|
<div className="flex items-center gap-3">
|
|
<h1 className="text-4xl font-bold tracking-tight">Assignment Detail</h1>
|
|
{!isProject && <Badge variant={'info'} className="text-lg px-3 py-0">{assignmentType}</Badge>}
|
|
</div>
|
|
</div>
|
|
<div className="flex gap-3">
|
|
<Button onClick={() => setAction('UPDATE_STATUS')} className="gap-2">
|
|
<History className="w-4 h-4" />
|
|
Update Assignment Status
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="grid gap-8 lg:grid-cols-3">
|
|
|
|
<div className="lg:col-span-2 space-y-8">
|
|
<Card900 className={`relative overflow-hidden border-border bg-card p-6 md:p-8`}>
|
|
{/* <div className={`pointer-events-none absolute inset-0 bg-linear-to-br from-his/25 via-his/5 to-transparent`} /> */}
|
|
<div className="relative">
|
|
<div className="flex flex-wrap items-center gap-2 text-[11px] font-bold uppercase tracking-[0.2em]">
|
|
<Badge variant={isProject ? 'default' : 'info'}>
|
|
{isProject ? 'Project' : 'Internal Memo'}
|
|
</Badge>
|
|
<BadgeStatus status={assignmentStatus} />
|
|
</div>
|
|
|
|
<div className="mt-4 flex flex-col gap-5 md:flex-row md:items-start md:justify-between">
|
|
<div className="flex items-start gap-4">
|
|
<div className={`flex h-12 sm:h-16 w-12 sm:w-16 shrink-0 items-center justify-center rounded-2xl border border-border bg-background/60 ${isProject ? 'text-his' : 'text-type-memo'}`}>
|
|
{isProject ? <Briefcase className="h-5 w-5 sm:h-8 sm:w-8" /> : <FileText className="h-5 w-5 sm:h-8 sm:w-8" />}
|
|
</div>
|
|
<div className="min-w-0">
|
|
<p className={`text-xs font-bold uppercase tracking-[0.2em] ${isProject ? 'text-his' : 'text-type-memo'}`}>{isProject ? 'Work Order' : 'Internal Memo'}</p>
|
|
<h1 className="mt-1 text-3xl font-bold tracking-tight text-foreground md:text-4xl">
|
|
{assignmentName}
|
|
</h1>
|
|
<p className="mt-2 max-w-2xl text-sm text-muted-foreground">{assignmentDesc}</p>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
{/* Quick meta row */}
|
|
<div className="mt-6 grid grid-cols-2 gap-3 sm:grid-cols-4">
|
|
<MetaTile icon={Hash} label={`${isProject ? 'WO' : 'IM'} Code`} value={assignmentCode} />
|
|
<MetaTile icon={Calendar} label="Start" value={new Date(assignmentStartDate).toLocaleDateString()} />
|
|
<MetaTile icon={Calendar} label="End" value={assignmentEndDate ? new Date(assignmentEndDate).toLocaleDateString() : 'N/A'} />
|
|
<MetaTile icon={Users} label="Team" value={`${assignment.project_assignment_work_order_detail?.length} People`} />
|
|
</div>
|
|
</div>
|
|
</Card900>
|
|
|
|
<div className="grid gap-5 md:grid-cols-2">
|
|
<PartyCard label="Client" party={client} accent="ring-primary/30" />
|
|
<PartyCard label="End User" party={assignment.end_user} accent="ring-primary/30" />
|
|
</div>
|
|
|
|
{isProject && (
|
|
<Card900 className="p-6 gap-5">
|
|
<div className="flex items-center gap-1.5 text-[10px] font-bold uppercase tracking-[0.18em] text-muted-foreground">
|
|
<FileText className="h-3 w-3" /> Project context
|
|
</div>
|
|
<div className='transition duration-300'>
|
|
<Link href={`/task/client-detail/${client?.id}/project-detail/${assignment.project?.id}`} className='flex group hover:space-x-3'>
|
|
<h2 className="font-semibold text-foreground group-hover:underline duration-300 hover:text-his">{assignment.project?.name}</h2>
|
|
<Button size="icon" variant="secondary" className="h-5 w-5 rounded-full opacity-0 group-hover:opacity-100 transition-opacity cursor-pointer duration-300">
|
|
<ExternalLink className="w-4 h-4" />
|
|
</Button>
|
|
<span className="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-muted text-muted-foreground">
|
|
Year {assignment.project?.year}
|
|
</span>
|
|
</Link>
|
|
</div>
|
|
<div className="flex items-center gap-4 text-sm">
|
|
<span className="text-muted-foreground"># Code: <span className="text-foreground/80">{assignment.project?.code}</span></span>
|
|
<span className="inline-flex items-center gap-1.5 text-muted-foreground">
|
|
<CalendarRange size={16} />
|
|
{formatDate(assignment.project?.start_date!)} → {`${assignment.project?.end_date ? formatDate(assignment.project.end_date) : 'N/A'}`}
|
|
</span>
|
|
</div>
|
|
<p className='text-muted-foreground text-xs sm:text-sm'>{assignment.project?.description ?? 'No description available.'}</p>
|
|
</Card900>
|
|
)}
|
|
|
|
{!assignment.wo_name ?
|
|
<Card className="border-dashed bg-accent/10">
|
|
<CardContent className="py-10 text-center text-muted-foreground">
|
|
No work orders defined for this assignment.
|
|
</CardContent>
|
|
</Card>
|
|
: null
|
|
}
|
|
|
|
<Card900 className="overflow-hidden border-l-4 border-l-primary">
|
|
<CardContent className="p-6 pt-2">
|
|
<div className='flex items-center justify-between mb-6 sm:mb-8'>
|
|
<div className="flex items-center gap-1.5 text-xs font-bold uppercase tracking-[0.18em] text-muted-foreground">
|
|
<Users className="h-4 w-4" /> Team
|
|
</div>
|
|
<Button onClick={() => setAction('MANAGE_EMPLOYEE')} className="gap-2" size='sm'>
|
|
<UserPlus className="w-4 h-4" />
|
|
Manage Team
|
|
</Button>
|
|
</div>
|
|
|
|
<AssignedPeople people={assignment.project_assignment_work_order_detail} defaultOpen={false} />
|
|
|
|
</CardContent>
|
|
</Card900>
|
|
|
|
</div>
|
|
|
|
{/* Right Column: Status History */}
|
|
<div className="space-y-8">
|
|
<Card900>
|
|
<CardHeader>
|
|
<CardTitle className="flex items-center gap-2">
|
|
<History className="w-5 h-5 text-primary" /> Assignment Timeline
|
|
</CardTitle>
|
|
<CardDescription>Timeline of status changes</CardDescription>
|
|
</CardHeader>
|
|
<CardContent>
|
|
<div className="relative space-y-8 before:absolute before:inset-0 before:ml-2.5 before:h-full before:w-0.5 before:-translate-x-px before:bg-accent">
|
|
{statuslist?.map((history, idx) =>
|
|
<div key={history.id} className="relative flex items-start gap-4 pl-8">
|
|
<div className={cn(
|
|
"absolute left-0 mt-1 w-5 h-5 rounded-full border-4 border-background z-10",
|
|
idx === 0 ? "bg-primary" : "bg-muted-foreground/30"
|
|
)} />
|
|
<div className="flex-1 space-y-1">
|
|
<div className="flex items-center justify-between">
|
|
<Badge variant={idx === 0 ? "default" : "outline"} className="text-[10px] h-5">
|
|
{history.project_assignment_status.name}
|
|
</Badge>
|
|
<span className="text-xs text-muted-foreground font-mono">
|
|
{history.start_date ? new Date(history.start_date).toLocaleDateString() : 'N/A'} - {history.end_date ? new Date(history.end_date).toLocaleDateString() : 'Present'}
|
|
</span>
|
|
</div>
|
|
<p className="text-xs text-muted-foreground">
|
|
Recorded: {new Date(history.end_date!).toLocaleString()}
|
|
</p>
|
|
</div>
|
|
</div>
|
|
)}
|
|
{(!statuslist || statuslist.length === 0) &&
|
|
<p className="text-sm text-muted-foreground italic pl-8">No history recorded yet.</p>
|
|
}
|
|
</div>
|
|
</CardContent>
|
|
</Card900>
|
|
</div>
|
|
</div>
|
|
</motion.div>
|
|
|
|
{action === 'UPDATE_STATUS' ?
|
|
<UpdateTimelineStatus assignmentId={assignment.id} onClose={handleClose} />
|
|
: null
|
|
}
|
|
|
|
{action === 'MANAGE_EMPLOYEE' ?
|
|
<ManageTeam
|
|
assignmentId={assignment.id} onClose={handleClose}
|
|
startDate={assignment.wo_start_date || assignment.im_start_date} endDate={assignment.wo_end_date || assignment.im_end_date}
|
|
/>
|
|
: null
|
|
}
|
|
</>
|
|
);
|
|
}
|
|
|
|
|
|
function MetaTile({ icon: Icon, label, value }: { icon: typeof Briefcase; label: string; value: string }) {
|
|
return (
|
|
<div className="p-3">
|
|
<p className="flex items-center gap-1 text-[10px] font-bold uppercase tracking-widest text-muted-foreground">
|
|
<Icon className="h-3 w-3" /> {label}
|
|
</p>
|
|
<p className="mt-1 truncate text-sm md:text-base font-semibold text-foreground">{value}</p>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
|
|
function PartyCard({ label, party, accent }: { label: string; party: any; accent: string }) {
|
|
function initials(name: string) {
|
|
return name?.split(" ").map((n) => n[0]).filter(Boolean).slice(0, 2).join("").toUpperCase();
|
|
}
|
|
|
|
return (
|
|
<Card900 className="p-5 -space-y-2">
|
|
<div className="flex items-center gap-1.5 text-[10px] font-bold uppercase tracking-[0.18em] text-muted-foreground">
|
|
<Building2 className="h-3 w-3" /> {label}
|
|
</div>
|
|
<div className="flex items-center gap-4">
|
|
<div className={`relative h-14 w-14 shrink-0 overflow-hidden rounded-xl border border-border bg-background ring-1 ${accent}`}>
|
|
{party?.photo ?
|
|
<LazyImage src={party.photo} alt={party?.name} className="h-full w-full object-contain" />
|
|
:
|
|
<div className="flex h-full w-full items-center justify-center text-base font-bold text-foreground/80">
|
|
{initials(party?.name)}
|
|
</div>
|
|
}
|
|
</div>
|
|
<div className="min-w-0 flex-1">
|
|
<p className="truncate text-base font-semibold text-foreground">{party?.name}</p>
|
|
<p className="mt-0.5 flex items-center truncate text-xs text-muted-foreground">{party?.description ?? 'No description available.'}</p>
|
|
</div>
|
|
</div>
|
|
</Card900>
|
|
);
|
|
}
|