import { css } from '@emotion/css'; import React, { useState } from 'react'; import { GrafanaTheme2 } from '@grafana/data'; import { Button, ClipboardButton, DatePickerWithInput, Field, FieldSet, HorizontalGroup, Icon, Input, Label, Modal, RadioButtonGroup, useStyles2, } from '@grafana/ui'; const EXPIRATION_OPTIONS = [ { label: 'No expiration', value: false }, { label: 'Set expiration date', value: true }, ]; export type ServiceAccountToken = { name: string; secondsToLive: number; }; interface CreateTokenModalProps { isOpen: boolean; token: string; onCreateToken: (token: ServiceAccountToken) => void; onClose: () => void; } export const CreateTokenModal = ({ isOpen, token, onCreateToken, onClose }: CreateTokenModalProps) => { const [newTokenName, setNewTokenName] = useState(''); const [isWithExpirationDate, setIsWithExpirationDate] = useState(false); const [newTokenExpirationDate, setNewTokenExpirationDate] = useState(''); const [isExpirationDateValid, setIsExpirationDateValid] = useState(false); const styles = useStyles2(getStyles); const onExpirationDateChange = (value: Date | string) => { const isValid = value !== ''; setIsExpirationDateValid(isValid); setNewTokenExpirationDate(value); }; const onCloseInternal = () => { setNewTokenName(''); setIsWithExpirationDate(false); setNewTokenExpirationDate(''); setIsExpirationDateValid(false); onClose(); }; const modalTitle = (
{!token ? 'Add service account token' : 'Service account token created'}
); return ( {!token ? ( <>
{ setNewTokenName(e.currentTarget.value); }} /> {isWithExpirationDate && ( )}
) : ( <>
token} > Copy to clipboard
token} onClipboardCopy={onCloseInternal}> Copy to clipboard and close )}
); }; const getSecondsToLive = (date: Date | string) => { const dateAsDate = new Date(date); const now = new Date(); return Math.ceil((dateAsDate.getTime() - now.getTime()) / 1000); }; const getStyles = (theme: GrafanaTheme2) => { return { modal: css` width: 550px; `, modalRow: css` margin-bottom: ${theme.spacing(4)}; `, modalTokenRow: css` display: flex; `, modalCopyToClipboardButton: css` margin-left: ${theme.spacing(0.5)}; `, modalHeaderTitle: css` font-size: ${theme.typography.size.lg}; margin: ${theme.spacing(0, 4, 0, 1)}; display: flex; align-items: center; position: relative; top: 2px; `, modalHeaderIcon: css` margin-right: ${theme.spacing(2)}; font-size: inherit; &:before { vertical-align: baseline; } `, }; };