TeamList.tsx 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. import React, { PureComponent } from 'react';
  2. import { NavModel } from '@grafana/data';
  3. import { DeleteButton, LinkButton, FilterInput, VerticalGroup, HorizontalGroup, Pagination } from '@grafana/ui';
  4. import EmptyListCTA from 'app/core/components/EmptyListCTA/EmptyListCTA';
  5. import Page from 'app/core/components/Page/Page';
  6. import { TeamRolePicker } from 'app/core/components/RolePicker/TeamRolePicker';
  7. import { fetchRoleOptions } from 'app/core/components/RolePicker/api';
  8. import { config } from 'app/core/config';
  9. import { getNavModel } from 'app/core/selectors/navModel';
  10. import { contextSrv, User } from 'app/core/services/context_srv';
  11. import { AccessControlAction, Role, StoreState, Team } from 'app/types';
  12. import { connectWithCleanUp } from '../../core/components/connectWithCleanUp';
  13. import { deleteTeam, loadTeams } from './state/actions';
  14. import { setSearchQuery, setTeamsSearchPage } from './state/reducers';
  15. import { getSearchQuery, getTeams, getTeamsCount, getTeamsSearchPage, isPermissionTeamAdmin } from './state/selectors';
  16. const pageLimit = 30;
  17. export interface Props {
  18. navModel: NavModel;
  19. teams: Team[];
  20. searchQuery: string;
  21. searchPage: number;
  22. teamsCount: number;
  23. hasFetched: boolean;
  24. loadTeams: typeof loadTeams;
  25. deleteTeam: typeof deleteTeam;
  26. setSearchQuery: typeof setSearchQuery;
  27. setTeamsSearchPage: typeof setTeamsSearchPage;
  28. editorsCanAdmin: boolean;
  29. signedInUser: User;
  30. }
  31. export interface State {
  32. roleOptions: Role[];
  33. }
  34. export class TeamList extends PureComponent<Props, State> {
  35. constructor(props: Props) {
  36. super(props);
  37. this.state = { roleOptions: [] };
  38. }
  39. componentDidMount() {
  40. this.fetchTeams();
  41. if (contextSrv.licensedAccessControlEnabled() && contextSrv.hasPermission(AccessControlAction.ActionRolesList)) {
  42. this.fetchRoleOptions();
  43. }
  44. }
  45. async fetchTeams() {
  46. await this.props.loadTeams();
  47. }
  48. async fetchRoleOptions() {
  49. const roleOptions = await fetchRoleOptions();
  50. this.setState({ roleOptions });
  51. }
  52. deleteTeam = (team: Team) => {
  53. this.props.deleteTeam(team.id);
  54. };
  55. onSearchQueryChange = (value: string) => {
  56. this.props.setSearchQuery(value);
  57. };
  58. renderTeam(team: Team) {
  59. const { editorsCanAdmin, signedInUser } = this.props;
  60. const permission = team.permission;
  61. const teamUrl = `org/teams/edit/${team.id}`;
  62. const isTeamAdmin = isPermissionTeamAdmin({ permission, editorsCanAdmin, signedInUser });
  63. const canDelete = contextSrv.hasAccessInMetadata(AccessControlAction.ActionTeamsDelete, team, isTeamAdmin);
  64. const canReadTeam = contextSrv.hasAccessInMetadata(AccessControlAction.ActionTeamsRead, team, isTeamAdmin);
  65. const canSeeTeamRoles = contextSrv.hasAccessInMetadata(AccessControlAction.ActionTeamsRolesList, team, false);
  66. const canUpdateTeamRoles =
  67. contextSrv.hasAccess(AccessControlAction.ActionTeamsRolesAdd, false) ||
  68. contextSrv.hasAccess(AccessControlAction.ActionTeamsRolesRemove, false);
  69. const displayRolePicker =
  70. contextSrv.licensedAccessControlEnabled() &&
  71. contextSrv.hasPermission(AccessControlAction.ActionTeamsRolesList) &&
  72. contextSrv.hasPermission(AccessControlAction.ActionRolesList);
  73. return (
  74. <tr key={team.id}>
  75. <td className="width-4 text-center link-td">
  76. {canReadTeam ? (
  77. <a href={teamUrl}>
  78. <img className="filter-table__avatar" src={team.avatarUrl} alt="Team avatar" />
  79. </a>
  80. ) : (
  81. <img className="filter-table__avatar" src={team.avatarUrl} alt="Team avatar" />
  82. )}
  83. </td>
  84. <td className="link-td">
  85. {canReadTeam ? <a href={teamUrl}>{team.name}</a> : <div style={{ padding: '0px 8px' }}>{team.name}</div>}
  86. </td>
  87. <td className="link-td">
  88. {canReadTeam ? (
  89. <a href={teamUrl} aria-label={team.email?.length > 0 ? undefined : 'Empty email cell'}>
  90. {team.email}
  91. </a>
  92. ) : (
  93. <div style={{ padding: '0px 8px' }} aria-label={team.email?.length > 0 ? undefined : 'Empty email cell'}>
  94. {team.email}
  95. </div>
  96. )}
  97. </td>
  98. <td className="link-td">
  99. {canReadTeam ? (
  100. <a href={teamUrl}>{team.memberCount}</a>
  101. ) : (
  102. <div style={{ padding: '0px 8px' }}>{team.memberCount}</div>
  103. )}
  104. </td>
  105. {displayRolePicker && (
  106. <td>
  107. {canSeeTeamRoles && (
  108. <TeamRolePicker teamId={team.id} roleOptions={this.state.roleOptions} disabled={!canUpdateTeamRoles} />
  109. )}
  110. </td>
  111. )}
  112. <td className="text-right">
  113. <DeleteButton
  114. aria-label="Delete team"
  115. size="sm"
  116. disabled={!canDelete}
  117. onConfirm={() => this.deleteTeam(team)}
  118. />
  119. </td>
  120. </tr>
  121. );
  122. }
  123. renderEmptyList() {
  124. return (
  125. <EmptyListCTA
  126. title="You haven't created any teams yet."
  127. buttonIcon="users-alt"
  128. buttonLink="org/teams/new"
  129. buttonTitle=" New team"
  130. buttonDisabled={!contextSrv.hasPermission(AccessControlAction.ActionTeamsCreate)}
  131. proTip="Assign folder and dashboard permissions to teams instead of users to ease administration."
  132. proTipLink=""
  133. proTipLinkTitle=""
  134. proTipTarget="_blank"
  135. />
  136. );
  137. }
  138. getPaginatedTeams = (teams: Team[]) => {
  139. const offset = (this.props.searchPage - 1) * pageLimit;
  140. return teams.slice(offset, offset + pageLimit);
  141. };
  142. renderTeamList() {
  143. const { teams, searchQuery, editorsCanAdmin, searchPage, setTeamsSearchPage } = this.props;
  144. const teamAdmin = contextSrv.hasRole('Admin') || (editorsCanAdmin && contextSrv.hasRole('Editor'));
  145. const canCreate = contextSrv.hasAccess(AccessControlAction.ActionTeamsCreate, teamAdmin);
  146. const displayRolePicker =
  147. contextSrv.licensedAccessControlEnabled() &&
  148. contextSrv.hasPermission(AccessControlAction.ActionTeamsRolesList) &&
  149. contextSrv.hasPermission(AccessControlAction.ActionRolesList);
  150. const newTeamHref = canCreate ? 'org/teams/new' : '#';
  151. const paginatedTeams = this.getPaginatedTeams(teams);
  152. const totalPages = Math.ceil(teams.length / pageLimit);
  153. return (
  154. <>
  155. <div className="page-action-bar">
  156. <div className="gf-form gf-form--grow">
  157. <FilterInput placeholder="Search teams" value={searchQuery} onChange={this.onSearchQueryChange} />
  158. </div>
  159. <LinkButton href={newTeamHref} disabled={!canCreate}>
  160. New Team
  161. </LinkButton>
  162. </div>
  163. <div className="admin-list-table">
  164. <VerticalGroup spacing="md">
  165. <table className="filter-table filter-table--hover form-inline">
  166. <thead>
  167. <tr>
  168. <th />
  169. <th>Name</th>
  170. <th>Email</th>
  171. <th>Members</th>
  172. {displayRolePicker && <th>Roles</th>}
  173. <th style={{ width: '1%' }} />
  174. </tr>
  175. </thead>
  176. <tbody>{paginatedTeams.map((team) => this.renderTeam(team))}</tbody>
  177. </table>
  178. <HorizontalGroup justify="flex-end">
  179. <Pagination
  180. onNavigate={setTeamsSearchPage}
  181. currentPage={searchPage}
  182. numberOfPages={totalPages}
  183. hideWhenSinglePage={true}
  184. />
  185. </HorizontalGroup>
  186. </VerticalGroup>
  187. </div>
  188. </>
  189. );
  190. }
  191. renderList() {
  192. const { teamsCount, hasFetched } = this.props;
  193. if (!hasFetched) {
  194. return null;
  195. }
  196. if (teamsCount > 0) {
  197. return this.renderTeamList();
  198. } else {
  199. return this.renderEmptyList();
  200. }
  201. }
  202. render() {
  203. const { hasFetched, navModel } = this.props;
  204. return (
  205. <Page navModel={navModel}>
  206. <Page.Contents isLoading={!hasFetched}>{this.renderList()}</Page.Contents>
  207. </Page>
  208. );
  209. }
  210. }
  211. function mapStateToProps(state: StoreState) {
  212. return {
  213. navModel: getNavModel(state.navIndex, 'teams'),
  214. teams: getTeams(state.teams),
  215. searchQuery: getSearchQuery(state.teams),
  216. searchPage: getTeamsSearchPage(state.teams),
  217. teamsCount: getTeamsCount(state.teams),
  218. hasFetched: state.teams.hasFetched,
  219. editorsCanAdmin: config.editorsCanAdmin, // this makes the feature toggle mockable/controllable from tests,
  220. signedInUser: contextSrv.user, // this makes the feature toggle mockable/controllable from tests,
  221. };
  222. }
  223. const mapDispatchToProps = {
  224. loadTeams,
  225. deleteTeam,
  226. setSearchQuery,
  227. setTeamsSearchPage,
  228. };
  229. export default connectWithCleanUp(mapStateToProps, mapDispatchToProps, (state) => state.teams)(TeamList);