Merge branch '17308-property-editor-fixes'
[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
61         return <Dialog
62             open={open}
63             onClose={closeDialog}
64             fullWidth={true}
65             maxWidth='md'>
66             <DialogTitle>Current Token</DialogTitle>
67             <DialogContent>
68                 <Typography paragraph={true}>
69                     The Arvados API token is a secret key that enables the Arvados SDKs to access Arvados with the proper permissions.
70                         <Typography component='span'>
71                             For more information see
72                             <a href='http://doc.arvados.org/user/reference/api-tokens.html' target='blank' className={classes.link}>
73                                 Getting an API token.
74                             </a>
75                         </Typography>
76                 </Typography>
77                 <Typography paragraph={true}>
78                     Paste the following lines at a shell prompt to set up the necessary environment for Arvados SDKs to authenticate to your account.
79                 </Typography>
80                 <DefaultCodeSnippet lines={[this.getSnippet(data)]} />
81                 <CopyToClipboard text={this.getSnippet(data)} onCopy={() => this.onCopy('Token copied to clipboard')}>
82                     <Button
83                         color="primary"
84                         size="small"
85                         variant="contained"
86                         className={classes.copyButton}
87                     >
88                         COPY TO CLIPBOARD
89                     </Button>
90                 </CopyToClipboard>
91                 <Typography >
92                     Arvados
93                             <a href='http://doc.arvados.org/user/reference/api-tokens.html' target='blank' className={classes.link}>virtual machines</a>
94                     do this for you automatically. This setup is needed only when you use the API remotely (e.g., from your own workstation).
95                 </Typography>
96             </DialogContent>
97             <DialogActions>
98                 <Button onClick={closeDialog} className={classes.button} color="primary">CLOSE</Button>
99             </DialogActions>
100         </Dialog>;
101     }
102 }
103
104 export const CurrentTokenDialog =
105     withStyles(styles)(
106         connect(getCurrentTokenDialogData)(
107             withDialog(CURRENT_TOKEN_DIALOG_NAME)(CurrentTokenDialogComponent)));
108