import React, { PureComponent, useRef, useState } from 'react'; import { Role, ServiceAccountDTO } from 'app/types'; import { css, cx } from '@emotion/css'; import { config } from 'app/core/config'; import { dateTimeFormat, GrafanaTheme, OrgRole, TimeZone } from '@grafana/data'; import { Button, ConfirmButton, ConfirmModal, Input, LegacyInputStatus, stylesFactory } from '@grafana/ui'; import { ServiceAccountRoleRow } from './ServiceAccountRoleRow'; interface Props { serviceAccount: ServiceAccountDTO; timeZone: TimeZone; onServiceAccountUpdate: (serviceAccount: ServiceAccountDTO) => void; onServiceAccountDelete: (serviceAccountId: number) => void; onServiceAccountDisable: (serviceAccountId: number) => void; onServiceAccountEnable: (serviceAccountId: number) => void; onRoleChange: (role: OrgRole, serviceAccount: ServiceAccountDTO) => void; roleOptions: Role[]; builtInRoles: Record; } export function ServiceAccountProfile({ serviceAccount, timeZone, onServiceAccountUpdate, onServiceAccountDelete, onServiceAccountDisable, onServiceAccountEnable, onRoleChange, roleOptions, builtInRoles, }: Props) { const [showDeleteModal, setShowDeleteModal] = useState(false); const [showDisableModal, setShowDisableModal] = useState(false); const deleteServiceAccountRef = useRef(null); const showDeleteServiceAccountModal = (show: boolean) => () => { setShowDeleteModal(show); if (!show && deleteServiceAccountRef.current) { deleteServiceAccountRef.current.focus(); } }; const disableServiceAccountRef = useRef(null); const showDisableServiceAccountModal = (show: boolean) => () => { setShowDisableModal(show); if (!show && disableServiceAccountRef.current) { disableServiceAccountRef.current.focus(); } }; const handleServiceAccountDelete = () => onServiceAccountDelete(serviceAccount.id); const handleServiceAccountDisable = () => onServiceAccountDisable(serviceAccount.id); const handleServiceAccountEnable = () => onServiceAccountEnable(serviceAccount.id); const onServiceAccountNameChange = (newValue: string) => { onServiceAccountUpdate({ ...serviceAccount, name: newValue, }); }; const styles = getStyles(config.theme); return ( <>

Information

{serviceAccount.isDisabled && ( )} {!serviceAccount.isDisabled && ( <> )} ); } const getStyles = stylesFactory((theme: GrafanaTheme) => { return { buttonRow: css` margin-top: 0.8rem; > * { margin-right: 16px; } `, }; }); interface ServiceAccountProfileRowProps { label: string; value?: string; inputType?: string; onChange?: (value: string) => void; } interface ServiceAccountProfileRowState { value: string; editing: boolean; } export class ServiceAccountProfileRow extends PureComponent< ServiceAccountProfileRowProps, ServiceAccountProfileRowState > { inputElem?: HTMLInputElement; static defaultProps: Partial = { value: '', inputType: 'text', }; state = { editing: false, value: this.props.value || '', }; setInputElem = (elem: any) => { this.inputElem = elem; }; onEditClick = () => { this.setState({ editing: true }, this.focusInput); }; onCancelClick = () => { this.setState({ editing: false, value: this.props.value || '' }); }; onInputChange = (event: React.ChangeEvent, status?: LegacyInputStatus) => { if (status === LegacyInputStatus.Invalid) { return; } this.setState({ value: event.target.value }); }; onInputBlur = (event: React.FocusEvent, status?: LegacyInputStatus) => { if (status === LegacyInputStatus.Invalid) { return; } this.setState({ value: event.target.value }); }; focusInput = () => { if (this.inputElem && this.inputElem.focus) { this.inputElem.focus(); } }; onSave = () => { if (this.props.onChange) { this.props.onChange(this.state.value); } }; render() { const { label, inputType } = this.props; const { value } = this.state; const labelClass = cx( 'width-16', css` font-weight: 500; ` ); const inputId = `${label}-input`; return ( {this.state.editing ? ( ) : ( {this.props.value} )} {this.props.onChange && ( Edit )} ); } }