CreateServiceAccountTokenModal.tsx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. import { css } from '@emotion/css';
  2. import React, { useState } from 'react';
  3. import { GrafanaTheme2 } from '@grafana/data';
  4. import {
  5. Button,
  6. ClipboardButton,
  7. DatePickerWithInput,
  8. Field,
  9. FieldSet,
  10. HorizontalGroup,
  11. Icon,
  12. Input,
  13. Label,
  14. Modal,
  15. RadioButtonGroup,
  16. useStyles2,
  17. } from '@grafana/ui';
  18. const EXPIRATION_OPTIONS = [
  19. { label: 'No expiration', value: false },
  20. { label: 'Set expiration date', value: true },
  21. ];
  22. export type ServiceAccountToken = {
  23. name: string;
  24. secondsToLive: number;
  25. };
  26. interface CreateTokenModalProps {
  27. isOpen: boolean;
  28. token: string;
  29. onCreateToken: (token: ServiceAccountToken) => void;
  30. onClose: () => void;
  31. }
  32. export const CreateTokenModal = ({ isOpen, token, onCreateToken, onClose }: CreateTokenModalProps) => {
  33. const [newTokenName, setNewTokenName] = useState('');
  34. const [isWithExpirationDate, setIsWithExpirationDate] = useState(false);
  35. const [newTokenExpirationDate, setNewTokenExpirationDate] = useState<Date | string>('');
  36. const [isExpirationDateValid, setIsExpirationDateValid] = useState(false);
  37. const styles = useStyles2(getStyles);
  38. const onExpirationDateChange = (value: Date | string) => {
  39. const isValid = value !== '';
  40. setIsExpirationDateValid(isValid);
  41. setNewTokenExpirationDate(value);
  42. };
  43. const onCloseInternal = () => {
  44. setNewTokenName('');
  45. setIsWithExpirationDate(false);
  46. setNewTokenExpirationDate('');
  47. setIsExpirationDateValid(false);
  48. onClose();
  49. };
  50. const modalTitle = (
  51. <div className={styles.modalHeaderTitle}>
  52. <Icon className={styles.modalHeaderIcon} name="key-skeleton-alt" size="lg" />
  53. <span>{!token ? 'Add service account token' : 'Service account token created'}</span>
  54. </div>
  55. );
  56. return (
  57. <Modal isOpen={isOpen} title={modalTitle} onDismiss={onCloseInternal} className={styles.modal}>
  58. {!token ? (
  59. <>
  60. <FieldSet>
  61. <Field
  62. label="Display name"
  63. description="name to easily identify the token"
  64. className={styles.modalRow}
  65. // for now this is required
  66. // need to make this optional in backend as well
  67. required={true}
  68. >
  69. <Input
  70. name="tokenName"
  71. value={newTokenName}
  72. onChange={(e) => {
  73. setNewTokenName(e.currentTarget.value);
  74. }}
  75. />
  76. </Field>
  77. <RadioButtonGroup
  78. className={styles.modalRow}
  79. options={EXPIRATION_OPTIONS}
  80. value={isWithExpirationDate}
  81. onChange={setIsWithExpirationDate}
  82. size="md"
  83. />
  84. {isWithExpirationDate && (
  85. <Field label="Expiration date" className={styles.modalRow}>
  86. <DatePickerWithInput onChange={onExpirationDateChange} value={newTokenExpirationDate} placeholder="" />
  87. </Field>
  88. )}
  89. </FieldSet>
  90. <Button
  91. onClick={() =>
  92. onCreateToken({
  93. name: newTokenName,
  94. secondsToLive: getSecondsToLive(newTokenExpirationDate),
  95. })
  96. }
  97. disabled={isWithExpirationDate && !isExpirationDateValid}
  98. >
  99. Generate token
  100. </Button>
  101. </>
  102. ) : (
  103. <>
  104. <FieldSet>
  105. <Label
  106. description="You will not be able to see or generate it again. Loosing a token requires creating new one."
  107. className={styles.modalRow}
  108. >
  109. Copy the token. It will be showed only once.
  110. </Label>
  111. <Field label="Token" className={styles.modalRow}>
  112. <div className={styles.modalTokenRow}>
  113. <Input name="tokenValue" value={token} readOnly />
  114. <ClipboardButton
  115. className={styles.modalCopyToClipboardButton}
  116. variant="secondary"
  117. size="md"
  118. getText={() => token}
  119. >
  120. <Icon name="copy" /> Copy to clipboard
  121. </ClipboardButton>
  122. </div>
  123. </Field>
  124. </FieldSet>
  125. <HorizontalGroup>
  126. <ClipboardButton variant="primary" getText={() => token} onClipboardCopy={onCloseInternal}>
  127. Copy to clipboard and close
  128. </ClipboardButton>
  129. <Button variant="secondary" onClick={onCloseInternal}>
  130. Close
  131. </Button>
  132. </HorizontalGroup>
  133. </>
  134. )}
  135. </Modal>
  136. );
  137. };
  138. const getSecondsToLive = (date: Date | string) => {
  139. const dateAsDate = new Date(date);
  140. const now = new Date();
  141. return Math.ceil((dateAsDate.getTime() - now.getTime()) / 1000);
  142. };
  143. const getStyles = (theme: GrafanaTheme2) => {
  144. return {
  145. modal: css`
  146. width: 550px;
  147. `,
  148. modalRow: css`
  149. margin-bottom: ${theme.spacing(4)};
  150. `,
  151. modalTokenRow: css`
  152. display: flex;
  153. `,
  154. modalCopyToClipboardButton: css`
  155. margin-left: ${theme.spacing(0.5)};
  156. `,
  157. modalHeaderTitle: css`
  158. font-size: ${theme.typography.size.lg};
  159. margin: ${theme.spacing(0, 4, 0, 1)};
  160. display: flex;
  161. align-items: center;
  162. position: relative;
  163. top: 2px;
  164. `,
  165. modalHeaderIcon: css`
  166. margin-right: ${theme.spacing(2)};
  167. font-size: inherit;
  168. &:before {
  169. vertical-align: baseline;
  170. }
  171. `,
  172. };
  173. };