import { useMemo } from 'react'; import { Avatar, CellProps, Column, InteractiveTable, Stack, Badge, Tooltip, Pagination, FetchDataFunc, } from '@grafana/ui'; import { Trans } from 'app/core/internationalization'; import { EmptyArea } from 'app/features/alerting/unified/components/EmptyArea'; import { UserAnonymousDeviceDTO } from 'app/types'; type Cell = CellProps< UserAnonymousDeviceDTO, UserAnonymousDeviceDTO[T] >; // A helper function to parse the user agent string and extract parts const parseUserAgent = (userAgent: string) => { // If the user agent string doesn't contain a space, it's probably just the browser name // or some other entity that are accessing grafana if (!userAgent.includes(' ')) { return { browser: userAgent, computer: '', }; } const parts = userAgent.split(' '); return { browser: parts[0], computer: parts[1], }; }; // A helper function to truncate each part of the user agent const truncatePart = (part: string, maxLength: number) => { return part.length > maxLength ? part.substring(0, maxLength) + '...' : part; }; interface UserAgentCellProps { value: string; } const UserAgentCell = ({ value }: UserAgentCellProps) => { const parts = parseUserAgent(value); return ( {truncatePart(parts.browser, 10)} {truncatePart(parts.computer, 10)} ); }; interface AnonUsersTableProps { devices: UserAnonymousDeviceDTO[]; // for pagination showPaging?: boolean; totalPages: number; onChangePage: (page: number) => void; currentPage: number; fetchData?: FetchDataFunc; } export const AnonUsersDevicesTable = ({ devices, showPaging, totalPages, onChangePage, currentPage, fetchData, }: AnonUsersTableProps) => { const columns: Array> = useMemo( () => [ { id: 'avatarUrl', header: '', cell: ({ cell: { value } }: Cell<'avatarUrl'>) => value && , }, { id: 'login', header: 'Login', cell: ({ cell: { value } }: Cell<'login'>) => 'Anonymous', }, { id: 'userAgent', header: 'User Agent', cell: ({ cell: { value } }: Cell<'userAgent'>) => , sortType: 'string', }, { id: 'updatedAt', header: 'Last active', cell: ({ cell: { value } }: Cell<'updatedAt'>) => value, sortType: (a, b) => new Date(a.original.updatedAt).getTime() - new Date(b.original.updatedAt).getTime(), }, { id: 'clientIp', header: 'Origin IP (address)', cell: ({ cell: { value } }: Cell<'clientIp'>) => value && , }, ], [] ); return ( user.deviceId} fetchData={fetchData} /> {showPaging && ( )} {devices.length === 0 && ( No anonymous users found. )} ); };