import React from 'react'; import { getBackendSrv } from '@grafana/runtime'; import { UserOrgDTO } from '@grafana/data'; import { Modal, Button, CustomScrollbar } from '@grafana/ui'; import { contextSrv } from 'app/core/services/context_srv'; import config from 'app/core/config'; import { css } from '@emotion/css'; interface Props { onDismiss: () => void; } interface State { orgs: UserOrgDTO[]; } export class OrgSwitcher extends React.PureComponent { state: State = { orgs: [], }; componentDidMount() { this.getUserOrgs(); } getUserOrgs = async () => { const orgs: UserOrgDTO[] = await getBackendSrv().get('/api/user/orgs'); this.setState({ orgs }); }; setCurrentOrg = async (org: UserOrgDTO) => { await getBackendSrv().post(`/api/user/using/${org.orgId}`); this.setWindowLocation(`${config.appSubUrl}${config.appSubUrl.endsWith('/') ? '' : '/'}?orgId=${org.orgId}`); }; setWindowLocation(href: string) { window.location.href = href; } render() { const { onDismiss } = this.props; const { orgs } = this.state; const currentOrgId = contextSrv.user.orgId; const contentClassName = css({ display: 'flex', maxHeight: 'calc(85vh - 42px)', }); return ( {orgs.map((org) => ( ))}
Name Role
{org.name} {org.role} {org.orgId === currentOrgId ? ( ) : ( )}
); } }