'use client'; import { UserDetail } from '@/app/dashboard/user/user-detail'; import { IpLink } from '@/components/ip-link'; import { ProTable } from '@/components/pro-table'; import { filterServerList } from '@/services/admin/server'; import { useQuery } from '@tanstack/react-query'; import { Badge } from '@workspace/ui/components/badge'; import { Sheet, SheetContent, SheetHeader, SheetTitle, SheetTrigger, } from '@workspace/ui/components/sheet'; import type { useTranslations } from 'next-intl'; import { useState } from 'react'; function mapOnlineUsers(online: API.ServerStatus['online'] = []): { uid: string; ips: string[]; subscribe?: string; subscribe_id?: number; traffic?: number; expired_at?: number; }[] { return (online || []).map((u) => ({ uid: String(u.user_id || ''), ips: Array.isArray(u.ip) ? u.ip.map(String) : [], subscribe: (u as any).subscribe, subscribe_id: (u as any).subscribe_id, traffic: (u as any).traffic, expired_at: (u as any).expired_at, })); } export default function OnlineUsersCell({ serverId, status, t, }: { serverId?: number; status?: API.ServerStatus; t: ReturnType; }) { const [open, setOpen] = useState(false); const { data: latest } = useQuery({ queryKey: ['serverStatusById', serverId, open], enabled: !!serverId && open, queryFn: async () => { const { data } = await filterServerList({ page: 1, size: 1, search: String(serverId) }); const list = (data?.data?.list || []) as API.Server[]; return list[0]?.status as API.ServerStatus | undefined; }, }); const rows = mapOnlineUsers((latest || status)?.online); const count = rows.length; return ( {t('onlineUsers')}
> header={{ hidden: true }} columns={[ { accessorKey: 'ips', header: t('ipAddresses'), cell: ({ row }) => { const ips = row.original.ips; return (
{ips.map((ip, i) => (
{i === 0 ? ( ) : ( )}
))}
); }, }, { accessorKey: 'user', header: t('user'), cell: ({ row }) => , }, { accessorKey: 'subscription', header: t('subscription'), cell: ({ row }) => ( {row.original.subscribe || '--'} ), }, { accessorKey: 'subscribeId', header: t('subscribeId'), cell: ({ row }) => ( {row.original.subscribe_id || '--'} ), }, { accessorKey: 'traffic', header: t('traffic'), cell: ({ row }) => { const v = Number(row.original.traffic || 0); return {(v / 1024 ** 3).toFixed(2)} GB; }, }, { accessorKey: 'expireTime', header: t('expireTime'), cell: ({ row }) => { const ts = Number(row.original.expired_at || 0); if (!ts) return --; const expired = ts < Date.now() / 1000; return (
{new Date(ts * 1000).toLocaleString()} {expired && ( {t('expired')} )}
); }, }, ]} request={async () => ({ list: rows, total: rows.length })} />
); }