16659: Added copy to clipboard button for the api token
[arvados-workbench2.git] / src / views-components / current-token-dialog / current-token-dialog.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import * as React from 'react';
6 import { Dialog, DialogActions, DialogTitle, DialogContent, WithStyles, withStyles, StyleRulesCallback, Button, Typography } from '@material-ui/core';
7 import * as CopyToClipboard from 'react-copy-to-clipboard';
8 import { ArvadosTheme } from '~/common/custom-theme';
9 import { withDialog } from '~/store/dialog/with-dialog';
10 import { WithDialogProps } from '~/store/dialog/with-dialog';
11 import { connect, DispatchProp } from 'react-redux';
12 import { CurrentTokenDialogData, getCurrentTokenDialogData, CURRENT_TOKEN_DIALOG_NAME } from '~/store/current-token-dialog/current-token-dialog-actions';
13 import { DefaultCodeSnippet } from '~/components/default-code-snippet/default-code-snippet';
14 import { snackbarActions, SnackbarKind } from '~/store/snackbar/snackbar-actions';
15
16 type CssRules = 'link' | 'paper' | 'button' | 'copyButton';
17
18 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
19     link: {
20         color: theme.palette.primary.main,
21         textDecoration: 'none',
22         margin: '0px 4px'
23     },
24     paper: {
25         padding: theme.spacing.unit,
26         marginBottom: theme.spacing.unit * 2,
27         backgroundColor: theme.palette.grey["200"],
28         border: `1px solid ${theme.palette.grey["300"]}`
29     },
30     button: {
31         fontSize: '0.8125rem',
32         fontWeight: 600
33     },
34     copyButton: {
35         boxShadow: 'none',
36         marginTop: theme.spacing.unit * 2,
37         marginBottom: theme.spacing.unit * 2,
38     }
39 });
40
41 type CurrentTokenProps = CurrentTokenDialogData & WithDialogProps<{}> & WithStyles<CssRules> & DispatchProp;
42
43 export class CurrentTokenDialogComponent extends React.Component<CurrentTokenProps> {
44     onCopy = (message: string) => {
45         this.props.dispatch(snackbarActions.OPEN_SNACKBAR({
46             message,
47             hideDuration: 2000,
48             kind: SnackbarKind.SUCCESS
49         }));
50     }
51
52     getSnippet = ({ apiHost, currentToken }: CurrentTokenDialogData) =>
53         `HISTIGNORE=$HISTIGNORE:'export ARVADOS_API_TOKEN=*'
54     export ARVADOS_API_TOKEN=${currentToken}
55     export ARVADOS_API_HOST=${apiHost}
56     unset ARVADOS_API_HOST_INSECURE`;
57
58     render() {
59         const { classes, open, closeDialog, ...data } = this.props;
60         return <Dialog
61             open={open}
62             onClose={closeDialog}
63             fullWidth={true}
64             maxWidth='md'>
65             <DialogTitle>Current Token</DialogTitle>
66             <DialogContent>
67                 <Typography paragraph={true}>
68                     The Arvados API token is a secret key that enables the Arvados SDKs to access Arvados with the proper permissions.
69                             <Typography component='p'>
70                         For more information see
71                                 <a href='http://doc.arvados.org/user/reference/api-tokens.html' target='blank' className={classes.link}>
72                             Getting an API token.
73                                 </a>
74                     </Typography>
75                 </Typography>
76                 <Typography paragraph={true}>
77                     Paste the following lines at a shell prompt to set up the necessary environment for Arvados SDKs to authenticate to your klingenc account.
78                         </Typography>
79                 <DefaultCodeSnippet lines={[this.getSnippet(data)]} />
80                 <CopyToClipboard text={data.currentToken} onCopy={() => this.onCopy('Token copied to clipboard')}>
81                     <Button
82                         color="primary"
83                         size="small"
84                         variant="contained"
85                         className={classes.copyButton}
86                     >
87                         Copy Api Token
88                     </Button>
89                 </CopyToClipboard>
90                 <Typography >
91                     Arvados
92                             <a href='http://doc.arvados.org/user/reference/api-tokens.html' target='blank' className={classes.link}>virtual machines</a>
93                     do this for you automatically. This setup is needed only when you use the API remotely (e.g., from your own workstation).
94                         </Typography>
95             </DialogContent>
96             <DialogActions>
97                 <Button onClick={closeDialog} className={classes.button} color="primary">CLOSE</Button>
98             </DialogActions>
99         </Dialog>;
100     }
101 }
102
103 export const CurrentTokenDialog =
104     withStyles(styles)(
105         connect(getCurrentTokenDialogData)(
106             withDialog(CURRENT_TOKEN_DIALOG_NAME)(CurrentTokenDialogComponent)));
107