ApiKeysAddedModal.tsx 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { css } from '@emotion/css';
  2. import React, { useCallback } from 'react';
  3. import { GrafanaTheme2 } from '@grafana/data';
  4. import { Alert, Field, Modal, useStyles2, Input, Icon, ClipboardButton } from '@grafana/ui';
  5. import { notifyApp } from '../../core/actions';
  6. import { createSuccessNotification } from '../../core/copy/appNotification';
  7. import { dispatch } from '../../store/store';
  8. export interface Props {
  9. onDismiss: () => void;
  10. apiKey: string;
  11. rootPath: string;
  12. }
  13. export function ApiKeysAddedModal({ onDismiss, apiKey, rootPath }: Props): JSX.Element {
  14. const styles = useStyles2(getStyles);
  15. const getClipboardText = useCallback(() => apiKey, [apiKey]);
  16. const onClipboardCopy = () => {
  17. dispatch(notifyApp(createSuccessNotification('Content copied to clipboard')));
  18. };
  19. return (
  20. <Modal title="API Key Created" onDismiss={onDismiss} onClickBackdrop={onDismiss} isOpen>
  21. <Field label="Key">
  22. <Input
  23. id="Key"
  24. value={apiKey}
  25. readOnly
  26. addonAfter={
  27. <ClipboardButton variant="primary" getText={getClipboardText} onClipboardCopy={onClipboardCopy}>
  28. <Icon name="copy" /> Copy
  29. </ClipboardButton>
  30. }
  31. />
  32. </Field>
  33. <Alert severity="info" title="You will only be able to view this key here once!">
  34. It is not stored in this form, so be sure to copy it now.
  35. </Alert>
  36. <p className="text-muted">You can authenticate a request using the Authorization HTTP header, example:</p>
  37. <pre className={styles.small}>
  38. curl -H &quot;Authorization: Bearer {apiKey}&quot; {rootPath}/api/dashboards/home
  39. </pre>
  40. </Modal>
  41. );
  42. }
  43. function getStyles(theme: GrafanaTheme2) {
  44. return {
  45. label: css`
  46. padding: ${theme.spacing(1)};
  47. background-color: ${theme.colors.background.secondary};
  48. border-radius: ${theme.shape.borderRadius()};
  49. `,
  50. small: css`
  51. font-size: ${theme.typography.bodySmall.fontSize};
  52. font-weight: ${theme.typography.bodySmall.fontWeight};
  53. `,
  54. };
  55. }