import { css, cx } from '@emotion/css'; import React, { PureComponent, useRef, useState } from 'react'; import { dateTimeFormat, GrafanaTheme2, OrgRole, TimeZone } from '@grafana/data'; import { Button, ConfirmButton, ConfirmModal, Input, LegacyInputStatus, useStyles2 } from '@grafana/ui'; import { contextSrv } from 'app/core/core'; import { Role, ServiceAccountDTO, AccessControlAction } from 'app/types'; import { ServiceAccountRoleRow } from './ServiceAccountRoleRow'; interface Props { serviceAccount: ServiceAccountDTO; timeZone: TimeZone; roleOptions: Role[]; builtInRoles: Record; deleteServiceAccount: (serviceAccountId: number) => void; updateServiceAccount: (serviceAccount: ServiceAccountDTO) => void; } export function ServiceAccountProfile({ serviceAccount, timeZone, roleOptions, builtInRoles, deleteServiceAccount, updateServiceAccount, }: Props) { const [showDeleteModal, setShowDeleteModal] = useState(false); const [showDisableModal, setShowDisableModal] = useState(false); const ableToWrite = contextSrv.hasPermission(AccessControlAction.ServiceAccountsWrite); 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 = () => { deleteServiceAccount(serviceAccount.id); }; const handleServiceAccountDisable = () => { updateServiceAccount({ ...serviceAccount, isDisabled: true }); setShowDisableModal(false); }; const handleServiceAccountEnable = () => { updateServiceAccount({ ...serviceAccount, isDisabled: false }); }; const handleServiceAccountRoleChange = (role: OrgRole) => { updateServiceAccount({ ...serviceAccount, role: role }); }; const onServiceAccountNameChange = (newValue: string) => { updateServiceAccount({ ...serviceAccount, name: newValue }); }; const styles = useStyles2(getStyles); return ( <>
Information
{/* */}
<> {serviceAccount.isDisabled ? ( ) : ( <> )}
); } const getStyles = (theme: GrafanaTheme2) => { return { buttonRow: css` margin-top: ${theme.spacing(1.5)}; > * { margin-right: ${theme.spacing(2)}; } `, }; }; interface ServiceAccountProfileRowProps { label: string; value?: string; inputType?: string; onChange?: (value: string) => void; disabled?: boolean; } 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 = () => { this.setState({ editing: false }); 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.props.disabled && this.state.editing ? ( ) : ( {this.props.value} )} {this.props.onChange && ( Edit )} ); } }