import type { ColumnDef } from '@tanstack/react-table'; import { KeyRound, Pencil, Trash2 } from 'lucide-react'; import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar'; import { RowActions } from '@/components/data-display'; import { ToggleStatus } from '@/components/data-display'; import { formatCurrency } from '@/lib/utils'; export type Employee = { id: number; email: string; username: string; is_active: boolean; photo_url: string | null; roles?: { name: string }[]; user_profile: { full_name: string; phone_number: string | null; } | null; employee: { join_date: string; resign_date: string | null; employment_status: string; base_salary: number; } | null; }; function getEmploymentStatusLabel(status: string): string { const labels: Record = { full_time: 'Full Time', part_time: 'Part Time', contract: 'Kontrak', internship: 'Magang', resigned: 'Keluar', }; return labels[status] ?? status; } type CreateColumnsParams = { handleEdit: (employee: Employee) => void; handleDeleteClick: (employee: Employee) => void; handleResetPassword: (employee: Employee) => void; toggleActiveUrl: (id: number) => string; can: (permission: string) => boolean; canViewAll: boolean; }; export function createEmployeeColumns( params: CreateColumnsParams, ): ColumnDef[] { const { handleEdit, handleDeleteClick, handleResetPassword, toggleActiveUrl, can, canViewAll, } = params; const columns: ColumnDef[] = [ { id: 'photo', header: () => Foto, meta: { className: 'w-[60px]' }, cell: ({ row }) => { const employee = row.original; const fullName = employee.user_profile?.full_name ?? ''; const initials = fullName .split(' ') .map((n) => n[0]) .join('') .toUpperCase() .slice(0, 2); return ( {initials || '-'} ); }, }, { accessorKey: 'user_profile.full_name', id: 'full_name', header: () => Nama, cell: ({ row }) => { const employee = row.original; return (
{employee.user_profile?.full_name ?? '-'} {employee.email}
); }, }, { accessorKey: 'username', header: () => Username, cell: ({ row }) => ( {row.getValue('username') as string} ), }, { accessorKey: 'user_profile.phone_number', id: 'phone_number', header: () => No. Telepon, cell: ({ row }) => { const employee = row.original; return ( {employee.user_profile?.phone_number ?? '-'} ); }, }, { accessorKey: 'employee.base_salary', id: 'base_salary', header: () => Gaji, cell: ({ row }) => { const employee = row.original; const salary = employee.employee?.base_salary; if (!salary) { return -; } return ( {formatCurrency(salary)} ); }, }, { id: 'period', header: () => Masa Kerja, cell: ({ row }) => { const employee = row.original; const joinDate = employee.employee?.join_date; const resignDate = employee.employee?.resign_date; if (!joinDate) { return -; } const formattedJoin = new Date(joinDate).toLocaleDateString('id-ID', { day: 'numeric', month: 'short', year: 'numeric', }); if (resignDate) { const formattedResign = new Date(resignDate).toLocaleDateString('id-ID', { day: 'numeric', month: 'short', year: 'numeric', }); return (
Masuk {formattedJoin} Keluar {formattedResign}
); } return (
Masuk {formattedJoin}
); }, }, ...(canViewAll ? [ { id: 'role', header: () => Role, cell: ({ row }) => { const employee = row.original; const roleName = employee.roles?.[0]?.name ?? '-'; return ( {roleName} ); }, }, ] : []), { accessorKey: 'employee.employment_status', id: 'employment_status', header: () => Status, cell: ({ row }) => { const employee = row.original; const status = employee.employee?.employment_status; if (!status) { return -; } return ( {getEmploymentStatusLabel(status)} ); }, }, { accessorKey: 'is_active', header: () => Aktif, meta: { className: 'w-[80px] text-center', headerClassName: 'w-[80px] text-center', }, cell: ({ row }) => { const employee = row.original; return (
); }, }, ]; if ( can('employees.update') || can('employees.reset_password') || can('employees.delete') ) { columns.push({ id: 'actions', header: () => Aksi, meta: { className: 'w-[120px] text-center', headerClassName: 'w-[120px] text-center', }, cell: ({ row }) => { const employee = row.original; return ( , show: can('employees.update'), onClick: () => handleEdit(employee), }, { label: 'Reset Kata Sandi', icon: ( ), show: can('employees.reset_password'), onClick: () => handleResetPassword(employee), }, { label: 'Hapus', icon: ( ), show: can('employees.delete'), onClick: () => handleDeleteClick(employee), }, ]} /> ); }, }); } return columns; }