LicensePage.tsx 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import React, { useEffect, useState } from 'react';
  2. import { connect, ConnectedProps } from 'react-redux';
  3. import { config } from '@grafana/runtime';
  4. import Page from 'app/core/components/Page/Page';
  5. import { GrafanaRouteComponentProps } from 'app/core/navigation/types';
  6. import { getNavModel } from 'app/core/selectors/navModel';
  7. import { contextSrv } from 'app/core/services/context_srv';
  8. import { ServerStats } from 'app/features/admin/ServerStats';
  9. import { AccessControlAction, EnterpriseStoreState } from '../types';
  10. import { LicenseInfo } from './LicenseInfo';
  11. import { getLicenseToken, refreshLicenseStats } from './state/api';
  12. import { ActiveUserStats, LicenseToken } from './types';
  13. import { initLicenseWarnings } from './index';
  14. interface QueryParams {
  15. tokenUpdated?: boolean;
  16. tokenRenewed?: boolean;
  17. }
  18. interface OwnProps extends GrafanaRouteComponentProps<{}, QueryParams> {}
  19. const mapStateToProps = (state: EnterpriseStoreState) => ({
  20. navModel: getNavModel(state.navIndex, 'licensing'),
  21. });
  22. const connector = connect(mapStateToProps, {});
  23. export type Props = ConnectedProps<typeof connector> & OwnProps;
  24. export const LicensePage = ({ navModel, queryParams }: Props) => {
  25. const [token, setToken] = useState<LicenseToken | null>(null);
  26. const [stats, setStats] = useState<ActiveUserStats | null>(null);
  27. const [isLoading, setIsLoading] = useState(false);
  28. const { tokenUpdated, tokenRenewed } = queryParams;
  29. useEffect(() => {
  30. if (!contextSrv.hasAccess(AccessControlAction.LicensingRead, contextSrv.isGrafanaAdmin)) {
  31. return;
  32. }
  33. setIsLoading(true);
  34. const getData = async () => {
  35. const token = await getLicenseToken().catch(() => null);
  36. const stats = await refreshLicenseStats().catch(() => null);
  37. setToken(token);
  38. setStats(stats);
  39. setIsLoading(false);
  40. };
  41. getData();
  42. return initLicenseWarnings;
  43. }, []);
  44. return (
  45. <Page navModel={navModel}>
  46. <Page.Contents>
  47. <ServerStats />
  48. <LicenseInfo
  49. token={token}
  50. stats={stats}
  51. tokenUpdated={tokenUpdated}
  52. tokenRenewed={tokenRenewed}
  53. isLoading={isLoading}
  54. licensedUrl={(config as any).licenseInfo?.appUrl}
  55. />
  56. </Page.Contents>
  57. </Page>
  58. );
  59. };
  60. export default connector(LicensePage);