Files
saladeaula.digital/apps/admin.saladeaula.digital/app/routes/_.$orgid.enrollments._index/columns.tsx
2025-11-10 00:24:07 -03:00

186 lines
4.7 KiB
TypeScript

'use client'
import type { CellContext, ColumnDef } from '@tanstack/react-table'
import { HelpCircleIcon } from 'lucide-react'
import { Avatar, AvatarFallback } from '@repo/ui/components/ui/avatar'
import { Badge } from '@repo/ui/components/ui/badge'
import { Checkbox } from '@repo/ui/components/ui/checkbox'
import { Progress } from '@repo/ui/components/ui/progress'
import { cn, initials } from '@repo/ui/lib/utils'
import { Abbr } from '@/components/abbr'
import { DataTableColumnHeader } from '@/components/data-table/column-header'
import { labels, statuses } from './data'
// This type is used to define the shape of our data.
// You can use a Zod schema here if you want.
type Course = {
id: string
name: string
}
export type Enrollment = {
id: string
name: string
course: Course
status: string
progress: string
created_at: string
}
const formatted = new Intl.DateTimeFormat('pt-BR', {
day: '2-digit',
month: '2-digit',
year: 'numeric',
hour: '2-digit',
minute: '2-digit'
})
export const columns: ColumnDef<Enrollment>[] = [
{
id: 'select',
header: ({ table }) => (
<Checkbox
checked={
table.getIsAllPageRowsSelected() ||
(table.getIsSomePageRowsSelected() && 'indeterminate')
}
onCheckedChange={(value) => table.toggleAllPageRowsSelected(!!value)}
className="cursor-pointer"
aria-label="Selecionar tudo"
/>
),
cell: ({ row }) => (
<Checkbox
checked={row.getIsSelected()}
disabled={!row.getCanSelect()}
onCheckedChange={(value) => row.toggleSelected(!!value)}
className="cursor-pointer"
aria-label="Selecionar linha"
/>
)
},
{
accessorKey: 'user',
header: 'Colaborador',
enableHiding: false,
cell: ({ row }) => {
const user = row.getValue('user') as { name: string; email: string }
return (
<div className="flex gap-2.5 items-center">
<Avatar className="size-10 hidden lg:block">
<AvatarFallback>{initials(user.name)}</AvatarFallback>
</Avatar>
<ul>
<li className="font-bold">
<Abbr>{user.name}</Abbr>
</li>
<li className="text-muted-foreground text-sm">
<Abbr>{user.email}</Abbr>
</li>
</ul>
</div>
)
}
},
{
accessorKey: 'course',
header: 'Curso',
enableHiding: false,
cell: ({ row }) => {
const { name } = row.getValue('course') as { name: string }
return <Abbr>{name}</Abbr>
}
},
{
accessorKey: 'status',
header: 'Status',
enableHiding: false,
cell: ({ row }) => {
const s = row.getValue('status') as string
const status = labels[s] ?? s
const { icon: Icon, color } = statuses?.[s] ?? { icon: HelpCircleIcon }
return (
<Badge variant="outline" className={cn(color, ' px-1.5')}>
<Icon className={cn('stroke-2', color)} />
{status}
</Badge>
)
}
},
{
accessorKey: 'progress',
header: 'Progresso',
enableHiding: false,
cell: ({ row }) => {
const progress = row.getValue('progress')
return (
<div className="flex gap-2.5 items-center ">
<Progress value={Number(progress)} className="w-32" />
<span className="text-xs">{String(progress)}%</span>
</div>
)
}
},
{
accessorKey: 'created_at',
header: ({ column }) => <DataTableColumnHeader column={column} />,
meta: { title: 'Cadastrado em' },
enableSorting: true,
enableHiding: true,
cell: cellDate
},
{
accessorKey: 'started_at',
header: ({ column }) => <DataTableColumnHeader column={column} />,
meta: { title: 'Iniciado em' },
enableSorting: true,
enableHiding: true,
cell: cellDate
},
{
accessorKey: 'completed_at',
header: ({ column }) => <DataTableColumnHeader column={column} />,
meta: { title: 'Concluído em' },
enableSorting: true,
enableHiding: true,
cell: cellDate
},
{
accessorKey: 'failed_at',
header: ({ column }) => <DataTableColumnHeader column={column} />,
meta: { title: 'Reprovado em' },
enableSorting: true,
enableHiding: true,
cell: cellDate
},
{
accessorKey: 'canceled_at',
header: ({ column }) => <DataTableColumnHeader column={column} />,
meta: { title: 'Cancelado em' },
enableSorting: true,
enableHiding: true,
cell: cellDate
}
]
function cellDate<TData>({
row: { original },
cell: { column }
}: CellContext<TData, unknown>) {
const accessorKey = column.columnDef.accessorKey as keyof TData
const value = original?.[accessorKey]
if (value) {
return formatted.format(new Date(value as string))
}
return <></>
}