import { type ColumnDef } from '@tanstack/react-table' import { useTranslation } from 'react-i18next' import { formatQuota, formatTimestamp } from '@/lib/format' import { cn } from '@/lib/utils' import { Checkbox } from '@/components/ui/checkbox' import { Progress } from '@/components/ui/progress' import { Tooltip, TooltipContent, TooltipTrigger, } from '@/components/ui/tooltip' import { DataTableColumnHeader } from '@/components/data-table' import { LongText } from '@/components/long-text' import { StatusBadge, dotColorMap } from '@/components/status-badge' import { USER_STATUSES, USER_ROLES, DEFAULT_GROUP, isUserDeleted, } from '../constants' import { type User } from '../types' import { DataTableRowActions } from './data-table-row-actions' export function useUsersColumns(): ColumnDef[] { const { t } = useTranslation() return [ { id: 'select', header: ({ table }) => ( table.toggleAllPageRowsSelected(!!value)} aria-label='Select all' className='translate-y-[2px]' /> ), cell: ({ row }) => ( row.toggleSelected(!!value)} aria-label='Select row' className='translate-y-[2px]' /> ), enableSorting: false, enableHiding: false, meta: { label: t('Select') }, }, { accessorKey: 'id', header: ({ column }) => ( ), cell: ({ row }) => { return
{row.getValue('id')}
}, meta: { label: t('ID'), mobileHidden: true }, }, { accessorKey: 'username', header: ({ column }) => ( ), cell: ({ row }) => { const username = row.getValue('username') as string const remark = row.original.remark return (
{username} {remark && ( {remark}

{remark}

)}
) }, enableHiding: false, meta: { label: t('Username'), mobileTitle: true }, }, { accessorKey: 'display_name', header: ({ column }) => ( ), cell: ({ row }) => { return ( {row.getValue('display_name') || '-'} ) }, meta: { label: t('Display Name'), mobileHidden: true }, }, { accessorKey: 'status', header: ({ column }) => ( ), cell: ({ row }) => { const user = row.original const requestCount = user.request_count const statusConfig = isUserDeleted(user) ? USER_STATUSES.DELETED : USER_STATUSES[user.status as keyof typeof USER_STATUSES] if (!statusConfig) { return null } return (

{t('Requests:')} {requestCount.toLocaleString()}

) }, filterFn: (row, id, value) => { return value.includes(String(row.getValue(id))) }, enableSorting: false, meta: { label: t('Status'), mobileBadge: true }, }, { id: 'quota', accessorKey: 'quota', header: ({ column }) => ( ), cell: ({ row }) => { const user = row.original const used = user.used_quota const remaining = user.quota const total = used + remaining const percentage = total > 0 ? (remaining / total) * 100 : 0 if (total === 0) { return ( ) } return (
{formatQuota(remaining)} {formatQuota(total)}
{t('Used:')} {formatQuota(used)}
{t('Remaining:')} {formatQuota(remaining)}
{t('Total:')} {formatQuota(total)}
{t('Percentage:')} {percentage.toFixed(1)}%
) }, meta: { label: t('Quota') }, }, { accessorKey: 'group', header: ({ column }) => ( ), cell: ({ row }) => { const group = row.getValue('group') as string return ( ) }, filterFn: (row, id, value) => { const group = String(row.getValue(id) || DEFAULT_GROUP).toLowerCase() const searchValue = String(value).toLowerCase() return group.includes(searchValue) }, meta: { label: t('Group') }, }, { accessorKey: 'role', header: ({ column }) => ( ), cell: ({ row }) => { const roleValue = row.getValue('role') as number const roleConfig = USER_ROLES[roleValue as keyof typeof USER_ROLES] if (!roleConfig) { return null } return (
{roleConfig.icon && ( )} {t(roleConfig.labelKey)}
) }, filterFn: (row, id, value) => { return value.includes(String(row.getValue(id))) }, enableSorting: false, meta: { label: t('Role') }, }, { id: 'invite_info', header: ({ column }) => ( ), cell: ({ row }) => { const user = row.original const affCount = user.aff_count || 0 const affHistoryQuota = user.aff_history_quota || 0 const inviterId = user.inviter_id || 0 return (
) }, enableSorting: false, meta: { label: t('Invite Info'), mobileHidden: true }, }, { accessorKey: 'created_at', header: ({ column }) => ( ), cell: ({ row }) => { const ts = row.getValue('created_at') as number | undefined return ( {ts ? formatTimestamp(ts) : '-'} ) }, meta: { label: t('Created At'), mobileHidden: true }, }, { accessorKey: 'last_login_at', header: ({ column }) => ( ), cell: ({ row }) => { const ts = row.getValue('last_login_at') as number | undefined return ( {ts ? formatTimestamp(ts) : '-'} ) }, meta: { label: t('Last Login'), mobileHidden: true }, }, { id: 'actions', cell: ({ row }) => , meta: { label: t('Actions') }, }, ] }