import { css } from '@emotion/css'; import React, { FC } from 'react'; import { dateTimeFormat, GrafanaTheme2, TimeZone } from '@grafana/data'; import { DeleteButton, Icon, IconName, Tooltip, useTheme2 } from '@grafana/ui'; import { contextSrv } from 'app/core/core'; import { AccessControlAction } from 'app/types'; import { ApiKey } from '../../types'; interface Props { apiKeys: ApiKey[]; timeZone: TimeZone; onDelete: (apiKey: ApiKey) => void; } export const ApiKeysTable: FC = ({ apiKeys, timeZone, onDelete }) => { const theme = useTheme2(); const styles = getStyles(theme); return ( {apiKeys.length > 0 ? ( {apiKeys.map((key) => { const isExpired = Boolean(key.expiration && Date.now() > new Date(key.expiration).getTime()); return ( ); })} ) : null}
Name Role Expires
{key.name} {key.role} {formatDate(key.expiration, timeZone)} {isExpired && ( )} onDelete(key)} disabled={!contextSrv.hasPermissionInMetadata(AccessControlAction.ActionAPIKeysDelete, key)} />
); }; function formatDate(expiration: string | undefined, timeZone: TimeZone): string { if (!expiration) { return 'No expiration date'; } return dateTimeFormat(expiration, { timeZone }); } const getStyles = (theme: GrafanaTheme2) => ({ tableRow: (isExpired: boolean) => css` color: ${isExpired ? theme.colors.text.secondary : theme.colors.text.primary}; `, tooltipContainer: css` margin-left: ${theme.spacing(1)}; `, });