2022-02-16 02:48:13 -06:00
|
|
|
import { css } from '@emotion/css';
|
2022-04-22 08:33:13 -05:00
|
|
|
import React, { ReactElement } from 'react';
|
|
|
|
import { useAsync } from 'react-use';
|
|
|
|
|
2019-12-03 15:47:20 -06:00
|
|
|
import { UserOrgDTO } from '@grafana/data';
|
2022-02-16 02:48:13 -06:00
|
|
|
import { Button, CustomScrollbar, Modal } from '@grafana/ui';
|
2019-12-03 15:47:20 -06:00
|
|
|
import config from 'app/core/config';
|
2022-04-22 08:33:13 -05:00
|
|
|
import { contextSrv } from 'app/core/services/context_srv';
|
|
|
|
|
2022-02-16 02:48:13 -06:00
|
|
|
import { api } from '../../features/profile/api';
|
2019-12-03 15:47:20 -06:00
|
|
|
|
|
|
|
interface Props {
|
|
|
|
onDismiss: () => void;
|
|
|
|
}
|
|
|
|
|
2022-02-16 02:48:13 -06:00
|
|
|
export function OrgSwitcher({ onDismiss }: Props): ReactElement {
|
|
|
|
const { value: orgs = [] } = useAsync(() => {
|
|
|
|
return api.loadOrgs();
|
|
|
|
}, []);
|
|
|
|
const currentOrgId = contextSrv.user.orgId;
|
|
|
|
const contentClassName = css({
|
|
|
|
display: 'flex',
|
|
|
|
maxHeight: 'calc(85vh - 42px)',
|
|
|
|
});
|
|
|
|
const setCurrentOrg = async (org: UserOrgDTO) => {
|
|
|
|
await api.setUserOrg(org);
|
|
|
|
window.location.href = `${config.appSubUrl}${config.appSubUrl.endsWith('/') ? '' : '/'}?orgId=${org.orgId}`;
|
2019-12-03 15:47:20 -06:00
|
|
|
};
|
|
|
|
|
2022-02-16 02:48:13 -06:00
|
|
|
return (
|
|
|
|
<Modal
|
|
|
|
title="Switch Organization"
|
|
|
|
icon="arrow-random"
|
|
|
|
onDismiss={onDismiss}
|
|
|
|
isOpen={true}
|
|
|
|
contentClassName={contentClassName}
|
|
|
|
>
|
|
|
|
<CustomScrollbar autoHeightMin="100%">
|
|
|
|
<table className="filter-table form-inline">
|
|
|
|
<thead>
|
|
|
|
<tr>
|
|
|
|
<th>Name</th>
|
|
|
|
<th>Role</th>
|
|
|
|
<th />
|
|
|
|
</tr>
|
|
|
|
</thead>
|
|
|
|
<tbody>
|
|
|
|
{orgs.map((org) => (
|
|
|
|
<tr key={org.orgId}>
|
|
|
|
<td>{org.name}</td>
|
|
|
|
<td>{org.role}</td>
|
|
|
|
<td className="text-right">
|
|
|
|
{org.orgId === currentOrgId ? (
|
|
|
|
<Button size="sm">Current</Button>
|
|
|
|
) : (
|
|
|
|
<Button variant="secondary" size="sm" onClick={() => setCurrentOrg(org)}>
|
|
|
|
Switch to
|
|
|
|
</Button>
|
|
|
|
)}
|
|
|
|
</td>
|
2019-12-03 15:47:20 -06:00
|
|
|
</tr>
|
2022-02-16 02:48:13 -06:00
|
|
|
))}
|
|
|
|
</tbody>
|
|
|
|
</table>
|
|
|
|
</CustomScrollbar>
|
|
|
|
</Modal>
|
|
|
|
);
|
2019-12-03 15:47:20 -06:00
|
|
|
}
|