import React, { ChangeEvent, FC, FormEvent, useEffect, useState } from 'react'; import { rangeUtil, SelectableValue } from '@grafana/data'; import { EventsWithValidation, LegacyForms, ValidationEvents, Button, Select, InlineField } from '@grafana/ui'; import { CloseButton } from 'app/core/components/CloseButton/CloseButton'; import { SlideDown } from '../../core/components/Animations/SlideDown'; import { NewApiKey, OrgRole } from '../../types'; const { Input } = LegacyForms; const ROLE_OPTIONS: Array> = Object.keys(OrgRole).map((role) => ({ label: role, value: role as OrgRole, })); interface Props { show: boolean; onClose: () => void; onKeyAdded: (apiKey: NewApiKey) => void; disabled: boolean; } function isValidInterval(value: string): boolean { if (!value) { return true; } try { rangeUtil.intervalToSeconds(value); return true; } catch {} return false; } const timeRangeValidationEvents: ValidationEvents = { [EventsWithValidation.onBlur]: [ { rule: isValidInterval, errorMessage: 'Not a valid duration', }, ], }; const tooltipText = 'The API key life duration. For example, 1d if your key is going to last for one day. Supported units are: s,m,h,d,w,M,y'; export const ApiKeysForm: FC = ({ show, onClose, onKeyAdded, disabled }) => { const [name, setName] = useState(''); const [role, setRole] = useState(OrgRole.Viewer); const [secondsToLive, setSecondsToLive] = useState(''); useEffect(() => { setName(''); setRole(OrgRole.Viewer); setSecondsToLive(''); }, [show]); const onSubmit = (event: FormEvent) => { event.preventDefault(); if (isValidInterval(secondsToLive)) { onKeyAdded({ name, role, secondsToLive }); onClose(); } }; const onNameChange = (event: ChangeEvent) => { setName(event.currentTarget.value); }; const onRoleChange = (role: SelectableValue) => { setRole(role.value!); }; const onSecondsToLiveChange = (event: ChangeEvent) => { setSecondsToLive(event.currentTarget.value); }; return (
Add API Key
Key name
); };