'use client' import { ColumnDef } from '@tanstack/react-table' import { Badge } from '@/components/ui/badge' import { Button } from '@/components/ui/button' import { Edit, Trash2, MoreHorizontal } from 'lucide-react' import { formatDate } from '@/lib/format' import { userStatusOptions } from '@/lib/schema/user' import { Checkbox } from '@/components/ui/checkbox' import { DataTableColumnHeader } from '@/components/data-table/column-header' import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger, } from '@/components/ui/dropdown-menu' import type { User } from '@/server/routers/users' // 操作回调类型 export type UserActions = { onEdit: (userId: string) => void onDelete: (userId: string) => void } // 列定义选项类型 export type UserColumnsOptions = { roles?: Array<{ id: number; name: string }> permissions?: Array<{ id: number; name: string }> depts?: Array<{ code: string; name: string; fullName: string }> } // 创建用户表格列定义 export const createUserColumns = ( actions: UserActions, options: UserColumnsOptions = {} ): ColumnDef[] => [ { id: "select", header: ({ table }) => ( table.toggleAllPageRowsSelected(!!value) } aria-label="Select all" /> ), cell: ({ row }) => ( row.toggleSelected(!!value)} aria-label="Select row" /> ), size: 32, enableSorting: false, enableHiding: false, }, { id: 'id', accessorKey: 'id', header: ({ column }) => ( ), cell: ({ row }) =>
{row.original.id}
, enableColumnFilter: true, meta: { label: '用户ID', filter: { placeholder: '请输入用户ID', variant: 'text', } }, }, { id: 'name', accessorKey: 'name', header: ({ column }) => ( ), cell: ({ row }) =>
{row.original.name || '-'}
, enableColumnFilter: true, meta: { label: '姓名', filter: { placeholder: '请输入姓名', variant: 'text', } }, }, { id: 'status', accessorKey: 'status', header: ({ column }) => ( ), cell: ({ row }) => { const status = row.original.status return ( {status || '未知'} ) }, enableColumnFilter: true, meta: { label: '状态', filter: { variant: 'select', options: userStatusOptions, } }, }, { id: 'dept', accessorKey: 'dept', header: ({ column }) => ( ), cell: ({ row }) =>
{row.original.dept?.name || '-'}
, enableColumnFilter: true, meta: { label: '所属院系', filter: { variant: 'multiSelect', options: options.depts?.map(dept => ({ id: dept.code, name: dept.fullName, })) || [], } }, }, { id: 'roles', accessorKey: 'roles', header: ({ column }) => ( ), cell: ({ row }) => { const roles = row.original.roles return (
{roles.map((role) => ( {role.name} ))}
) }, enableColumnFilter: true, enableSorting: false, meta: { label: '角色', filter: { variant: 'select', options: options.roles?.map(role => ({ id: role.id.toString(), name: role.name, })) || [], } }, }, { id: 'permissions', accessorFn: row => Array.from( new Set( row.roles.flatMap((role) => role.permissions.map((p) => p.name)) ) ), header: ({ column }) => ( ), cell: ({ getValue }) => { return (
{getValue().map((permName) => ( {permName} ))}
) }, enableColumnFilter: true, enableSorting: false, meta: { label: '权限', filter: { variant: 'select', options: options.permissions?.map(permission => ({ id: permission.id.toString(), name: permission.name, })) || [], } }, }, { id: 'lastLoginAt', accessorKey: 'lastLoginAt', header: ({ column }) => ( ), cell: ({ row }) => { const lastLoginAt = row.original.lastLoginAt as Date | null return
{lastLoginAt ? formatDate(lastLoginAt) : '从未登录'}
}, meta: { label: '最后登录', }, }, { id: 'createdAt', accessorKey: 'createdAt', header: ({ column }) => ( ), cell: ({ row }) => { return
{formatDate(row.original.createdAt) || '-'}
}, meta: { label: '创建时间', } }, { id: 'actions', cell: ({ row }) => { const user = row.original return ( actions.onEdit(user.id)}> 编辑 actions.onDelete(user.id)} > 删除 ) }, size: 32, enableSorting: false, enableHiding: false, }, ]