'use client' import type { CellContext, ColumnDef } from '@tanstack/react-table' import { useToggle } from 'ahooks' import { EllipsisVerticalIcon, FileBadgeIcon, HelpCircleIcon } from 'lucide-react' import type { ComponentProps } from 'react' import { Abbr } from '@repo/ui/components/abbr' import { DataTableColumnHeader } from '@repo/ui/components/data-table' import { Avatar, AvatarFallback } from '@repo/ui/components/ui/avatar' import { Badge } from '@repo/ui/components/ui/badge' import { Button } from '@repo/ui/components/ui/button' import { Checkbox } from '@repo/ui/components/ui/checkbox' import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from '@repo/ui/components/ui/dropdown-menu' import { Progress } from '@repo/ui/components/ui/progress' import { Spinner } from '@repo/ui/components/ui/spinner' import { cn, initials } from '@repo/ui/lib/utils' import { labels, statuses } from '@repo/ui/routes/enrollments/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[] = [ { id: 'select', header: ({ table }) => ( table.toggleAllPageRowsSelected(!!value)} className="cursor-pointer" aria-label="Selecionar tudo" /> ), cell: ({ row }) => ( 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 (
{initials(user.name)}
  • {user.name}
  • {user.email}
) } }, { accessorKey: 'course', header: 'Curso', enableHiding: false, cell: ({ row }) => { const { name } = row.getValue('course') as { name: string } return {name} } }, { 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 ( {status} ) } }, { accessorKey: 'progress', header: 'Progresso', enableHiding: false, cell: ({ row }) => { const progress = row.getValue('progress') return (
{String(progress)}%
) } }, { accessorKey: 'created_at', header: ({ column }) => , meta: { title: 'Cadastrado em' }, enableSorting: true, enableHiding: true, cell: cellDate }, { accessorKey: 'started_at', header: ({ column }) => , meta: { title: 'Iniciado em' }, enableSorting: true, enableHiding: true, cell: cellDate }, { accessorKey: 'completed_at', header: ({ column }) => , meta: { title: 'ConcluĂ­do em' }, enableSorting: true, enableHiding: true, cell: cellDate }, { accessorKey: 'failed_at', header: ({ column }) => , meta: { title: 'Reprovado em' }, enableSorting: true, enableHiding: true, cell: cellDate }, { accessorKey: 'canceled_at', header: ({ column }) => , meta: { title: 'Cancelado em' }, enableSorting: true, enableHiding: true, cell: cellDate }, { id: 'actions', cell: ({ row }) => } ] function cellDate({ row: { original }, cell: { column } }: CellContext) { const accessorKey = column.columnDef.accessorKey as keyof TData const value = original?.[accessorKey] if (value) { return formatted.format(new Date(value as string)) } return <> } function ActionMenu({ row }: { row: any }) { const [open, { set: setOpen }] = useToggle(false) const cert = row.original?.cert return (
{ setOpen(open) }} > setOpen(false)} />
) } type ItemProps = ComponentProps & { id: string onSuccess?: () => void } function DownloadItem({ id, onSuccess, ...props }: ItemProps) { const [loading, { set }] = useToggle(false) const download = async (e: Event) => { e.preventDefault() set(true) const r = await fetch(`/~/api/enrollments/${id}/download`, { method: 'GET' }) if (r.ok) { const { presigned_url } = (await r.json()) as { presigned_url: string } window.open(presigned_url, '_blank') set(false) onSuccess?.() } } return ( {loading ? : } Baixar certificado ) }