import React, { memo, useEffect } from 'react'; import { connect, ConnectedProps } from 'react-redux'; import { Button, ConfirmModal, Icon, LinkButton, useStyles2 } from '@grafana/ui'; import { css, cx } from '@emotion/css'; import Page from 'app/core/components/Page/Page'; import { StoreState, ServiceAccountDTO, AccessControlAction, Role } from 'app/types'; import { fetchACOptions, loadServiceAccounts, removeServiceAccount, updateServiceAccount, setServiceAccountToRemove, } from './state/actions'; import { getNavModel } from 'app/core/selectors/navModel'; import { getServiceAccounts, getServiceAccountsSearchPage, getServiceAccountsSearchQuery } from './state/selectors'; import PageLoader from 'app/core/components/PageLoader/PageLoader'; import { GrafanaTheme2, OrgRole } from '@grafana/data'; import { contextSrv } from 'app/core/core'; import { UserRolePicker } from 'app/core/components/RolePicker/UserRolePicker'; import { OrgRolePicker } from '../admin/OrgRolePicker'; import pluralize from 'pluralize'; export type Props = ConnectedProps; function mapStateToProps(state: StoreState) { return { navModel: getNavModel(state.navIndex, 'serviceaccounts'), serviceAccounts: getServiceAccounts(state.serviceAccounts), searchQuery: getServiceAccountsSearchQuery(state.serviceAccounts), searchPage: getServiceAccountsSearchPage(state.serviceAccounts), isLoading: state.serviceAccounts.isLoading, roleOptions: state.serviceAccounts.roleOptions, builtInRoles: state.serviceAccounts.builtInRoles, toRemove: state.serviceAccounts.serviceAccountToRemove, }; } const mapDispatchToProps = { loadServiceAccounts, fetchACOptions, updateServiceAccount, removeServiceAccount, setServiceAccountToRemove, }; const connector = connect(mapStateToProps, mapDispatchToProps); const ServiceAccountsListPage = ({ loadServiceAccounts, removeServiceAccount, fetchACOptions, updateServiceAccount, setServiceAccountToRemove, navModel, serviceAccounts, isLoading, roleOptions, builtInRoles, toRemove, }: Props) => { const styles = useStyles2(getStyles); useEffect(() => { loadServiceAccounts(); if (contextSrv.accessControlEnabled()) { fetchACOptions(); } }, [loadServiceAccounts, fetchACOptions]); const onRoleChange = (role: OrgRole, serviceAccount: ServiceAccountDTO) => { const updatedServiceAccount = { ...serviceAccount, role: role }; updateServiceAccount(updatedServiceAccount); }; return (

Service accounts

{contextSrv.hasPermission(AccessControlAction.ServiceAccountsCreate) && ( New service account )}
{isLoading ? ( ) : ( <>
{serviceAccounts.map((serviceAccount: ServiceAccountDTO) => ( ))}
Display name ID Roles Tokens
)} {toRemove && ( Are you sure you want to delete '{toRemove.name}' {Boolean(toRemove.tokens) && ` and ${toRemove.tokens} accompanying ${pluralize('token', toRemove.tokens)}`} ? } confirmText="Delete" title="Delete service account" onDismiss={() => { setServiceAccountToRemove(null); }} isOpen={true} onConfirm={() => { removeServiceAccount(toRemove.id); setServiceAccountToRemove(null); }} /> )}
); }; type ServiceAccountListItemProps = { serviceAccount: ServiceAccountDTO; onRoleChange: (role: OrgRole, serviceAccount: ServiceAccountDTO) => void; roleOptions: Role[]; builtInRoles: Record; onSetToRemove: (serviceAccount: ServiceAccountDTO) => void; }; const getServiceAccountsAriaLabel = (name: string) => { return `Edit service account's ${name} details`; }; const ServiceAccountListItem = memo( ({ serviceAccount, onRoleChange, roleOptions, builtInRoles, onSetToRemove }: ServiceAccountListItemProps) => { const editUrl = `org/serviceAccounts/${serviceAccount.id}`; const styles = useStyles2(getStyles); const canUpdateRole = contextSrv.hasPermissionInMetadata(AccessControlAction.ServiceAccountsWrite, serviceAccount); const rolePickerDisabled = !canUpdateRole; return ( {`Avatar {serviceAccount.name} {serviceAccount.login} {contextSrv.licensedAccessControlEnabled() ? ( onRoleChange(newRole, serviceAccount)} roleOptions={roleOptions} builtInRoles={builtInRoles} disabled={rolePickerDisabled} /> ) : ( onRoleChange(newRole, serviceAccount)} /> )} {serviceAccount.tokens} {contextSrv.hasPermissionInMetadata(AccessControlAction.ServiceAccountsDelete, serviceAccount) && (