import { css } from '@emotion/css'; import React, { FC } from 'react'; import { dateTimeFormat, GrafanaTheme2, TimeZone } from '@grafana/data'; import { DeleteButton, Icon, Tooltip, useStyles2, useTheme2 } from '@grafana/ui'; import { contextSrv } from 'app/core/core'; import { AccessControlAction } from 'app/types'; import { ApiKey } from '../../types'; interface Props { tokens: ApiKey[]; timeZone: TimeZone; onDelete: (token: ApiKey) => void; } export const ServiceAccountTokensTable: FC = ({ tokens, timeZone, onDelete }) => { const theme = useTheme2(); const styles = getStyles(theme); return ( <> {tokens.map((key) => { return ( {contextSrv.hasPermission(AccessControlAction.ServiceAccountsDelete) && ( )} ); })}
Name Expires Created
{key.name} {formatDate(timeZone, key.created)} onDelete(key)} />
); }; function formatDate(timeZone: TimeZone, expiration?: string): string { if (!expiration) { return 'No expiration date'; } return dateTimeFormat(expiration, { timeZone }); } function formatSecondsLeftUntilExpiration(secondsUntilExpiration: number): string { const days = Math.floor(secondsUntilExpiration / (3600 * 24)); const daysFormat = days > 1 ? `${days} days` : `${days} day`; return `Expires in ${daysFormat}`; } interface TokenExpirationProps { timeZone: TimeZone; token: ApiKey; } const TokenExpiration = ({ timeZone, token }: TokenExpirationProps) => { const styles = useStyles2(getStyles); if (!token.expiration) { return Never; } if (token.secondsUntilExpiration) { return ( {formatSecondsLeftUntilExpiration(token.secondsUntilExpiration)} ); } if (token.hasExpired) { return ( Expired ); } return {formatDate(timeZone, token.expiration)}; }; const getStyles = (theme: GrafanaTheme2) => ({ tableRow: (hasExpired: boolean | undefined) => css` color: ${hasExpired ? theme.colors.text.secondary : theme.colors.text.primary}; `, tooltipContainer: css` margin-left: ${theme.spacing(1)}; `, toolTipIcon: css` color: ${theme.colors.error.text}; `, secondsUntilExpiration: css` color: ${theme.colors.warning.text}; `, hasExpired: css` color: ${theme.colors.error.text}; `, neverExpire: css` color: ${theme.colors.text.secondary}; `, });