ApiKeysPage.tsx 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. import React, { PureComponent } from 'react';
  2. import { connect, ConnectedProps } from 'react-redux';
  3. // Utils
  4. import { rangeUtil } from '@grafana/data';
  5. import { InlineField, InlineSwitch, VerticalGroup } from '@grafana/ui';
  6. import appEvents from 'app/core/app_events';
  7. import EmptyListCTA from 'app/core/components/EmptyListCTA/EmptyListCTA';
  8. import Page from 'app/core/components/Page/Page';
  9. import config from 'app/core/config';
  10. import { contextSrv } from 'app/core/core';
  11. import { getNavModel } from 'app/core/selectors/navModel';
  12. import { getTimeZone } from 'app/features/profile/state/selectors';
  13. import { AccessControlAction, ApiKey, NewApiKey, StoreState } from 'app/types';
  14. import { ShowModalReactEvent } from 'app/types/events';
  15. import { ApiKeysActionBar } from './ApiKeysActionBar';
  16. import { ApiKeysAddedModal } from './ApiKeysAddedModal';
  17. import { ApiKeysController } from './ApiKeysController';
  18. import { ApiKeysForm } from './ApiKeysForm';
  19. import { ApiKeysTable } from './ApiKeysTable';
  20. import { addApiKey, deleteApiKey, loadApiKeys, toggleIncludeExpired } from './state/actions';
  21. import { setSearchQuery } from './state/reducers';
  22. import { getApiKeys, getApiKeysCount, getIncludeExpired, getIncludeExpiredDisabled } from './state/selectors';
  23. function mapStateToProps(state: StoreState) {
  24. const canCreate = contextSrv.hasAccess(AccessControlAction.ActionAPIKeysCreate, true);
  25. return {
  26. navModel: getNavModel(state.navIndex, 'apikeys'),
  27. apiKeys: getApiKeys(state.apiKeys),
  28. searchQuery: state.apiKeys.searchQuery,
  29. apiKeysCount: getApiKeysCount(state.apiKeys),
  30. hasFetched: state.apiKeys.hasFetched,
  31. timeZone: getTimeZone(state.user),
  32. includeExpired: getIncludeExpired(state.apiKeys),
  33. includeExpiredDisabled: getIncludeExpiredDisabled(state.apiKeys),
  34. canCreate: canCreate,
  35. };
  36. }
  37. const mapDispatchToProps = {
  38. loadApiKeys,
  39. deleteApiKey,
  40. setSearchQuery,
  41. toggleIncludeExpired,
  42. addApiKey,
  43. };
  44. const connector = connect(mapStateToProps, mapDispatchToProps);
  45. interface OwnProps {}
  46. export type Props = OwnProps & ConnectedProps<typeof connector>;
  47. interface State {
  48. isAdding: boolean;
  49. }
  50. export class ApiKeysPageUnconnected extends PureComponent<Props, State> {
  51. constructor(props: Props) {
  52. super(props);
  53. }
  54. componentDidMount() {
  55. this.fetchApiKeys();
  56. }
  57. async fetchApiKeys() {
  58. await this.props.loadApiKeys();
  59. }
  60. onDeleteApiKey = (key: ApiKey) => {
  61. this.props.deleteApiKey(key.id!);
  62. };
  63. onSearchQueryChange = (value: string) => {
  64. this.props.setSearchQuery(value);
  65. };
  66. onIncludeExpiredChange = (event: React.SyntheticEvent<HTMLInputElement>) => {
  67. this.props.toggleIncludeExpired();
  68. };
  69. onAddApiKey = (newApiKey: NewApiKey) => {
  70. const openModal = (apiKey: string) => {
  71. const rootPath = window.location.origin + config.appSubUrl;
  72. appEvents.publish(
  73. new ShowModalReactEvent({
  74. props: {
  75. apiKey,
  76. rootPath,
  77. },
  78. component: ApiKeysAddedModal,
  79. })
  80. );
  81. };
  82. const secondsToLive = newApiKey.secondsToLive;
  83. try {
  84. const secondsToLiveAsNumber = secondsToLive ? rangeUtil.intervalToSeconds(secondsToLive) : null;
  85. const apiKey: ApiKey = {
  86. ...newApiKey,
  87. secondsToLive: secondsToLiveAsNumber,
  88. };
  89. this.props.addApiKey(apiKey, openModal);
  90. this.setState((prevState: State) => {
  91. return {
  92. ...prevState,
  93. isAdding: false,
  94. };
  95. });
  96. } catch (err) {
  97. console.error(err);
  98. }
  99. };
  100. render() {
  101. const {
  102. hasFetched,
  103. navModel,
  104. apiKeysCount,
  105. apiKeys,
  106. searchQuery,
  107. timeZone,
  108. includeExpired,
  109. includeExpiredDisabled,
  110. canCreate,
  111. } = this.props;
  112. if (!hasFetched) {
  113. return (
  114. <Page navModel={navModel}>
  115. <Page.Contents isLoading={true}>{}</Page.Contents>
  116. </Page>
  117. );
  118. }
  119. return (
  120. <Page navModel={navModel}>
  121. <Page.Contents isLoading={false}>
  122. <ApiKeysController>
  123. {({ isAdding, toggleIsAdding }) => {
  124. const showCTA = !isAdding && apiKeysCount === 0;
  125. const showTable = apiKeysCount > 0;
  126. return (
  127. <>
  128. {showCTA ? (
  129. <EmptyListCTA
  130. title="You haven't added any API keys yet."
  131. buttonIcon="key-skeleton-alt"
  132. onClick={toggleIsAdding}
  133. buttonTitle="New API key"
  134. proTip="Remember, you can provide view-only API access to other applications."
  135. buttonDisabled={!canCreate}
  136. />
  137. ) : null}
  138. {showTable ? (
  139. <ApiKeysActionBar
  140. searchQuery={searchQuery}
  141. disabled={isAdding || !canCreate}
  142. onAddClick={toggleIsAdding}
  143. onSearchChange={this.onSearchQueryChange}
  144. />
  145. ) : null}
  146. <ApiKeysForm
  147. show={isAdding}
  148. onClose={toggleIsAdding}
  149. onKeyAdded={this.onAddApiKey}
  150. disabled={!canCreate}
  151. />
  152. {showTable ? (
  153. <VerticalGroup>
  154. <InlineField disabled={includeExpiredDisabled} label="Include expired keys">
  155. <InlineSwitch id="showExpired" value={includeExpired} onChange={this.onIncludeExpiredChange} />
  156. </InlineField>
  157. <ApiKeysTable apiKeys={apiKeys} timeZone={timeZone} onDelete={this.onDeleteApiKey} />
  158. </VerticalGroup>
  159. ) : null}
  160. </>
  161. );
  162. }}
  163. </ApiKeysController>
  164. </Page.Contents>
  165. </Page>
  166. );
  167. }
  168. }
  169. const ApiKeysPage = connector(ApiKeysPageUnconnected);
  170. export default ApiKeysPage;