Files
hats-app/app/(dashboard)/task/client-detail/[client_id]/[assignment_id]/AssignmentDetailComponent.tsx
T
Ichsan Kurniawan d0db9ee429 Add VPS deployment automation
This change adds staging and production deployment configuration for the VPS setup, including `.env.deploy.example`, `.env.staging`, and updated production environment values. It also introduces a PowerShell deployment script that builds the app, packages the Next.js artifact, uploads it to the server, and reloads PM2. Package scripts were added for staging and production deploys, and the TypeScript config now excludes legacy `.old` files.
2026-10-05 19:52:11 +07:00

472 lines
25 KiB
TypeScript

"use client"
/**
* Design read: internal ops detail page for staff, calm Linear-style language,
* existing shadcn + Tailwind stack, brand orange (`his`) as the single accent.
* Dials: VARIANCE 5, MOTION 4, DENSITY 5.
*
* Shape lock: surfaces rounded-2xl, inner tiles rounded-xl, small chips rounded-lg.
* Elevation comes from borders, not shadows. Accent is used only for the current
* timeline node, link hovers and the primary action.
*/
import { useState, type ReactNode } from 'react';
import Link from 'next/link';
import { motion, useReducedMotion } from 'motion/react';
import {
ArrowLeft, Briefcase, CalendarDays, ChevronDown, ExternalLink, FileText, FolderGit2,
History, Mail, Phone, UserPlus, Users,
} from 'lucide-react';
import { Button } from '@/components/ui/button';
import { Badge } from '@/components/ui/badge';
import { LazyImage } from '@/components/ui/lazy-image';
import BadgeStatus from '@/components/ui/badge-status';
import { cn } from '@/lib/utils';
import { IClient } from '../../../types';
import { IProjectAssignment, ProjectAssignmentWorkOrderDetail } from '../../../type.assignment';
import { ManageTeam } from '../../../component/ManageTeam';
import UpdateTimelineStatus from '../../../component/UpdateTimelineStatus';
interface Props {
client: IClient | undefined;
initialAssignment: IProjectAssignment;
}
const TEAM_PREVIEW_LIMIT = 6;
const MONTHS = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
/** Deterministic dd Mon yyyy, avoids locale differences between server and browser. */
function formatAssignmentDate(dateStr?: string | null) {
if (!dateStr) return 'N/A';
const d = new Date(dateStr);
if (isNaN(d.getTime())) return 'N/A';
return `${String(d.getDate()).padStart(2, '0')} ${MONTHS[d.getMonth()]} ${d.getFullYear()}`;
}
function diffInDays(start?: string | null, end?: string | null) {
if (!start || !end) return null;
const s = new Date(start);
const e = new Date(end);
if (isNaN(s.getTime()) || isNaN(e.getTime())) return null;
return Math.max(0, Math.round((e.getTime() - s.getTime()) / 86_400_000));
}
function pluralDays(n: number) {
return `${n} ${n === 1 ? 'day' : 'days'}`;
}
export default function AssignmentDetail({ client, initialAssignment: assignment }: Props) {
const [action, setAction] = useState<'MANAGE_EMPLOYEE' | 'UPDATE_STATUS' | undefined>();
const [showAllTeam, setShowAllTeam] = useState(false);
const isWorkOrder = !!assignment.wo_name;
const assignmentType = isWorkOrder ? 'Work Order' : 'Internal Memo';
const illustrationSrc = isWorkOrder
? '/assets/illustrations/work_order.jpg'
: '/assets/illustrations/internal_memo.jpg';
const assignmentName = assignment.wo_name || assignment.im_name || 'Untitled Assignment';
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 owner = client ?? assignment.client;
const clientHref = owner?.id ? `/task/client-detail/${owner.id}` : '/task';
const team = assignment.project_assignment_work_order_detail ?? [];
const visibleTeam = showAllTeam ? team : team.slice(0, TEAM_PREVIEW_LIMIT);
const hiddenTeamCount = team.length - visibleTeam.length;
const statusHistory = assignment.project_assignment_status_detail?.filter(s => s.start_date) ?? [];
const currentEntry = statusHistory.find(s => !s.end_date) ?? statusHistory[0];
const duration = diffInDays(assignmentStartDate, assignmentEndDate);
const handleClose = () => setAction(undefined);
return (
<>
<div className="mx-auto w-full max-w-7xl flex-1 space-y-6 p-6 pt-6 sm:p-8">
<Reveal>
<Link
href={clientHref}
className="group inline-flex items-center gap-1.5 text-sm text-muted-foreground transition-colors hover:text-foreground"
>
<ArrowLeft className="h-4 w-4 transition-transform duration-300 group-hover:-translate-x-0.5" />
<span>Back to {owner?.name ?? 'tasks'}</span>
</Link>
</Reveal>
{/* Header: identity, status, primary action, key facts */}
<Reveal delay={0.04}>
<section aria-labelledby="assignment-title" className="rounded-2xl border border-border bg-card p-5 sm:p-7">
<div className="flex flex-col gap-6 md:flex-row md:items-start md:justify-between">
<div className="flex min-w-0 items-start gap-4 sm:gap-5">
<div className="flex h-20 w-20 shrink-0 items-center justify-center overflow-hidden rounded-2xl border border-border bg-zinc-50 p-1 sm:h-24 sm:w-24 dark:bg-zinc-100">
<LazyImage
src={illustrationSrc}
alt={`${assignmentType} emblem`}
className="h-full w-full bg-transparent"
imgClassName="object-contain"
/>
</div>
<div className="min-w-0 space-y-2.5">
<div className="flex flex-wrap items-center gap-2">
<Badge variant="outline" className="gap-1 rounded-lg px-2 py-0.5 text-xs font-medium">
{isWorkOrder ? <Briefcase className="h-3 w-3" /> : <FileText className="h-3 w-3" />}
{assignmentType}
</Badge>
{assignmentStatus ? <BadgeStatus status={assignmentStatus} /> : null}
{assignmentCode ? (
<span className="rounded-lg border border-border bg-muted/50 px-2 py-0.5 font-mono text-xs text-muted-foreground">
{assignmentCode}
</span>
) : null}
</div>
<h1 id="assignment-title" className="text-balance text-2xl font-semibold tracking-tight text-foreground md:text-3xl">
{assignmentName}
</h1>
{assignmentDesc ? (
<p className="max-w-[65ch] text-sm leading-relaxed text-muted-foreground">
{assignmentDesc}
</p>
) : null}
</div>
</div>
<Button
onClick={() => setAction('UPDATE_STATUS')}
className="w-full shrink-0 gap-2 transition-transform active:scale-[0.98] md:w-auto"
>
<History className="h-4 w-4" />
Update status
</Button>
</div>
<dl className="mt-7 grid grid-cols-2 gap-x-6 gap-y-5 border-t border-border/70 pt-6 sm:grid-cols-4">
<Fact label="Start" value={formatAssignmentDate(assignmentStartDate)} />
<Fact label="End" value={formatAssignmentDate(assignmentEndDate)} />
<Fact label="Duration" value={duration === null ? 'N/A' : pluralDays(duration)} />
<Fact label="Team" value={team.length === 1 ? '1 person' : `${team.length} people`} />
</dl>
</section>
</Reveal>
<div className="grid items-start gap-6 lg:grid-cols-[minmax(0,1fr)_360px]">
{/* Main column */}
<div className="min-w-0 space-y-6">
<Reveal delay={0.08}>
<Surface title="Client and end user">
<div className="divide-y divide-border/70">
<PartyRow
role="Client"
party={owner}
href={owner?.id ? clientHref : undefined}
/>
<PartyRow role="End user" party={assignment.end_user} />
</div>
</Surface>
</Reveal>
{assignment.project?.id ? (
<Reveal delay={0.12}>
<Surface title="Project">
<Link
href={`/task/client-detail/${owner?.id}/project-detail/${assignment.project.id}`}
className="group flex items-start gap-4 rounded-xl p-2 -m-2 transition-colors hover:bg-muted/40"
>
<div className="flex h-11 w-11 shrink-0 items-center justify-center rounded-xl border border-border bg-muted/40 text-muted-foreground transition-colors group-hover:text-his">
<FolderGit2 className="h-5 w-5" />
</div>
<div className="min-w-0 flex-1 space-y-1.5">
<div className="flex flex-wrap items-center gap-x-2.5 gap-y-1">
<p className="font-medium text-foreground transition-colors group-hover:text-his">
{assignment.project.name}
</p>
{assignment.project.code ? (
<span className="font-mono text-xs text-muted-foreground">{assignment.project.code}</span>
) : null}
{assignment.project.year ? (
<span className="text-xs text-muted-foreground">Year {assignment.project.year}</span>
) : null}
</div>
<p className="flex items-center gap-1.5 text-xs text-muted-foreground">
<CalendarDays className="h-3.5 w-3.5" />
{formatAssignmentDate(assignment.project.start_date)} - {formatAssignmentDate(assignment.project.end_date)}
</p>
<p className="line-clamp-3 max-w-[65ch] text-sm leading-relaxed text-muted-foreground">
{assignment.project.description || 'No description available.'}
</p>
</div>
<ExternalLink className="mt-1 h-4 w-4 shrink-0 text-muted-foreground/60 transition-colors group-hover:text-his" />
</Link>
</Surface>
</Reveal>
) : null}
{assignment.note ? (
<Reveal delay={0.14}>
<Surface title="Notes">
<p className="max-w-[65ch] whitespace-pre-line text-sm leading-relaxed text-foreground/90">
{assignment.note}
</p>
</Surface>
</Reveal>
) : null}
<Reveal delay={0.16}>
<Surface
title="Team"
meta={team.length > 0 ? `${team.length}` : undefined}
action={
<Button
onClick={() => setAction('MANAGE_EMPLOYEE')}
variant="outline"
size="sm"
className="gap-1.5 transition-transform active:scale-[0.98]"
>
<UserPlus className="h-4 w-4" />
Manage team
</Button>
}
>
{team.length === 0 ? (
<div className="flex flex-col items-center gap-3 rounded-xl border border-dashed border-border bg-muted/20 px-6 py-10 text-center">
<div className="flex h-10 w-10 items-center justify-center rounded-xl bg-muted text-muted-foreground">
<Users className="h-5 w-5" />
</div>
<div className="space-y-1">
<p className="text-sm font-medium text-foreground">Nobody is assigned yet</p>
<p className="max-w-xs text-sm text-muted-foreground">
Add people from the team directory to start tracking who works on this assignment.
</p>
</div>
</div>
) : (
<>
<ul className="grid gap-3 sm:grid-cols-2">
{visibleTeam.map(member => (
<TeamMember key={member.id} member={member} />
))}
</ul>
{team.length > TEAM_PREVIEW_LIMIT ? (
<Button
variant="ghost"
size="sm"
onClick={() => setShowAllTeam(v => !v)}
className="mt-3 gap-1.5 text-muted-foreground"
aria-expanded={showAllTeam}
>
<ChevronDown className={cn('h-4 w-4 transition-transform duration-300', showAllTeam && 'rotate-180')} />
{showAllTeam ? 'Show fewer' : `Show ${hiddenTeamCount} more`}
</Button>
) : null}
</>
)}
</Surface>
</Reveal>
</div>
{/* Side rail: status history */}
<Reveal delay={0.12} className="lg:sticky lg:top-6">
<Surface title="Status history">
{statusHistory.length === 0 ? (
<p className="text-sm leading-relaxed text-muted-foreground">
No status changes recorded yet. Use Update status to log the first one.
</p>
) : (
<ol className="relative space-y-6 before:absolute before:bottom-2 before:left-[7px] before:top-2 before:w-px before:bg-border">
{statusHistory.map(entry => {
const isCurrent = entry.id === currentEntry?.id;
const days = diffInDays(entry.start_date, entry.end_date);
return (
<li key={entry.id} className="relative pl-7">
<span
aria-hidden
className={cn(
'absolute left-0 top-1 h-[15px] w-[15px] rounded-full border-[3px] border-card',
isCurrent ? 'bg-his' : 'bg-muted-foreground/40'
)}
/>
<div className="flex flex-wrap items-center gap-x-2 gap-y-1">
{isCurrent ? (
<BadgeStatus status={entry.project_assignment_status.name} />
) : (
<span className="text-sm font-medium text-foreground/80">
{entry.project_assignment_status.name}
</span>
)}
{isCurrent ? <span className="text-xs text-muted-foreground">Current</span> : null}
</div>
<p className="mt-1.5 font-mono text-xs text-muted-foreground">
{formatAssignmentDate(entry.start_date)} - {entry.end_date ? formatAssignmentDate(entry.end_date) : 'Present'}
</p>
{days !== null ? (
<p className="mt-0.5 text-xs text-muted-foreground/80">{pluralDays(days)}</p>
) : null}
</li>
);
})}
</ol>
)}
</Surface>
</Reveal>
</div>
</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}
</>
);
}
/** Entry reveal. Honors prefers-reduced-motion by rendering static. */
function Reveal({ children, delay = 0, className }: { children: ReactNode; delay?: number; className?: string }) {
const reduce = useReducedMotion();
return (
<motion.div
className={className}
initial={reduce ? false : { opacity: 0, y: 12 }}
animate={{ opacity: 1, y: 0 }}
transition={{ duration: 0.45, delay, ease: [0.16, 1, 0.3, 1] }}
>
{children}
</motion.div>
);
}
function Surface({
title, meta, action, children,
}: { title: string; meta?: string; action?: ReactNode; children: ReactNode }) {
return (
<section className="rounded-2xl border border-border bg-card p-5 sm:p-6">
<div className="mb-4 flex items-center justify-between gap-3">
<h2 className="flex items-center gap-2 text-base font-semibold tracking-tight text-foreground">
{title}
{meta ? (
<span className="rounded-lg bg-muted px-1.5 py-0.5 font-mono text-xs font-medium text-muted-foreground">{meta}</span>
) : null}
</h2>
{action}
</div>
{children}
</section>
);
}
function Fact({ label, value }: { label: string; value: string }) {
return (
<div className="min-w-0 sm:border-l sm:border-border/70 sm:pl-5 sm:first:border-l-0 sm:first:pl-0">
<dt className="text-xs text-muted-foreground">{label}</dt>
<dd className="mt-1 truncate font-mono text-sm font-medium text-foreground">{value}</dd>
</div>
);
}
function initials(name?: string) {
return (name ?? '')
.split(' ')
.map(n => n[0])
.filter(Boolean)
.slice(0, 2)
.join('')
.toUpperCase();
}
function PartyRow({
role, party, href,
}: { role: string; party?: { name?: string; photo?: string; description?: string | null; code?: string } | null; href?: string }) {
const body = (
<>
<div className="relative h-12 w-12 shrink-0 overflow-hidden rounded-xl border border-border bg-background">
{party?.photo ? (
<LazyImage src={party.photo} alt={party.name ?? role} className="h-full w-full bg-transparent" imgClassName="object-contain p-1" />
) : (
<div className="flex h-full w-full items-center justify-center text-sm font-semibold text-muted-foreground">
{initials(party?.name) || '-'}
</div>
)}
</div>
<div className="min-w-0 flex-1">
<p className="text-xs text-muted-foreground">{role}</p>
<p className={cn('truncate font-medium text-foreground', href && 'transition-colors group-hover:text-his')}>
{party?.name ?? 'Not set'}
</p>
{party?.description ? (
<p className="mt-0.5 line-clamp-1 text-xs text-muted-foreground">{party.description}</p>
) : null}
</div>
</>
);
const className = 'group flex items-center gap-4 py-4 first:pt-0 last:pb-0';
return href ? (
<Link href={href} className={cn(className, 'rounded-xl')}>
{body}
<ExternalLink className="h-4 w-4 shrink-0 text-muted-foreground/60 transition-colors group-hover:text-his" />
</Link>
) : (
<div className={className}>{body}</div>
);
}
function TeamMember({ member }: { member: ProjectAssignmentWorkOrderDetail }) {
const user = member.user;
const name = user?.fullname || user?.name || 'Unnamed member';
return (
<li className="flex min-w-0 items-start gap-3 rounded-xl border border-border/70 bg-muted/20 p-3 transition-colors hover:border-border hover:bg-muted/40">
<LazyImage
src={user?.avatar ? `${process.env.BASE_URL_IMG}/${user.avatar}` : '/assets/no-avatar.jpg'}
alt={name}
className="h-10 w-10 shrink-0 rounded-full border border-border"
/>
<div className="min-w-0 flex-1 space-y-1">
<p className="truncate text-sm font-medium text-foreground">{name}</p>
{user?.position?.name ? (
<p className="truncate text-xs text-muted-foreground">{user.position.name}</p>
) : null}
<div className="flex flex-col gap-0.5 pt-0.5 text-xs text-muted-foreground">
{user?.email ? (
<a
href={`mailto:${user.email}`}
referrerPolicy="no-referrer"
className="inline-flex min-w-0 items-center gap-1.5 transition-colors hover:text-his"
>
<Mail className="h-3 w-3 shrink-0" />
<span className="truncate">{user.email}</span>
</a>
) : null}
{user?.phone_number ? (
<span className="inline-flex items-center gap-1.5">
<Phone className="h-3 w-3 shrink-0" />
<span className="font-mono">{user.phone_number}</span>
</span>
) : null}
</div>
{member.start_date ? (
<p className="pt-1 font-mono text-[11px] text-muted-foreground/80">
{formatAssignmentDate(member.start_date)} - {member.end_date ? formatAssignmentDate(member.end_date) : 'Open'}
</p>
) : null}
</div>
</li>
);
}