UsersActionBar.tsx 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import React, { PureComponent } from 'react';
  2. import { connect } from 'react-redux';
  3. import { RadioButtonGroup, LinkButton, FilterInput } from '@grafana/ui';
  4. import { contextSrv } from 'app/core/core';
  5. import { AccessControlAction } from 'app/types';
  6. import { selectTotal } from '../invites/state/selectors';
  7. import { setUsersSearchQuery } from './state/reducers';
  8. import { getUsersSearchQuery } from './state/selectors';
  9. export interface Props {
  10. searchQuery: string;
  11. setUsersSearchQuery: typeof setUsersSearchQuery;
  12. onShowInvites: () => void;
  13. pendingInvitesCount: number;
  14. canInvite: boolean;
  15. showInvites: boolean;
  16. externalUserMngLinkUrl: string;
  17. externalUserMngLinkName: string;
  18. }
  19. export class UsersActionBar extends PureComponent<Props> {
  20. render() {
  21. const {
  22. canInvite,
  23. externalUserMngLinkName,
  24. externalUserMngLinkUrl,
  25. searchQuery,
  26. pendingInvitesCount,
  27. setUsersSearchQuery,
  28. onShowInvites,
  29. showInvites,
  30. } = this.props;
  31. const options = [
  32. { label: 'Users', value: 'users' },
  33. { label: `Pending Invites (${pendingInvitesCount})`, value: 'invites' },
  34. ];
  35. const canAddToOrg = contextSrv.hasAccess(AccessControlAction.UsersCreate, canInvite);
  36. return (
  37. <div className="page-action-bar" data-testid="users-action-bar">
  38. <div className="gf-form gf-form--grow">
  39. <FilterInput
  40. value={searchQuery}
  41. onChange={setUsersSearchQuery}
  42. placeholder="Search user by login, email or name"
  43. />
  44. </div>
  45. {pendingInvitesCount > 0 && (
  46. <div style={{ marginLeft: '1rem' }}>
  47. <RadioButtonGroup value={showInvites ? 'invites' : 'users'} options={options} onChange={onShowInvites} />
  48. </div>
  49. )}
  50. {canAddToOrg && <LinkButton href="org/users/invite">Invite</LinkButton>}
  51. {externalUserMngLinkUrl && (
  52. <LinkButton href={externalUserMngLinkUrl} target="_blank" rel="noopener">
  53. {externalUserMngLinkName}
  54. </LinkButton>
  55. )}
  56. </div>
  57. );
  58. }
  59. }
  60. function mapStateToProps(state: any) {
  61. return {
  62. searchQuery: getUsersSearchQuery(state.users),
  63. pendingInvitesCount: selectTotal(state.invites),
  64. externalUserMngLinkName: state.users.externalUserMngLinkName,
  65. externalUserMngLinkUrl: state.users.externalUserMngLinkUrl,
  66. canInvite: state.users.canInvite,
  67. };
  68. }
  69. const mapDispatchToProps = {
  70. setUsersSearchQuery,
  71. };
  72. export default connect(mapStateToProps, mapDispatchToProps)(UsersActionBar);